Skip to content

Commit

Permalink
feat: Using isNull field instead of nil reading value (#4974)
Browse files Browse the repository at this point in the history
* revert: "feat: Allow store reading as null value"

This reverts commit faff7e1

Signed-off-by: bruce <[email protected]>

* feat: Using isNull field instead of nil reading value

Using isNull instead of nil reading value

Signed-off-by: bruce <[email protected]>

---------

Signed-off-by: bruce <[email protected]>
  • Loading branch information
weichou1229 authored Oct 17, 2024
1 parent 0f2c1aa commit 82350b2
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 20 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
10 changes: 5 additions & 5 deletions internal/core/data/application/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -57,7 +57,7 @@ func buildReadings() []models.Reading {
ProfileName: "TempProfile",
ValueType: common.ValueTypeUint16,
},
Value: &testValue,
Value: "45",
}

r2 := models.BinaryReading{
Expand All @@ -81,7 +81,7 @@ func buildReadings() []models.Reading {
ProfileName: "TempProfile",
ValueType: common.ValueTypeUint16,
},
Value: &testValue,
Value: "33",
}

r4 := models.SimpleReading{
Expand All @@ -93,7 +93,7 @@ func buildReadings() []models.Reading {
ProfileName: "TempProfile",
ValueType: common.ValueTypeUint16,
},
Value: &testValue,
Value: "44",
}

r5 := models.SimpleReading{
Expand All @@ -105,7 +105,7 @@ func buildReadings() []models.Reading {
ProfileName: "TempProfile",
ValueType: common.ValueTypeUint16,
},
Value: &testValue,
Value: "55",
}

var readings []models.Reading
Expand Down
8 changes: 3 additions & 5 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 @@ -75,7 +74,7 @@ var persistedReading = models.SimpleReading{
ProfileName: TestDeviceProfileName,
ValueType: common.ValueTypeUint8,
},
Value: &testReadingValue,
Value: TestReadingValue,
}

var persistedEvent = models.Event{
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
4 changes: 2 additions & 2 deletions internal/pkg/infrastructure/postgres/reading.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
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
24 changes: 20 additions & 4 deletions internal/pkg/infrastructure/redis/reading_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const (
)

func simpleReadingData() models.SimpleReading {
testValue := "123"
return models.SimpleReading{
BaseReading: models.BaseReading{
Id: exampleUUID,
Expand All @@ -33,7 +32,7 @@ func simpleReadingData() models.SimpleReading {
ResourceName: testResourceName,
ValueType: common.ValueTypeString,
},
Value: &testValue,
Value: "123",
}
}

Expand Down Expand Up @@ -69,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 82350b2

Please sign in to comment.