Skip to content

Commit

Permalink
as: Support returning no normalized payload
Browse files Browse the repository at this point in the history
  • Loading branch information
johanstokking committed Sep 1, 2022
1 parent 2c2ee5c commit 4fe002c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/messageprocessors/javascript/javascript.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,14 @@ func (*host) decodeUplink(
return errOutput.WithCause(err)
}
msg.DecodedPayload, msg.DecodedPayloadWarnings = decodedPayload, output.Decoded.Warnings
msg.NormalizedPayload, msg.NormalizedPayloadWarnings = nil, nil

if normalized := output.Normalized; normalized != nil {
if errs := normalized.Errors; len(errs) > 0 {
return errOutputErrors.WithAttributes("errors", strings.Join(errs, ", "))
}
if normalized.Data == nil {
return errOutput.New()
return nil
}
// The returned data can be an array of measurements or a single measurement object.
var measurements []map[string]interface{}
Expand Down
43 changes: 43 additions & 0 deletions pkg/messageprocessors/javascript/javascript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,49 @@ func TestDecodeUplink(t *testing.T) {
a.So(errors.IsAborted(err), should.BeTrue)
}

// Return no normalized payload (data is nil).
{
script := `
function decodeUplink(input) {
return {
data: {
state: input.bytes[0]
}
}
}
function normalizeUplink(input) {
return {
data: null
}
}
`
err := host.DecodeUplink(ctx, ids, nil, message, script)
a.So(err, should.BeNil)
a.So(message.NormalizedPayload, should.BeNil)
a.So(message.NormalizedPayloadWarnings, should.BeEmpty)
}

// Return no normalized payload (no return value).
{
script := `
function decodeUplink(input) {
return {
data: {
state: input.bytes[0]
}
}
}
function normalizeUplink(input) {
}
`
err := host.DecodeUplink(ctx, ids, nil, message, script)
a.So(err, should.BeNil)
a.So(message.NormalizedPayload, should.BeNil)
a.So(message.NormalizedPayloadWarnings, should.BeEmpty)
}

// Decode and normalize a single measurement with out-of-range value.
{
message := &ttnpb.ApplicationUplink{
Expand Down

0 comments on commit 4fe002c

Please sign in to comment.