Skip to content

Commit

Permalink
✨ Create map conversion functions to assetType and assetProp
Browse files Browse the repository at this point in the history
  • Loading branch information
andremacedopv committed Jan 19, 2023
1 parent 6490664 commit d14e12e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
48 changes: 48 additions & 0 deletions assets/assetProp.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,51 @@ type AssetProp struct {
// Validate is a function called when validating property format.
Validate func(interface{}) error `json:"-"`
}

// ToMap converts an AssetProp to a map[string]interface{}
func (p AssetProp) ToMap() map[string]interface{} {
return map[string]interface{}{
"tag": p.Tag,
"label": p.Label,
"description": p.Description,
"isKey": p.IsKey,
"required": p.Required,
"readOnly": p.ReadOnly,
"defaultValue": p.DefaultValue,
"dataType": p.DataType,
"writers": p.Writers,
}
}

// AssetPropFromMap converts a map[string]interface{} to an AssetProp
func AssetPropFromMap(m map[string]interface{}) AssetProp {
return AssetProp{
Tag: m["tag"].(string),
Label: m["label"].(string),
Description: m["description"].(string),
IsKey: m["isKey"].(bool),
Required: m["required"].(bool),
ReadOnly: m["readOnly"].(bool),
DefaultValue: m["defaultValue"],
DataType: m["dataType"].(string),
Writers: m["writers"].([]string),
}
}

// ArrayFromAssetPropList converts an array of AssetProp to an array of map[string]interface
func ArrayFromAssetPropList(a []AssetProp) []map[string]interface{} {
list := []map[string]interface{}{}
for _, m := range a {
list = append(list, m.ToMap())
}
return list
}

// AssetPropListFromArray converts an array of map[string]interface to an array of AssetProp
func AssetPropListFromArray(a []map[string]interface{}) []AssetProp {
list := []AssetProp{}
for _, m := range a {
list = append(list, AssetPropFromMap(m))
}
return list
}
38 changes: 38 additions & 0 deletions assets/assetType.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,41 @@ func (t AssetType) GetPropDef(propTag string) *AssetProp {
func (t AssetType) IsPrivate() bool {
return len(t.Readers) > 0
}

// ToMap returns a map representation of the asset type.
func (t AssetType) ToMap() map[string]interface{} {
return map[string]interface{}{
"tag": t.Tag,
"label": t.Label,
"description": t.Description,
"props": ArrayFromAssetPropList(t.Props),
"readers": t.Readers,
}
}

// AssetTypeFromMap returns an asset type from a map representation.
func AssetTypeFromMap(m map[string]interface{}) AssetType {
return AssetType{
Tag: m["tag"].(string),
Label: m["label"].(string),
Description: m["description"].(string),
Props: AssetPropListFromArray(m["props"].([]map[string]interface{})),
Readers: m["readers"].([]string),
}
}

// ArrayFromAssetTypeList converts an array of AssetType to an array of map[string]interface
func ArrayFromAssetTypeList(assetTypes []AssetType) (array []map[string]interface{}) {
for _, assetType := range assetTypes {
array = append(array, assetType.ToMap())
}
return
}

// AssetTypeListFromArray converts an array of map[string]interface to an array of AssetType
func AssetTypeListFromArray(array []map[string]interface{}) (assetTypes []AssetType) {
for _, m := range array {
assetTypes = append(assetTypes, AssetTypeFromMap(m))
}
return
}

0 comments on commit d14e12e

Please sign in to comment.