From b186b94033fc883366f3a659cd93e1cb7bb6e2e8 Mon Sep 17 00:00:00 2001 From: Tit Petric Date: Fri, 13 Jul 2018 14:02:55 +0200 Subject: [PATCH] update codegen for crm --- crm/_gen.php | 13 ++++++++++++- crm/module.request.go | 5 +++++ crm/module.structs.go | 10 +++++----- crm/templates/http_request.tpl | 5 +++-- crm/templates/http_structs.tpl | 4 ++-- crm/types.request.go | 2 ++ 6 files changed, 29 insertions(+), 10 deletions(-) diff --git a/crm/_gen.php b/crm/_gen.php index 07cab9f81..f62a58b4b 100644 --- a/crm/_gen.php +++ b/crm/_gen.php @@ -40,7 +40,8 @@ foreach (array("structs", "handlers", "interfaces", "request", "") as $type) { $tpl->load("http_$type.tpl"); $tpl->assign("parsers", array( - "uint64" => "parseUInt64" + "uint64" => "parseUInt64", + "bool" => "parseBool", )); $tpl->assign("package", $api['package']); $tpl->assign("name", $name); @@ -80,3 +81,13 @@ foreach (array("routes") as $type) { file_put_contents($filename, $contents); } + +// camel case to snake case +function decamel($input) { + preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches); + $ret = $matches[0]; + foreach ($ret as &$match) { + $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match); + } + return implode('_', $ret); +} diff --git a/crm/module.request.go b/crm/module.request.go index 808d87083..5c0e1ad5b 100644 --- a/crm/module.request.go +++ b/crm/module.request.go @@ -32,6 +32,7 @@ func (moduleListRequest) new() *moduleListRequest { } func (m *moduleListRequest) Fill(r *http.Request) error { + r.ParseForm() get := map[string]string{} post := map[string]string{} urlQuery := r.URL.Query() @@ -60,6 +61,7 @@ func (moduleEditRequest) new() *moduleEditRequest { } func (m *moduleEditRequest) Fill(r *http.Request) error { + r.ParseForm() get := map[string]string{} post := map[string]string{} urlQuery := r.URL.Query() @@ -89,6 +91,7 @@ func (moduleContentListRequest) new() *moduleContentListRequest { } func (m *moduleContentListRequest) Fill(r *http.Request) error { + r.ParseForm() get := map[string]string{} post := map[string]string{} urlQuery := r.URL.Query() @@ -117,6 +120,7 @@ func (moduleContentEditRequest) new() *moduleContentEditRequest { } func (m *moduleContentEditRequest) Fill(r *http.Request) error { + r.ParseForm() get := map[string]string{} post := map[string]string{} urlQuery := r.URL.Query() @@ -146,6 +150,7 @@ func (moduleContentDeleteRequest) new() *moduleContentDeleteRequest { } func (m *moduleContentDeleteRequest) Fill(r *http.Request) error { + r.ParseForm() get := map[string]string{} post := map[string]string{} urlQuery := r.URL.Query() diff --git a/crm/module.structs.go b/crm/module.structs.go index 79f6ce656..163e05950 100644 --- a/crm/module.structs.go +++ b/crm/module.structs.go @@ -22,17 +22,17 @@ import ( type ( // Modules Module struct { - ID uint64 - Name string + ID uint64 `db:"id"` + Name string `db:"name"` changed []string } // Modules ModuleContentRow struct { - ID uint64 `db:"id"` - ModuleID uint64 `db:"module_id"` - Fields types.JSONText `db:"address"` + ID uint64 `db:"id" db:"id"` + ModuleID uint64 `db:"module_id" db:"module_id"` + Fields types.JSONText `db:"address" db:"fields"` changed []string } diff --git a/crm/templates/http_request.tpl b/crm/templates/http_request.tpl index 789625ff5..e0a30ebee 100644 --- a/crm/templates/http_request.tpl +++ b/crm/templates/http_request.tpl @@ -24,6 +24,7 @@ func ({name|lcfirst}{call.name|capitalize}Request) new() *{name|lcfirst}{call.na } func ({self} *{name|lcfirst}{call.name|capitalize}Request) Fill(r *http.Request) error { + r.ParseForm() get := map[string]string{} post := map[string]string{} urlQuery := r.URL.Query() @@ -37,9 +38,9 @@ func ({self} *{name|lcfirst}{call.name|capitalize}Request) Fill(r *http.Request) {foreach $call.parameters as $method => $params} {foreach $params as $param} {if strtolower($method) === "path"} - {self}.{param.name} = chi.URLParam(r, "{param.name}") + {self}.{param.name} = {if ($param.type !== "string")}{$parsers[$param.type]}({/if}chi.URLParam(r, "{param.name}"){if ($param.type !== "string")}){/if}{newline} {elseif substr($param.type, 0, 2) !== '[]'} - {self}.{param.name} = {if $param.type !== "string"}{$parsers[$param.type]}({method|strtolower}["{param.name}"]){else}{method|strtolower}["{param.name}"]{/if}{newline} + {self}.{param.name} = {if ($param.type !== "string")}{$parsers[$param.type]}({method|strtolower}["{param.name}"]){else}{method|strtolower}["{param.name}"]{/if}{newline} {/if} {/foreach} {/foreach} diff --git a/crm/templates/http_structs.tpl b/crm/templates/http_structs.tpl index f095b9180..8b4fd7f9a 100644 --- a/crm/templates/http_structs.tpl +++ b/crm/templates/http_structs.tpl @@ -20,7 +20,7 @@ type ({foreach $structs as $struct} // {api.title} {struct.name} struct { {foreach $struct.fields as $field} - {field.name} {field.type}{if $field.tag} `{$field.tag}`{/if}{newline} + {field.name} {field.type} `{if $field.tag}{$field.tag} {/if}db:"{if $field.dbname}{$field.dbname}{else}{$field.name|decamel}{/if}"`{newline} {/foreach} changed []string @@ -55,4 +55,4 @@ func ({self} *{struct.name}) Set{field.name}(value {field.type}) *{struct.name} } {/foreach} -{/foreach} \ No newline at end of file +{/foreach} diff --git a/crm/types.request.go b/crm/types.request.go index 74b5574c3..99d9f869a 100644 --- a/crm/types.request.go +++ b/crm/types.request.go @@ -31,6 +31,7 @@ func (typesListRequest) new() *typesListRequest { } func (t *typesListRequest) Fill(r *http.Request) error { + r.ParseForm() get := map[string]string{} post := map[string]string{} urlQuery := r.URL.Query() @@ -56,6 +57,7 @@ func (typesTypeRequest) new() *typesTypeRequest { } func (t *typesTypeRequest) Fill(r *http.Request) error { + r.ParseForm() get := map[string]string{} post := map[string]string{} urlQuery := r.URL.Query()