Skip to content
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

Open
wants to merge 1 commit into
base: release-23.5
Choose a base branch
from

Conversation

eddbbt
Copy link
Contributor

@eddbbt eddbbt commented May 6, 2024

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

@eddbbt eddbbt changed the title Add exception for empty array when publishing data Correctly send empty array to astarte May 6, 2024
@eddbbt eddbbt force-pushed the fix_empty_array_parser branch 2 times, most recently from a6bd1a6 to f03db4a Compare May 6, 2024 13:47
@eddbbt eddbbt marked this pull request as draft May 6, 2024 13:49
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>
@eddbbt eddbbt marked this pull request as ready for review May 6, 2024 14:09
Comment on lines +1640 to +1649
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
Copy link
Collaborator

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"]'

Copy link
Contributor Author

@eddbbt eddbbt May 7, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants