Skip to content

Commit

Permalink
feat(go): add Asset method to Value
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Feb 29, 2024
1 parent b8e8155 commit 5b5ad04
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
40 changes: 40 additions & 0 deletions go/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,43 @@ type PublicAsset struct {
func (a PublicAsset) IsExtractionDone() bool {
return a.ArchiveExtractionStatus == "done"
}

func PublicAssetFrom(a any) *PublicAsset {
if a == nil {
return nil
}
if a, ok := a.(PublicAsset); ok {
return &a
}
if a, ok := a.(*PublicAsset); ok {
return a
}

m, ok := a.(map[string]any)
if !ok {
return nil
}

aa := PublicAsset{}
if v, ok := m["type"].(string); ok {
aa.Type = v
}

if v, ok := m["id"].(string); ok {
aa.ID = v
}

if v, ok := m["url"].(string); ok {
aa.URL = v
}

if v, ok := m["contentType"].(string); ok {
aa.ContentType = v
}

if v, ok := m["archiveExtractionStatus"].(string); ok {
aa.ArchiveExtractionStatus = v
}

return &aa
}
26 changes: 25 additions & 1 deletion go/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,29 @@ func (v *Value) Bools() []bool {
return getValues[bool](v)
}

func (v *Value) Asset() *PublicAsset {
if v == nil {
return nil
}
return PublicAssetFrom(v.value)
}

func (v *Value) AssetID() string {
a := v.Asset()
if a == nil {
return ""
}
return a.ID
}

func (v *Value) AssetURL() string {
a := v.Asset()
if a == nil {
return ""
}
return a.URL
}

func (v *Value) Tag() *Tag {
if v == nil {
return nil
Expand Down Expand Up @@ -221,8 +244,9 @@ func (v *Value) MarshalJSON() ([]byte, error) {

func (v *Value) UnmarshalJSON(b []byte) error {
if v == nil {
*v = Value{}
return nil
}

if err := json.Unmarshal(b, &v.value); err != nil {
return err
}
Expand Down

0 comments on commit 5b5ad04

Please sign in to comment.