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:
parent
ec37bca996
commit
bb4917d737
3 changed files with 22 additions and 1 deletions
|
@ -133,6 +133,16 @@ func genericMarshal(in interface{}) ([]byte, error) {
|
|||
}
|
||||
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:
|
||||
for _, val := range valField.Interface().([]int) {
|
||||
values = append(values, strconv.FormatInt(int64(val), 10))
|
||||
|
|
|
@ -173,6 +173,17 @@ func genericUnmarshal(in []byte, out interface{}, unit *Unit) error {
|
|||
}
|
||||
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:
|
||||
var v []int
|
||||
for _, bv := range ba {
|
||||
|
|
|
@ -17,7 +17,7 @@ func float2sii(f float32) ([]byte, 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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue