v2.go: Use custom JSON value type to convert numbers to strings.

This commit is contained in:
luk3yx 2020-06-28 09:40:01 +12:00
parent 42f4ccefc6
commit 774278ac6a
2 changed files with 14 additions and 4 deletions

View File

@ -39,8 +39,19 @@ type v2Form interface {
Get(string) string Get(string) string
} }
// Converts non-string values (such as numbers) to strings.
type v2JsonValue string
func (self *v2JsonValue) UnmarshalJSON(data []byte) error {
if len(data) > 0 && data[0] == '"' {
return json.Unmarshal(data, (*string)(self))
}
*self = v2JsonValue(data)
return nil
}
type v2MapForm struct { type v2MapForm struct {
form map[string]json.Number form map[string]v2JsonValue
} }
func (self *v2MapForm) Get(key string) string { func (self *v2MapForm) Get(key string) string {
@ -62,8 +73,7 @@ func v2GetQuery(r *http.Request) v2Form {
return r.Form return r.Form
} }
// Because json.Number extends string it can be used for strings. form := make(map[string]v2JsonValue)
form := make(map[string]json.Number)
(&HTTPRequest{Request: r}).Unmarshal(&form) (&HTTPRequest{Request: r}).Unmarshal(&form)
return &v2MapForm{form} return &v2MapForm{form}
} }

View File

@ -35,7 +35,7 @@ import (
) )
const SYMBOL = "¤" const SYMBOL = "¤"
const VERSION = "3.0.2" const VERSION = "3.0.3"
// Note that public source code is required by the AGPL // Note that public source code is required by the AGPL
const SOURCE_URL = "https://github.com/luk3yx/lurkcoin-core" const SOURCE_URL = "https://github.com/luk3yx/lurkcoin-core"