aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/zclconf/go-cty/cty/msgpack/dynamic.go
blob: 1b631d0a17775a979d4ce8e4068733467bc406d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package msgpack

import (
	"bytes"

	"github.com/vmihailenco/msgpack"
	"github.com/zclconf/go-cty/cty"
)

type dynamicVal struct {
	Value cty.Value
	Path  cty.Path
}

func (dv *dynamicVal) MarshalMsgpack() ([]byte, error) {
	// Rather than defining a msgpack-specific serialization of types,
	// instead we use the existing JSON serialization.
	typeJSON, err := dv.Value.Type().MarshalJSON()
	if err != nil {
		return nil, dv.Path.NewErrorf("failed to serialize type: %s", err)
	}
	var buf bytes.Buffer
	enc := msgpack.NewEncoder(&buf)
	enc.EncodeArrayLen(2)
	enc.EncodeBytes(typeJSON)
	err = marshal(dv.Value, dv.Value.Type(), dv.Path, enc)
	if err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}