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

Support slice of float32, display long floats as hex format

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2019-12-11 17:16:48 +01:00
parent ec37bca996
commit bb4917d737
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
3 changed files with 22 additions and 1 deletions

View file

@ -133,6 +133,16 @@ func genericMarshal(in interface{}) ([]byte, error) {
} }
buf.Write(encodeSliceValue(attributeName, values)) buf.Write(encodeSliceValue(attributeName, values))
case reflect.Float32:
for _, val := range valField.Interface().([]float32) {
v, err := float2sii(val)
if err != nil {
return nil, errors.Wrap(err, "Unable to encode float32")
}
values = append(values, string(v))
}
buf.Write(encodeSliceValue(attributeName, values))
case reflect.Int: case reflect.Int:
for _, val := range valField.Interface().([]int) { for _, val := range valField.Interface().([]int) {
values = append(values, strconv.FormatInt(int64(val), 10)) values = append(values, strconv.FormatInt(int64(val), 10))

View file

@ -173,6 +173,17 @@ func genericUnmarshal(in []byte, out interface{}, unit *Unit) error {
} }
valField.Set(reflect.ValueOf(v)) valField.Set(reflect.ValueOf(v))
case reflect.Float32:
var v []float32
for _, bv := range ba {
pbv, err := sii2float(bv)
if err != nil {
return errors.Wrapf(err, "Unable to parse float32 for attribute %q", attributeName)
}
v = append(v, pbv)
}
valField.Set(reflect.ValueOf(v))
case reflect.Int: case reflect.Int:
var v []int var v []int
for _, bv := range ba { for _, bv := range ba {

View file

@ -17,7 +17,7 @@ func float2sii(f float32) ([]byte, error) {
err error err error
) )
if math.Floor(float64(f)) == float64(f) { if math.Floor(float64(f)) == float64(f) && f < 1000 {
return []byte(fmt.Sprintf("%.0f", f)), nil return []byte(fmt.Sprintf("%.0f", f)), nil
} }