Skip to content

Commit

Permalink
Merge pull request #937 from weichou1229/null-reading
Browse files Browse the repository at this point in the history
feat: Add CBOR encode and decode for BaseReading
  • Loading branch information
cloudxxx8 authored Oct 17, 2024
2 parents 77124f1 + 8f27c7a commit a490cf7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 21 deletions.
49 changes: 31 additions & 18 deletions dtos/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"testing"

"github.com/fxamacker/cbor/v2"
"github.com/stretchr/testify/require"

"github.com/edgexfoundry/go-mod-core-contracts/v3/common"
Expand Down Expand Up @@ -238,26 +239,38 @@ func TestEvent_MarshalNullReading(t *testing.T) {
assert.True(t, actual.isNull)
}

jsonEncoded, err := json.Marshal(testEvent)
assert.NoError(t, err)
tests := []struct {
name string
marshal func(any) ([]byte, error)
unmarshal func([]byte, any) error
}{
{"marshal with JSON encode", json.Marshal, json.Unmarshal},
{"marshal with CBOR encode", cbor.Marshal, cbor.Unmarshal},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
encoded, err := tt.marshal(testEvent)
assert.NoError(t, err)

var result Event
err = json.Unmarshal(jsonEncoded, &result)
assert.NoError(t, err)
var result Event
err = tt.unmarshal(encoded, &result)
assert.NoError(t, err)

assert.Equal(t, testEvent.DeviceName, result.DeviceName)
assert.Equal(t, testEvent.ProfileName, result.ProfileName)
assert.Equal(t, testEvent.SourceName, result.SourceName)
assert.Equal(t, len(testEvent.Readings), len(result.Readings))
assert.Equal(t, testEvent.DeviceName, result.DeviceName)
assert.Equal(t, testEvent.ProfileName, result.ProfileName)
assert.Equal(t, testEvent.SourceName, result.SourceName)
assert.Equal(t, len(testEvent.Readings), len(result.Readings))

for i, r := range result.Readings {
assert.Equal(t, testEvent.Readings[i].DeviceName, r.DeviceName)
assert.Equal(t, testEvent.Readings[i].ProfileName, r.ProfileName)
assert.Equal(t, testEvent.Readings[i].ResourceName, r.ResourceName)
assert.Equal(t, testEvent.Readings[i].ValueType, r.ValueType)
assert.True(t, r.isNull, "isNull should be true after marshaling")
assert.Empty(t, r.Value, "reading value should be empty after marshaling")
assert.Empty(t, r.ObjectValue, "reading objectValue should be empty after marshaling")
assert.Empty(t, r.BinaryValue, "reading binaryValue should be empty after marshaling")
for i, r := range result.Readings {
assert.Equal(t, testEvent.Readings[i].DeviceName, r.DeviceName)
assert.Equal(t, testEvent.Readings[i].ProfileName, r.ProfileName)
assert.Equal(t, testEvent.Readings[i].ResourceName, r.ResourceName)
assert.Equal(t, testEvent.Readings[i].ValueType, r.ValueType)
assert.True(t, r.isNull, "isNull should be true after marshaling")
assert.Empty(t, r.Value, "reading value should be empty after marshaling")
assert.Empty(t, r.ObjectValue, "reading objectValue should be empty after marshaling")
assert.Empty(t, r.BinaryValue, "reading binaryValue should be empty after marshaling")
}
})
}
}
23 changes: 20 additions & 3 deletions dtos/reading.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"
"time"

"github.com/fxamacker/cbor/v2"
"github.com/google/uuid"

"github.com/edgexfoundry/go-mod-core-contracts/v3/common"
Expand Down Expand Up @@ -507,6 +508,14 @@ func (b BaseReading) UnmarshalObjectValue(target any) error {
}

func (b BaseReading) MarshalJSON() ([]byte, error) {
return b.marshal(json.Marshal)
}

func (b BaseReading) MarshalCBOR() ([]byte, error) {
return b.marshal(cbor.Marshal)
}

func (b BaseReading) marshal(marshal func(any) ([]byte, error)) ([]byte, error) {
type reading struct {
Id string `json:"id,omitempty"`
Origin int64 `json:"origin"`
Expand All @@ -518,7 +527,7 @@ func (b BaseReading) MarshalJSON() ([]byte, error) {
Tags Tags `json:"tags,omitempty"`
}
if b.isNull {
return json.Marshal(&struct {
return marshal(&struct {
reading `json:",inline"`
Value any `json:"value"`
BinaryValue any `json:"binaryValue"`
Expand All @@ -539,7 +548,7 @@ func (b BaseReading) MarshalJSON() ([]byte, error) {
ObjectValue: nil,
})
}
return json.Marshal(&struct {
return marshal(&struct {
reading `json:",inline"`
BinaryReading `json:",inline" validate:"-"`
SimpleReading `json:",inline" validate:"-"`
Expand All @@ -562,6 +571,14 @@ func (b BaseReading) MarshalJSON() ([]byte, error) {
}

func (b *BaseReading) UnmarshalJSON(data []byte) error {
return b.Unmarshal(data, json.Unmarshal)
}

func (b *BaseReading) UnmarshalCBOR(data []byte) error {
return b.Unmarshal(data, cbor.Unmarshal)
}

func (b *BaseReading) Unmarshal(data []byte, unmarshal func([]byte, any) error) error {
var aux struct {
Id string
Origin int64
Expand All @@ -575,7 +592,7 @@ func (b *BaseReading) UnmarshalJSON(data []byte) error {
BinaryReading
ObjectReading
}
if err := json.Unmarshal(data, &aux); err != nil {
if err := unmarshal(data, &aux); err != nil {
return err
}

Expand Down

0 comments on commit a490cf7

Please sign in to comment.