Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow reading value to be null #1633

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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