Skip to content

Commit

Permalink
✨ Add new props on update
Browse files Browse the repository at this point in the history
  • Loading branch information
andremacedopv committed Jan 16, 2023
1 parent 877a3f2 commit bcf3393
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion transactions/updateAssetType.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package transactions
import (
"encoding/json"
"fmt"
"net/http"

"github.com/goledgerdev/cc-tools/assets"
"github.com/goledgerdev/cc-tools/errors"
Expand Down Expand Up @@ -50,6 +51,15 @@ var UpdateAssetType = Transaction{

for key, value := range assetTypeMap {
switch key {
case "props":
propsArr, ok := value.([]interface{})
if !ok {
return nil, errors.NewCCError("invalid props array", http.StatusBadRequest)
}
assetTypeObj, err = handleProps(assetTypeObj, propsArr)
if err != nil {
return nil, errors.WrapError(err, "invalid props array")
}
case "label":
labelValue, err := CheckValue(value, true, "string", "label")
if err != nil {
Expand All @@ -76,7 +86,6 @@ var UpdateAssetType = Transaction{
}
assetTypeObj.Readers = readers
}
// case "props":
default:
continue
}
Expand All @@ -96,3 +105,26 @@ var UpdateAssetType = Transaction{
return resBytes, nil
},
}

func handleProps(assetType assets.AssetType, propMap []interface{}) (assets.AssetType, errors.ICCError) {
propObj := assetType.Props

for _, v := range propMap {
v, ok := v.(map[string]interface{})
if !ok {
return assetType, errors.NewCCError("invalid prop object", http.StatusBadRequest)
}

hasProp := assetType.HasProp(v["tag"].(string))
if !hasProp {
newProp, err := BuildAssetProp(v)
if err != nil {
return assetType, errors.WrapError(err, "failed to build prop")
}
propObj = append(propObj, newProp)
}
}

assetType.Props = propObj
return assetType, nil
}

0 comments on commit bcf3393

Please sign in to comment.