diff options
Diffstat (limited to 'vendor/github.com/zclconf/go-cty/cty/json/value.go')
-rw-r--r-- | vendor/github.com/zclconf/go-cty/cty/json/value.go | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/vendor/github.com/zclconf/go-cty/cty/json/value.go b/vendor/github.com/zclconf/go-cty/cty/json/value.go new file mode 100644 index 0000000..f2f7dd5 --- /dev/null +++ b/vendor/github.com/zclconf/go-cty/cty/json/value.go | |||
@@ -0,0 +1,65 @@ | |||
1 | package json | ||
2 | |||
3 | import ( | ||
4 | "bytes" | ||
5 | |||
6 | "github.com/zclconf/go-cty/cty" | ||
7 | "github.com/zclconf/go-cty/cty/convert" | ||
8 | ) | ||
9 | |||
10 | // Marshal produces a JSON representation of the given value that can later | ||
11 | // be decoded into a value of the given type. | ||
12 | // | ||
13 | // A type is specified separately to allow for the given type to include | ||
14 | // cty.DynamicPseudoType to represent situations where any type is permitted | ||
15 | // and so type information must be included to allow recovery of the stored | ||
16 | // structure when decoding. | ||
17 | // | ||
18 | // The given type will also be used to attempt automatic conversions of any | ||
19 | // non-conformant types in the given value, although this will not always | ||
20 | // be possible. If the value cannot be made to be conformant then an error is | ||
21 | // returned, which may be a cty.PathError. | ||
22 | // | ||
23 | // Capsule-typed values can be marshalled, but with some caveats. Since | ||
24 | // capsule values are compared by pointer equality, it is impossible to recover | ||
25 | // a value that will compare equal to the original value. Additionally, | ||
26 | // it's not possible to JSON-serialize the capsule type itself, so it's not | ||
27 | // valid to use capsule types within parts of the value that are conformed to | ||
28 | // cty.DynamicPseudoType. Otherwise, a capsule value can be used as long as | ||
29 | // the encapsulated type itself is serializable with the Marshal function | ||
30 | // in encoding/json. | ||
31 | func Marshal(val cty.Value, t cty.Type) ([]byte, error) { | ||
32 | errs := val.Type().TestConformance(t) | ||
33 | if errs != nil { | ||
34 | // Attempt a conversion | ||
35 | var err error | ||
36 | val, err = convert.Convert(val, t) | ||
37 | if err != nil { | ||
38 | return nil, err | ||
39 | } | ||
40 | } | ||
41 | |||
42 | // From this point onward, val can be assumed to be conforming to t. | ||
43 | |||
44 | buf := &bytes.Buffer{} | ||
45 | var path cty.Path | ||
46 | err := marshal(val, t, path, buf) | ||
47 | |||
48 | if err != nil { | ||
49 | return nil, err | ||
50 | } | ||
51 | |||
52 | return buf.Bytes(), nil | ||
53 | } | ||
54 | |||
55 | // Unmarshal decodes a JSON representation of the given value into a cty Value | ||
56 | // conforming to the given type. | ||
57 | // | ||
58 | // While decoding, type conversions will be done where possible to make | ||
59 | // the result conformant even if the types given in JSON are not exactly | ||
60 | // correct. If conversion isn't possible then an error is returned, which | ||
61 | // may be a cty.PathError. | ||
62 | func Unmarshal(buf []byte, t cty.Type) (cty.Value, error) { | ||
63 | var path cty.Path | ||
64 | return unmarshal(buf, t, path) | ||
65 | } | ||