From 1e46a4549aa237706caaaa5eba1bd590a12c55bb Mon Sep 17 00:00:00 2001 From: Tit Petric Date: Thu, 5 Jul 2018 13:51:51 +0200 Subject: [PATCH] add capitalization for named paths --- sam/_gen.php | 5 +++++ sam/templates/http_.tpl | 2 +- sam/templates/http_handlers.tpl | 6 +++--- sam/templates/http_interfaces.tpl | 4 ++-- sam/templates/http_request.tpl | 10 +++++----- sam/templates/http_routes.tpl | 4 ++-- 6 files changed, 18 insertions(+), 13 deletions(-) diff --git a/sam/_gen.php b/sam/_gen.php index 0dfef5e4f..db1260216 100755 --- a/sam/_gen.php +++ b/sam/_gen.php @@ -5,6 +5,11 @@ error_reporting(E_ALL^E_NOTICE); include("docs/vendor/autoload.php"); +function capitalize($name) { + $names = explode("/", $name); + return implode("", array_map("ucfirst", $names)); +} + function array_change_key_case_recursive($arr) { return array_map(function ($item) { if (is_array($item)) { diff --git a/sam/templates/http_.tpl b/sam/templates/http_.tpl index 73c0fb39f..ab4bc069f 100644 --- a/sam/templates/http_.tpl +++ b/sam/templates/http_.tpl @@ -7,7 +7,7 @@ import ( var _ = errors.Wrap {foreach $calls as $call} -func (*{name}) {call.name|ucfirst}(r *{name|lcfirst}{call.name|ucfirst}Request) (interface{}, error) { +func (*{name}) {call.name|capitalize}(r *{name|lcfirst}{call.name|capitalize}Request) (interface{}, error) { return nil, errors.New("Not implemented: {name}.{call.name}") } diff --git a/sam/templates/http_handlers.tpl b/sam/templates/http_handlers.tpl index 3eab30652..29fdf4c11 100644 --- a/sam/templates/http_handlers.tpl +++ b/sam/templates/http_handlers.tpl @@ -7,8 +7,8 @@ import ( ) {foreach $calls as $call} -func ({self}h *{name}Handlers) {call.name|ucfirst}(w http.ResponseWriter, r *http.Request) { - params := {name|lcfirst}{call.name|ucfirst}Request{}.new() - resputil.JSON(w, params.Fill(r), func() (interface{}, error) { return {self}h.{name}.{call.name|ucfirst}(params) }) +func ({self}h *{name}Handlers) {call.name|capitalize}(w http.ResponseWriter, r *http.Request) { + params := {name|lcfirst}{call.name|capitalize}Request{}.new() + resputil.JSON(w, params.Fill(r), func() (interface{}, error) { return {self}h.{name}.{call.name|capitalize}(params) }) } {/foreach} diff --git a/sam/templates/http_interfaces.tpl b/sam/templates/http_interfaces.tpl index 8f2e2f213..36eb9cc63 100644 --- a/sam/templates/http_interfaces.tpl +++ b/sam/templates/http_interfaces.tpl @@ -18,14 +18,14 @@ func ({name}Handlers) new() *{name}Handlers { // Internal API interface type {name}API interface { {foreach $calls as $call} - {call.name|ucfirst}(*{name|lcfirst}{call.name|ucfirst}Request) (interface{}, error) + {call.name|capitalize}(*{name|lcfirst}{call.name|capitalize}Request) (interface{}, error) {/foreach} } // HTTP API interface type {name}HandlersAPI interface { {foreach $calls as $call} - {call.name|ucfirst}(http.ResponseWriter, *http.Request) + {call.name|capitalize}(http.ResponseWriter, *http.Request) {/foreach} } diff --git a/sam/templates/http_request.tpl b/sam/templates/http_request.tpl index b728f21c1..911832e9c 100644 --- a/sam/templates/http_request.tpl +++ b/sam/templates/http_request.tpl @@ -9,7 +9,7 @@ var _ = chi.URLParam {foreach $calls as $call} // {name} {call.name} request parameters -type {name|lcfirst}{call.name|ucfirst}Request struct { +type {name|lcfirst}{call.name|capitalize}Request struct { {foreach $call.parameters as $params} {foreach $params as $method => $param} {param.name} {param.type}{newline} @@ -17,11 +17,11 @@ type {name|lcfirst}{call.name|ucfirst}Request struct { {/foreach} } -func ({name|lcfirst}{call.name|ucfirst}Request) new() *{name|lcfirst}{call.name|ucfirst}Request { - return &{name|lcfirst}{call.name|ucfirst}Request{} +func ({name|lcfirst}{call.name|capitalize}Request) new() *{name|lcfirst}{call.name|capitalize}Request { + return &{name|lcfirst}{call.name|capitalize}Request{} } -func ({self} *{name|lcfirst}{call.name|ucfirst}Request) Fill(r *http.Request) error { +func ({self} *{name|lcfirst}{call.name|capitalize}Request) Fill(r *http.Request) error { get := map[string]string{} post := map[string]string{} urlQuery := r.URL.Query() @@ -44,5 +44,5 @@ func ({self} *{name|lcfirst}{call.name|ucfirst}Request) Fill(r *http.Request) er return nil } -var _ RequestFiller = {name|lcfirst}{call.name|ucfirst}Request{}.new() +var _ RequestFiller = {name|lcfirst}{call.name|capitalize}Request{}.new() {/foreach} diff --git a/sam/templates/http_routes.tpl b/sam/templates/http_routes.tpl index 61736bc86..116c30ba7 100644 --- a/sam/templates/http_routes.tpl +++ b/sam/templates/http_routes.tpl @@ -6,12 +6,12 @@ import ( func MountRoutes(r chi.Router) { {foreach $apis as $api} - {api.interface|strtolower} := {api.interface|ucfirst}Handlers{}.new() + {api.interface|strtolower} := {api.interface|capitalize}Handlers{}.new() {/foreach} {foreach $apis as $api} r.Route("{api.path}", func(r chi.Router) { {foreach $api.apis as $call} - r.{eval echo ucfirst(strtolower($call.method))}("{call.path}", {api.interface|strtolower}.{call.name|ucfirst}) + r.{eval echo capitalize(strtolower($call.method))}("{call.path}", {api.interface|strtolower}.{call.name|capitalize}) {/foreach} }) {/foreach}