Skip to content

Commit

Permalink
Merge pull request #213 from matt-mazzucato/fix-appengine-send-binary…
Browse files Browse the repository at this point in the history
…blob

Fix appengine send-data for binaryblob
  • Loading branch information
Annopaolo authored Jul 18, 2023
2 parents 59cb00c + dab6dc3 commit c09a9c8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- `realm-management interfaces {install,upgrade}` commands are run synchronously.
- Use Go 1.20 for releases.

### Fixed
- `appengine send-data`: fix the encoding of binaryblob and binaryblobarray data.

## [22.11.02] - 23/05/2023
### Changed
- `appengine device`: print a parametric command rather than a partial one with
Expand Down
46 changes: 43 additions & 3 deletions cmd/appengine/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -1094,14 +1094,54 @@ func devicesSendDataF(command *cobra.Command, args []string) error {
// correctly, we should convert to int every payload for which an integer conversion does not lose
// in precision
for k, v := range aggrPayload {
// since we're dealing with object aggregation, we need to reconstruct
// the full path to get the mapping and its type
fullPath := fmt.Sprintf("%s/%s", interfacePath, k)
mapping, err := interfaces.InterfaceMappingFromPath(iface, fullPath)
if err != nil {
return err
}

// now we have a mapping type
payloadType = mapping.Type

switch val := v.(type) {
case float64:
if val == math.Trunc(val) {
aggrPayload[k] = int(val)
}
case string:
// in case the type is binaryblob, we want the value as []byte
if payloadType == interfaces.BinaryBlob {
decoded, err := base64.StdEncoding.DecodeString(string(val))
if err != nil {
fmt.Fprintln(os.Stderr, "Input string is not base64 encoded.", err)
os.Exit(1)
}
aggrPayload[k] = decoded
}
case []interface{}:
// in case the type is binaryblobarray, we want the values as [][]byte
if payloadType == interfaces.BinaryBlobArray {
acc := [][]byte{}
for _, item := range val {
theString, ok := item.(string)
if !ok {
fmt.Fprintf(os.Stderr, "Invalid item while handling endpoint %s\n", mapping.Endpoint)
os.Exit(1)
}
decoded, err := base64.StdEncoding.DecodeString(theString)
if err != nil {
fmt.Fprintln(os.Stderr, "Input string is not base64 encoded.")
os.Exit(1)
}

acc = append(acc, decoded)
}
aggrPayload[k] = acc
}
}
}

parsedPayloadData = aggrPayload
}

Expand Down Expand Up @@ -1284,8 +1324,8 @@ func parseSendDataPayload(payload string, mappingType interfaces.AstarteMappingT
return nil, err
}
case interfaces.BinaryBlob:
// We have to verify base64 decoding works
if _, err := base64.StdEncoding.DecodeString(payload); err != nil {
// if we're dealing with binaryblobs, we want to return a []byte
if ret, err = base64.StdEncoding.DecodeString(payload); err != nil {
return nil, err
}
case interfaces.DateTime:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
code.cloudfoundry.org/bytefmt v0.0.0-20211005130812-5bb3c17173e5
github.com/Masterminds/semver/v3 v3.1.1
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/astarte-platform/astarte-go v0.91.0
github.com/astarte-platform/astarte-go v0.91.1-0.20230718084224-d5bbb47f5179
github.com/go-openapi/strfmt v0.21.1 // indirect
github.com/google/go-cmp v0.5.8
github.com/google/go-github/v30 v30.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgI
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef h1:46PFijGLmAjMPwCCCo7Jf0W6f9slllCkkv7vyc1yOSg=
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/astarte-platform/astarte-go v0.91.0 h1:VEsTKfyEDxFzKOQLcgS9HHPiiC0p5eMkj+luSiQ3AwE=
github.com/astarte-platform/astarte-go v0.91.0/go.mod h1:6e/IkwjAS7fXdCerA/xr/Fcv6OseNRhDFytuhCGfjvM=
github.com/astarte-platform/astarte-go v0.91.1-0.20230718084224-d5bbb47f5179 h1:/RJX27SPJ7XmfGAp1RhNSFtuMYjBKyqTk/LDZpbdLUQ=
github.com/astarte-platform/astarte-go v0.91.1-0.20230718084224-d5bbb47f5179/go.mod h1:XZUDeZxSGG9Z1bQJm/x5B58S7Azhu2mAdVaZV+XrEK0=
github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down

0 comments on commit c09a9c8

Please sign in to comment.