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 16, 2024
1 parent 4b9c974 commit 44d778c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
10 changes: 4 additions & 6 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 Expand Up @@ -218,7 +216,7 @@ func TestAddEvent(t *testing.T) {
{"Invalid - Invalid Reading ValueType JSON", invalidReadingInvalidValueType, common.ContentTypeJSON, invalidReadingInvalidValueType.Event.ProfileName, invalidReadingInvalidValueType.Event.DeviceName, true, http.StatusBadRequest},
{"Invalid - No SimpleReading Value JSON", noSimpleValue, common.ContentTypeJSON, noSimpleValue.Event.ProfileName, noSimpleValue.Event.DeviceName, true, http.StatusBadRequest},
{"Valid - No BinaryReading BinaryValue JSON", noBinaryValue, common.ContentTypeJSON, noBinaryValue.Event.ProfileName, noBinaryValue.Event.DeviceName, false, http.StatusCreated},
{"Valid - No BinaryReading MediaType JSON", noBinaryMediaType, common.ContentTypeJSON, noBinaryMediaType.Event.ProfileName, noBinaryMediaType.Event.DeviceName, false, http.StatusCreated},
{"Invalid - No BinaryReading MediaType JSON", noBinaryMediaType, common.ContentTypeJSON, noBinaryMediaType.Event.ProfileName, noBinaryMediaType.Event.DeviceName, true, http.StatusBadRequest},
{"Valid - AddEventRequest CBOR", validRequest, common.ContentTypeCBOR, validRequest.Event.ProfileName, validRequest.Event.DeviceName, false, http.StatusCreated},
{"Valid - No RequestId CBOR", noRequestId, common.ContentTypeCBOR, noRequestId.Event.ProfileName, noRequestId.Event.DeviceName, false, http.StatusCreated},
{"Invalid - Bad RequestId CBOR", badRequestId, common.ContentTypeCBOR, badRequestId.Event.ProfileName, badRequestId.Event.DeviceName, true, http.StatusBadRequest},
Expand All @@ -238,7 +236,7 @@ func TestAddEvent(t *testing.T) {
{"Invalid - Invalid Reading ValueType CBOR", invalidReadingInvalidValueType, common.ContentTypeCBOR, invalidReadingInvalidValueType.Event.ProfileName, invalidReadingInvalidValueType.Event.DeviceName, true, http.StatusBadRequest},
{"Invalid - No SimpleReading Value CBOR", noSimpleValue, common.ContentTypeCBOR, noSimpleValue.Event.ProfileName, noSimpleValue.Event.DeviceName, true, http.StatusBadRequest},
{"Valid - No BinaryReading BinaryValue CBOR", noBinaryValue, common.ContentTypeCBOR, noBinaryValue.Event.ProfileName, noBinaryValue.Event.DeviceName, false, http.StatusCreated},
{"Valid - No BinaryReading MediaType CBOR", noBinaryMediaType, common.ContentTypeCBOR, noBinaryMediaType.Event.ProfileName, noBinaryMediaType.Event.DeviceName, false, http.StatusCreated},
{"Invalid - No BinaryReading MediaType CBOR", noBinaryMediaType, common.ContentTypeCBOR, noBinaryMediaType.Event.ProfileName, noBinaryMediaType.Event.DeviceName, true, http.StatusBadRequest},
}

for _, testCase := range tests {
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

0 comments on commit 44d778c

Please sign in to comment.