Repository: edgexfoundry/edgex-go
Pull request #4950 (2024-10-07 09:20:27)

feat: Allow store reading as null value
<!-- Expected Commit Message Description (imported automatically by GitHub) -->
<!-- Must conform to [conventional commits guidelines](https://github.com/edgexfoundry/edgex-go/blob/main/.github/Contributing.md) -->
<!-- Expected Commit message must contain Closes/Fixes #IssueNumber statement when there is a related issue -->

<!-- Add additional detailed description of need for change if no related issue -->

**If your build fails**  due to your commit message not passing the build checks, please review the guidelines here: https://github.com/edgexfoundry/edgex-go/blob/main/.github/Contributing.md

## PR Checklist
Please check if your PR fulfills the following requirements:

- [x] I am not introducing a breaking change (if you are, flag in conventional commit message with `BREAKING CHANGE:` describing the break)
- [x] I am not introducing a new dependency (add notes below if you are)
- [x] I have added unit tests for the new feature or bug fix (if not, why?)
- [x] I have fully tested (add details below) this the new feature or bug fix (if not, why?)
- [ ] I have opened a PR for the related docs change (if not, why?)
  <link to docs PR>

## Testing Instructions
<!-- How can the reviewers test your change? -->
- Test with simple device service
- Run make test

## New Dependency Instructions (If applicable)
<!-- Please follow [vetting instructions](https://wiki.edgexfoundry.org/display/FA/Vetting+Process+for+3rd+Party+Dependencies) and place results here -->


Patch:
diff --git a/go.mod b/go.mod
index 24cd2077..9caf636c 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.58
 	github.com/edgexfoundry/go-mod-configuration/v3 v3.2.0-dev.17
-	github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.48
+	github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.49
 	github.com/edgexfoundry/go-mod-messaging/v3 v3.2.0-dev.35
 	github.com/edgexfoundry/go-mod-secrets/v3 v3.2.0-dev.12
 	github.com/fxamacker/cbor/v2 v2.7.0
diff --git a/go.sum b/go.sum
index 275f163b..4e481911 100644
--- a/go.sum
+++ b/go.sum
@@ -88,8 +88,8 @@ github.com/edgexfoundry/go-mod-bootstrap/v3 v3.2.0-dev.58 h1:AHp6ChEe44PhQX72Ord
 github.com/edgexfoundry/go-mod-bootstrap/v3 v3.2.0-dev.58/go.mod h1:MoAhP94mD4GI1ReeHQz0M7R4vD8MlA3/paP3u6QrEM4=
 github.com/edgexfoundry/go-mod-configuration/v3 v3.2.0-dev.17 h1:eNLGThT5fYtFLSrr6vtvHV92cm5IohJzwSRYcd5pvtg=
 github.com/edgexfoundry/go-mod-configuration/v3 v3.2.0-dev.17/go.mod h1:va3l+Nri1KijDM5KItsaYrwUSG3Nhj/4EDvR4uB+Jds=
-github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.48 h1:wL2f6AFsByGGXfmez7LdEKsK4oMnGprJqK6PA/RpS/g=
-github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.48/go.mod h1:MLk37/79M26+bZr3IptNZuYmQBEVbXwzDp1VHQkFhIk=
+github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.49 h1:uBI0nA3oO9P2MWBR1Tcn/543u4GFfWXd4J9Gii6CB1k=
+github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.49/go.mod h1:MLk37/79M26+bZr3IptNZuYmQBEVbXwzDp1VHQkFhIk=
 github.com/edgexfoundry/go-mod-messaging/v3 v3.2.0-dev.35 h1:ZKmq5Vd1QN86OLTbD8A2JIKTxR8k21gtqmEUS3hApBY=
 github.com/edgexfoundry/go-mod-messaging/v3 v3.2.0-dev.35/go.mod h1:SUTdo8v9V+XacowkZP5puBXnBcVP2NDSk5PBzOdsLEk=
 github.com/edgexfoundry/go-mod-registry/v3 v3.2.0-dev.16 h1:vG6cI1LU8QLpzUXYleYuVyqbpeYghLF3Jj6afcaUCHA=
diff --git a/internal/pkg/infrastructure/postgres/reading.go b/internal/pkg/infrastructure/postgres/reading.go
index e779622f..a225c1fc 100644
--- a/internal/pkg/infrastructure/postgres/reading.go
+++ b/internal/pkg/infrastructure/postgres/reading.go
@@ -257,7 +257,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 {
@@ -342,7 +342,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)


Test patch:
diff --git a/internal/core/data/application/event_test.go b/internal/core/data/application/event_test.go
index e14df992..f975df73 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: "45",
+		Value: &testValue,
 	}
 
 	r2 := models.BinaryReading{
@@ -81,7 +81,7 @@ func buildReadings() []models.Reading {
 			ProfileName:  "TempProfile",
 			ValueType:    common.ValueTypeUint16,
 		},
-		Value: "33",
+		Value: &testValue,
 	}
 
 	r4 := models.SimpleReading{
@@ -93,7 +93,7 @@ func buildReadings() []models.Reading {
 			ProfileName:  "TempProfile",
 			ValueType:    common.ValueTypeUint16,
 		},
-		Value: "44",
+		Value: &testValue,
 	}
 
 	r5 := models.SimpleReading{
@@ -105,7 +105,7 @@ func buildReadings() []models.Reading {
 			ProfileName:  "TempProfile",
 			ValueType:    common.ValueTypeUint16,
 		},
-		Value: "55",
+		Value: &testValue,
 	}
 
 	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 871caa58..4b1dfa8e 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/redis/reading_test.go b/internal/pkg/infrastructure/redis/reading_test.go
index e5919947..3ceca466 100644
--- a/internal/pkg/infrastructure/redis/reading_test.go
+++ b/internal/pkg/infrastructure/redis/reading_test.go
@@ -23,6 +23,7 @@ const (
 )
 
 func simpleReadingData() models.SimpleReading {
+	testValue := "123"
 	return models.SimpleReading{
 		BaseReading: models.BaseReading{
 			Id:           exampleUUID,
@@ -32,7 +33,7 @@ func simpleReadingData() models.SimpleReading {
 			ResourceName: testResourceName,
 			ValueType:    common.ValueTypeString,
 		},
-		Value: "123",
+		Value: &testValue,
 	}
 }