| 1 | package ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src go1.18 bytes/" |
| 5 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/" |
| 6 | "fmtscip-go gomod github.com/golang/go/src go1.18 fmt/" |
| 7 | "sortscip-go gomod github.com/golang/go/src go1.18 sort/" |
| 8 | ) |
| 9 | |
| 10 | // MarshalJSON is an implementation of json.Marshaler that allows Type |
| 11 | // instances to be serialized as JSON. |
| 12 | // |
| 13 | // All standard types can be serialized, but capsule types cannot since there |
| 14 | // is no way to automatically recover the original pointer and capsule types |
| 15 | // compare by equality. |
| 16 | func (t Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#) MarshalJSONscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#MarshalJSON().() ([]byte, error) { |
| 17 | switch impl := t.typeImplscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#typeImpl..(type) { |
| 18 | case primitiveTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/primitiveType#: |
| 19 | switch impl.Kindscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/primitiveType#Kind. { |
| 20 | case primitiveTypeBoolscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/primitiveTypeBool.: |
| 21 | return []byte{'"', 'b', 'o', 'o', 'l', '"'}, nil |
| 22 | case primitiveTypeNumberscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/primitiveTypeNumber.: |
| 23 | return []byte{'"', 'n', 'u', 'm', 'b', 'e', 'r', '"'}, nil |
| 24 | case primitiveTypeStringscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/primitiveTypeString.: |
| 25 | return []byte{'"', 's', 't', 'r', 'i', 'n', 'g', '"'}, nil |
| 26 | default: |
| 27 | panic("unknown primitive type kind") |
| 28 | } |
| 29 | case typeListscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/typeList#, typeMapscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/typeMap#, typeSetscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/typeSet#: |
| 30 | buf := &bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#{} |
| 31 | etyJSON, err := t.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().().MarshalJSONscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#MarshalJSON().() |
| 32 | if err != nil { |
| 33 | return nil, err |
| 34 | } |
| 35 | buf.WriteRunescip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#WriteRune().('[') |
| 36 | switch impl.(type) { |
| 37 | case typeListscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/typeList#: |
| 38 | buf.WriteStringscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#WriteString().(`"list"`) |
| 39 | case typeMapscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/typeMap#: |
| 40 | buf.WriteStringscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#WriteString().(`"map"`) |
| 41 | case typeSetscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/typeSet#: |
| 42 | buf.WriteStringscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#WriteString().(`"set"`) |
| 43 | } |
| 44 | buf.WriteRunescip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#WriteRune().(',') |
| 45 | buf.Writescip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#Write().(etyJSON) |
| 46 | buf.WriteRunescip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#WriteRune().(']') |
| 47 | return buf.Bytesscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#Bytes().(), nil |
| 48 | case typeObjectscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/typeObject#: |
| 49 | buf := &bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#{} |
| 50 | atysJSON, err := jsonscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/Marshal().(t.AttributeTypesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#AttributeTypes().()) |
| 51 | if err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | buf.WriteStringscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#WriteString().(`["object",`) |
| 55 | buf.Writescip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#Write().(atysJSON) |
| 56 | if optionals := t.OptionalAttributesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#OptionalAttributes().(); len(optionals) > 0 { |
| 57 | buf.WriteBytescip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#WriteByte().(',') |
| 58 | optionalNames := make([]string, 0, len(optionals)) |
| 59 | for k := range optionals { |
| 60 | optionalNames = append(optionalNames, k) |
| 61 | } |
| 62 | sortscip-go gomod github.com/golang/go/src go1.18 sort/.Stringsscip-go gomod github.com/golang/go/src go1.18 sort/Strings().(optionalNames) |
| 63 | optionalsJSON, err := jsonscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/Marshal().(optionalNames) |
| 64 | if err != nil { |
| 65 | return nil, err |
| 66 | } |
| 67 | buf.Writescip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#Write().(optionalsJSON) |
| 68 | } |
| 69 | buf.WriteRunescip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#WriteRune().(']') |
| 70 | return buf.Bytesscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#Bytes().(), nil |
| 71 | case typeTuplescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/typeTuple#: |
| 72 | buf := &bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#{} |
| 73 | etysJSON, err := jsonscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/Marshal().(t.TupleElementTypesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#TupleElementTypes().()) |
| 74 | if err != nil { |
| 75 | return nil, err |
| 76 | } |
| 77 | buf.WriteStringscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#WriteString().(`["tuple",`) |
| 78 | buf.Writescip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#Write().(etysJSON) |
| 79 | buf.WriteRunescip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#WriteRune().(']') |
| 80 | return buf.Bytesscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#Bytes().(), nil |
| 81 | case pseudoTypeDynamicscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/pseudoTypeDynamic#: |
| 82 | return []byte{'"', 'd', 'y', 'n', 'a', 'm', 'i', 'c', '"'}, nil |
| 83 | case *capsuleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/capsuleType#: |
| 84 | return nil, fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("type not allowed: %s", t.FriendlyNamescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#FriendlyName().()) |
| 85 | default: |
| 86 | // should never happen |
| 87 | panic("unknown type implementation") |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // UnmarshalJSON is the opposite of MarshalJSON. See the documentation of |
| 92 | // MarshalJSON for information on the limitations of JSON serialization of |
| 93 | // types. |
| 94 | func (t *Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#) UnmarshalJSONscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#UnmarshalJSON().(buf []byte) error { |
| 95 | r := bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.NewReaderscip-go gomod github.com/golang/go/src go1.18 bytes/NewReader().(buf) |
| 96 | dec := jsonscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/.NewDecoderscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/NewDecoder().(r) |
| 97 | |
| 98 | tok, err := dec.Tokenscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/Decoder#Token().() |
| 99 | if err != nil { |
| 100 | return err |
| 101 | } |
| 102 | |
| 103 | switch v := tok.(type) { |
| 104 | case string: |
| 105 | switch v { |
| 106 | case "bool": |
| 107 | *t = Boolscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Bool. |
| 108 | case "number": |
| 109 | *t = Numberscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Number. |
| 110 | case "string": |
| 111 | *t = Stringscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/String. |
| 112 | case "dynamic": |
| 113 | *t = DynamicPseudoTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/DynamicPseudoType. |
| 114 | default: |
| 115 | return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("invalid primitive type name %q", v) |
| 116 | } |
| 117 | |
| 118 | if dec.Morescip-go gomod github.com/golang/go/src go1.18 `encoding/json`/Decoder#More().() { |
| 119 | return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("extraneous data after type description") |
| 120 | } |
| 121 | return nil |
| 122 | case jsonscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/.Delimscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/Delim#: |
| 123 | if rune(v) != '[' { |
| 124 | return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("invalid complex type description") |
| 125 | } |
| 126 | |
| 127 | tok, err = dec.Tokenscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/Decoder#Token().() |
| 128 | if err != nil { |
| 129 | return err |
| 130 | } |
| 131 | |
| 132 | kind, ok := tok.(string) |
| 133 | if !ok { |
| 134 | return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("invalid complex type kind name") |
| 135 | } |
| 136 | |
| 137 | switch kind { |
| 138 | case "list": |
| 139 | var ety Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type# |
| 140 | err = dec.Decodescip-go gomod github.com/golang/go/src go1.18 `encoding/json`/Decoder#Decode().(&ety) |
| 141 | if err != nil { |
| 142 | return err |
| 143 | } |
| 144 | *t = Listscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/List().(ety) |
| 145 | case "map": |
| 146 | var ety Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type# |
| 147 | err = dec.Decodescip-go gomod github.com/golang/go/src go1.18 `encoding/json`/Decoder#Decode().(&ety) |
| 148 | if err != nil { |
| 149 | return err |
| 150 | } |
| 151 | *t = Mapscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Map().(ety) |
| 152 | case "set": |
| 153 | var ety Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type# |
| 154 | err = dec.Decodescip-go gomod github.com/golang/go/src go1.18 `encoding/json`/Decoder#Decode().(&ety) |
| 155 | if err != nil { |
| 156 | return err |
| 157 | } |
| 158 | *t = Setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Set().(ety) |
| 159 | case "object": |
| 160 | var atys map[string]Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type# |
| 161 | err = dec.Decodescip-go gomod github.com/golang/go/src go1.18 `encoding/json`/Decoder#Decode().(&atys) |
| 162 | if err != nil { |
| 163 | return err |
| 164 | } |
| 165 | if dec.Morescip-go gomod github.com/golang/go/src go1.18 `encoding/json`/Decoder#More().() { |
| 166 | var optionals []string |
| 167 | err = dec.Decodescip-go gomod github.com/golang/go/src go1.18 `encoding/json`/Decoder#Decode().(&optionals) |
| 168 | if err != nil { |
| 169 | return err |
| 170 | } |
| 171 | *t = ObjectWithOptionalAttrsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/ObjectWithOptionalAttrs().(atys, optionals) |
| 172 | } else { |
| 173 | *t = Objectscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Object().(atys) |
| 174 | } |
| 175 | case "tuple": |
| 176 | var etys []Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type# |
| 177 | err = dec.Decodescip-go gomod github.com/golang/go/src go1.18 `encoding/json`/Decoder#Decode().(&etys) |
| 178 | if err != nil { |
| 179 | return err |
| 180 | } |
| 181 | *t = Tuplescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Tuple().(etys) |
| 182 | default: |
| 183 | return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("invalid complex type kind name") |
| 184 | } |
| 185 | |
| 186 | tok, err = dec.Tokenscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/Decoder#Token().() |
| 187 | if err != nil { |
| 188 | return err |
| 189 | } |
| 190 | if delim, ok := tok.(jsonscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/.Delimscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/Delim#); !ok || rune(delim) != ']' || dec.Morescip-go gomod github.com/golang/go/src go1.18 `encoding/json`/Decoder#More().() { |
| 191 | return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("unexpected extra data in type description") |
| 192 | } |
| 193 | |
| 194 | return nil |
| 195 | |
| 196 | default: |
| 197 | return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("invalid type description") |
| 198 | } |
| 199 | } |