Skip to content

Commit

Permalink
make lwm2m object response body pointer to avoid decode failed (#1084)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhoninl authored Dec 19, 2024
1 parent 542ceb0 commit 92d8341
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions pkg/gateway/lwm2m/client/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func (r *Resource) ReadAsJSON() string {
}

type ObjectData struct {
ParameterName string `json:"n,omitempty"`
FloatValue float64 `json:"v,omitempty"`
StringValue string `json:"sv,omitempty"`
ParameterName *string `json:"n,omitempty"`
FloatValue *float64 `json:"v,omitempty"`
StringValue *string `json:"sv,omitempty"`
// Pointer to avoid marshal false value
BoolValue *bool `json:"bv,omitempty"`
}
Expand Down Expand Up @@ -113,16 +113,19 @@ func (o *Object) readAll(basePath string) ([]ObjectData, error) {

switch newData := data.(type) {
case int, int32, int64, int16, int8:
objectDataList = append(objectDataList, ObjectData{ParameterName: basePath, FloatValue: float64(newData.(int))})
var floatValue = float64(newData.(int))
objectDataList = append(objectDataList, ObjectData{ParameterName: &basePath, FloatValue: &floatValue})
case float32, float64:
objectDataList = append(objectDataList, ObjectData{ParameterName: basePath, FloatValue: newData.(float64)})
var floatValue = newData.(float64)
objectDataList = append(objectDataList, ObjectData{ParameterName: &basePath, FloatValue: &floatValue})
case string:
objectDataList = append(objectDataList, ObjectData{ParameterName: basePath, StringValue: newData})
objectDataList = append(objectDataList, ObjectData{ParameterName: &basePath, StringValue: &newData})
case bool:
objectDataList = append(objectDataList, ObjectData{ParameterName: basePath, BoolValue: &newData})
objectDataList = append(objectDataList, ObjectData{ParameterName: &basePath, BoolValue: &newData})
default:
// default to string
objectDataList = append(objectDataList, ObjectData{ParameterName: basePath, StringValue: fmt.Sprintf("%v", data)})
var stringValue = fmt.Sprintf("%v", data)
objectDataList = append(objectDataList, ObjectData{ParameterName: &basePath, StringValue: &stringValue})
}
}

Expand Down

0 comments on commit 92d8341

Please sign in to comment.