@@ -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 {
@@ -139,159 +149,11 @@ func (v tValueShim) StringValue() string {
139149 return result
140150}
141151
142- func jsonMarshal (v tftypes.Value , p * tftypes.AttributePath ) (interface {}, error ) {
143- if v .IsNull () {
144- return nil , nil
145- }
146- if ! v .IsKnown () {
147- return nil , p .NewErrorf ("unknown values cannot be serialized to JSON" )
148- }
149- typ := v .Type ()
150- switch {
151- case typ .Is (tftypes .String ):
152- return jsonMarshalString (v , p )
153- case typ .Is (tftypes .Number ):
154- return jsonMarshalNumber (v , p )
155- case typ .Is (tftypes .Bool ):
156- return jsonMarshalBool (v , p )
157- case typ .Is (tftypes.List {}):
158- return jsonMarshalList (v , p )
159- case typ .Is (tftypes.Set {}):
160- return jsonMarshalSet (v , p )
161- case typ .Is (tftypes.Map {}):
162- return jsonMarshalMap (v , p )
163- case typ .Is (tftypes.Tuple {}):
164- return jsonMarshalTuple (v , p )
165- case typ .Is (tftypes.Object {}):
166- return jsonMarshalObject (v , p )
167- }
168-
169- return nil , p .NewErrorf ("unknown type %s" , typ )
170- }
171-
172- func jsonMarshalString (v tftypes.Value , p * tftypes.AttributePath ) (interface {}, error ) {
173- var stringValue string
174- err := v .As (& stringValue )
175- if err != nil {
176- return nil , p .NewError (err )
177- }
178- return stringValue , nil
179- }
180-
181- func jsonMarshalNumber (v tftypes.Value , p * tftypes.AttributePath ) (interface {}, error ) {
182- var n big.Float
183- err := v .As (& n )
184- if err != nil {
185- return nil , p .NewError (err )
186- }
187- return json .Number (n .Text ('f' , - 1 )), nil
188- }
189-
190- func jsonMarshalBool (v tftypes.Value , p * tftypes.AttributePath ) (interface {}, error ) {
191- var b bool
192- err := v .As (& b )
193- if err != nil {
194- return nil , p .NewError (err )
195- }
196- return b , nil
197- }
198-
199- func jsonMarshalList (v tftypes.Value , p * tftypes.AttributePath ) (interface {}, error ) {
200- var vs []tftypes.Value
201- err := v .As (& vs )
202- if err != nil {
203- return nil , p .NewError (err )
204- }
205- res := make ([]any , len (vs ))
206- for i , v := range vs {
207- ep := p .WithElementKeyInt (i )
208- e , err := jsonMarshal (v , ep )
209- if err != nil {
210- return nil , ep .NewError (err )
211- }
212- res [i ] = e
213- }
214- return res , nil
215- }
216-
217- // Important to preserve original order of tftypes.Value here.
218- func jsonMarshalSet (v tftypes.Value , p * tftypes.AttributePath ) (interface {}, error ) {
219- var vs []tftypes.Value
220- err := v .As (& vs )
221- if err != nil {
222- return nil , p .NewError (err )
223- }
224- res := make ([]any , len (vs ))
225- for i , v := range vs {
226- ep := p .WithElementKeyValue (v )
227- e , err := jsonMarshal (v , ep )
228- if err != nil {
229- return nil , ep .NewError (err )
230- }
231- res [i ] = e
232- }
233- return res , nil
234- }
235-
236- func jsonMarshalMap (v tftypes.Value , p * tftypes.AttributePath ) (interface {}, error ) {
237- var vs map [string ]tftypes.Value
238- err := v .As (& vs )
239- if err != nil {
240- return nil , p .NewError (err )
241- }
242- res := make (map [string ]any , len (vs ))
243- for k , v := range vs {
244- ep := p .WithElementKeyValue (v )
245- e , err := jsonMarshal (v , ep )
246- if err != nil {
247- return nil , ep .NewError (err )
248- }
249- res [k ] = e
250- }
251- return res , nil
252- }
253-
254- func jsonMarshalTuple (v tftypes.Value , p * tftypes.AttributePath ) (interface {}, error ) {
255- var vs []tftypes.Value
256- err := v .As (& vs )
257- if err != nil {
258- return nil , p .NewError (err )
259- }
260- res := make ([]any , len (vs ))
261- for i , v := range vs {
262- ep := p .WithElementKeyInt (i )
263- e , err := jsonMarshal (v , ep )
264- if err != nil {
265- return nil , ep .NewError (err )
266- }
267- res [i ] = e
268- }
269- return res , nil
270- }
271-
272- func jsonMarshalObject (v tftypes.Value , p * tftypes.AttributePath ) (interface {}, error ) {
273- var vs map [string ]tftypes.Value
274- err := v .As (& vs )
275- if err != nil {
276- return nil , p .NewError (err )
277- }
278- res := make (map [string ]any , len (vs ))
279- for k , v := range vs {
280- ep := p .WithAttributeName (k )
281- e , err := jsonMarshal (v , ep )
282- if err != nil {
283- return nil , ep .NewError (err )
284- }
285- res [k ] = e
286- }
287- return res , nil
288- }
289-
290152type tTypeShim struct {
291153 t tftypes.Type
292154}
293155
294- var _ Type = ( * tTypeShim )( nil )
156+ var _ Type = tTypeShim {}
295157
296158func (t tTypeShim ) ty () tftypes.Type {
297159 return t .t
0 commit comments