From 92d83413e8fa01f4d04189b205fc15bcdb5ff59e Mon Sep 17 00:00:00 2001 From: Rhonin Lee <64508229+rhoninl@users.noreply.github.com> Date: Thu, 19 Dec 2024 09:53:36 +0800 Subject: [PATCH] make lwm2m object response body pointer to avoid decode failed (#1084) --- pkg/gateway/lwm2m/client/object.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/pkg/gateway/lwm2m/client/object.go b/pkg/gateway/lwm2m/client/object.go index a78f7589f..d6c5f5ab9 100644 --- a/pkg/gateway/lwm2m/client/object.go +++ b/pkg/gateway/lwm2m/client/object.go @@ -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"` } @@ -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}) } }