Skip to content

Commit

Permalink
fix: detect list/map schema
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas committed Oct 12, 2022
1 parent d692e15 commit 891b0f0
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ func marshalAttribute(w io.Writer, indent string, attribute *Attribute) error {
return err
}
constraints := []string{}
if _, ok := attribute.Value.(*Type); ok {
if isType(attribute.Value) {
if attribute.Optional {
constraints = append(constraints, "optional")
}
Expand All @@ -615,6 +615,22 @@ func marshalAttribute(w io.Writer, indent string, attribute *Attribute) error {
return nil
}

func isType(value Value) bool {
switch value := value.(type) {
case *Type:
return true

case *List:
return len(value.List) == 1 && isType(value.List[0])

case *Map:
return len(value.Entries) == 1 && isType(value.Entries[0].Value)

default:
return false
}
}

func marshalValue(w io.Writer, indent string, value Value) error {
if value, ok := value.(*Map); ok {
return marshalMap(w, indent+" ", value.Entries)
Expand Down

0 comments on commit 891b0f0

Please sign in to comment.