-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add quality of life methods for existing types
- Loading branch information
1 parent
3e6e604
commit 1ca2af9
Showing
5 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |