From 1c246960a694065b9acfc75a9c3153a4a1dd21f5 Mon Sep 17 00:00:00 2001 From: Lucas Campelo Date: Thu, 8 Sep 2022 13:49:04 -0300 Subject: [PATCH 1/4] Add generic object datatype --- assets/dataType.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/assets/dataType.go b/assets/dataType.go index b42cd82..2ddcae3 100644 --- a/assets/dataType.go +++ b/assets/dataType.go @@ -1,8 +1,10 @@ package assets import ( + "encoding/json" "fmt" "math" + "net/http" "strconv" "time" @@ -158,4 +160,33 @@ var dataTypeMap = map[string]*DataType{ return dataTime.Format(time.RFC3339), dataTime, nil }, }, + "@object": { + AcceptedFormats: []string{"@object"}, + Parse: func(data interface{}) (string, interface{}, errors.ICCError) { + dataVal, ok := data.(map[string]interface{}) + if !ok { + switch v := data.(type) { + case []byte: + err := json.Unmarshal(v, &dataVal) + if err != nil { + return "", nil, errors.WrapErrorWithStatus(err, "failed to unmarshal []byte into map[string]interface{}", http.StatusBadRequest) + } + case string: + err := json.Unmarshal([]byte(v), &dataVal) + if err != nil { + return "", nil, errors.WrapErrorWithStatus(err, "failed to unmarshal string into map[string]interface{}", http.StatusBadRequest) + } + default: + return "", nil, errors.NewCCError(fmt.Sprintf("asset property must be either a byte array or a string, but received type is: %T", data), http.StatusBadRequest) + } + } + + retVal, err := json.Marshal(dataVal) + if err != nil { + return "", nil, errors.WrapErrorWithStatus(err, "failed to marshal return value", http.StatusInternalServerError) + } + + return string(retVal), dataVal, nil + }, + }, } From cd5d90d9b6a5241f1c5582695ab2917c9eb30620 Mon Sep 17 00:00:00 2001 From: Lucas Campelo Date: Thu, 8 Sep 2022 15:16:18 -0300 Subject: [PATCH 2/4] Add @object datatype to getDataTypes test --- test/tx_getDataTypes_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/tx_getDataTypes_test.go b/test/tx_getDataTypes_test.go index cada7a6..8921dd2 100644 --- a/test/tx_getDataTypes_test.go +++ b/test/tx_getDataTypes_test.go @@ -45,6 +45,12 @@ func TestGetDataTypes(t *testing.T) { }, "DropDownValues": nil, }, + "@object": map[string]interface{}{ + "acceptedFormats": []interface{}{ + "@object", + }, + "DropDownValues": nil, + }, } err := invokeAndVerify(stub, "getDataTypes", nil, expectedResponse, 200) if err != nil { From 885a6a20bdb222b24d317781e600236019467597 Mon Sep 17 00:00:00 2001 From: Samuel Date: Mon, 12 Sep 2022 11:41:42 +0100 Subject: [PATCH 3/4] Increment testing for @object data type --- test/chaincode_test.go | 6 ++++++ test/tx_createAsset_test.go | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/test/chaincode_test.go b/test/chaincode_test.go index f7c86bd..4fb017f 100644 --- a/test/chaincode_test.go +++ b/test/chaincode_test.go @@ -65,6 +65,12 @@ var testAssetList = []assets.AssetType{ DefaultValue: 0, DataType: "number", }, + { + // Generic JSON object + Tag: "info", + Label: "Other Info", + DataType: "@object", + }, }, }, { diff --git a/test/tx_createAsset_test.go b/test/tx_createAsset_test.go index 3bb65eb..ec7d016 100644 --- a/test/tx_createAsset_test.go +++ b/test/tx_createAsset_test.go @@ -16,6 +16,9 @@ func TestCreateAsset(t *testing.T) { "@assetType": "person", "name": "Maria", "id": "318.207.920-48", + "info": map[string]interface{}{ + "passport": "1234", + }, } req := map[string]interface{}{ "asset": []map[string]interface{}{person}, @@ -39,6 +42,9 @@ func TestCreateAsset(t *testing.T) { "name": "Maria", "id": "31820792048", "height": 0.0, + "info": map[string]interface{}{ + "passport": "1234", + }, } if res.GetStatus() != 200 { From e823e9aa39ab41fa7a7ba06821533059ee849bb1 Mon Sep 17 00:00:00 2001 From: Samuel Date: Mon, 12 Sep 2022 11:46:34 +0100 Subject: [PATCH 4/4] Fix tests with new @object property --- test/tx_getSchema_test.go | 10 ++++++++++ test/tx_updateAsset_test.go | 1 + 2 files changed, 11 insertions(+) diff --git a/test/tx_getSchema_test.go b/test/tx_getSchema_test.go index a559f1d..e0fe468 100644 --- a/test/tx_getSchema_test.go +++ b/test/tx_getSchema_test.go @@ -99,6 +99,16 @@ func TestGetSchema(t *testing.T) { "tag": "height", "writers": nil, }, + map[string]interface{}{ + "dataType": "@object", + "description": "", + "isKey": false, + "label": "Other Info", + "readOnly": false, + "required": false, + "tag": "info", + "writers": nil, + }, }, } err = invokeAndVerify(stub, "getSchema", req, expectedPersonSchema, 200) diff --git a/test/tx_updateAsset_test.go b/test/tx_updateAsset_test.go index 6b26cdc..d2edb8d 100644 --- a/test/tx_updateAsset_test.go +++ b/test/tx_updateAsset_test.go @@ -39,6 +39,7 @@ func TestUpdateAsset(t *testing.T) { "id": "318.207.920-48", "dateOfBirth": "1999-05-06T22:12:41Z", "height": 1.66, + "info": map[string]interface{}{}, } req := map[string]interface{}{