1
0
Fork 0
mirror of https://github.com/Luzifer/sii.git synced 2024-10-18 05:14:19 +00:00

Support nil values in ints and RawValue unmarshalling

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2019-11-10 15:49:54 +01:00
parent a061da28b0
commit a40e9a5c6e
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

View file

@ -79,7 +79,11 @@ func genericUnmarshal(in []byte, out interface{}, unit *Unit) error {
valField.Set(reflect.ValueOf(v))
case reflect.Int, reflect.Int64:
v, err := strconv.ParseInt(string(getSingleValue(in, attributeName)), 10, 64)
bv := getSingleValue(in, attributeName)
if isNilValue(bv) {
continue
}
v, err := strconv.ParseInt(string(bv), 10, 64)
if err != nil {
return errors.Wrapf(err, "Unable to parse int for attribute %q", attributeName)
}
@ -121,7 +125,19 @@ func genericUnmarshal(in []byte, out interface{}, unit *Unit) error {
for _, bv := range ba {
e := Placement{}
if err := e.UnmarshalSII(bv); err != nil {
return errors.Wrapf(err, "Unable to parse Ptr for attribute %q", attributeName)
return errors.Wrapf(err, "Unable to parse Placement for attribute %q", attributeName)
}
v = append(v, e)
}
valField.Set(reflect.ValueOf(v))
continue
case reflect.TypeOf(RawValue{}):
var v []RawValue
for _, bv := range ba {
e := RawValue{}
if err := e.UnmarshalSII(bv); err != nil {
return errors.Wrapf(err, "Unable to parse RawValue for attribute %q", attributeName)
}
v = append(v, e)
}
@ -229,3 +245,7 @@ func getArrayValues(in []byte, name string) ([][]byte, error) {
return out, nil
}
func isNilValue(in []byte) bool {
return reflect.DeepEqual(in, []byte("nil"))
}