From 636aceefeb0e8a0b1d0e040308bd67e97b1e5cb5 Mon Sep 17 00:00:00 2001 From: bruce Date: Wed, 16 Oct 2024 10:07:09 +0800 Subject: [PATCH 1/2] revert: "feat: Allow store reading as null value" This reverts commit faff7e15 Signed-off-by: bruce --- internal/core/data/application/event_test.go | 10 +++++----- internal/core/data/controller/http/event_test.go | 2 +- internal/pkg/infrastructure/postgres/reading.go | 4 ++-- internal/pkg/infrastructure/redis/reading_test.go | 3 +-- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/internal/core/data/application/event_test.go b/internal/core/data/application/event_test.go index f975df73d3..e14df9920a 100644 --- a/internal/core/data/application/event_test.go +++ b/internal/core/data/application/event_test.go @@ -47,7 +47,7 @@ var persistedEvent = models.Event{ func buildReadings() []models.Reading { ticks := pkgCommon.MakeTimestamp() - testValue := "45" + r1 := models.SimpleReading{ BaseReading: models.BaseReading{ Id: uuid.New().String(), @@ -57,7 +57,7 @@ func buildReadings() []models.Reading { ProfileName: "TempProfile", ValueType: common.ValueTypeUint16, }, - Value: &testValue, + Value: "45", } r2 := models.BinaryReading{ @@ -81,7 +81,7 @@ func buildReadings() []models.Reading { ProfileName: "TempProfile", ValueType: common.ValueTypeUint16, }, - Value: &testValue, + Value: "33", } r4 := models.SimpleReading{ @@ -93,7 +93,7 @@ func buildReadings() []models.Reading { ProfileName: "TempProfile", ValueType: common.ValueTypeUint16, }, - Value: &testValue, + Value: "44", } r5 := models.SimpleReading{ @@ -105,7 +105,7 @@ func buildReadings() []models.Reading { ProfileName: "TempProfile", ValueType: common.ValueTypeUint16, }, - Value: &testValue, + Value: "55", } var readings []models.Reading diff --git a/internal/core/data/controller/http/event_test.go b/internal/core/data/controller/http/event_test.go index 6c43e6791a..3c4428e940 100644 --- a/internal/core/data/controller/http/event_test.go +++ b/internal/core/data/controller/http/event_test.go @@ -75,7 +75,7 @@ var persistedReading = models.SimpleReading{ ProfileName: TestDeviceProfileName, ValueType: common.ValueTypeUint8, }, - Value: &testReadingValue, + Value: TestReadingValue, } var persistedEvent = models.Event{ diff --git a/internal/pkg/infrastructure/postgres/reading.go b/internal/pkg/infrastructure/postgres/reading.go index 0b6f982d56..38ef036c13 100644 --- a/internal/pkg/infrastructure/postgres/reading.go +++ b/internal/pkg/infrastructure/postgres/reading.go @@ -258,7 +258,7 @@ func queryReadings(ctx context.Context, connPool *pgxpool.Pool, sql string, args // reading type is SimpleReading simpleReading := model.SimpleReading{ BaseReading: baseReading, - Value: readingDBModel.Value, + Value: *readingDBModel.Value, } reading = simpleReading } else { @@ -351,7 +351,7 @@ func addReadingsInTx(tx pgx.Tx, readings []model.Reading, eventId string) error // convert SimpleReading struct to Reading DB model readingDBModel = dbModels.Reading{ BaseReading: baseReading, - SimpleReading: dbModels.SimpleReading{Value: contractReadingModel.Value}, + SimpleReading: dbModels.SimpleReading{Value: &contractReadingModel.Value}, } default: return errors.NewCommonEdgeX(errors.KindContractInvalid, "failed to convert reading to none of BinaryReading/ObjectReading/SimpleReading structs", nil) diff --git a/internal/pkg/infrastructure/redis/reading_test.go b/internal/pkg/infrastructure/redis/reading_test.go index 3ceca46658..e59199476e 100644 --- a/internal/pkg/infrastructure/redis/reading_test.go +++ b/internal/pkg/infrastructure/redis/reading_test.go @@ -23,7 +23,6 @@ const ( ) func simpleReadingData() models.SimpleReading { - testValue := "123" return models.SimpleReading{ BaseReading: models.BaseReading{ Id: exampleUUID, @@ -33,7 +32,7 @@ func simpleReadingData() models.SimpleReading { ResourceName: testResourceName, ValueType: common.ValueTypeString, }, - Value: &testValue, + Value: "123", } } From f2bf259de2567c911c6fdce48867d483f2c8e5de Mon Sep 17 00:00:00 2001 From: bruce Date: Wed, 16 Oct 2024 16:24:48 +0800 Subject: [PATCH 2/2] feat: Using isNull field instead of nil reading value Using isNull instead of nil reading value Signed-off-by: bruce --- go.mod | 2 +- go.sum | 4 ++-- .../core/data/controller/http/event_test.go | 6 ++---- internal/pkg/infrastructure/redis/reading.go | 18 +++++++++++++++- .../pkg/infrastructure/redis/reading_test.go | 21 +++++++++++++++++-- 5 files changed, 41 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 5e230c3740..bc7c7c9d90 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 0908b37137..9e6f3662c9 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/core/data/controller/http/event_test.go b/internal/core/data/controller/http/event_test.go index 3c4428e940..a1e1661dc1 100644 --- a/internal/core/data/controller/http/event_test.go +++ b/internal/core/data/controller/http/event_test.go @@ -38,7 +38,6 @@ import ( var expectedEventId = uuid.New().String() -var testReadingValue = "45" var testReading = dtos.BaseReading{ DeviceName: TestDeviceName, ResourceName: TestDeviceResourceName, @@ -46,7 +45,7 @@ var testReading = dtos.BaseReading{ Origin: TestOriginTime, ValueType: common.ValueTypeUint8, SimpleReading: dtos.SimpleReading{ - Value: &testReadingValue, + Value: "45", }, } @@ -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, diff --git a/internal/pkg/infrastructure/redis/reading.go b/internal/pkg/infrastructure/redis/reading.go index c4022926d6..9d3067ba6d 100644 --- a/internal/pkg/infrastructure/redis/reading.go +++ b/internal/pkg/infrastructure/redis/reading.go @@ -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) } @@ -277,6 +284,7 @@ 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 { @@ -284,7 +292,15 @@ func convertObjectsToReadings(objects [][]byte) (readings []models.Reading, edge 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 { diff --git a/internal/pkg/infrastructure/redis/reading_test.go b/internal/pkg/infrastructure/redis/reading_test.go index e59199476e..45ca34e199 100644 --- a/internal/pkg/infrastructure/redis/reading_test.go +++ b/internal/pkg/infrastructure/redis/reading_test.go @@ -68,10 +68,25 @@ 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) @@ -79,10 +94,12 @@ func TestConvertObjectsToReadings(t *testing.T) { 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)