|
| 1 | +// Copyright 2016-2025, Pulumi Corporation. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package valueshim |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "fmt" |
| 20 | + "math/big" |
| 21 | + |
| 22 | + "github.com/hashicorp/go-cty/cty" |
| 23 | + ctyjson "github.com/hashicorp/go-cty/cty/json" |
| 24 | +) |
| 25 | + |
| 26 | +// Wrap a cty.Value as Value. |
| 27 | +func FromHCtyValue(v cty.Value) Value { |
| 28 | + return hctyValueShim(v) |
| 29 | +} |
| 30 | + |
| 31 | +// Wrap a cty.Type as Type. |
| 32 | +func FromHCtyType(v cty.Type) Type { |
| 33 | + return hctyTypeShim(v) |
| 34 | +} |
| 35 | + |
| 36 | +type hctyValueShim cty.Value |
| 37 | + |
| 38 | +var _ Value = (*hctyValueShim)(nil) |
| 39 | + |
| 40 | +func (v hctyValueShim) val() cty.Value { |
| 41 | + return cty.Value(v) |
| 42 | +} |
| 43 | + |
| 44 | +func (v hctyValueShim) IsNull() bool { |
| 45 | + return v.val().IsNull() |
| 46 | +} |
| 47 | + |
| 48 | +func (v hctyValueShim) GoString() string { |
| 49 | + return v.val().GoString() |
| 50 | +} |
| 51 | + |
| 52 | +func (v hctyValueShim) Type() Type { |
| 53 | + return FromHCtyType(v.val().Type()) |
| 54 | +} |
| 55 | + |
| 56 | +func (v hctyValueShim) StringValue() string { |
| 57 | + return v.val().AsString() |
| 58 | +} |
| 59 | + |
| 60 | +func (v hctyValueShim) BoolValue() bool { |
| 61 | + return v.val().True() |
| 62 | +} |
| 63 | + |
| 64 | +func (v hctyValueShim) NumberValue() float64 { |
| 65 | + f, _ := v.val().AsBigFloat().Float64() |
| 66 | + return f |
| 67 | +} |
| 68 | + |
| 69 | +func (v hctyValueShim) BigFloatValue() *big.Float { |
| 70 | + return v.val().AsBigFloat() |
| 71 | +} |
| 72 | + |
| 73 | +func (v hctyValueShim) AsValueSlice() []Value { |
| 74 | + s := v.val().AsValueSlice() |
| 75 | + res := make([]Value, len(s)) |
| 76 | + for i, v := range s { |
| 77 | + res[i] = hctyValueShim(v) |
| 78 | + } |
| 79 | + return res |
| 80 | +} |
| 81 | + |
| 82 | +func (v hctyValueShim) AsValueMap() map[string]Value { |
| 83 | + m := v.val().AsValueMap() |
| 84 | + res := make(map[string]Value, len(m)) |
| 85 | + |
| 86 | + for k, v := range m { |
| 87 | + res[k] = hctyValueShim(v) |
| 88 | + } |
| 89 | + return res |
| 90 | +} |
| 91 | + |
| 92 | +func (v hctyValueShim) Remove(key string) Value { |
| 93 | + switch { |
| 94 | + case v.val().Type().IsObjectType(): |
| 95 | + m := v.val().AsValueMap() |
| 96 | + delete(m, key) |
| 97 | + if len(m) == 0 { |
| 98 | + return hctyValueShim(cty.EmptyObjectVal) |
| 99 | + } |
| 100 | + return hctyValueShim(cty.ObjectVal(m)) |
| 101 | + default: |
| 102 | + return v |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func (v hctyValueShim) Marshal(schemaType Type) (json.RawMessage, error) { |
| 107 | + vv := v.val() |
| 108 | + tt, ok := schemaType.(hctyTypeShim) |
| 109 | + if !ok { |
| 110 | + return nil, fmt.Errorf("Cannot marshal to RawState: "+ |
| 111 | + "expected schemaType to be of type hctyTypeShim, got %#T", |
| 112 | + schemaType) |
| 113 | + } |
| 114 | + raw, err := ctyjson.Marshal(vv, tt.ty()) |
| 115 | + if err != nil { |
| 116 | + return nil, fmt.Errorf("Cannot marshal to RawState: %w", err) |
| 117 | + } |
| 118 | + return json.RawMessage(raw), nil |
| 119 | +} |
| 120 | + |
| 121 | +type hctyTypeShim cty.Type |
| 122 | + |
| 123 | +var _ Type = hctyTypeShim{} |
| 124 | + |
| 125 | +func (t hctyTypeShim) ty() cty.Type { |
| 126 | + return cty.Type(t) |
| 127 | +} |
| 128 | + |
| 129 | +func (t hctyTypeShim) IsNumberType() bool { |
| 130 | + return t.ty().Equals(cty.Number) |
| 131 | +} |
| 132 | + |
| 133 | +func (t hctyTypeShim) IsBooleanType() bool { |
| 134 | + return t.ty().Equals(cty.Bool) |
| 135 | +} |
| 136 | + |
| 137 | +func (t hctyTypeShim) IsStringType() bool { |
| 138 | + return t.ty().Equals(cty.String) |
| 139 | +} |
| 140 | + |
| 141 | +func (t hctyTypeShim) IsListType() bool { |
| 142 | + return t.ty().IsListType() |
| 143 | +} |
| 144 | + |
| 145 | +func (t hctyTypeShim) IsMapType() bool { |
| 146 | + return t.ty().IsMapType() |
| 147 | +} |
| 148 | + |
| 149 | +func (t hctyTypeShim) IsSetType() bool { |
| 150 | + return t.ty().IsSetType() |
| 151 | +} |
| 152 | + |
| 153 | +func (t hctyTypeShim) IsObjectType() bool { |
| 154 | + return t.ty().IsObjectType() |
| 155 | +} |
| 156 | + |
| 157 | +func (t hctyTypeShim) IsDynamicType() bool { |
| 158 | + return t.ty().Equals(cty.DynamicPseudoType) |
| 159 | +} |
| 160 | + |
| 161 | +func (t hctyTypeShim) AttributeType(name string) (Type, bool) { |
| 162 | + tt := t.ty() |
| 163 | + if !tt.IsObjectType() { |
| 164 | + return nil, false |
| 165 | + } |
| 166 | + if !tt.HasAttribute(name) { |
| 167 | + return nil, false |
| 168 | + } |
| 169 | + return FromHCtyType(tt.AttributeType(name)), true |
| 170 | +} |
| 171 | + |
| 172 | +func (t hctyTypeShim) ElementType() (Type, bool) { |
| 173 | + tt := t.ty() |
| 174 | + if !tt.IsCollectionType() { |
| 175 | + return nil, false |
| 176 | + } |
| 177 | + return FromHCtyType(tt.ElementType()), true |
| 178 | +} |
| 179 | + |
| 180 | +func (t hctyTypeShim) GoString() string { |
| 181 | + return t.ty().GoString() |
| 182 | +} |
0 commit comments