Skip to content

Commit

Permalink
Add quality of life methods for existing types
Browse files Browse the repository at this point in the history
  • Loading branch information
bandreghetti committed Nov 23, 2020
1 parent 3e6e604 commit 1ca2af9
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion assets/assetListFuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ func AssetTypeList() []AssetType {
return listCopy
}

// FetchAssetType returns a pointer to the AssetType object or nil if tx is not found
// FetchAssetType returns a pointer to the AssetType object or nil if asset type is not found
func FetchAssetType(assetTypeTag string) *AssetType {
for _, assetType := range assetTypeList {
if assetType.Tag == assetTypeTag {
Expand Down
10 changes: 10 additions & 0 deletions assets/assetType.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ func (t AssetType) HasProp(propTag string) bool {
return false
}

// GetPropDef fetches the propDef with tag propTag
func (t AssetType) GetPropDef(propTag string) *AssetProp {
for _, prop := range t.Props {
if prop.Tag == propTag {
return &prop
}
}
return nil
}

// IsPrivate returns true if asset is in a private collection
func (t AssetType) IsPrivate() bool {
return len(t.Readers) > 0
Expand Down
19 changes: 19 additions & 0 deletions assets/existsInLedger.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,22 @@ func (a *Asset) ExistsInLedger(stub shim.ChaincodeStubInterface) (bool, errors.I

return false, nil
}

// ExistsInLedger checks if asset referenced by key already exists
func (k *Key) ExistsInLedger(stub shim.ChaincodeStubInterface) (bool, errors.ICCError) {
var assetBytes []byte
var err error
if k.IsPrivate() {
assetBytes, err = stub.GetPrivateDataHash(k.TypeTag(), k.Key())
} else {
assetBytes, err = stub.GetState(k.Key())
}
if err != nil {
return false, errors.WrapErrorWithStatus(err, "unable to check asset existence", 400)
}
if assetBytes != nil {
return true, nil
}

return false, nil
}
7 changes: 7 additions & 0 deletions assets/getProp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package assets

// GetProp returns the prop value. It returns nil if it doesn't exist.
func (a Asset) GetProp(propTag string) interface{} {
val, _ := a[propTag]
return val
}
34 changes: 34 additions & 0 deletions assets/setProp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package assets

import (
"fmt"

"github.com/goledgerdev/cc-tools/errors"
)

// SetProp sets the prop value with proper validation.
func (a *Asset) SetProp(propTag string, value interface{}) errors.ICCError {
if len(propTag) == 0 {
return errors.NewCCError("propTag cannot be empty", 500)
}
if propTag[0] == '@' {
return errors.NewCCError("cannot modify internal properties", 500)
}
assetType := a.Type()
if assetType == nil {
return errors.NewCCError("asset type does not exist", 500)
}
propDef := assetType.GetPropDef(propTag)
if propDef == nil {
return errors.NewCCError(fmt.Sprintf("asset type '%s' does not have prop named '%s'", assetType.Tag, propTag), 500)
}
propType := DataTypeMap()[propDef.DataType]

_, parsedVal, err := propType.Parse(value)
if err != nil {
return errors.WrapError(err, fmt.Sprintf("invalid '%s' value", propTag))
}
(*a)[propTag] = parsedVal

return nil
}

0 comments on commit 1ca2af9

Please sign in to comment.