aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/zclconf/go-cty/cty/json.go
blob: c421a62ed949ecc02a30376516dd8aa94a7b0b4e (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package cty

import (
	"bytes"
	"encoding/json"
	"fmt"
)

// MarshalJSON is an implementation of json.Marshaler that allows Type
// instances to be serialized as JSON.
//
// All standard types can be serialized, but capsule types cannot since there
// is no way to automatically recover the original pointer and capsule types
// compare by equality.
func (t Type) MarshalJSON() ([]byte, error) {
	switch impl := t.typeImpl.(type) {
	case primitiveType:
		switch impl.Kind {
		case primitiveTypeBool:
			return []byte{'"', 'b', 'o', 'o', 'l', '"'}, nil
		case primitiveTypeNumber:
			return []byte{'"', 'n', 'u', 'm', 'b', 'e', 'r', '"'}, nil
		case primitiveTypeString:
			return []byte{'"', 's', 't', 'r', 'i', 'n', 'g', '"'}, nil
		default:
			panic("unknown primitive type kind")
		}
	case typeList, typeMap, typeSet:
		buf := &bytes.Buffer{}
		etyJSON, err := t.ElementType().MarshalJSON()
		if err != nil {
			return nil, err
		}
		buf.WriteRune('[')
		switch impl.(type) {
		case typeList:
			buf.WriteString(`"list"`)
		case typeMap:
			buf.WriteString(`"map"`)
		case typeSet:
			buf.WriteString(`"set"`)
		}
		buf.WriteRune(',')
		buf.Write(etyJSON)
		buf.WriteRune(']')
		return buf.Bytes(), nil
	case typeObject:
		buf := &bytes.Buffer{}
		atysJSON, err := json.Marshal(t.AttributeTypes())
		if err != nil {
			return nil, err
		}
		buf.WriteString(`["object",`)
		buf.Write(atysJSON)
		buf.WriteRune(']')
		return buf.Bytes(), nil
	case typeTuple:
		buf := &bytes.Buffer{}
		etysJSON, err := json.Marshal(t.TupleElementTypes())
		if err != nil {
			return nil, err
		}
		buf.WriteString(`["tuple",`)
		buf.Write(etysJSON)
		buf.WriteRune(']')
		return buf.Bytes(), nil
	case pseudoTypeDynamic:
		return []byte{'"', 'd', 'y', 'n', 'a', 'm', 'i', 'c', '"'}, nil
	case *capsuleType:
		return nil, fmt.Errorf("type not allowed: %s", t.FriendlyName())
	default:
		// should never happen
		panic("unknown type implementation")
	}
}

// UnmarshalJSON is the opposite of MarshalJSON. See the documentation of
// MarshalJSON for information on the limitations of JSON serialization of
// types.
func (t *Type) UnmarshalJSON(buf []byte) error {
	r := bytes.NewReader(buf)
	dec := json.NewDecoder(r)

	tok, err := dec.Token()
	if err != nil {
		return err
	}

	switch v := tok.(type) {
	case string:
		switch v {
		case "bool":
			*t = Bool
		case "number":
			*t = Number
		case "string":
			*t = String
		case "dynamic":
			*t = DynamicPseudoType
		default:
			return fmt.Errorf("invalid primitive type name %q", v)
		}

		if dec.More() {
			return fmt.Errorf("extraneous data after type description")
		}
		return nil
	case json.Delim:
		if rune(v) != '[' {
			return fmt.Errorf("invalid complex type description")
		}

		tok, err = dec.Token()
		if err != nil {
			return err
		}

		kind, ok := tok.(string)
		if !ok {
			return fmt.Errorf("invalid complex type kind name")
		}

		switch kind {
		case "list":
			var ety Type
			err = dec.Decode(&ety)
			if err != nil {
				return err
			}
			*t = List(ety)
		case "map":
			var ety Type
			err = dec.Decode(&ety)
			if err != nil {
				return err
			}
			*t = Map(ety)
		case "set":
			var ety Type
			err = dec.Decode(&ety)
			if err != nil {
				return err
			}
			*t = Set(ety)
		case "object":
			var atys map[string]Type
			err = dec.Decode(&atys)
			if err != nil {
				return err
			}
			*t = Object(atys)
		case "tuple":
			var etys []Type
			err = dec.Decode(&etys)
			if err != nil {
				return err
			}
			*t = Tuple(etys)
		default:
			return fmt.Errorf("invalid complex type kind name")
		}

		tok, err = dec.Token()
		if err != nil {
			return err
		}
		if delim, ok := tok.(json.Delim); !ok || rune(delim) != ']' || dec.More() {
			return fmt.Errorf("unexpected extra data in type description")
		}

		return nil

	default:
		return fmt.Errorf("invalid type description")
	}
}