From e36621f0164f5ac41f15765137c44ef5bd8e3ac2 Mon Sep 17 00:00:00 2001 From: Tit Petric Date: Thu, 25 Oct 2018 12:10:35 +0200 Subject: [PATCH] upd(all): parse uint64 from post arrays --- codegen/codegen.php | 1 + codegen/templates/http_request_inline.tpl | 2 ++ crm/rest/request/page.go | 1 + crm/rest/request/util.go | 16 +++++++++++--- sam/rest/request/channel.go | 1 + sam/rest/request/team.go | 2 ++ sam/rest/request/util.go | 26 +++++++++++++++++++---- 7 files changed, 42 insertions(+), 7 deletions(-) diff --git a/codegen/codegen.php b/codegen/codegen.php index 85fa984ac..540ba1fb7 100755 --- a/codegen/codegen.php +++ b/codegen/codegen.php @@ -76,6 +76,7 @@ usort($apis, function($a, $b) { $parsers = array( "uint64" => "parseUInt64", + "[]uint64" => "parseUInt64A", "int" => "parseInt", "bool" => "parseBool", "types.JSONText" => "parseJSONText", diff --git a/codegen/templates/http_request_inline.tpl b/codegen/templates/http_request_inline.tpl index c8f7f1d76..dd1c5d5b2 100644 --- a/codegen/templates/http_request_inline.tpl +++ b/codegen/templates/http_request_inline.tpl @@ -63,6 +63,8 @@ func ({self} *{name|expose}{call.name|capitalize}) Fill(r *http.Request) (err er {foreach $params as $param} {if strtolower($method) === "path"} {self}.{param.name|expose} = {if ($param.type !== "string")}{$parsers[$param.type]}({/if}chi.URLParam(r, "{param.name}"){if ($param.type !== "string")}){/if} +{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") diff --git a/crm/rest/request/page.go b/crm/rest/request/page.go index f4a1f3302..7d3d57ecc 100644 --- a/crm/rest/request/page.go +++ b/crm/rest/request/page.go @@ -305,6 +305,7 @@ func (p *PageReorder) Fill(r *http.Request) (err error) { } p.SelfID = parseUInt64(chi.URLParam(r, "selfID")) + p.PageIDs = parseUInt64A(r.Form["pageIDs"]) return err } diff --git a/crm/rest/request/util.go b/crm/rest/request/util.go index eb27222f2..f8b7fda70 100644 --- a/crm/rest/request/util.go +++ b/crm/rest/request/util.go @@ -26,7 +26,7 @@ func parseInt(s string) int { return i } -// parseInt64 parses an string to int64 +// parseInt64 parses a string to int64 func parseInt64(s string) int64 { if s == "" { return 0 @@ -35,7 +35,7 @@ func parseInt64(s string) int64 { return i } -// parseUInt64 parses an string to uint64 +// parseUInt64 parses a string to uint64 func parseUInt64(s string) uint64 { if s == "" { return 0 @@ -44,7 +44,17 @@ func parseUInt64(s string) uint64 { return i } -// parseUInt64 parses an string to uint64 +func parseUInt64A(values []string) []uint64 { + var result []uint64 + if values != nil && len(values) > 0 { + for _, val := range values { + result = append(result, parseUInt64(val)) + } + } + return result +} + +// parseUInt64 parses a string to uint64 func parseBool(s string) bool { return truthy.MatchString(strings.ToLower(s)) } diff --git a/sam/rest/request/channel.go b/sam/rest/request/channel.go index a7c428b8a..5da7ac79c 100644 --- a/sam/rest/request/channel.go +++ b/sam/rest/request/channel.go @@ -458,6 +458,7 @@ func (c *ChannelInvite) Fill(r *http.Request) (err error) { } c.ChannelID = parseUInt64(chi.URLParam(r, "channelID")) + c.UserID = parseUInt64A(r.Form["userID"]) return err } diff --git a/sam/rest/request/team.go b/sam/rest/request/team.go index 823cc2f5e..13f57d8c3 100644 --- a/sam/rest/request/team.go +++ b/sam/rest/request/team.go @@ -117,6 +117,7 @@ func (t *TeamCreate) Fill(r *http.Request) (err error) { t.Name = val } + t.Members = parseUInt64A(r.Form["members"]) return err } @@ -166,6 +167,7 @@ func (t *TeamEdit) Fill(r *http.Request) (err error) { t.Name = val } + t.Members = parseUInt64A(r.Form["members"]) return err } diff --git a/sam/rest/request/util.go b/sam/rest/request/util.go index 652e6b662..f8b7fda70 100644 --- a/sam/rest/request/util.go +++ b/sam/rest/request/util.go @@ -17,17 +17,25 @@ func parseJSONText(s string) (types.JSONText, error) { return *result, err } -// parseInt64 parses an string to int64 +// parseInt parses a string to int +func parseInt(s string) int { + if s == "" { + return 0 + } + i, _ := strconv.Atoi(s) + return i +} + +// parseInt64 parses a string to int64 func parseInt64(s string) int64 { if s == "" { return 0 } i, _ := strconv.ParseInt(s, 10, 64) - return i } -// parseUInt64 parses an string to uint64 +// parseUInt64 parses a string to uint64 func parseUInt64(s string) uint64 { if s == "" { return 0 @@ -36,7 +44,17 @@ func parseUInt64(s string) uint64 { return i } -// parseUInt64 parses an string to uint64 +func parseUInt64A(values []string) []uint64 { + var result []uint64 + if values != nil && len(values) > 0 { + for _, val := range values { + result = append(result, parseUInt64(val)) + } + } + return result +} + +// parseUInt64 parses a string to uint64 func parseBool(s string) bool { return truthy.MatchString(strings.ToLower(s)) }