-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1603,6 +1603,7 @@ func parseSendDataPayload(payload string, mappingType interfaces.AstarteMappingT | |
// Default to string, as it will be ok for most cases | ||
var ret interface{} = payload | ||
var err error | ||
|
||
switch mappingType { | ||
case interfaces.Double: | ||
if ret, err = strconv.ParseFloat(payload, 64); err != nil { | ||
|
@@ -1634,32 +1635,42 @@ func parseSendDataPayload(payload string, mappingType interfaces.AstarteMappingT | |
case interfaces.BinaryBlobArray, interfaces.BooleanArray, interfaces.DateTimeArray, interfaces.DoubleArray, | ||
interfaces.IntegerArray, interfaces.LongIntegerArray, interfaces.StringArray: | ||
|
||
//wait it's all string? | ||
payload = strings.Replace(payload, "[", "", -1) | ||
payload = strings.Replace(payload, "]", "", -1) | ||
payload_parsed := strings.Split(payload, ",") | ||
//always has been | ||
//check if payload is an empty array, bypass conversions and just return an empty array | ||
// if it is not, proceed as usual | ||
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 | ||
|
||
jsonOut := make([]interface{}, len(payload_parsed)) | ||
for i, v := range payload_parsed { | ||
jsonOut[i] = v | ||
} | ||
|
||
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 | ||
} | ||
retArray = append(retArray, p) | ||
default: | ||
retArray = append(retArray, val) | ||
Comment on lines
+1667
to
+1668
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we ever reach this point? I'm not sure, and in case we hit this line we'd append a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
looks like that's the case, command line always passes a string and that's why this problem arise There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A type assertion could be used then, as @matt-mazzucato suggested |
||
} | ||
retArray = append(retArray, p) | ||
default: | ||
retArray = append(retArray, val) | ||
} | ||
ret = retArray | ||
} | ||
ret = retArray | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not add extra lines if not needed. |
||
} | ||
|
||
return ret, nil | ||
|
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:
[<comma separated data>]
[<comma separated data>]
value is a valid JSON objectjson.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: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