Skip to content

Commit 50d6045

Browse files
Revert "remove redundant cty shim"
This reverts commit a580ec9.
1 parent a580ec9 commit 50d6045

File tree

2 files changed

+192
-9
lines changed

2 files changed

+192
-9
lines changed

pkg/valueshim/cty.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ import (
1919
"fmt"
2020
"math/big"
2121

22-
"github.com/hashicorp/go-cty/cty"
23-
ctyjson "github.com/hashicorp/go-cty/cty/json"
22+
"github.com/zclconf/go-cty/cty"
23+
ctyjson "github.com/zclconf/go-cty/cty/json"
2424
)
2525

2626
// Wrap a cty.Value as Value.
27-
func FromHCtyValue(v cty.Value) Value {
27+
func FromCtyValue(v cty.Value) Value {
2828
return ctyValueShim(v)
2929
}
3030

3131
// Wrap a cty.Type as Type.
32-
func FromHCtyType(v cty.Type) Type {
32+
func FromCtyType(v cty.Type) Type {
3333
return ctyTypeShim(v)
3434
}
3535

@@ -50,7 +50,7 @@ func (v ctyValueShim) GoString() string {
5050
}
5151

5252
func (v ctyValueShim) Type() Type {
53-
return FromHCtyType(v.val().Type())
53+
return FromCtyType(v.val().Type())
5454
}
5555

5656
func (v ctyValueShim) StringValue() string {
@@ -62,7 +62,8 @@ func (v ctyValueShim) BoolValue() bool {
6262
}
6363

6464
func (v ctyValueShim) NumberValue() float64 {
65-
f, _ := v.val().AsBigFloat().Float64()
65+
bf := v.BigFloatValue()
66+
f, _ := bf.Float64()
6667
return f
6768
}
6869

@@ -108,7 +109,7 @@ func (v ctyValueShim) Marshal(schemaType Type) (json.RawMessage, error) {
108109
tt, ok := schemaType.(ctyTypeShim)
109110
if !ok {
110111
return nil, fmt.Errorf("Cannot marshal to RawState: "+
111-
"expected schemaType to be of type hctyTypeShim, got %#T",
112+
"expected schemaType to be of type ctyTypeShim, got %#T",
112113
schemaType)
113114
}
114115
raw, err := ctyjson.Marshal(vv, tt.ty())
@@ -166,15 +167,15 @@ func (t ctyTypeShim) AttributeType(name string) (Type, bool) {
166167
if !tt.HasAttribute(name) {
167168
return nil, false
168169
}
169-
return FromHCtyType(tt.AttributeType(name)), true
170+
return FromCtyType(tt.AttributeType(name)), true
170171
}
171172

172173
func (t ctyTypeShim) ElementType() (Type, bool) {
173174
tt := t.ty()
174175
if !tt.IsCollectionType() {
175176
return nil, false
176177
}
177-
return FromHCtyType(tt.ElementType()), true
178+
return FromCtyType(tt.ElementType()), true
178179
}
179180

180181
func (t ctyTypeShim) GoString() string {

pkg/valueshim/hcty.go

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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

Comments
 (0)