Skip to content

Commit

Permalink
Utility method improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleu committed Sep 14, 2024
1 parent a46d987 commit e4dd85a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/util/mapordered.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/samber/lo"
"golang.org/x/exp/maps"
)

type OrderedPair[V any] struct {
Expand Down Expand Up @@ -78,6 +79,13 @@ func (o *OrderedMap[V]) Remove(k string) {
delete(o.Map, k)
}

func (o *OrderedMap[V]) Clone() *OrderedMap[V] {
if o == nil {
return nil
}
return &OrderedMap[V]{Lexical: o.Lexical, Order: slices.Clone(o.Order), Map: maps.Clone(o.Map)}
}

func (o OrderedMap[V]) MarshalYAML() (any, error) {
return o.Map, nil
}
Expand Down
6 changes: 6 additions & 0 deletions app/util/mapparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ func (m ValueMap) ParseInt(path string, allowMissing bool, allowEmpty bool) (int
})
}

func (m ValueMap) ParseJSON(path string, allowMissing bool, allowEmpty bool) (any, error) {
return parseMapField(m, path, allowMissing, func(res any) (any, error) {
return ParseJSON(res, path, allowEmpty)
})
}

func (m ValueMap) ParseMap(path string, allowMissing bool, allowEmpty bool) (ValueMap, error) {
return parseMapField(m, path, allowMissing, func(res any) (ValueMap, error) {
return ParseMap(res, path, allowEmpty)
Expand Down
16 changes: 16 additions & 0 deletions app/util/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ func ParseInt(r any, path string, allowEmpty bool) (int, error) {
}
}

func ParseJSON(r any, path string, allowEmpty bool) (any, error) {
switch t := r.(type) {
case []byte:
return FromJSONAny(t)
case string:
return FromJSONAny([]byte(t))
case nil:
if !allowEmpty {
return nil, errors.Errorf("could not find json for path [%s]", path)
}
return nil, nil
default:
return t, nil
}
}

func ParseMap(r any, path string, allowEmpty bool) (ValueMap, error) {
switch t := r.(type) {
case ValueMap:
Expand Down

0 comments on commit e4dd85a

Please sign in to comment.