Extend hcl2 support with more functions

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2020-12-23 12:33:55 +01:00
parent 74f76cf4e9
commit 96e7f3224a
90 changed files with 4425 additions and 198 deletions

View File

@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"sort"
)
// MarshalJSON is an implementation of json.Marshaler that allows Type
@ -52,6 +53,19 @@ func (t Type) MarshalJSON() ([]byte, error) {
}
buf.WriteString(`["object",`)
buf.Write(atysJSON)
if optionals := t.OptionalAttributes(); len(optionals) > 0 {
buf.WriteByte(',')
optionalNames := make([]string, 0, len(optionals))
for k := range optionals {
optionalNames = append(optionalNames, k)
}
sort.Strings(optionalNames)
optionalsJSON, err := json.Marshal(optionalNames)
if err != nil {
return nil, err
}
buf.Write(optionalsJSON)
}
buf.WriteRune(']')
return buf.Bytes(), nil
case typeTuple:
@ -148,7 +162,16 @@ func (t *Type) UnmarshalJSON(buf []byte) error {
if err != nil {
return err
}
*t = Object(atys)
if dec.More() {
var optionals []string
err = dec.Decode(&optionals)
if err != nil {
return err
}
*t = ObjectWithOptionalAttrs(atys, optionals)
} else {
*t = Object(atys)
}
case "tuple":
var etys []Type
err = dec.Decode(&etys)