Skip to content

Commit a183b57

Browse files
Merge pull request #13 from GoLedgerDev/develop
Add support for generic JSON objects as asset property: @object
2 parents d1e657c + 9c85233 commit a183b57

File tree

6 files changed

+60
-0
lines changed

6 files changed

+60
-0
lines changed

assets/dataType.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package assets
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"math"
7+
"net/http"
68
"strconv"
79
"time"
810

@@ -158,4 +160,33 @@ var dataTypeMap = map[string]*DataType{
158160
return dataTime.Format(time.RFC3339), dataTime, nil
159161
},
160162
},
163+
"@object": {
164+
AcceptedFormats: []string{"@object"},
165+
Parse: func(data interface{}) (string, interface{}, errors.ICCError) {
166+
dataVal, ok := data.(map[string]interface{})
167+
if !ok {
168+
switch v := data.(type) {
169+
case []byte:
170+
err := json.Unmarshal(v, &dataVal)
171+
if err != nil {
172+
return "", nil, errors.WrapErrorWithStatus(err, "failed to unmarshal []byte into map[string]interface{}", http.StatusBadRequest)
173+
}
174+
case string:
175+
err := json.Unmarshal([]byte(v), &dataVal)
176+
if err != nil {
177+
return "", nil, errors.WrapErrorWithStatus(err, "failed to unmarshal string into map[string]interface{}", http.StatusBadRequest)
178+
}
179+
default:
180+
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)
181+
}
182+
}
183+
184+
retVal, err := json.Marshal(dataVal)
185+
if err != nil {
186+
return "", nil, errors.WrapErrorWithStatus(err, "failed to marshal return value", http.StatusInternalServerError)
187+
}
188+
189+
return string(retVal), dataVal, nil
190+
},
191+
},
161192
}

test/chaincode_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ var testAssetList = []assets.AssetType{
6565
DefaultValue: 0,
6666
DataType: "number",
6767
},
68+
{
69+
// Generic JSON object
70+
Tag: "info",
71+
Label: "Other Info",
72+
DataType: "@object",
73+
},
6874
},
6975
},
7076
{

test/tx_createAsset_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ func TestCreateAsset(t *testing.T) {
1616
"@assetType": "person",
1717
"name": "Maria",
1818
"id": "318.207.920-48",
19+
"info": map[string]interface{}{
20+
"passport": "1234",
21+
},
1922
}
2023
req := map[string]interface{}{
2124
"asset": []map[string]interface{}{person},
@@ -39,6 +42,9 @@ func TestCreateAsset(t *testing.T) {
3942
"name": "Maria",
4043
"id": "31820792048",
4144
"height": 0.0,
45+
"info": map[string]interface{}{
46+
"passport": "1234",
47+
},
4248
}
4349

4450
if res.GetStatus() != 200 {

test/tx_getDataTypes_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ func TestGetDataTypes(t *testing.T) {
4545
},
4646
"DropDownValues": nil,
4747
},
48+
"@object": map[string]interface{}{
49+
"acceptedFormats": []interface{}{
50+
"@object",
51+
},
52+
"DropDownValues": nil,
53+
},
4854
}
4955
err := invokeAndVerify(stub, "getDataTypes", nil, expectedResponse, 200)
5056
if err != nil {

test/tx_getSchema_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ func TestGetSchema(t *testing.T) {
9999
"tag": "height",
100100
"writers": nil,
101101
},
102+
map[string]interface{}{
103+
"dataType": "@object",
104+
"description": "",
105+
"isKey": false,
106+
"label": "Other Info",
107+
"readOnly": false,
108+
"required": false,
109+
"tag": "info",
110+
"writers": nil,
111+
},
102112
},
103113
}
104114
err = invokeAndVerify(stub, "getSchema", req, expectedPersonSchema, 200)

test/tx_updateAsset_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func TestUpdateAsset(t *testing.T) {
3939
"id": "318.207.920-48",
4040
"dateOfBirth": "1999-05-06T22:12:41Z",
4141
"height": 1.66,
42+
"info": map[string]interface{}{},
4243
}
4344

4445
req := map[string]interface{}{

0 commit comments

Comments
 (0)