Skip to content

Commit

Permalink
feat: Allow reading value to be null
Browse files Browse the repository at this point in the history
Allow reading value to be null

Signed-off-by: bruce <[email protected]>
  • Loading branch information
weichou1229 committed Oct 4, 2024
1 parent 1b8f5c3 commit 888d014
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/transformer/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ func commandValueToReading(cv *models.CommandValue, deviceName, profileName, med
reading = dtos.NewBinaryReading(profileName, deviceName, cv.DeviceResourceName, binary, mediaType)
} else if cv.Type == common.ValueTypeObject {
reading = dtos.NewObjectReading(profileName, deviceName, cv.DeviceResourceName, cv.Value)
} else if cv.Type == common.ValueTypeObjectArray {
reading = dtos.NewObjectReadingWithArray(profileName, deviceName, cv.DeviceResourceName, cv.Value)
} else {
reading, err = dtos.NewSimpleReading(profileName, deviceName, cv.DeviceResourceName, cv.Type, cv.Value)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/transformer/transformresult.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ func transformReadShift(value any, shift int64) (any, errors.EdgeX) {
}

func commandValueForTransform(cv *sdkModels.CommandValue) (interface{}, errors.EdgeX) {
if cv.Value == nil {
return nil, nil
}
var v interface{}
var err error
switch cv.Type {
Expand Down
6 changes: 6 additions & 0 deletions pkg/models/commandvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,9 @@ func (cv *CommandValue) Float64ArrayValue() ([]float64, error) {

// BinaryValue returns the value in []byte data type, and returns error if the Type is not Binary.
func (cv *CommandValue) BinaryValue() ([]byte, error) {
if cv.Value == nil {
return nil, nil
}
var value []byte
if cv.Type != common.ValueTypeBinary {
errMsg := fmt.Sprintf("cannot convert %s to %s", cv.Type, common.ValueTypeBinary)
Expand All @@ -467,6 +470,9 @@ func (cv *CommandValue) ObjectValue() (interface{}, error) {
// validate checks if the given value can be converted to specified valueType by
// performing type assertion
func validate(valueType string, value interface{}) error {
if value == nil {
return nil // value can be nil, skip validation
}
var ok bool
switch valueType {
case common.ValueTypeString:
Expand Down

0 comments on commit 888d014

Please sign in to comment.