Skip to content

Commit

Permalink
implement array request
Browse files Browse the repository at this point in the history
  • Loading branch information
spali committed Jul 30, 2021
1 parent 78bd126 commit cf46781
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
25 changes: 25 additions & 0 deletions cmd/e3dc/json_input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,31 @@ func Test_unmarshalJSONRequest(t *testing.T) {
rscp.Message{Tag: rscp.INFO_SET_TIME, DataType: rscp.Timestamp, Value: time.Date(1234, 5, 6, 7, 8, 9, 123456000, time.UTC)},
false,
},
{`bytearray value`,
`["WB_EXTERN_DATA",[0,6,0,0,0,0]]`,
rscp.Message{Tag: rscp.WB_EXTERN_DATA, DataType: rscp.ByteArray, Value: []byte{0x0, 0x6, 0x0, 0x0, 0x0, 0x0}},
false,
},
{`empty bytearray value`,
`["WB_EXTERN_DATA",[]]`,
rscp.Message{Tag: rscp.WB_EXTERN_DATA, DataType: rscp.ByteArray, Value: []byte{}},
false,
},
{`bytearray invalid number`,
`["WB_EXTERN_DATA",1]`,
rscp.Message{Tag: rscp.WB_EXTERN_DATA, DataType: rscp.ByteArray, Value: nil},
true,
},
{`bytearray invalid string`,
`["WB_EXTERN_DATA","S"]`,
rscp.Message{Tag: rscp.WB_EXTERN_DATA, DataType: rscp.ByteArray, Value: nil},
true,
},
{`bytearray invalid string array`,
`["WB_EXTERN_DATA",["S"]]`,
rscp.Message{Tag: rscp.WB_EXTERN_DATA, DataType: rscp.ByteArray, Value: nil},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
23 changes: 20 additions & 3 deletions rscp/message_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (m *Message) UnmarshalJSONValue(jm json.RawMessage) error {
} else if jm != nil && m.DataType != None {
var tmp interface{}
// TODO: we need to cleanup generic data type handling somewhen to prevent such hacks
if m.DataType == Timestamp {
if m.DataType == Timestamp || m.DataType == ByteArray {
tmp = m.DataType.newEmpty(0)
} else {
tmp = reflect.ValueOf(m.DataType.newEmpty(0)).Elem().Interface()
Expand All @@ -32,13 +32,30 @@ func (m *Message) UnmarshalJSONValue(jm json.RawMessage) error {
return fmt.Errorf("could not convert value '%s' for data type %s: %s", jm, m.DataType, err)
}
// convert number values to expected data type (json does by default unmarshal to float64)
v, isFloat := tmp.(float64)
if isFloat {
if v, isFloat := tmp.(float64); isFloat {
var err error
if tmp, err = m.DataType.new(v); err != nil {
return fmt.Errorf("could not convert number value '%f' for data type %s", v, m.DataType)
}
}
// convert number array to byte array (json does by default unmarshal to []float64)
if m.DataType == ByteArray {
var arr []interface{}
var isInterfaceArray bool
if arr, isInterfaceArray = tmp.([]interface{}); !isInterfaceArray {
return fmt.Errorf("could not convert byte array value '%v' for data type %s", tmp, m.DataType)
}
l := len(arr)
tmp = make([]byte, l)
for i := 0; i < l; i++ {
var v float64
var isFloat bool
if v, isFloat = arr[i].(float64); !isFloat {
return fmt.Errorf("could not convert byte array value '%v' for data type %s", tmp, m.DataType)
}
tmp.([]uint8)[i] = uint8(v)
}
}
// TODO: we need to cleanup generic data type handling somewhen to prevent such hacks
if m.DataType == Timestamp {
m.Value = reflect.ValueOf(tmp).Elem().Interface()
Expand Down

0 comments on commit cf46781

Please sign in to comment.