Skip to content

Commit

Permalink
feat: Using isNull field instead of nil reading value
Browse files Browse the repository at this point in the history
Using isNull instead of nil reading value

Signed-off-by: bruce <[email protected]>
  • Loading branch information
weichou1229 committed Oct 17, 2024
1 parent 636acee commit f2bf259
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
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 (
github.com/eclipse/paho.mqtt.golang v1.5.0
github.com/edgexfoundry/go-mod-bootstrap/v3 v3.2.0-dev.66
github.com/edgexfoundry/go-mod-configuration/v3 v3.2.0-dev.19
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.51
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.53
github.com/edgexfoundry/go-mod-messaging/v3 v3.2.0-dev.40
github.com/edgexfoundry/go-mod-secrets/v3 v3.2.0-dev.18
github.com/fxamacker/cbor/v2 v2.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ github.com/edgexfoundry/go-mod-bootstrap/v3 v3.2.0-dev.66 h1:kmBEAhNi4ftrJMXM3Iv
github.com/edgexfoundry/go-mod-bootstrap/v3 v3.2.0-dev.66/go.mod h1:3IXVpc5Qez5nwFJ8IkMyJMba8Iavj620E0XB42BQzfQ=
github.com/edgexfoundry/go-mod-configuration/v3 v3.2.0-dev.19 h1:274NZdVBkJBuQP6yT3tVrb7psTFuIPogX/DLQqv7OCQ=
github.com/edgexfoundry/go-mod-configuration/v3 v3.2.0-dev.19/go.mod h1:BG6hCDxXizpgMdNEljwNfLWSsd4Op7GAHd3Pis1dVv8=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.51 h1:X73zP3/kgOGTXIRRxOMSGr174GrhxwuCfeBs+cCIudM=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.51/go.mod h1:MLk37/79M26+bZr3IptNZuYmQBEVbXwzDp1VHQkFhIk=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.53 h1:aHnYwqpg0LcnMrgNQlkRQjzHAS/IML/9GI368OmNCz4=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.53/go.mod h1:MLk37/79M26+bZr3IptNZuYmQBEVbXwzDp1VHQkFhIk=
github.com/edgexfoundry/go-mod-messaging/v3 v3.2.0-dev.40 h1:YyB21HEapV5pENG01vFlpjPI6UkmKpJuaWFfgGFVUsY=
github.com/edgexfoundry/go-mod-messaging/v3 v3.2.0-dev.40/go.mod h1:8NpZ6/eAsiyZHgn/s3DRIpcOjUrve+ZONIgvcDvA3Yg=
github.com/edgexfoundry/go-mod-registry/v3 v3.2.0-dev.18 h1:AzILZ/xcEmSYVhIwSF4zkWpXyFoBA733E/j8ttzlNnI=
Expand Down
6 changes: 2 additions & 4 deletions internal/core/data/controller/http/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,14 @@ import (

var expectedEventId = uuid.New().String()

var testReadingValue = "45"
var testReading = dtos.BaseReading{
DeviceName: TestDeviceName,
ResourceName: TestDeviceResourceName,
ProfileName: TestDeviceProfileName,
Origin: TestOriginTime,
ValueType: common.ValueTypeUint8,
SimpleReading: dtos.SimpleReading{
Value: &testReadingValue,
Value: "45",
},
}

Expand Down Expand Up @@ -163,8 +162,7 @@ func TestAddEvent(t *testing.T) {

noSimpleValue := validRequest
noSimpleValue.Event.Readings = []dtos.BaseReading{testReading}
emptyStr := ""
noSimpleValue.Event.Readings[0].Value = &emptyStr
noSimpleValue.Event.Readings[0].Value = ""
noBinaryValue := validRequest
noBinaryValue.Event.Readings = []dtos.BaseReading{{
DeviceName: TestDeviceName,
Expand Down
18 changes: 17 additions & 1 deletion internal/pkg/infrastructure/redis/reading.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ func addReading(conn redis.Conn, r models.Reading) (reading models.Reading, edge
}
m, err = json.Marshal(newReading)
reading = newReading
case models.NullReading:
baseReading = &newReading.BaseReading
if err = checkReadingValue(baseReading); err != nil {
return nil, errors.NewCommonEdgeXWrapper(err)
}
m, err = json.Marshal(newReading)
reading = newReading
default:
return nil, errors.NewCommonEdgeX(errors.KindContractInvalid, "unsupported reading type", nil)
}
Expand Down Expand Up @@ -277,14 +284,23 @@ func readingsByDeviceNameAndTimeRange(conn redis.Conn, deviceName string, startT
func convertObjectsToReadings(objects [][]byte) (readings []models.Reading, edgeXerr errors.EdgeX) {
readings = make([]models.Reading, len(objects))
var alias struct {
Value any
ValueType string
}
for i, in := range objects {
err := json.Unmarshal(in, &alias)
if err != nil {
return []models.Reading{}, errors.NewCommonEdgeX(errors.KindDatabaseError, "reading format parsing failed from the database", err)
}
if alias.ValueType == common.ValueTypeBinary {
if alias.Value == nil {
var nullReading models.NullReading
err = json.Unmarshal(in, &nullReading)
if err != nil {
return []models.Reading{}, errors.NewCommonEdgeX(errors.KindDatabaseError, "simple reading format parsing failed from the database", err)
}
readings[i] = nullReading
continue
} else if alias.ValueType == common.ValueTypeBinary {
var binaryReading models.BinaryReading
err = json.Unmarshal(in, &binaryReading)
if err != nil {
Expand Down
21 changes: 19 additions & 2 deletions internal/pkg/infrastructure/redis/reading_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,38 @@ func objectReadingData() models.ObjectReading {
}
}

func nullReadingData() models.NullReading {
return models.NullReading{
BaseReading: models.BaseReading{
Id: exampleUUID,
Origin: 1616728256236000000,
DeviceName: testDeviceName,
ProfileName: testProfileName,
ResourceName: testResourceName,
ValueType: common.ValueTypeFloat32,
},
Value: nil,
}
}

func TestConvertObjectsToReadings(t *testing.T) {
simpleReading := simpleReadingData()
binaryReading := binaryReadingData()
objectReading := objectReadingData()
nullReading := nullReadingData()

simpleReadingBytes, err := json.Marshal(simpleReading)
require.NoError(t, err)
binaryReadingBytes, err := json.Marshal(binaryReading)
require.NoError(t, err)
objectReadingBytes, err := json.Marshal(objectReading)
require.NoError(t, err)
nullReadingBytes, err := json.Marshal(nullReading)
require.NoError(t, err)

readingsData := [][]byte{simpleReadingBytes, binaryReadingBytes, objectReadingBytes}
readingsData := [][]byte{simpleReadingBytes, binaryReadingBytes, objectReadingBytes, nullReadingBytes}
expectedReadings := []models.Reading{
simpleReading, binaryReading, objectReading,
simpleReading, binaryReading, objectReading, nullReading,
}

events, err := convertObjectsToReadings(readingsData)
Expand Down

0 comments on commit f2bf259

Please sign in to comment.