Debian Codegraph @

cty/msgpack/marshal.go

raw symbols SCIP
1 package msgpackscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/msgpack`/
2
3 import (
4 "bytesscip-go gomod github.com/golang/go/src go1.18 bytes/"
5 "math/bigscip-go gomod github.com/golang/go/src go1.18 `math/big`/"
6 "sortscip-go gomod github.com/golang/go/src go1.18 sort/"
7
8 "github.com/vmihailenco/msgpack/v4scip-go gomod github.com/vmihailenco/msgpack/v4 v4.3.12 `github.com/vmihailenco/msgpack/v4`/"
9 "github.com/zclconf/go-cty/ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/"
10 "github.com/zclconf/go-cty/cty/convertscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/"
11 )
12
13 // Marshal produces a msgpack serialization of the given value that
14 // can be decoded into the given type later using Unmarshal.
15 //
16 // The given value must conform to the given type, or an error will
17 // be returned.
18 func Marshalscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/msgpack`/Marshal().(val ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Valuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#, ty ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#) ([]byte, error) {
19 errs := val.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#Type().().TestConformancescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#TestConformance().(ty)
20 if errs != nil {
21 // Attempt a conversion
22 var err error
23 val, err = convertscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/.Convertscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/Convert().(val, ty)
24 if err != nil {
25 return nil, err
26 }
27 }
28
29 // From this point onward, val can be assumed to be conforming to t.
30
31 var path ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Pathscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Path#
32 var buf bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#
33 enc := msgpackscip-go gomod github.com/vmihailenco/msgpack/v4 v4.3.12 `github.com/vmihailenco/msgpack/v4`/.NewEncoderscip-go gomod github.com/vmihailenco/msgpack/v4 v4.3.12 `github.com/vmihailenco/msgpack/v4`/NewEncoder().(&buf)
34 enc.UseCompactEncodingscip-go gomod github.com/vmihailenco/msgpack/v4 v4.3.12 `github.com/vmihailenco/msgpack/v4`/Encoder#UseCompactEncoding().(true)
35
36 err := marshalscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/msgpack`/marshal().(val, ty, path, enc)
37 if err != nil {
38 return nil, err
39 }
40
41 return buf.Bytesscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#Bytes().(), nil
42 }
43
44 func marshalscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/msgpack`/marshal().(val ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Valuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#, ty ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#, path ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Pathscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Path#, enc *msgpackscip-go gomod github.com/vmihailenco/msgpack/v4 v4.3.12 `github.com/vmihailenco/msgpack/v4`/.Encoderscip-go gomod github.com/vmihailenco/msgpack/v4 v4.3.12 `github.com/vmihailenco/msgpack/v4`/Encoder#) error {
45 if val.IsMarkedscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#IsMarked().() {
46 return path.NewErrorfscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Path#NewErrorf().("value has marks, so it cannot be serialized")
47 }
48
49 // If we're going to decode as DynamicPseudoType then we need to save
50 // dynamic type information to recover the real type.
51 if ty == ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.DynamicPseudoTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/DynamicPseudoType. && val.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#Type().() != ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.DynamicPseudoTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/DynamicPseudoType. {
52 return marshalDynamicscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/msgpack`/marshalDynamic().(val, path, enc)
53 }
54
55 if !val.IsKnownscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#IsKnown().() {
56 err := enc.Encodescip-go gomod github.com/vmihailenco/msgpack/v4 v4.3.12 `github.com/vmihailenco/msgpack/v4`/Encoder#Encode().(unknownValscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/msgpack`/unknownVal.)
57 if err != nil {
58 return path.NewErrorscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Path#NewError().(err)
59 }
60 return nil
61 }
62 if val.IsNullscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#IsNull().() {
63 err := enc.EncodeNilscip-go gomod github.com/vmihailenco/msgpack/v4 v4.3.12 `github.com/vmihailenco/msgpack/v4`/Encoder#EncodeNil().()
64 if err != nil {
65 return path.NewErrorscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Path#NewError().(err)
66 }
67 return nil
68 }
69
70 // The caller should've guaranteed that the given val is conformant with
71 // the given type ty, so we'll proceed under that assumption here.
72 switch {
73 case ty.IsPrimitiveTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsPrimitiveType().():
74 switch ty {
75 case ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Stringscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/String.:
76 err := enc.EncodeStringscip-go gomod github.com/vmihailenco/msgpack/v4 v4.3.12 `github.com/vmihailenco/msgpack/v4`/Encoder#EncodeString().(val.AsStringscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#AsString().())
77 if err != nil {
78 return path.NewErrorscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Path#NewError().(err)
79 }
80 return nil
81 case ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Numberscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Number.:
82 var err error
83 switch {
84 case val.RawEqualsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#RawEquals().(ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.PositiveInfinityscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/PositiveInfinity.):
85 err = enc.EncodeFloat64scip-go gomod github.com/vmihailenco/msgpack/v4 v4.3.12 `github.com/vmihailenco/msgpack/v4`/Encoder#EncodeFloat64().(positiveInfinityscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/msgpack`/positiveInfinity.)
86 case val.RawEqualsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#RawEquals().(ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.NegativeInfinityscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/NegativeInfinity.):
87 err = enc.EncodeFloat64scip-go gomod github.com/vmihailenco/msgpack/v4 v4.3.12 `github.com/vmihailenco/msgpack/v4`/Encoder#EncodeFloat64().(negativeInfinityscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/msgpack`/negativeInfinity.)
88 default:
89 bf := val.AsBigFloatscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#AsBigFloat().()
90 if iv, acc := bf.Int64scip-go gomod github.com/golang/go/src go1.18 `math/big`/Float#Int64().(); acc == bigscip-go gomod github.com/golang/go/src go1.18 `math/big`/.Exactscip-go gomod github.com/golang/go/src go1.18 `math/big`/Exact. {
91 err = enc.EncodeIntscip-go gomod github.com/vmihailenco/msgpack/v4 v4.3.12 `github.com/vmihailenco/msgpack/v4`/Encoder#EncodeInt().(iv)
92 } else if fv, acc := bf.Float64scip-go gomod github.com/golang/go/src go1.18 `math/big`/Float#Float64().(); acc == bigscip-go gomod github.com/golang/go/src go1.18 `math/big`/.Exactscip-go gomod github.com/golang/go/src go1.18 `math/big`/Exact. {
93 err = enc.EncodeFloat64scip-go gomod github.com/vmihailenco/msgpack/v4 v4.3.12 `github.com/vmihailenco/msgpack/v4`/Encoder#EncodeFloat64().(fv)
94 } else {
95 err = enc.EncodeStringscip-go gomod github.com/vmihailenco/msgpack/v4 v4.3.12 `github.com/vmihailenco/msgpack/v4`/Encoder#EncodeString().(bf.Textscip-go gomod github.com/golang/go/src go1.18 `math/big`/Float#Text().('f', -1))
96 }
97 }
98 if err != nil {
99 return path.NewErrorscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Path#NewError().(err)
100 }
101 return nil
102 case ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Boolscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Bool.:
103 err := enc.EncodeBoolscip-go gomod github.com/vmihailenco/msgpack/v4 v4.3.12 `github.com/vmihailenco/msgpack/v4`/Encoder#EncodeBool().(val.Truescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#True().())
104 if err != nil {
105 return path.NewErrorscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Path#NewError().(err)
106 }
107 return nil
108 default:
109 panic("unsupported primitive type")
110 }
111 case ty.IsListTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsListType().(), ty.IsSetTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsSetType().():
112 enc.EncodeArrayLenscip-go gomod github.com/vmihailenco/msgpack/v4 v4.3.12 `github.com/vmihailenco/msgpack/v4`/Encoder#EncodeArrayLen().(val.LengthIntscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#LengthInt().())
113 ety := ty.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().()
114 it := val.ElementIteratorscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#ElementIterator().()
115 path := append(path, nil) // local override of 'path' with extra element
116 for it.Nextscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/ElementIterator#Next.() {
117 ek, ev := it.Elementscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/ElementIterator#Element.()
118 path[len(path)-1] = ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.IndexStepscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/IndexStep#{
119 Keyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/IndexStep#Key.: ek,
120 }
121 err := marshalscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/msgpack`/marshal().(ev, ety, path, enc)
122 if err != nil {
123 return err
124 }
125 }
126 return nil
127 case ty.IsMapTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsMapType().():
128 enc.EncodeMapLenscip-go gomod github.com/vmihailenco/msgpack/v4 v4.3.12 `github.com/vmihailenco/msgpack/v4`/Encoder#EncodeMapLen().(val.LengthIntscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#LengthInt().())
129 ety := ty.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().()
130 it := val.ElementIteratorscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#ElementIterator().()
131 path := append(path, nil) // local override of 'path' with extra element
132 for it.Nextscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/ElementIterator#Next.() {
133 ek, ev := it.Elementscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/ElementIterator#Element.()
134 path[len(path)-1] = ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.IndexStepscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/IndexStep#{
135 Keyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/IndexStep#Key.: ek,
136 }
137 var err error
138 err = marshalscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/msgpack`/marshal().(ek, ek.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#Type().(), path, enc)
139 if err != nil {
140 return err
141 }
142 err = marshalscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/msgpack`/marshal().(ev, ety, path, enc)
143 if err != nil {
144 return err
145 }
146 }
147 return nil
148 case ty.IsTupleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsTupleType().():
149 etys := ty.TupleElementTypesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#TupleElementTypes().()
150 it := val.ElementIteratorscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#ElementIterator().()
151 path := append(path, nil) // local override of 'path' with extra element
152 i := 0
153 enc.EncodeArrayLenscip-go gomod github.com/vmihailenco/msgpack/v4 v4.3.12 `github.com/vmihailenco/msgpack/v4`/Encoder#EncodeArrayLen().(len(etys))
154 for it.Nextscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/ElementIterator#Next.() {
155 ety := etys[i]
156 ek, ev := it.Elementscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/ElementIterator#Element.()
157 path[len(path)-1] = ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.IndexStepscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/IndexStep#{
158 Keyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/IndexStep#Key.: ek,
159 }
160 err := marshalscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/msgpack`/marshal().(ev, ety, path, enc)
161 if err != nil {
162 return err
163 }
164 i++
165 }
166 return nil
167 case ty.IsObjectTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsObjectType().():
168 atys := ty.AttributeTypesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#AttributeTypes().()
169 path := append(path, nil) // local override of 'path' with extra element
170
171 names := make([]string, 0, len(atys))
172 for k := range atys {
173 names = append(names, k)
174 }
175 sortscip-go gomod github.com/golang/go/src go1.18 sort/.Stringsscip-go gomod github.com/golang/go/src go1.18 sort/Strings().(names)
176
177 enc.EncodeMapLenscip-go gomod github.com/vmihailenco/msgpack/v4 v4.3.12 `github.com/vmihailenco/msgpack/v4`/Encoder#EncodeMapLen().(len(names))
178
179 for _, k := range names {
180 aty := atys[k]
181 av := val.GetAttrscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#GetAttr().(k)
182 path[len(path)-1] = ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.GetAttrStepscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/GetAttrStep#{
183 Namescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/GetAttrStep#Name.: k,
184 }
185 var err error
186 err = marshalscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/msgpack`/marshal().(ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.StringValscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/StringVal().(k), ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Stringscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/String., path, enc)
187 if err != nil {
188 return err
189 }
190 err = marshalscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/msgpack`/marshal().(av, aty, path, enc)
191 if err != nil {
192 return err
193 }
194 }
195 return nil
196 case ty.IsCapsuleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsCapsuleType().():
197 return path.NewErrorfscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Path#NewErrorf().("capsule types not supported for msgpack encoding")
198 default:
199 // should never happen
200 return path.NewErrorfscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Path#NewErrorf().("cannot msgpack-serialize %s", ty.FriendlyNamescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#FriendlyName().())
201 }
202 }
203
204 // marshalDynamic adds an extra wrapping object containing dynamic type
205 // information for the given value.
206 func marshalDynamicscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/msgpack`/marshalDynamic().(val ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Valuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#, path ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Pathscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Path#, enc *msgpackscip-go gomod github.com/vmihailenco/msgpack/v4 v4.3.12 `github.com/vmihailenco/msgpack/v4`/.Encoderscip-go gomod github.com/vmihailenco/msgpack/v4 v4.3.12 `github.com/vmihailenco/msgpack/v4`/Encoder#) error {
207 dv := dynamicValscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/msgpack`/dynamicVal#{
208 Valuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/msgpack`/dynamicVal#Value.: val,
209 Pathscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/msgpack`/dynamicVal#Path.: path,
210 }
211 return enc.Encodescip-go gomod github.com/vmihailenco/msgpack/v4 v4.3.12 `github.com/vmihailenco/msgpack/v4`/Encoder#Encode().(&dv)
212 }