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

Support slice of array of float32

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2019-12-11 19:21:49 +01:00
parent 8ab2291202
commit f324361cf8
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
2 changed files with 98 additions and 0 deletions

View file

@ -199,6 +199,51 @@ func genericMarshal(in interface{}) ([]byte, error) {
}
buf.Write(encodeSliceValue(attributeName, values))
case reflect.Array:
switch typeField.Type.Elem().Elem().Kind() {
case reflect.Float32:
switch typeField.Type.Elem().Len() {
case 3:
for _, val := range valField.Interface().([][3]float32) {
var vals [][]byte
for _, v := range val {
bv, err := float2sii(v)
if err != nil {
return nil, errors.Wrap(err, "Unable to encode float32")
}
vals = append(vals, bv)
}
values = append(values, fmt.Sprintf("(%s)", bytes.Join(vals, []byte(", "))))
}
case 4:
for _, val := range valField.Interface().([][4]float32) {
var vals [][]byte
for _, v := range val {
bv, err := float2sii(v)
if err != nil {
return nil, errors.Wrap(err, "Unable to encode float32")
}
vals = append(vals, bv)
}
values = append(values, fmt.Sprintf("(%s; %s)", vals[0], bytes.Join(vals[1:], []byte(", "))))
}
default:
return nil, errors.Errorf("Unsupported len of type: [][%d]%s", typeField.Type.Elem().Len(), typeField.Type.Elem().Elem().Kind())
}
default:
return nil, errors.Errorf("Unsupported type: [][%d]%s", typeField.Type.Elem().Len(), typeField.Type.Elem().Elem().Kind())
}
buf.Write(encodeSliceValue(attributeName, values))
default:
return nil, errors.Errorf("Unsupported type: []%s", typeField.Type.Elem().Kind())

View file

@ -253,6 +253,59 @@ func genericUnmarshal(in []byte, out interface{}, unit *Unit) error {
}
valField.Set(reflect.ValueOf(v))
case reflect.Array:
switch typeField.Type.Elem().Elem().Kind() {
case reflect.Float32:
switch typeField.Type.Elem().Len() {
case 3:
var v [][3]float32
for _, bv := range ba {
grps := regexp.MustCompile(`^\(([0-9.-]+|&[0-9a-f]+), ([0-9.-]+|&[0-9a-f]+), ([0-9.-]+|&[0-9a-f]+)\)$`).
FindSubmatch(bv)
var sv [3]float32
for i := range sv {
val, err := sii2float(grps[i+1][:])
if err != nil {
return errors.Wrapf(err, "Unable to parse float32 for attribute %q", attributeName)
}
sv[i] = val
}
v = append(v, sv)
}
valField.Set(reflect.ValueOf(v))
case 4:
var v [][4]float32
for _, bv := range ba {
grps := regexp.MustCompile(`^\(([0-9.-]+|&[0-9a-f]+); ([0-9.-]+|&[0-9a-f]+), ([0-9.-]+|&[0-9a-f]+), ([0-9.-]+|&[0-9a-f]+)\)$`).
FindSubmatch(bv)
var sv [4]float32
for i := range sv {
val, err := sii2float(grps[i+1][:])
if err != nil {
return errors.Wrapf(err, "Unable to parse float32 for attribute %q", attributeName)
}
sv[i] = val
}
v = append(v, sv)
}
valField.Set(reflect.ValueOf(v))
default:
return errors.Errorf("Unsupported len of type: [][%d]%s", typeField.Type.Elem().Len(), typeField.Type.Elem().Elem().Kind())
}
default:
return errors.Errorf("Unsupported type: [][%d]%s", typeField.Type.Elem().Len(), typeField.Type.Elem().Elem().Kind())
}
default:
return errors.Errorf("Unsupported type: []%s", typeField.Type.Elem().Kind())
}