Skip to content

Commit

Permalink
Add Value#Interface()
Browse files Browse the repository at this point in the history
  • Loading branch information
ConradIrwin committed Jun 11, 2023
1 parent 0e5f2cb commit f81214c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func As[T any](v *Value, errs ...error) (ret T, err error) {

default:
var val any
val = v.goValue()
val = v.Interface()
if r, ok := val.(T); ok {
return r, nil
}
Expand Down
12 changes: 8 additions & 4 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,22 @@ func (v *Value) assertKind(k Kind) {
}
}

func (v *Value) goValue() any {
// Interface returns the value as a go interface.
// It recursively converts automerge.Map to map[string]any,
// automerge.List to []any, automerge.Text to string, and
// automerge.Counter to int64.
func (v *Value) Interface() any {
switch v.kind {
case KindMap:
values, err := v.Map().Values()
// this should not be able to happen because .load() is only
// called from Value.goValue() which checks that this is a map.
// called from Value.Interface() which checks that this is a map.
if err != nil {
panic(err)
}
out := map[string]any{}
for k, v := range values {
out[k] = v.goValue()
out[k] = v.Interface()
}
return out
case KindList:
Expand All @@ -193,7 +197,7 @@ func (v *Value) goValue() any {
}
out := []any{}
for _, v := range values {
out = append(out, v.goValue())
out = append(out, v.Interface())
}
return out
case KindText:
Expand Down

0 comments on commit f81214c

Please sign in to comment.