Fixing unmarshalling of numberOrString

New unmarshalling rules for fields which could be numeric or string
values were not properly typecasted upon being deserialized.

Cleaned up the api template file and moved custom unmarshalling type
logic into go code out of the template.
This commit is contained in:
James Stephenson
2021-04-16 09:47:21 -04:00
committed by Paul Tyng
parent 4ab4036985
commit fbed685c37
4 changed files with 48 additions and 26 deletions

View File

@@ -98,13 +98,14 @@ type Resource struct {
}
type FieldInfo struct {
FieldName string
JSONName string
FieldType string
FieldValidation string
OmitEmpty bool
IsArray bool
Fields map[string]*FieldInfo
FieldName string
JSONName string
FieldType string
FieldValidation string
OmitEmpty bool
IsArray bool
Fields map[string]*FieldInfo
CustomUnmarshalType string
}
func NewResource(structName string, resourcePath string) *Resource {
@@ -278,6 +279,16 @@ func main() {
}
return nil
}
case "ChannelPlan":
resource.FieldProcessor = func(name string, f *FieldInfo) error {
switch name {
case "Channel", "BackupChannel", "TxPower":
if f.FieldType == "string" {
f.CustomUnmarshalType = "numberOrString"
}
}
return nil
}
case "Device":
resource.FieldProcessor = func(name string, f *FieldInfo) error {
switch name {
@@ -285,6 +296,11 @@ func main() {
f.FieldType = "float64"
case "StpPriority", "Ht":
f.FieldType = "string"
f.CustomUnmarshalType = ""
case "Channel", "BackupChannel", "TxPower":
if f.FieldType == "string" {
f.CustomUnmarshalType = "numberOrString"
}
}
f.OmitEmpty = true
@@ -294,6 +310,7 @@ func main() {
resource.FieldProcessor = func(name string, f *FieldInfo) error {
if strings.HasSuffix(name, "Timeout") && name != "ArpCacheTimeout" {
f.FieldType = "int"
f.CustomUnmarshalType = "emptyStringInt"
}
return nil
}
@@ -304,6 +321,7 @@ func main() {
f.FieldType = "bool"
case "LastSeen":
f.FieldType = "int"
f.CustomUnmarshalType = "emptyStringInt"
}
return nil
}
@@ -416,6 +434,7 @@ func (r *Resource) fieldInfoFromValidation(name string, validation interface{})
omitEmpty = true
fieldInfo, err = NewFieldInfo(fieldName, name, "int", fieldValidation, omitEmpty, false), nil
fieldInfo.CustomUnmarshalType = "emptyStringInt"
return fieldInfo, r.FieldProcessor(fieldName, fieldInfo)
}
}