-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Correctly send empty array to astarte #247
base: release-23.5
Are you sure you want to change the base?
Correctly send empty array to astarte #247
Conversation
a6bd1a6
to
f03db4a
Compare
f03db4a
to
5e717c4
Compare
Add exception for enabling empty array sending (that differs from arrays with one null element) An empty array with corrrect type is now sent to astarte Signed-off-by: Eddy Babetto <eddy.babetto@secomind.com>
5e717c4
to
912a37d
Compare
if payload == "[]" { | ||
ret = make([]interface{}, 0) | ||
|
||
jsonOut := make([]interface{}, len(payload_parsed)) | ||
for i, v := range payload_parsed { | ||
jsonOut[i] = v | ||
} | ||
} else { | ||
|
||
retArray := []interface{}{} | ||
// Do a smarter conversion here. | ||
for _, v := range jsonOut { | ||
switch val := v.(type) { | ||
case string: | ||
p, err := parseSendDataPayload(strings.TrimSpace(val), interfaces.AstarteMappingType(strings.TrimSuffix(string(mappingType), "array"))) | ||
if err != nil { | ||
return nil, err | ||
//wait it's all string? | ||
payload = strings.Replace(payload, "[", "", -1) | ||
payload = strings.Replace(payload, "]", "", -1) | ||
payload_parsed := strings.Split(payload, ",") | ||
//always has been |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest using a different approach for parsing the array data. We know that:
- The input payload has the form
[<comma separated data>]
- A
[<comma separated data>]
value is a valid JSON object - Go's
json.Unmarshal
is able to handle these objects (see https://go.dev/play/p/ixaicx2S7cj).
Therefore, my suggestion is to let Go handle characters such as [
and just do the conversion of payloads, something like:
var jsonPayload []interface{}
if err := json.Unmarshal([]byte(payload), &jsonPayload); err != nil {
fmt.Println(err) //Maybe also warn the user of the bad input payload
System.exit(1)
}
retArray := []interface{}{}
// Do a smarter conversion here.
for _, v := range jsonPayload {
// etc etc, the rest is the same
This would mean that the user will have to write a valid JSON payload, e.g. strings inside arrays should be put in double quotes, like astartectl appengine devices publish-datastream <device_id> <interface> <path> '["a", "b", "c"]'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good, but we need to check that requiring an array of explicit strings ('["a", "b", "c"]'
) will not break compatibility with already existent implementations (if any).
Current string interpolation was added in #235
Fix send of empty array to astarte, that defaulted to empty string that was obviously not the correct payload.
Now it is sent as an empty array.
Please note that an empty array differs from an array with one element of value "empty" or zero, that's why i did not edit the existent parser solution but added another one instead