Skip to content

Commit

Permalink
Merge pull request #13 from GoLedgerDev/develop
Browse files Browse the repository at this point in the history
Add support for generic JSON objects as asset property: @object
  • Loading branch information
lucas-campelo authored Sep 14, 2022
2 parents d1e657c + 9c85233 commit a183b57
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 0 deletions.
31 changes: 31 additions & 0 deletions assets/dataType.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package assets

import (
"encoding/json"
"fmt"
"math"
"net/http"
"strconv"
"time"

Expand Down Expand Up @@ -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
},
},
}
6 changes: 6 additions & 0 deletions test/chaincode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ var testAssetList = []assets.AssetType{
DefaultValue: 0,
DataType: "number",
},
{
// Generic JSON object
Tag: "info",
Label: "Other Info",
DataType: "@object",
},
},
},
{
Expand Down
6 changes: 6 additions & 0 deletions test/tx_createAsset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions test/tx_getDataTypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions test/tx_getSchema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions test/tx_updateAsset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{
Expand Down

0 comments on commit a183b57

Please sign in to comment.