Skip to content

Commit

Permalink
as: Do not emit measurements for empty objects
Browse files Browse the repository at this point in the history
  • Loading branch information
adriansmares committed Sep 14, 2022
1 parent fcac6b0 commit 5718e27
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
18 changes: 12 additions & 6 deletions pkg/messageprocessors/javascript/javascript.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,12 @@ func (*host) decodeUplink(
if err != nil {
return errOutput.WithCause(err)
}
msg.NormalizedPayload = make([]*pbtypes.Struct, len(normalizedMeasurements))
for i, measurement := range normalizedMeasurements {
msg.NormalizedPayload[i] = measurement.Valid
msg.NormalizedPayload = make([]*pbtypes.Struct, 0, len(normalizedMeasurements))
for _, measurement := range normalizedMeasurements {
if len(measurement.Valid.GetFields()) == 0 {
continue
}
msg.NormalizedPayload = append(msg.NormalizedPayload, measurement.Valid)
}
msg.NormalizedPayloadWarnings = make([]string, 0, len(normalized.Warnings))
msg.NormalizedPayloadWarnings = append(msg.NormalizedPayloadWarnings, normalized.Warnings...)
Expand All @@ -366,9 +369,12 @@ func (*host) decodeUplink(
}
normalizedMeasurements, err := normalizedpayload.Parse(normalizedPayload)
if err == nil {
msg.NormalizedPayload = make([]*pbtypes.Struct, len(normalizedMeasurements))
for i, measurement := range normalizedMeasurements {
msg.NormalizedPayload[i] = measurement.Valid
msg.NormalizedPayload = make([]*pbtypes.Struct, 0, len(normalizedMeasurements))
for _, measurement := range normalizedMeasurements {
if len(measurement.Valid.GetFields()) == 0 {
continue
}
msg.NormalizedPayload = append(msg.NormalizedPayload, measurement.Valid)
}
msg.NormalizedPayloadWarnings = appendValidationErrors(msg.NormalizedPayloadWarnings, normalizedMeasurements)
}
Expand Down
10 changes: 2 additions & 8 deletions pkg/messageprocessors/javascript/javascript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,11 +712,7 @@ func TestDecodeUplink(t *testing.T) {
err := host.DecodeUplink(ctx, ids, nil, message, script)
a.So(err, should.BeNil)

a.So(message.NormalizedPayload, should.Resemble, []*pbtypes.Struct{
{
Fields: map[string]*pbtypes.Value{},
},
})
a.So(message.NormalizedPayload, should.Resemble, []*pbtypes.Struct{})
a.So(message.NormalizedPayloadWarnings, should.Resemble, []string{
"measurement 1: `air.temperature` should be equal or greater than `-273.15`",
})
Expand All @@ -727,9 +723,7 @@ func TestDecodeUplink(t *testing.T) {
for i, m := range parsedMeasurements {
measurements[i] = m.Measurement
}
a.So(measurements, should.Resemble, []normalizedpayload.Measurement{
{},
})
a.So(measurements, should.Resemble, []normalizedpayload.Measurement{})
}

// The Things Node example.
Expand Down
9 changes: 9 additions & 0 deletions pkg/messageprocessors/normalizedpayload/uplink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ func TestUplink(t *testing.T) {
},
},
},
{
name: "no fields",
normalizedPayload: []*pbtypes.Struct{
{},
},
expected: []normalizedpayload.Measurement{
{},
},
},
{
name: "below absolute zero",
normalizedPayload: []*pbtypes.Struct{
Expand Down

0 comments on commit 5718e27

Please sign in to comment.