From 43554592e4379427db283a72d43ad70225341f68 Mon Sep 17 00:00:00 2001 From: Tit Petric Date: Tue, 4 Dec 2018 15:26:51 +0100 Subject: [PATCH] upd(codegen): enable imports with requests --- api/crm/spec.json | 28 ++++++++++++--------- api/crm/spec/module.json | 16 ++++++------ api/crm/spec/page.json | 12 ++++++--- api/sam/spec.json | 7 ++++++ api/sam/spec/permissions.json | 8 +++++- api/system/spec.json | 6 ++--- api/system/spec/user.json | 6 ++--- codegen/codegen.php | 20 ++++++++++++++- codegen/crm/rest/handlers/index.php | 9 +------ codegen/crm/rest/index.php | 9 +------ codegen/crm/rest/request/index.php | 9 +------ codegen/crm/types/index.php | 8 +----- codegen/sam/rest/handlers/index.php | 9 +------ codegen/sam/rest/index.php | 9 +------ codegen/sam/rest/request/index.php | 9 +------ codegen/system/rest/handlers/index.php | 9 +------ codegen/system/rest/index.php | 9 +------ codegen/system/rest/request/index.php | 9 +------ codegen/templates/http_request_inline.tpl | 30 +++++++++++------------ codegen/templates/http_structs.tpl | 3 +-- crm/rest/request/field.go | 9 +++---- crm/rest/request/module.go | 27 ++++++++++---------- crm/rest/request/page.go | 19 +++++++------- crm/rest/request/util.go | 2 +- docs/crm/README.md | 12 ++++----- docs/system/README.md | 4 +-- sam/rest/request/attachment.go | 9 +++---- sam/rest/request/channel.go | 9 +++---- sam/rest/request/message.go | 9 +++---- sam/rest/request/permissions.go | 11 ++++----- sam/rest/request/search.go | 9 +++---- sam/types/channel.perms.go | 2 +- sam/types/organisation.perms.go | 2 +- sam/types/team.perms.go | 2 +- system/rest/request/auth.go | 9 +++---- system/rest/request/organisation.go | 9 +++---- system/rest/request/team.go | 9 +++---- system/rest/request/user.go | 19 +++++++------- system/rest/request/util.go | 2 +- 39 files changed, 172 insertions(+), 227 deletions(-) diff --git a/api/crm/spec.json b/api/crm/spec.json index edb805577..9e646eb0d 100644 --- a/api/crm/spec.json +++ b/api/crm/spec.json @@ -61,7 +61,13 @@ "entrypoint": "page", "path": "/page", "authentication": [], - "struct": [], + "struct": [ + { + "imports": [ + "sqlxTypes github.com/jmoiron/sqlx/types" + ] + } + ], "apis": [ { "name": "list", @@ -117,7 +123,7 @@ "title": "Visible in navigation" }, { - "type": "types.JSONText", + "type": "sqlxTypes.JSONText", "name": "blocks", "required": true, "title": "Blocks JSON" @@ -194,7 +200,7 @@ "title": "Visible in navigation" }, { - "type": "types.JSONText", + "type": "sqlxTypes.JSONText", "name": "blocks", "required": true, "title": "Blocks JSON" @@ -255,7 +261,7 @@ { "name": "Module", "imports": [ - "github.com/jmoiron/sqlx/types" + "sqlxTypes github.com/jmoiron/sqlx/types" ], "fields": [ { @@ -268,7 +274,7 @@ }, { "name": "Fields", - "type": "types.JSONText", + "type": "sqlxTypes.JSONText", "json": "fields", "db": "json" } @@ -302,7 +308,7 @@ { "name": "Content", "imports": [ - "github.com/jmoiron/sqlx/types" + "sqlxTypes github.com/jmoiron/sqlx/types" ], "fields": [ { @@ -315,7 +321,7 @@ }, { "name": "Fields", - "type": "types.JSONText", + "type": "sqlxTypes.JSONText", "db": "json" } ] @@ -352,7 +358,7 @@ "title": "Module Name" }, { - "type": "types.JSONText", + "type": "sqlxTypes.JSONText", "name": "fields", "required": true, "title": "Fields JSON" @@ -476,7 +482,7 @@ "title": "Module Name" }, { - "type": "types.JSONText", + "type": "sqlxTypes.JSONText", "name": "fields", "required": true, "title": "Fields JSON" @@ -552,7 +558,7 @@ ], "post": [ { - "type": "types.JSONText", + "type": "sqlxTypes.JSONText", "name": "fields", "required": true, "title": "Content JSON" @@ -604,7 +610,7 @@ ], "post": [ { - "type": "types.JSONText", + "type": "sqlxTypes.JSONText", "name": "fields", "required": true, "title": "Content JSON" diff --git a/api/crm/spec/module.json b/api/crm/spec/module.json index 0f27237fa..3945210f7 100644 --- a/api/crm/spec/module.json +++ b/api/crm/spec/module.json @@ -18,11 +18,11 @@ "db": "json", "json": "fields", "name": "Fields", - "type": "types.JSONText" + "type": "sqlxTypes.JSONText" } ], "imports": [ - "github.com/jmoiron/sqlx/types" + "sqlxTypes github.com/jmoiron/sqlx/types" ], "name": "Module" }, @@ -64,11 +64,11 @@ { "db": "json", "name": "Fields", - "type": "types.JSONText" + "type": "sqlxTypes.JSONText" } ], "imports": [ - "github.com/jmoiron/sqlx/types" + "sqlxTypes github.com/jmoiron/sqlx/types" ], "name": "Content" } @@ -111,7 +111,7 @@ "name": "fields", "required": true, "title": "Fields JSON", - "type": "types.JSONText" + "type": "sqlxTypes.JSONText" } ] } @@ -235,7 +235,7 @@ "name": "fields", "required": true, "title": "Fields JSON", - "type": "types.JSONText" + "type": "sqlxTypes.JSONText" } ] } @@ -311,7 +311,7 @@ "name": "fields", "required": true, "title": "Content JSON", - "type": "types.JSONText" + "type": "sqlxTypes.JSONText" } ] } @@ -363,7 +363,7 @@ "name": "fields", "required": true, "title": "Content JSON", - "type": "types.JSONText" + "type": "sqlxTypes.JSONText" } ] } diff --git a/api/crm/spec/page.json b/api/crm/spec/page.json index 2af3ecb5c..bbc542670 100644 --- a/api/crm/spec/page.json +++ b/api/crm/spec/page.json @@ -3,7 +3,13 @@ "Description": "CRM module pages", "Package": "crm", "Interface": "Page", - "Struct": [], + "Struct": [ + { + "imports": [ + "sqlxTypes github.com/jmoiron/sqlx/types" + ] + } + ], "Parameters": null, "Protocol": "", "Authentication": [], @@ -66,7 +72,7 @@ "name": "blocks", "required": true, "title": "Blocks JSON", - "type": "types.JSONText" + "type": "sqlxTypes.JSONText" } ] } @@ -143,7 +149,7 @@ "name": "blocks", "required": true, "title": "Blocks JSON", - "type": "types.JSONText" + "type": "sqlxTypes.JSONText" } ] } diff --git a/api/sam/spec.json b/api/sam/spec.json index 8cd604b4c..b972b22d4 100644 --- a/api/sam/spec.json +++ b/api/sam/spec.json @@ -644,6 +644,13 @@ "Client ID", "Session ID" ], + "struct": [ + { + "imports": [ + "github.com/crusttech/crust/internal/rbac" + ] + } + ], "apis": [ { "name": "list", diff --git a/api/sam/spec/permissions.json b/api/sam/spec/permissions.json index 6f3a42cca..2b5225f9b 100644 --- a/api/sam/spec/permissions.json +++ b/api/sam/spec/permissions.json @@ -2,7 +2,13 @@ "Title": "Permissions", "Package": "sam", "Interface": "Permissions", - "Struct": null, + "Struct": [ + { + "imports": [ + "github.com/crusttech/crust/internal/rbac" + ] + } + ], "Parameters": {}, "Protocol": "", "Authentication": [ diff --git a/api/system/spec.json b/api/system/spec.json index b3b220ca0..f9454df5d 100644 --- a/api/system/spec.json +++ b/api/system/spec.json @@ -398,7 +398,7 @@ "struct": [ { "imports": [ - "github.com/jmoiron/sqlx/types" + "sqlxTypes github.com/jmoiron/sqlx/types" ] } ], @@ -464,7 +464,7 @@ }, { "name": "meta", - "type": "types.JSONText", + "type": "sqlxTypes.JSONText", "required": false, "title": "Meta data" }, @@ -536,7 +536,7 @@ }, { "name": "meta", - "type": "types.JSONText", + "type": "sqlxTypes.JSONText", "required": false, "title": "Meta data" }, diff --git a/api/system/spec/user.json b/api/system/spec/user.json index 865f0a8a4..f0470cbb4 100644 --- a/api/system/spec/user.json +++ b/api/system/spec/user.json @@ -5,7 +5,7 @@ "Struct": [ { "imports": [ - "github.com/jmoiron/sqlx/types" + "sqlxTypes github.com/jmoiron/sqlx/types" ] } ], @@ -80,7 +80,7 @@ "name": "meta", "required": false, "title": "Meta data", - "type": "types.JSONText" + "type": "sqlxTypes.JSONText" }, { "name": "satosaID", @@ -152,7 +152,7 @@ "name": "meta", "required": false, "title": "Meta data", - "type": "types.JSONText" + "type": "sqlxTypes.JSONText" }, { "name": "satosaID", diff --git a/codegen/codegen.php b/codegen/codegen.php index 29edd454f..6ed3212b2 100755 --- a/codegen/codegen.php +++ b/codegen/codegen.php @@ -38,9 +38,27 @@ function array_change_key_case_recursive($arr) { }, array_change_key_case($arr)); } +function imports($api) { + $imports = array(); + if (is_array($api['struct'])) + foreach ($api['struct'] as $struct) { + if (isset($struct['imports'])) + foreach ($struct['imports'] as $import) { + $import = explode(" ", $import); + if (count($import) == 1) { + $imports[] = '"' . $import[0] . '"'; + } else { + $imports[] = $import[0] . ' "' . $import[1] . '"'; + } + } + } + return array_unique($imports); +} + $tpl = new Monotek\MiniTPL\Template; $tpl->set_compile_location("/tmp", true); $tpl->add_default("newline", "\n"); +$tpl->add_default("EOL", "\n"); $generators = array(); exec("find -L " . __DIR__ . "/" . $project . " -name index.php", $generators); @@ -81,7 +99,7 @@ $parsers = array( "[]uint64" => "parseUInt64A", "int" => "parseInt", "bool" => "parseBool", - "types.JSONText" => "parseJSONText", + "sqlxTypes.JSONText" => "parseJSONTextWithErr", ); foreach ($generators as $generator) { diff --git a/codegen/crm/rest/handlers/index.php b/codegen/crm/rest/handlers/index.php index 7aa5983f3..276aa9a7a 100644 --- a/codegen/crm/rest/handlers/index.php +++ b/codegen/crm/rest/handlers/index.php @@ -19,14 +19,7 @@ foreach ($apis as $api) { $tpl->assign("apis", $apis); $tpl->assign("self", strtolower(substr($name, 0, 1))); $tpl->assign("structs", $api['struct']); - $imports = array(); - if (is_array($api['struct'])) - foreach ($api['struct'] as $struct) { - if (isset($struct['imports'])) - foreach ($struct['imports'] as $import) { - $imports[] = $import; - } - } + $imports = imports($api); $tpl->assign("imports", $imports); $tpl->assign("calls", $api['apis']); $contents = str_replace("\n\n}", "\n}", $tpl->get()); diff --git a/codegen/crm/rest/index.php b/codegen/crm/rest/index.php index fc5eabcd9..f7102bb34 100644 --- a/codegen/crm/rest/index.php +++ b/codegen/crm/rest/index.php @@ -11,14 +11,7 @@ foreach ($apis as $api) { $tpl->assign("api", $api); $tpl->assign("self", strtolower(substr($name, 0, 1))); $tpl->assign("structs", $api['struct']); - $imports = array(); - if (is_array($api['struct'])) - foreach ($api['struct'] as $struct) { - if (isset($struct['imports'])) - foreach ($struct['imports'] as $import) { - $imports[] = $import; - } - } + $imports = imports($api); $tpl->assign("imports", $imports); $tpl->assign("calls", $api['apis']); $contents = str_replace("\n\n}", "\n}", $tpl->get()); diff --git a/codegen/crm/rest/request/index.php b/codegen/crm/rest/request/index.php index c98c0349b..bd6bb47cb 100644 --- a/codegen/crm/rest/request/index.php +++ b/codegen/crm/rest/request/index.php @@ -19,14 +19,7 @@ foreach ($apis as $api) { $tpl->assign("apis", $apis); $tpl->assign("self", strtolower(substr($name, 0, 1))); $tpl->assign("structs", $api['struct']); - $imports = array(); - if (is_array($api['struct'])) - foreach ($api['struct'] as $struct) { - if (isset($struct['imports'])) - foreach ($struct['imports'] as $import) { - $imports[] = $import; - } - } + $imports = imports($api); $tpl->assign("imports", $imports); $tpl->assign("calls", $api['apis']); $contents = str_replace("\n\n}", "\n}", $tpl->get()); diff --git a/codegen/crm/types/index.php b/codegen/crm/types/index.php index 6b4d6f14b..e8f53cfd2 100644 --- a/codegen/crm/types/index.php +++ b/codegen/crm/types/index.php @@ -22,13 +22,7 @@ foreach ($apis as $api) { $tpl->assign("apis", $apis); $tpl->assign("self", strtolower(substr($name, 0, 1))); $tpl->assign("structs", $api['struct']); - $imports = array(); - foreach ($api['struct'] as $struct) { - if (isset($struct['imports'])) - foreach ($struct['imports'] as $import) { - $imports[] = $import; - } - } + $imports = imports($api); $tpl->assign("imports", $imports); $tpl->assign("calls", $api['apis']); $contents = str_replace("\n\n}", "\n}", $tpl->get()); diff --git a/codegen/sam/rest/handlers/index.php b/codegen/sam/rest/handlers/index.php index 7aa5983f3..276aa9a7a 100644 --- a/codegen/sam/rest/handlers/index.php +++ b/codegen/sam/rest/handlers/index.php @@ -19,14 +19,7 @@ foreach ($apis as $api) { $tpl->assign("apis", $apis); $tpl->assign("self", strtolower(substr($name, 0, 1))); $tpl->assign("structs", $api['struct']); - $imports = array(); - if (is_array($api['struct'])) - foreach ($api['struct'] as $struct) { - if (isset($struct['imports'])) - foreach ($struct['imports'] as $import) { - $imports[] = $import; - } - } + $imports = imports($api); $tpl->assign("imports", $imports); $tpl->assign("calls", $api['apis']); $contents = str_replace("\n\n}", "\n}", $tpl->get()); diff --git a/codegen/sam/rest/index.php b/codegen/sam/rest/index.php index fc5eabcd9..f7102bb34 100644 --- a/codegen/sam/rest/index.php +++ b/codegen/sam/rest/index.php @@ -11,14 +11,7 @@ foreach ($apis as $api) { $tpl->assign("api", $api); $tpl->assign("self", strtolower(substr($name, 0, 1))); $tpl->assign("structs", $api['struct']); - $imports = array(); - if (is_array($api['struct'])) - foreach ($api['struct'] as $struct) { - if (isset($struct['imports'])) - foreach ($struct['imports'] as $import) { - $imports[] = $import; - } - } + $imports = imports($api); $tpl->assign("imports", $imports); $tpl->assign("calls", $api['apis']); $contents = str_replace("\n\n}", "\n}", $tpl->get()); diff --git a/codegen/sam/rest/request/index.php b/codegen/sam/rest/request/index.php index c98c0349b..bd6bb47cb 100644 --- a/codegen/sam/rest/request/index.php +++ b/codegen/sam/rest/request/index.php @@ -19,14 +19,7 @@ foreach ($apis as $api) { $tpl->assign("apis", $apis); $tpl->assign("self", strtolower(substr($name, 0, 1))); $tpl->assign("structs", $api['struct']); - $imports = array(); - if (is_array($api['struct'])) - foreach ($api['struct'] as $struct) { - if (isset($struct['imports'])) - foreach ($struct['imports'] as $import) { - $imports[] = $import; - } - } + $imports = imports($api); $tpl->assign("imports", $imports); $tpl->assign("calls", $api['apis']); $contents = str_replace("\n\n}", "\n}", $tpl->get()); diff --git a/codegen/system/rest/handlers/index.php b/codegen/system/rest/handlers/index.php index 7aa5983f3..276aa9a7a 100644 --- a/codegen/system/rest/handlers/index.php +++ b/codegen/system/rest/handlers/index.php @@ -19,14 +19,7 @@ foreach ($apis as $api) { $tpl->assign("apis", $apis); $tpl->assign("self", strtolower(substr($name, 0, 1))); $tpl->assign("structs", $api['struct']); - $imports = array(); - if (is_array($api['struct'])) - foreach ($api['struct'] as $struct) { - if (isset($struct['imports'])) - foreach ($struct['imports'] as $import) { - $imports[] = $import; - } - } + $imports = imports($api); $tpl->assign("imports", $imports); $tpl->assign("calls", $api['apis']); $contents = str_replace("\n\n}", "\n}", $tpl->get()); diff --git a/codegen/system/rest/index.php b/codegen/system/rest/index.php index fc5eabcd9..f7102bb34 100644 --- a/codegen/system/rest/index.php +++ b/codegen/system/rest/index.php @@ -11,14 +11,7 @@ foreach ($apis as $api) { $tpl->assign("api", $api); $tpl->assign("self", strtolower(substr($name, 0, 1))); $tpl->assign("structs", $api['struct']); - $imports = array(); - if (is_array($api['struct'])) - foreach ($api['struct'] as $struct) { - if (isset($struct['imports'])) - foreach ($struct['imports'] as $import) { - $imports[] = $import; - } - } + $imports = imports($api); $tpl->assign("imports", $imports); $tpl->assign("calls", $api['apis']); $contents = str_replace("\n\n}", "\n}", $tpl->get()); diff --git a/codegen/system/rest/request/index.php b/codegen/system/rest/request/index.php index c98c0349b..bd6bb47cb 100644 --- a/codegen/system/rest/request/index.php +++ b/codegen/system/rest/request/index.php @@ -19,14 +19,7 @@ foreach ($apis as $api) { $tpl->assign("apis", $apis); $tpl->assign("self", strtolower(substr($name, 0, 1))); $tpl->assign("structs", $api['struct']); - $imports = array(); - if (is_array($api['struct'])) - foreach ($api['struct'] as $struct) { - if (isset($struct['imports'])) - foreach ($struct['imports'] as $import) { - $imports[] = $import; - } - } + $imports = imports($api); $tpl->assign("imports", $imports); $tpl->assign("calls", $api['apis']); $contents = str_replace("\n\n}", "\n}", $tpl->get()); diff --git a/codegen/templates/http_request_inline.tpl b/codegen/templates/http_request_inline.tpl index 3b0c99e46..b50f826a2 100644 --- a/codegen/templates/http_request_inline.tpl +++ b/codegen/templates/http_request_inline.tpl @@ -6,18 +6,18 @@ import ( "io" "net/http" "encoding/json" - "github.com/go-chi/chi" - "github.com/pkg/errors" - "github.com/jmoiron/sqlx/types" - "github.com/crusttech/crust/internal/rbac" "mime/multipart" "strings" + + "github.com/go-chi/chi" + "github.com/pkg/errors" + +{if !empty($imports)}{foreach ($imports as $import)} + {import}{EOL}{/foreach}{/if} ) var _ = chi.URLParam -var _ = types.JSONText{} var _ = multipart.FileHeader{} -var _ = rbac.Operation{} {foreach $calls as $call} // {name} {call.name} request parameters @@ -68,17 +68,17 @@ func ({self} *{name|expose}{call.name|capitalize}) Fill(r *http.Request) (err er {elseif substr($param.type, 0, 2) === '[]' && isset($parsers[$param.type])} {self}.{param.name|expose} = {$parsers[$param.type]}({if $method === "post"}r.Form["{param.name}"]{else}urlQuery["{param.name}"]{/if}) {elseif $param.type === "*multipart.FileHeader"} - if _, {self}.{param.name|expose}, err = r.FormFile("{$param.name}"); err != nil { - return errors.Wrap(err, "error procesing uploaded file") - } + if _, {self}.{param.name|expose}, err = r.FormFile("{$param.name}"); err != nil { + return errors.Wrap(err, "error procesing uploaded file") + } {elseif substr($param.type, 0, 2) !== '[]'} - if val, ok := {method|strtolower}["{param.name}"]; ok { -{if $param.type === "types.JSONText"} - if {self}.{param.name|expose}, err = {$parsers[$param.type]}(val); err != nil { - return err - } + if val, ok := {method|strtolower}["{param.name}"]; ok { +{if substr($parsers[$param.type], -7) === 'WithErr'} + if {self}.{param.name|expose}, err = {$parsers[$param.type]}(val); err != nil { + return err + } {else} - {self}.{param.name|expose} = {if ($param.type !== "string")}{$parsers[$param.type]}(val){else}val{/if} + {self}.{param.name|expose} = {if ($param.type !== "string")}{$parsers[$param.type]}(val){else}val{/if}{EOL} {/if} }{/if} {/foreach} diff --git a/codegen/templates/http_structs.tpl b/codegen/templates/http_structs.tpl index 433e9f271..897ce2df1 100644 --- a/codegen/templates/http_structs.tpl +++ b/codegen/templates/http_structs.tpl @@ -5,8 +5,7 @@ package {package} {if !empty($imports)} import ( {foreach ($imports as $import)} - "{import}" -{/foreach} + {import}{EOL}{/foreach} ) {/if} diff --git a/crm/rest/request/field.go b/crm/rest/request/field.go index 7e46cae53..cf3da30d5 100644 --- a/crm/rest/request/field.go +++ b/crm/rest/request/field.go @@ -17,20 +17,17 @@ package request import ( "encoding/json" - "github.com/crusttech/crust/internal/rbac" - "github.com/go-chi/chi" - "github.com/jmoiron/sqlx/types" - "github.com/pkg/errors" "io" "mime/multipart" "net/http" "strings" + + "github.com/go-chi/chi" + "github.com/pkg/errors" ) var _ = chi.URLParam -var _ = types.JSONText{} var _ = multipart.FileHeader{} -var _ = rbac.Operation{} // Field list request parameters type FieldList struct { diff --git a/crm/rest/request/module.go b/crm/rest/request/module.go index dd6441907..720470412 100644 --- a/crm/rest/request/module.go +++ b/crm/rest/request/module.go @@ -17,20 +17,19 @@ package request import ( "encoding/json" - "github.com/crusttech/crust/internal/rbac" - "github.com/go-chi/chi" - "github.com/jmoiron/sqlx/types" - "github.com/pkg/errors" "io" "mime/multipart" "net/http" "strings" + + "github.com/go-chi/chi" + "github.com/pkg/errors" + + sqlxTypes "github.com/jmoiron/sqlx/types" ) var _ = chi.URLParam -var _ = types.JSONText{} var _ = multipart.FileHeader{} -var _ = rbac.Operation{} // Module list request parameters type ModuleList struct { @@ -81,7 +80,7 @@ var _ RequestFiller = NewModuleList() // Module create request parameters type ModuleCreate struct { Name string - Fields types.JSONText + Fields sqlxTypes.JSONText } func NewModuleCreate() *ModuleCreate { @@ -121,7 +120,7 @@ func (m *ModuleCreate) Fill(r *http.Request) (err error) { } if val, ok := post["fields"]; ok { - if m.Fields, err = parseJSONText(val); err != nil { + if m.Fields, err = parseJSONTextWithErr(val); err != nil { return err } } @@ -271,7 +270,7 @@ var _ RequestFiller = NewModuleChart() type ModuleEdit struct { ModuleID uint64 `json:",string"` Name string - Fields types.JSONText + Fields sqlxTypes.JSONText } func NewModuleEdit() *ModuleEdit { @@ -312,7 +311,7 @@ func (m *ModuleEdit) Fill(r *http.Request) (err error) { } if val, ok := post["fields"]; ok { - if m.Fields, err = parseJSONText(val); err != nil { + if m.Fields, err = parseJSONTextWithErr(val); err != nil { return err } } @@ -426,7 +425,7 @@ var _ RequestFiller = NewModuleContentList() // Module content/create request parameters type ModuleContentCreate struct { ModuleID uint64 `json:",string"` - Fields types.JSONText + Fields sqlxTypes.JSONText } func NewModuleContentCreate() *ModuleContentCreate { @@ -463,7 +462,7 @@ func (m *ModuleContentCreate) Fill(r *http.Request) (err error) { m.ModuleID = parseUInt64(chi.URLParam(r, "moduleID")) if val, ok := post["fields"]; ok { - if m.Fields, err = parseJSONText(val); err != nil { + if m.Fields, err = parseJSONTextWithErr(val); err != nil { return err } } @@ -522,7 +521,7 @@ var _ RequestFiller = NewModuleContentRead() type ModuleContentEdit struct { ModuleID uint64 `json:",string"` ContentID uint64 `json:",string"` - Fields types.JSONText + Fields sqlxTypes.JSONText } func NewModuleContentEdit() *ModuleContentEdit { @@ -560,7 +559,7 @@ func (m *ModuleContentEdit) Fill(r *http.Request) (err error) { m.ContentID = parseUInt64(chi.URLParam(r, "contentID")) if val, ok := post["fields"]; ok { - if m.Fields, err = parseJSONText(val); err != nil { + if m.Fields, err = parseJSONTextWithErr(val); err != nil { return err } } diff --git a/crm/rest/request/page.go b/crm/rest/request/page.go index b4c8662cd..519e5e4b6 100644 --- a/crm/rest/request/page.go +++ b/crm/rest/request/page.go @@ -17,20 +17,19 @@ package request import ( "encoding/json" - "github.com/crusttech/crust/internal/rbac" - "github.com/go-chi/chi" - "github.com/jmoiron/sqlx/types" - "github.com/pkg/errors" "io" "mime/multipart" "net/http" "strings" + + "github.com/go-chi/chi" + "github.com/pkg/errors" + + sqlxTypes "github.com/jmoiron/sqlx/types" ) var _ = chi.URLParam -var _ = types.JSONText{} var _ = multipart.FileHeader{} -var _ = rbac.Operation{} // Page list request parameters type PageList struct { @@ -85,7 +84,7 @@ type PageCreate struct { Title string Description string Visible bool - Blocks types.JSONText + Blocks sqlxTypes.JSONText } func NewPageCreate() *PageCreate { @@ -141,7 +140,7 @@ func (p *PageCreate) Fill(r *http.Request) (err error) { } if val, ok := post["blocks"]; ok { - if p.Blocks, err = parseJSONText(val); err != nil { + if p.Blocks, err = parseJSONTextWithErr(val); err != nil { return err } } @@ -242,7 +241,7 @@ type PageEdit struct { Title string Description string Visible bool - Blocks types.JSONText + Blocks sqlxTypes.JSONText } func NewPageEdit() *PageEdit { @@ -299,7 +298,7 @@ func (p *PageEdit) Fill(r *http.Request) (err error) { } if val, ok := post["blocks"]; ok { - if p.Blocks, err = parseJSONText(val); err != nil { + if p.Blocks, err = parseJSONTextWithErr(val); err != nil { return err } } diff --git a/crm/rest/request/util.go b/crm/rest/request/util.go index f8b7fda70..4451032fd 100644 --- a/crm/rest/request/util.go +++ b/crm/rest/request/util.go @@ -11,7 +11,7 @@ import ( var truthy = regexp.MustCompile("^\\s*(t(rue)?|y(es)?|1)\\s*$") -func parseJSONText(s string) (types.JSONText, error) { +func parseJSONTextWithErr(s string) (types.JSONText, error) { result := &types.JSONText{} err := errors.Wrap(result.Scan(s), "error when parsing JSONText") return *result, err diff --git a/docs/crm/README.md b/docs/crm/README.md index 03bdc26ed..ad8110add 100644 --- a/docs/crm/README.md +++ b/docs/crm/README.md @@ -63,7 +63,7 @@ CRM module definitions | Parameter | Type | Method | Description | Default | Required? | | --------- | ---- | ------ | ----------- | ------- | --------- | | name | string | POST | Module Name | N/A | YES | -| fields | types.JSONText | POST | Fields JSON | N/A | YES | +| fields | sqlxTypes.JSONText | POST | Fields JSON | N/A | YES | ## Read module @@ -302,7 +302,7 @@ Example bar chart with number of leads per country: select lead.country from lea | --------- | ---- | ------ | ----------- | ------- | --------- | | moduleID | uint64 | PATH | Module ID | N/A | YES | | name | string | POST | Module Name | N/A | YES | -| fields | types.JSONText | POST | Fields JSON | N/A | YES | +| fields | sqlxTypes.JSONText | POST | Fields JSON | N/A | YES | ## Delete module @@ -348,7 +348,7 @@ Example bar chart with number of leads per country: select lead.country from lea | Parameter | Type | Method | Description | Default | Required? | | --------- | ---- | ------ | ----------- | ------- | --------- | | moduleID | uint64 | PATH | Module ID | N/A | YES | -| fields | types.JSONText | POST | Content JSON | N/A | YES | +| fields | sqlxTypes.JSONText | POST | Content JSON | N/A | YES | ## Read contents by ID from module section @@ -379,7 +379,7 @@ Example bar chart with number of leads per country: select lead.country from lea | --------- | ---- | ------ | ----------- | ------- | --------- | | moduleID | uint64 | PATH | Module ID | N/A | YES | | contentID | uint64 | PATH | Content ID | N/A | YES | -| fields | types.JSONText | POST | Content JSON | N/A | YES | +| fields | sqlxTypes.JSONText | POST | Content JSON | N/A | YES | ## Delete content row from module section @@ -434,7 +434,7 @@ CRM module pages | title | string | POST | Title | N/A | YES | | description | string | POST | Description | N/A | NO | | visible | bool | POST | Visible in navigation | N/A | NO | -| blocks | types.JSONText | POST | Blocks JSON | N/A | YES | +| blocks | sqlxTypes.JSONText | POST | Blocks JSON | N/A | YES | ## Get page details @@ -481,7 +481,7 @@ CRM module pages | title | string | POST | Title | N/A | YES | | description | string | POST | Description | N/A | NO | | visible | bool | POST | Visible in navigation | N/A | NO | -| blocks | types.JSONText | POST | Blocks JSON | N/A | YES | +| blocks | sqlxTypes.JSONText | POST | Blocks JSON | N/A | YES | ## Reorder pages diff --git a/docs/system/README.md b/docs/system/README.md index da1003d21..bb88d31a8 100644 --- a/docs/system/README.md +++ b/docs/system/README.md @@ -324,7 +324,7 @@ An organisation may have many teams. Teams may have many channels available. Acc | name | string | POST | Name | N/A | YES | | handle | string | POST | Handle | N/A | YES | | kind | string | POST | Kind (normal, bot) | N/A | NO | -| meta | types.JSONText | POST | Meta data | N/A | NO | +| meta | sqlxTypes.JSONText | POST | Meta data | N/A | NO | | satosaID | string | POST | Satosa ID | N/A | NO | | organisationID | uint64 | POST | Organisation ID | N/A | NO | @@ -347,7 +347,7 @@ An organisation may have many teams. Teams may have many channels available. Acc | name | string | POST | Name | N/A | YES | | handle | string | POST | Handle | N/A | YES | | kind | string | POST | Kind (normal, bot) | N/A | NO | -| meta | types.JSONText | POST | Meta data | N/A | NO | +| meta | sqlxTypes.JSONText | POST | Meta data | N/A | NO | | satosaID | string | POST | Satosa ID | N/A | NO | | organisationID | uint64 | POST | Organisation ID | N/A | NO | diff --git a/sam/rest/request/attachment.go b/sam/rest/request/attachment.go index cf4acb686..308f1dfa9 100644 --- a/sam/rest/request/attachment.go +++ b/sam/rest/request/attachment.go @@ -17,20 +17,17 @@ package request import ( "encoding/json" - "github.com/crusttech/crust/internal/rbac" - "github.com/go-chi/chi" - "github.com/jmoiron/sqlx/types" - "github.com/pkg/errors" "io" "mime/multipart" "net/http" "strings" + + "github.com/go-chi/chi" + "github.com/pkg/errors" ) var _ = chi.URLParam -var _ = types.JSONText{} var _ = multipart.FileHeader{} -var _ = rbac.Operation{} // Attachment original request parameters type AttachmentOriginal struct { diff --git a/sam/rest/request/channel.go b/sam/rest/request/channel.go index 4825225e6..121fa78a6 100644 --- a/sam/rest/request/channel.go +++ b/sam/rest/request/channel.go @@ -17,20 +17,17 @@ package request import ( "encoding/json" - "github.com/crusttech/crust/internal/rbac" - "github.com/go-chi/chi" - "github.com/jmoiron/sqlx/types" - "github.com/pkg/errors" "io" "mime/multipart" "net/http" "strings" + + "github.com/go-chi/chi" + "github.com/pkg/errors" ) var _ = chi.URLParam -var _ = types.JSONText{} var _ = multipart.FileHeader{} -var _ = rbac.Operation{} // Channel list request parameters type ChannelList struct { diff --git a/sam/rest/request/message.go b/sam/rest/request/message.go index 9ca4c4398..1816a7e74 100644 --- a/sam/rest/request/message.go +++ b/sam/rest/request/message.go @@ -17,20 +17,17 @@ package request import ( "encoding/json" - "github.com/crusttech/crust/internal/rbac" - "github.com/go-chi/chi" - "github.com/jmoiron/sqlx/types" - "github.com/pkg/errors" "io" "mime/multipart" "net/http" "strings" + + "github.com/go-chi/chi" + "github.com/pkg/errors" ) var _ = chi.URLParam -var _ = types.JSONText{} var _ = multipart.FileHeader{} -var _ = rbac.Operation{} // Message create request parameters type MessageCreate struct { diff --git a/sam/rest/request/permissions.go b/sam/rest/request/permissions.go index bd5f7386f..7cb9eefa0 100644 --- a/sam/rest/request/permissions.go +++ b/sam/rest/request/permissions.go @@ -17,20 +17,19 @@ package request import ( "encoding/json" - "github.com/crusttech/crust/internal/rbac" - "github.com/go-chi/chi" - "github.com/jmoiron/sqlx/types" - "github.com/pkg/errors" "io" "mime/multipart" "net/http" "strings" + + "github.com/go-chi/chi" + "github.com/pkg/errors" + + "github.com/crusttech/crust/internal/rbac" ) var _ = chi.URLParam -var _ = types.JSONText{} var _ = multipart.FileHeader{} -var _ = rbac.Operation{} // Permissions list request parameters type PermissionsList struct { diff --git a/sam/rest/request/search.go b/sam/rest/request/search.go index 536a5a221..24c010084 100644 --- a/sam/rest/request/search.go +++ b/sam/rest/request/search.go @@ -17,20 +17,17 @@ package request import ( "encoding/json" - "github.com/crusttech/crust/internal/rbac" - "github.com/go-chi/chi" - "github.com/jmoiron/sqlx/types" - "github.com/pkg/errors" "io" "mime/multipart" "net/http" "strings" + + "github.com/go-chi/chi" + "github.com/pkg/errors" ) var _ = chi.URLParam -var _ = types.JSONText{} var _ = multipart.FileHeader{} -var _ = rbac.Operation{} // Search messages request parameters type SearchMessages struct { diff --git a/sam/types/channel.perms.go b/sam/types/channel.perms.go index c09d6e821..0dc908565 100644 --- a/sam/types/channel.perms.go +++ b/sam/types/channel.perms.go @@ -2,7 +2,7 @@ package types import "github.com/crusttech/crust/internal/rbac" -/* File is generated from sam/types/permissions/3-channel.json with permissions.go */ +/* File is generated from sam/types/permissions/3-channel.json with permissions.go */ func (c *Channel) Permissions() []rbac.OperationGroup { return []rbac.OperationGroup{ diff --git a/sam/types/organisation.perms.go b/sam/types/organisation.perms.go index 5549b5440..17faf5b1f 100644 --- a/sam/types/organisation.perms.go +++ b/sam/types/organisation.perms.go @@ -2,7 +2,7 @@ package types import "github.com/crusttech/crust/internal/rbac" -/* File is generated from sam/types/permissions/1-organisation.json with permissions.go */ +/* File is generated from sam/types/permissions/1-organisation.json with permissions.go */ func (c *Organisation) Permissions() []rbac.OperationGroup { return []rbac.OperationGroup{ diff --git a/sam/types/team.perms.go b/sam/types/team.perms.go index 04fad75f7..92e7ffee3 100644 --- a/sam/types/team.perms.go +++ b/sam/types/team.perms.go @@ -2,7 +2,7 @@ package types import "github.com/crusttech/crust/internal/rbac" -/* File is generated from sam/types/permissions/2-team.json with permissions.go */ +/* File is generated from sam/types/permissions/2-team.json with permissions.go */ func (c *Team) Permissions() []rbac.OperationGroup { return []rbac.OperationGroup{ diff --git a/system/rest/request/auth.go b/system/rest/request/auth.go index edf31bb1b..981b1f575 100644 --- a/system/rest/request/auth.go +++ b/system/rest/request/auth.go @@ -17,20 +17,17 @@ package request import ( "encoding/json" - "github.com/crusttech/crust/internal/rbac" - "github.com/go-chi/chi" - "github.com/jmoiron/sqlx/types" - "github.com/pkg/errors" "io" "mime/multipart" "net/http" "strings" + + "github.com/go-chi/chi" + "github.com/pkg/errors" ) var _ = chi.URLParam -var _ = types.JSONText{} var _ = multipart.FileHeader{} -var _ = rbac.Operation{} // Auth check request parameters type AuthCheck struct { diff --git a/system/rest/request/organisation.go b/system/rest/request/organisation.go index a1e9525fc..88cb4243e 100644 --- a/system/rest/request/organisation.go +++ b/system/rest/request/organisation.go @@ -17,20 +17,17 @@ package request import ( "encoding/json" - "github.com/crusttech/crust/internal/rbac" - "github.com/go-chi/chi" - "github.com/jmoiron/sqlx/types" - "github.com/pkg/errors" "io" "mime/multipart" "net/http" "strings" + + "github.com/go-chi/chi" + "github.com/pkg/errors" ) var _ = chi.URLParam -var _ = types.JSONText{} var _ = multipart.FileHeader{} -var _ = rbac.Operation{} // Organisation list request parameters type OrganisationList struct { diff --git a/system/rest/request/team.go b/system/rest/request/team.go index b0a39fe34..90753a873 100644 --- a/system/rest/request/team.go +++ b/system/rest/request/team.go @@ -17,20 +17,17 @@ package request import ( "encoding/json" - "github.com/crusttech/crust/internal/rbac" - "github.com/go-chi/chi" - "github.com/jmoiron/sqlx/types" - "github.com/pkg/errors" "io" "mime/multipart" "net/http" "strings" + + "github.com/go-chi/chi" + "github.com/pkg/errors" ) var _ = chi.URLParam -var _ = types.JSONText{} var _ = multipart.FileHeader{} -var _ = rbac.Operation{} // Team list request parameters type TeamList struct { diff --git a/system/rest/request/user.go b/system/rest/request/user.go index 11982de2c..b87d3dabb 100644 --- a/system/rest/request/user.go +++ b/system/rest/request/user.go @@ -17,20 +17,19 @@ package request import ( "encoding/json" - "github.com/crusttech/crust/internal/rbac" - "github.com/go-chi/chi" - "github.com/jmoiron/sqlx/types" - "github.com/pkg/errors" "io" "mime/multipart" "net/http" "strings" + + "github.com/go-chi/chi" + "github.com/pkg/errors" + + sqlxTypes "github.com/jmoiron/sqlx/types" ) var _ = chi.URLParam -var _ = types.JSONText{} var _ = multipart.FileHeader{} -var _ = rbac.Operation{} // User list request parameters type UserList struct { @@ -86,7 +85,7 @@ type UserCreate struct { Name string Handle string Kind string - Meta types.JSONText + Meta sqlxTypes.JSONText SatosaID string OrganisationID uint64 `json:",string"` } @@ -148,7 +147,7 @@ func (u *UserCreate) Fill(r *http.Request) (err error) { } if val, ok := post["meta"]; ok { - if u.Meta, err = parseJSONText(val); err != nil { + if u.Meta, err = parseJSONTextWithErr(val); err != nil { return err } } @@ -175,7 +174,7 @@ type UserEdit struct { Name string Handle string Kind string - Meta types.JSONText + Meta sqlxTypes.JSONText SatosaID string OrganisationID uint64 `json:",string"` } @@ -238,7 +237,7 @@ func (u *UserEdit) Fill(r *http.Request) (err error) { } if val, ok := post["meta"]; ok { - if u.Meta, err = parseJSONText(val); err != nil { + if u.Meta, err = parseJSONTextWithErr(val); err != nil { return err } } diff --git a/system/rest/request/util.go b/system/rest/request/util.go index f8b7fda70..4451032fd 100644 --- a/system/rest/request/util.go +++ b/system/rest/request/util.go @@ -11,7 +11,7 @@ import ( var truthy = regexp.MustCompile("^\\s*(t(rue)?|y(es)?|1)\\s*$") -func parseJSONText(s string) (types.JSONText, error) { +func parseJSONTextWithErr(s string) (types.JSONText, error) { result := &types.JSONText{} err := errors.Wrap(result.Scan(s), "error when parsing JSONText") return *result, err