|
| 1 | +package tftypes_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-go/tfprotov5/tftypes" |
| 7 | +) |
| 8 | + |
| 9 | +func ExampleValue_As_string() { |
| 10 | + // Values come over the wire, usually from a DynamicValue for this |
| 11 | + // example, we're just building one inline |
| 12 | + val := tftypes.NewValue(tftypes.String, "hello, world") |
| 13 | + |
| 14 | + var salutation string |
| 15 | + |
| 16 | + // we need to use a pointer so we can modify the value, just like |
| 17 | + // json.Unmarshal |
| 18 | + err := val.As(&salutation) |
| 19 | + if err != nil { |
| 20 | + panic(err) |
| 21 | + } |
| 22 | + |
| 23 | + fmt.Println(salutation) |
| 24 | + // Output: |
| 25 | + // hello, world |
| 26 | +} |
| 27 | + |
| 28 | +func ExampleValue_As_stringNull() { |
| 29 | + type exampleResource struct { |
| 30 | + salutation string |
| 31 | + nullableSalutation *string |
| 32 | + } |
| 33 | + |
| 34 | + // let's see what happens when we have a null value |
| 35 | + val := tftypes.NewValue(tftypes.String, nil) |
| 36 | + |
| 37 | + var res exampleResource |
| 38 | + |
| 39 | + // we can use a pointer to a variable, but the variable can't hold nil, |
| 40 | + // so we'll get the empty value. You can use this if you don't care |
| 41 | + // about null, and consider it equivalent to the empty value. |
| 42 | + err := val.As(&res.salutation) |
| 43 | + if err != nil { |
| 44 | + panic(err) |
| 45 | + } |
| 46 | + |
| 47 | + // we can use a pointer to a pointer to a variable, which can hold nil, |
| 48 | + // so we'll be able to distinguish between a null and an empty string |
| 49 | + err = val.As(&res.nullableSalutation) |
| 50 | + if err != nil { |
| 51 | + panic(err) |
| 52 | + } |
| 53 | + |
| 54 | + fmt.Println(res.salutation) |
| 55 | + fmt.Println(res.nullableSalutation) |
| 56 | + // Output: |
| 57 | + // |
| 58 | + // <nil> |
| 59 | +} |
| 60 | + |
| 61 | +type exampleResource struct { |
| 62 | + name string |
| 63 | + suppliedName *bool // true for yes, false for no, nil for we haven't asked |
| 64 | +} |
| 65 | + |
| 66 | +// fill the tftypes.Unmarshaler interface to control how As works |
| 67 | +// we want a pointer to exampleResource so we can change the properties |
| 68 | +func (e *exampleResource) UnmarshalTerraform5Type(val tftypes.Value) error { |
| 69 | + // this is an object type, so we're always going to get a |
| 70 | + // `tftypes.Value` that coerces to a map[string]tftypes.Value |
| 71 | + // as input |
| 72 | + v := map[string]tftypes.Value{} |
| 73 | + err := val.As(&v) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + // now that we can get to the tftypes.Value for each field, |
| 79 | + // call its As method and assign the result to the appropriate |
| 80 | + // variable. |
| 81 | + |
| 82 | + err = v["name"].As(&e.name) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + err = v["supplied_name"].As(&e.suppliedName) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +func ExampleValue_As_interface() { |
| 96 | + // our tftypes.Value would usually come over the wire as a |
| 97 | + // DynamicValue, but for simplicity, let's just declare one inline here |
| 98 | + val := tftypes.NewValue(tftypes.Object{ |
| 99 | + AttributeTypes: map[string]tftypes.Type{ |
| 100 | + "name": tftypes.String, |
| 101 | + "supplied_name": tftypes.Bool, |
| 102 | + }, |
| 103 | + }, map[string]tftypes.Value{ |
| 104 | + "name": tftypes.NewValue(tftypes.String, "ozymandias"), |
| 105 | + "supplied_name": tftypes.NewValue(tftypes.Bool, nil), |
| 106 | + }) |
| 107 | + |
| 108 | + // exampleResource has UnmarshalTerraform5Type method defined on it, |
| 109 | + // see value_example_test.go for implementation details |
| 110 | + // we'd put the function and type inline here, but apparently Go can't |
| 111 | + // have methods defined on types defined inside a function |
| 112 | + var res exampleResource |
| 113 | + |
| 114 | + // call As as usual |
| 115 | + err := val.As(&res) |
| 116 | + if err != nil { |
| 117 | + panic(err) |
| 118 | + } |
| 119 | + fmt.Println(res.name) |
| 120 | + fmt.Println(res.suppliedName) |
| 121 | + // Output: |
| 122 | + // ozymandias |
| 123 | + // <nil> |
| 124 | +} |
0 commit comments