2018-06-14 01:44:01 +02:00
|
|
|
package msgpack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
2020-09-03 17:01:15 +02:00
|
|
|
"github.com/vmihailenco/msgpack/v4"
|
|
|
|
msgpackCodes "github.com/vmihailenco/msgpack/v4/codes"
|
2018-06-14 01:44:01 +02:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Unmarshal interprets the given bytes as a msgpack-encoded cty Value of
|
|
|
|
// the given type, returning the result.
|
|
|
|
//
|
|
|
|
// If an error is returned, the error is written with a hypothetical
|
|
|
|
// end-user that wrote the msgpack file as its audience, using cty type
|
|
|
|
// system concepts rather than Go type system concepts.
|
|
|
|
func Unmarshal(b []byte, ty cty.Type) (cty.Value, error) {
|
|
|
|
r := bytes.NewReader(b)
|
|
|
|
dec := msgpack.NewDecoder(r)
|
|
|
|
|
|
|
|
var path cty.Path
|
|
|
|
return unmarshal(dec, ty, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshal(dec *msgpack.Decoder, ty cty.Type, path cty.Path) (cty.Value, error) {
|
|
|
|
peek, err := dec.PeekCode()
|
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, path.NewError(err)
|
|
|
|
}
|
|
|
|
if msgpackCodes.IsExt(peek) {
|
|
|
|
// We just assume _all_ extensions are unknown values,
|
|
|
|
// since we don't have any other extensions.
|
2018-08-27 20:26:52 +02:00
|
|
|
dec.Skip() // skip what we've peeked
|
2018-06-14 01:44:01 +02:00
|
|
|
return cty.UnknownVal(ty), nil
|
|
|
|
}
|
|
|
|
if ty == cty.DynamicPseudoType {
|
|
|
|
return unmarshalDynamic(dec, path)
|
|
|
|
}
|
|
|
|
if peek == msgpackCodes.Nil {
|
2018-08-27 20:26:52 +02:00
|
|
|
dec.Skip() // skip what we've peeked
|
2018-06-14 01:44:01 +02:00
|
|
|
return cty.NullVal(ty), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ty.IsPrimitiveType():
|
|
|
|
val, err := unmarshalPrimitive(dec, ty, path)
|
|
|
|
if err != nil {
|
|
|
|
return cty.NilVal, err
|
|
|
|
}
|
|
|
|
return val, nil
|
|
|
|
case ty.IsListType():
|
|
|
|
return unmarshalList(dec, ty.ElementType(), path)
|
|
|
|
case ty.IsSetType():
|
|
|
|
return unmarshalSet(dec, ty.ElementType(), path)
|
|
|
|
case ty.IsMapType():
|
|
|
|
return unmarshalMap(dec, ty.ElementType(), path)
|
|
|
|
case ty.IsTupleType():
|
|
|
|
return unmarshalTuple(dec, ty.TupleElementTypes(), path)
|
|
|
|
case ty.IsObjectType():
|
|
|
|
return unmarshalObject(dec, ty.AttributeTypes(), path)
|
|
|
|
default:
|
|
|
|
return cty.NilVal, path.NewErrorf("unsupported type %s", ty.FriendlyName())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalPrimitive(dec *msgpack.Decoder, ty cty.Type, path cty.Path) (cty.Value, error) {
|
|
|
|
switch ty {
|
|
|
|
case cty.Bool:
|
|
|
|
rv, err := dec.DecodeBool()
|
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, path.NewErrorf("bool is required")
|
|
|
|
}
|
|
|
|
return cty.BoolVal(rv), nil
|
|
|
|
case cty.Number:
|
|
|
|
// Marshal will try int and float first, if the value can be
|
|
|
|
// losslessly represented in these encodings, and then fall
|
|
|
|
// back on a string if the number is too large or too precise.
|
|
|
|
peek, err := dec.PeekCode()
|
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, path.NewErrorf("number is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
if msgpackCodes.IsFixedNum(peek) {
|
|
|
|
rv, err := dec.DecodeInt64()
|
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, path.NewErrorf("number is required")
|
|
|
|
}
|
|
|
|
return cty.NumberIntVal(rv), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch peek {
|
|
|
|
case msgpackCodes.Int8, msgpackCodes.Int16, msgpackCodes.Int32, msgpackCodes.Int64:
|
|
|
|
rv, err := dec.DecodeInt64()
|
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, path.NewErrorf("number is required")
|
|
|
|
}
|
|
|
|
return cty.NumberIntVal(rv), nil
|
|
|
|
case msgpackCodes.Uint8, msgpackCodes.Uint16, msgpackCodes.Uint32, msgpackCodes.Uint64:
|
|
|
|
rv, err := dec.DecodeUint64()
|
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, path.NewErrorf("number is required")
|
|
|
|
}
|
|
|
|
return cty.NumberUIntVal(rv), nil
|
|
|
|
case msgpackCodes.Float, msgpackCodes.Double:
|
|
|
|
rv, err := dec.DecodeFloat64()
|
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, path.NewErrorf("number is required")
|
|
|
|
}
|
|
|
|
return cty.NumberFloatVal(rv), nil
|
|
|
|
default:
|
|
|
|
rv, err := dec.DecodeString()
|
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, path.NewErrorf("number is required")
|
|
|
|
}
|
2019-01-25 00:12:57 +01:00
|
|
|
v, err := cty.ParseNumberVal(rv)
|
2018-06-14 01:44:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, path.NewErrorf("number is required")
|
|
|
|
}
|
2019-01-25 00:12:57 +01:00
|
|
|
return v, nil
|
2018-06-14 01:44:01 +02:00
|
|
|
}
|
|
|
|
case cty.String:
|
|
|
|
rv, err := dec.DecodeString()
|
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, path.NewErrorf("string is required")
|
|
|
|
}
|
|
|
|
return cty.StringVal(rv), nil
|
|
|
|
default:
|
|
|
|
// should never happen
|
|
|
|
panic("unsupported primitive type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalList(dec *msgpack.Decoder, ety cty.Type, path cty.Path) (cty.Value, error) {
|
|
|
|
length, err := dec.DecodeArrayLen()
|
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, path.NewErrorf("a list is required")
|
|
|
|
}
|
|
|
|
|
2018-09-26 20:43:32 +02:00
|
|
|
switch {
|
|
|
|
case length < 0:
|
|
|
|
return cty.NullVal(cty.List(ety)), nil
|
|
|
|
case length == 0:
|
2018-06-14 01:44:01 +02:00
|
|
|
return cty.ListValEmpty(ety), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
vals := make([]cty.Value, 0, length)
|
|
|
|
path = append(path, nil)
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
path[len(path)-1] = cty.IndexStep{
|
|
|
|
Key: cty.NumberIntVal(int64(i)),
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := unmarshal(dec, ety, path)
|
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, err
|
|
|
|
}
|
|
|
|
|
|
|
|
vals = append(vals, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cty.ListVal(vals), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalSet(dec *msgpack.Decoder, ety cty.Type, path cty.Path) (cty.Value, error) {
|
|
|
|
length, err := dec.DecodeArrayLen()
|
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, path.NewErrorf("a set is required")
|
|
|
|
}
|
|
|
|
|
2018-09-26 20:43:32 +02:00
|
|
|
switch {
|
|
|
|
case length < 0:
|
|
|
|
return cty.NullVal(cty.Set(ety)), nil
|
|
|
|
case length == 0:
|
2018-06-14 01:44:01 +02:00
|
|
|
return cty.SetValEmpty(ety), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
vals := make([]cty.Value, 0, length)
|
|
|
|
path = append(path, nil)
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
path[len(path)-1] = cty.IndexStep{
|
|
|
|
Key: cty.NumberIntVal(int64(i)),
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := unmarshal(dec, ety, path)
|
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, err
|
|
|
|
}
|
|
|
|
|
|
|
|
vals = append(vals, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cty.SetVal(vals), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalMap(dec *msgpack.Decoder, ety cty.Type, path cty.Path) (cty.Value, error) {
|
|
|
|
length, err := dec.DecodeMapLen()
|
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, path.NewErrorf("a map is required")
|
|
|
|
}
|
|
|
|
|
2018-09-26 20:43:32 +02:00
|
|
|
switch {
|
|
|
|
case length < 0:
|
|
|
|
return cty.NullVal(cty.Map(ety)), nil
|
|
|
|
case length == 0:
|
2018-06-14 01:44:01 +02:00
|
|
|
return cty.MapValEmpty(ety), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
vals := make(map[string]cty.Value, length)
|
|
|
|
path = append(path, nil)
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
key, err := dec.DecodeString()
|
|
|
|
if err != nil {
|
|
|
|
path[:len(path)-1].NewErrorf("non-string key in map")
|
|
|
|
}
|
|
|
|
|
|
|
|
path[len(path)-1] = cty.IndexStep{
|
|
|
|
Key: cty.StringVal(key),
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := unmarshal(dec, ety, path)
|
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, err
|
|
|
|
}
|
|
|
|
|
|
|
|
vals[key] = val
|
|
|
|
}
|
|
|
|
|
|
|
|
return cty.MapVal(vals), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalTuple(dec *msgpack.Decoder, etys []cty.Type, path cty.Path) (cty.Value, error) {
|
|
|
|
length, err := dec.DecodeArrayLen()
|
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, path.NewErrorf("a tuple is required")
|
|
|
|
}
|
|
|
|
|
2018-09-26 20:43:32 +02:00
|
|
|
switch {
|
|
|
|
case length < 0:
|
|
|
|
return cty.NullVal(cty.Tuple(etys)), nil
|
|
|
|
case length == 0:
|
|
|
|
return cty.TupleVal(nil), nil
|
|
|
|
case length != len(etys):
|
2018-06-14 01:44:01 +02:00
|
|
|
return cty.DynamicVal, path.NewErrorf("a tuple of length %d is required", len(etys))
|
|
|
|
}
|
|
|
|
|
|
|
|
vals := make([]cty.Value, 0, length)
|
|
|
|
path = append(path, nil)
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
path[len(path)-1] = cty.IndexStep{
|
|
|
|
Key: cty.NumberIntVal(int64(i)),
|
|
|
|
}
|
|
|
|
ety := etys[i]
|
|
|
|
|
|
|
|
val, err := unmarshal(dec, ety, path)
|
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, err
|
|
|
|
}
|
|
|
|
|
|
|
|
vals = append(vals, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cty.TupleVal(vals), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalObject(dec *msgpack.Decoder, atys map[string]cty.Type, path cty.Path) (cty.Value, error) {
|
|
|
|
length, err := dec.DecodeMapLen()
|
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, path.NewErrorf("an object is required")
|
|
|
|
}
|
|
|
|
|
2018-09-26 20:43:32 +02:00
|
|
|
switch {
|
|
|
|
case length < 0:
|
|
|
|
return cty.NullVal(cty.Object(atys)), nil
|
|
|
|
case length == 0:
|
|
|
|
return cty.ObjectVal(nil), nil
|
|
|
|
case length != len(atys):
|
2018-12-19 00:15:50 +01:00
|
|
|
return cty.DynamicVal, path.NewErrorf("an object with %d attributes is required (%d given)",
|
|
|
|
len(atys), length)
|
2018-06-14 01:44:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
vals := make(map[string]cty.Value, length)
|
|
|
|
path = append(path, nil)
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
key, err := dec.DecodeString()
|
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, path[:len(path)-1].NewErrorf("all keys must be strings")
|
|
|
|
}
|
|
|
|
|
|
|
|
path[len(path)-1] = cty.IndexStep{
|
|
|
|
Key: cty.StringVal(key),
|
|
|
|
}
|
|
|
|
aty, exists := atys[key]
|
|
|
|
if !exists {
|
|
|
|
return cty.DynamicVal, path.NewErrorf("unsupported attribute")
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := unmarshal(dec, aty, path)
|
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, err
|
|
|
|
}
|
|
|
|
|
|
|
|
vals[key] = val
|
|
|
|
}
|
|
|
|
|
|
|
|
return cty.ObjectVal(vals), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalDynamic(dec *msgpack.Decoder, path cty.Path) (cty.Value, error) {
|
|
|
|
length, err := dec.DecodeArrayLen()
|
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, path.NewError(err)
|
|
|
|
}
|
|
|
|
|
2018-09-26 20:43:32 +02:00
|
|
|
switch {
|
|
|
|
case length == -1:
|
|
|
|
return cty.NullVal(cty.DynamicPseudoType), nil
|
|
|
|
case length != 2:
|
2018-06-14 01:44:01 +02:00
|
|
|
return cty.DynamicVal, path.NewErrorf(
|
|
|
|
"dynamic value array must have exactly two elements",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
typeJSON, err := dec.DecodeBytes()
|
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, path.NewError(err)
|
|
|
|
}
|
|
|
|
var ty cty.Type
|
|
|
|
err = (&ty).UnmarshalJSON(typeJSON)
|
|
|
|
if err != nil {
|
|
|
|
return cty.DynamicVal, path.NewError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return unmarshal(dec, ty, path)
|
|
|
|
}
|