Skip to content
Snippets Groups Projects
Commit 8f8cf91d authored by Jan Semmelink's avatar Jan Semmelink
Browse files

Update parsing not to go into sub structs which has their own parsers, e.g. time.Time

parent 00b0546b
No related branches found
No related tags found
1 merge request!12One more change to detect when custom types (like time.Time) has their own parsers, then do not step into those structs.
......@@ -52,7 +52,6 @@ func NamedValuesFromReader(prefix string, reader string_utils.KeyReader) map[str
logger.Debugf("Key(%s) undefined", key)
continue
}
logger.Debugf("key(%s)=\"%s\"", key, value)
result[strings.ToLower(key)] = []string{value}
//split only if valid CSV between [...]
......@@ -224,7 +223,15 @@ func unmarshalNamedValuesIntoStructPtr(prefix string, namedValues map[string][]s
}
}
//recurse into anonymous sub-structs
//if type can scan/unmarshal, do not recurse into its fields
//because we do not want to iterate over fields inside structs like time.Time
//that implements their own parsers
{
fieldPtr := structPtrValue.Elem().Field(i).Addr().Interface()
_, ok1 := fieldPtr.(sql.Scanner)
_, ok2 := fieldPtr.(json.Unmarshaler)
if !ok1 && !ok2 {
//recurse into sub-structs
if structTypeField.Type.Kind() == reflect.Struct {
if nameList, err := unmarshalNamedValuesIntoStructPtr(fieldName, namedValues, structTypeField.Type, structPtrValue.Elem().Field(i).Addr()); err != nil {
return nil, errors.Wrapf(err, "failed on %s.%s", structType.Name(), fieldName)
......@@ -233,6 +240,8 @@ func unmarshalNamedValuesIntoStructPtr(prefix string, namedValues map[string][]s
}
continue
}
}
}
fieldValues, ok := namedValues[fieldName]
if !ok {
......@@ -244,6 +253,7 @@ func unmarshalNamedValuesIntoStructPtr(prefix string, namedValues map[string][]s
continue //field has no value specified in URL, do not remove values not defined (cannot clear defined struct fields with named values)
}
structPtrFieldValue := structPtrValue.Elem().Field(i)
if structPtrFieldValue.Kind() == reflect.Ptr {
//this is a ptr, allocate a new value and set it
//then we can dereference to set it below
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment