Skip to content

Commit 92d8341

Browse files
authored
make lwm2m object response body pointer to avoid decode failed (#1084)
1 parent 542ceb0 commit 92d8341

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

‎pkg/gateway/lwm2m/client/object.go‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ func (r *Resource) ReadAsJSON() string {
3737
}
3838

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

114114
switch newData := data.(type) {
115115
case int, int32, int64, int16, int8:
116-
objectDataList = append(objectDataList, ObjectData{ParameterName: basePath, FloatValue: float64(newData.(int))})
116+
var floatValue = float64(newData.(int))
117+
objectDataList = append(objectDataList, ObjectData{ParameterName: &basePath, FloatValue: &floatValue})
117118
case float32, float64:
118-
objectDataList = append(objectDataList, ObjectData{ParameterName: basePath, FloatValue: newData.(float64)})
119+
var floatValue = newData.(float64)
120+
objectDataList = append(objectDataList, ObjectData{ParameterName: &basePath, FloatValue: &floatValue})
119121
case string:
120-
objectDataList = append(objectDataList, ObjectData{ParameterName: basePath, StringValue: newData})
122+
objectDataList = append(objectDataList, ObjectData{ParameterName: &basePath, StringValue: &newData})
121123
case bool:
122-
objectDataList = append(objectDataList, ObjectData{ParameterName: basePath, BoolValue: &newData})
124+
objectDataList = append(objectDataList, ObjectData{ParameterName: &basePath, BoolValue: &newData})
123125
default:
124126
// default to string
125-
objectDataList = append(objectDataList, ObjectData{ParameterName: basePath, StringValue: fmt.Sprintf("%v", data)})
127+
var stringValue = fmt.Sprintf("%v", data)
128+
objectDataList = append(objectDataList, ObjectData{ParameterName: &basePath, StringValue: &stringValue})
126129
}
127130
}
128131

0 commit comments

Comments
 (0)