Skip to content

Commit e860d96

Browse files
author
Paddy Carver
committed
Fix first few issues of kicking the tire.
Implemented returning a schema and refreshing a data source, and these are the fixes that need to be made. They don't matter, we're going to have to do some revamping of the type system, anyways, this is just to preserve a known-"working" configuration.
1 parent e3f0838 commit e860d96

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

tfprotov5/internal/fromproto/types.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ func msgpackToRawValue(in []byte) (tftypes.RawValue, error) {
4747
Value: tftypes.UnknownValue,
4848
}, nil
4949
}
50-
// if the caller wants this to be dynamic, we unmarshalDynamic
5150

5251
if peek == msgpackCodes.Nil {
5352
err = dec.Skip() // skip what we've peeked
@@ -106,7 +105,7 @@ func typeFromValue(in interface{}) (tftypes.Type, error) {
106105
return tftypes.UnknownType, nil
107106
}
108107
switch v := in.(type) {
109-
case string:
108+
case string, []uint8:
110109
return tftypes.String, nil
111110
case json.Number, int64, int32, int16, int8, int, uint64, uint32, uint16, uint8, uint:
112111
return tftypes.Number, nil

tfprotov5/internal/toproto/cty.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,15 @@ func marshal(val interface{}, typ tftypes.Type, path []string, enc *msgpack.Enco
8585
case typ.Is(tftypes.String):
8686
s, ok := val.(string)
8787
if !ok {
88-
return unexpectedValueTypeError(path, s, val, typ)
88+
u, ok := val.([]uint8)
89+
if !ok {
90+
return unexpectedValueTypeError(path, s, val, typ)
91+
}
92+
b := make([]byte, 0, len(u))
93+
for _, i := range u {
94+
b = append(b, i)
95+
}
96+
s = string(b)
8997
}
9098
err := enc.EncodeString(s)
9199
if err != nil {
@@ -98,7 +106,15 @@ func marshal(val interface{}, typ tftypes.Type, path []string, enc *msgpack.Enco
98106
// for Go's int/float type variations
99107
n, ok := val.(*big.Float)
100108
if !ok {
101-
return unexpectedValueTypeError(path, n, val, typ)
109+
u, ok := val.(uint16)
110+
if !ok {
111+
return unexpectedValueTypeError(path, n, val, typ)
112+
}
113+
err := enc.EncodeUint16(u)
114+
if err != nil {
115+
return pathError(path, "error encoding uint16 value: %w", err)
116+
}
117+
return nil
102118
}
103119
if iv, acc := n.Int64(); acc == big.Exact {
104120
err := enc.EncodeInt(iv)
@@ -224,7 +240,7 @@ func marshal(val interface{}, typ tftypes.Type, path []string, enc *msgpack.Enco
224240
for k, v := range o {
225241
ty := types[k]
226242
attrPath := newPath(path, k)
227-
err := marshal(k, ty, attrPath, enc)
243+
err := marshal(k, tftypes.String, attrPath, enc)
228244
if err != nil {
229245
return pathError(path, "error encoding object key: %w", err)
230246
}
@@ -251,7 +267,9 @@ func unexpectedValueTypeError(path []string, expected, got interface{}, typ tfty
251267

252268
func newPath(path []string, v interface{}) []string {
253269
n := make([]string, len(path)+1)
254-
copy(path, n)
270+
for i, v := range path {
271+
n[i] = v
272+
}
255273
n[len(n)-1] = fmt.Sprintf("%v", v)
256274
return n
257275
}

tfprotov5/tftypes/raw_value.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ type RawValue struct {
2626

2727
func (r RawValue) Unmarshal(dst interface{}) error {
2828
if unmarshaler, ok := dst.(Unmarshaler); ok {
29-
return unmarshaler.UnmarshalTerraform5Type(r)
29+
//typ, val := standardizeRawValue(r)
30+
var typ Type
31+
var val interface{}
32+
return unmarshaler.UnmarshalTerraform5Type(RawValue{
33+
Type: typ,
34+
Value: val,
35+
})
3036
}
3137
switch {
3238
case r.Type.Is(String):
@@ -117,7 +123,14 @@ func (r RawValue) Unmarshal(dst interface{}) error {
117123
// So this _has_ to be a List, no ambiguity exists here.
118124
switch dst.(type) {
119125
case *[]interface{}:
120-
*(dst.(*[]interface{})) = r.Value.([]interface{})
126+
var res []interface{}
127+
for _, i := range r.Value.([]interface{}) {
128+
res = append(res, RawValue{
129+
Type: r.Type.(List).ElementType,
130+
Value: i,
131+
})
132+
}
133+
*(dst.(*[]interface{})) = res
121134
default:
122135
return fmt.Errorf("can't unmarshal %s into %T", r.Type, dst)
123136
}
@@ -127,7 +140,14 @@ func (r RawValue) Unmarshal(dst interface{}) error {
127140
// So this _has_ to be a Set, no ambiguity exists here.
128141
switch dst.(type) {
129142
case *[]interface{}:
130-
*(dst.(*[]interface{})) = r.Value.([]interface{})
143+
var res []interface{}
144+
for _, i := range r.Value.([]interface{}) {
145+
res = append(res, RawValue{
146+
Type: r.Type.(Set).ElementType,
147+
Value: i,
148+
})
149+
}
150+
*(dst.(*[]interface{})) = res
131151
default:
132152
return fmt.Errorf("can't unmarshal %s into %T", r.Type, dst)
133153
}

0 commit comments

Comments
 (0)