@@ -16,6 +16,8 @@ package valueshim
1616
1717import (
1818 "encoding/json"
19+ "errors"
20+ "fmt"
1921 "math/big"
2022
2123 "github.com/hashicorp/terraform-plugin-go/tftypes"
@@ -74,12 +76,20 @@ func (v tValueShim) AsValueMap() map[string]Value {
7476 return res
7577}
7678
77- func (v tValueShim ) Marshal () (json.RawMessage , error ) {
78- inmem , err := jsonMarshal (v .val (), tftypes .NewAttributePath ())
79+ func (v tValueShim ) Marshal (schemaType Type ) (json.RawMessage , error ) {
80+ tt , ok := schemaType .(tTypeShim )
81+ if ! ok {
82+ return nil , errors .New ("Cannot marshal to RawState: expected schemaType to be of type tTypeShim" )
83+ }
84+ ctyType , err := toCtyType (tt .ty ())
85+ if err != nil {
86+ return nil , fmt .Errorf ("Cannot marshal to RawState. Error converting to cty.Type: %w" , err )
87+ }
88+ cty , err := toCtyValue (tt .ty (), ctyType , v .val ())
7989 if err != nil {
80- return nil , err
90+ return nil , fmt . Errorf ( "Cannot marshal to RawState. Error converting to cty.Value: %w" , err )
8191 }
82- return json .Marshal (inmem )
92+ return FromHCtyValue ( cty ) .Marshal (FromHCtyType ( ctyType ) )
8393}
8494
8595func (v tValueShim ) Remove (prop string ) Value {
@@ -144,159 +154,11 @@ func (v tValueShim) StringValue() string {
144154 return result
145155}
146156
147- func jsonMarshal (v tftypes.Value , p * tftypes.AttributePath ) (interface {}, error ) {
148- if v .IsNull () {
149- return nil , nil
150- }
151- if ! v .IsKnown () {
152- return nil , p .NewErrorf ("unknown values cannot be serialized to JSON" )
153- }
154- typ := v .Type ()
155- switch {
156- case typ .Is (tftypes .String ):
157- return jsonMarshalString (v , p )
158- case typ .Is (tftypes .Number ):
159- return jsonMarshalNumber (v , p )
160- case typ .Is (tftypes .Bool ):
161- return jsonMarshalBool (v , p )
162- case typ .Is (tftypes.List {}):
163- return jsonMarshalList (v , p )
164- case typ .Is (tftypes.Set {}):
165- return jsonMarshalSet (v , p )
166- case typ .Is (tftypes.Map {}):
167- return jsonMarshalMap (v , p )
168- case typ .Is (tftypes.Tuple {}):
169- return jsonMarshalTuple (v , p )
170- case typ .Is (tftypes.Object {}):
171- return jsonMarshalObject (v , p )
172- }
173-
174- return nil , p .NewErrorf ("unknown type %s" , typ )
175- }
176-
177- func jsonMarshalString (v tftypes.Value , p * tftypes.AttributePath ) (interface {}, error ) {
178- var stringValue string
179- err := v .As (& stringValue )
180- if err != nil {
181- return nil , p .NewError (err )
182- }
183- return stringValue , nil
184- }
185-
186- func jsonMarshalNumber (v tftypes.Value , p * tftypes.AttributePath ) (interface {}, error ) {
187- var n big.Float
188- err := v .As (& n )
189- if err != nil {
190- return nil , p .NewError (err )
191- }
192- return json .Number (n .Text ('f' , - 1 )), nil
193- }
194-
195- func jsonMarshalBool (v tftypes.Value , p * tftypes.AttributePath ) (interface {}, error ) {
196- var b bool
197- err := v .As (& b )
198- if err != nil {
199- return nil , p .NewError (err )
200- }
201- return b , nil
202- }
203-
204- func jsonMarshalList (v tftypes.Value , p * tftypes.AttributePath ) (interface {}, error ) {
205- var vs []tftypes.Value
206- err := v .As (& vs )
207- if err != nil {
208- return nil , p .NewError (err )
209- }
210- res := make ([]any , len (vs ))
211- for i , v := range vs {
212- ep := p .WithElementKeyInt (i )
213- e , err := jsonMarshal (v , ep )
214- if err != nil {
215- return nil , ep .NewError (err )
216- }
217- res [i ] = e
218- }
219- return res , nil
220- }
221-
222- // Important to preserve original order of tftypes.Value here.
223- func jsonMarshalSet (v tftypes.Value , p * tftypes.AttributePath ) (interface {}, error ) {
224- var vs []tftypes.Value
225- err := v .As (& vs )
226- if err != nil {
227- return nil , p .NewError (err )
228- }
229- res := make ([]any , len (vs ))
230- for i , v := range vs {
231- ep := p .WithElementKeyValue (v )
232- e , err := jsonMarshal (v , ep )
233- if err != nil {
234- return nil , ep .NewError (err )
235- }
236- res [i ] = e
237- }
238- return res , nil
239- }
240-
241- func jsonMarshalMap (v tftypes.Value , p * tftypes.AttributePath ) (interface {}, error ) {
242- var vs map [string ]tftypes.Value
243- err := v .As (& vs )
244- if err != nil {
245- return nil , p .NewError (err )
246- }
247- res := make (map [string ]any , len (vs ))
248- for k , v := range vs {
249- ep := p .WithElementKeyValue (v )
250- e , err := jsonMarshal (v , ep )
251- if err != nil {
252- return nil , ep .NewError (err )
253- }
254- res [k ] = e
255- }
256- return res , nil
257- }
258-
259- func jsonMarshalTuple (v tftypes.Value , p * tftypes.AttributePath ) (interface {}, error ) {
260- var vs []tftypes.Value
261- err := v .As (& vs )
262- if err != nil {
263- return nil , p .NewError (err )
264- }
265- res := make ([]any , len (vs ))
266- for i , v := range vs {
267- ep := p .WithElementKeyInt (i )
268- e , err := jsonMarshal (v , ep )
269- if err != nil {
270- return nil , ep .NewError (err )
271- }
272- res [i ] = e
273- }
274- return res , nil
275- }
276-
277- func jsonMarshalObject (v tftypes.Value , p * tftypes.AttributePath ) (interface {}, error ) {
278- var vs map [string ]tftypes.Value
279- err := v .As (& vs )
280- if err != nil {
281- return nil , p .NewError (err )
282- }
283- res := make (map [string ]any , len (vs ))
284- for k , v := range vs {
285- ep := p .WithAttributeName (k )
286- e , err := jsonMarshal (v , ep )
287- if err != nil {
288- return nil , ep .NewError (err )
289- }
290- res [k ] = e
291- }
292- return res , nil
293- }
294-
295157type tTypeShim struct {
296158 t tftypes.Type
297159}
298160
299- var _ Type = ( * tTypeShim )( nil )
161+ var _ Type = tTypeShim {}
300162
301163func (t tTypeShim ) ty () tftypes.Type {
302164 return t .t
0 commit comments