Skip to content

Commit 4218773

Browse files
committed
adding some todos and copy impl
1 parent b81768d commit 4218773

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

tftypes/refinement/refinement.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
package refinement
22

3-
import "github.com/vmihailenco/msgpack/v5"
3+
import (
4+
"fmt"
5+
6+
"github.com/vmihailenco/msgpack/v5"
7+
)
48

59
type Key int64
610

711
func (k Key) String() string {
8-
return "todo"
12+
// TODO: Not sure when this is used, double check the names
13+
switch k {
14+
case KeyNullness:
15+
return "nullness"
16+
case KeyStringPrefix:
17+
return "string_prefix"
18+
default:
19+
return fmt.Sprintf("unsupported refinement: %d", k)
20+
}
921
}
1022

1123
const (
@@ -21,7 +33,7 @@ type Refinement interface {
2133
Equal(Refinement) bool
2234
Encode(*msgpack.Encoder) error
2335
String() string
24-
unimplementable() // prevent external implementations
36+
unimplementable() // prevents external implementations, all refinements are defined in the Terraform/HCL type system go-cty.
2537
}
2638

2739
type Refinements map[Key]Refinement
@@ -30,5 +42,6 @@ func (r Refinements) Equal(o Refinements) bool {
3042
return false
3143
}
3244
func (r Refinements) String() string {
45+
// TODO: Not sure when this is used, should just aggregate and call all underlying refinements.String() method
3346
return "todo"
3447
}

tftypes/refinement/string_prefix.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ type stringPrefix struct {
77
}
88

99
// TODO: What if the prefix is empty? Should we skip encoding? Throw an error earlier? Throw an error here?
10+
// - I think an empty prefix string is valid, empty string is still more information then wholly unknown?
11+
// - I wonder what Terraform does in this situation today
1012
func (s stringPrefix) Encode(enc *msgpack.Encoder) error {
1113
// Matching go-cty for the max prefix length allowed here
1214
//

tftypes/value.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ type ValueCreator interface {
4242
// The recommended usage of a Value is to check that it is known, using
4343
// Value.IsKnown, then to convert it to a Go type, using Value.As. The Go type
4444
// can then be manipulated.
45+
//
46+
// TODO: add docs for new refinement data
4547
type Value struct {
4648
typ Type
4749
value interface{}
4850

49-
refinements refinement.Refinements //nolint
51+
refinements refinement.Refinements
5052
}
5153

5254
func (val Value) String() string {
@@ -62,6 +64,7 @@ func (val Value) String() string {
6264
}
6365

6466
// TODO: print refinements
67+
6568
if !val.IsKnown() {
6669
return typ.String() + "<unknown>"
6770
}
@@ -226,7 +229,9 @@ func (val Value) Equal(o Value) bool {
226229
if !val.Type().Equal(o.Type()) {
227230
return false
228231
}
232+
229233
// TODO: compare refinements
234+
230235
deepEqual, err := val.deepEqual(o)
231236
if err != nil {
232237
return false
@@ -237,6 +242,9 @@ func (val Value) Equal(o Value) bool {
237242
// Copy returns a defensively-copied clone of Value that shares no underlying
238243
// data structures with the original Value and can be mutated without
239244
// accidentally mutating the original.
245+
//
246+
// TODO: Make sure this actually works for refinements. Consuming packages should not be able to mutate refinements of val
247+
// TODO: Add docs referencing refinements
240248
func (val Value) Copy() Value {
241249
newVal := val.value
242250
switch v := val.value.(type) {
@@ -253,7 +261,11 @@ func (val Value) Copy() Value {
253261
}
254262
newVal = newVals
255263
}
256-
return NewValue(val.Type(), newVal)
264+
265+
newTfValue := NewValue(val.Type(), newVal)
266+
newTfValue.refinements = val.refinements
267+
268+
return newTfValue
257269
}
258270

259271
// NewValue returns a Value constructed using the specified Type and stores the
@@ -599,7 +611,7 @@ func unexpectedValueTypeError(p *AttributePath, expected, got interface{}, typ T
599611
return p.NewErrorf("unexpected value type %T, %s values must be of type %T", got, typ, expected)
600612
}
601613

602-
// TODO: return error?
614+
// TODO: do we need to return an error? Like if you attempt to refine a value improperly (like string prefix on a number)?
603615
func (val Value) Refine(refinements refinement.Refinements) Value {
604616
newVal := val.Copy()
605617

@@ -611,6 +623,8 @@ func (val Value) Refine(refinements refinement.Refinements) Value {
611623
}
612624

613625
func (val Value) Refinements() refinement.Refinements {
626+
// TODO: is this copy really needed?
614627
valCopy := val.Copy()
628+
615629
return valCopy.refinements
616630
}

tftypes/value_msgpack.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@ func msgpackUnmarshalUnknown(dec *msgpack.Decoder, typ Type, path *AttributePath
434434
return Value{}, path.NewErrorf("failed to decode msgpack extension body: string prefix refinement is not valid UTF-8")
435435
}
436436

437+
// TODO: If terraform doesn't support an empty string prefix, then neither should we, potentially return an error here.
438+
437439
newRefinements[keyCode] = refinement.StringPrefix(prefix)
438440
default:
439441
// We don't want to error here, as go-cty could introduce new refinements that we'd
@@ -501,7 +503,8 @@ func marshalUnknownValue(val Value, typ Type, p *AttributePath, enc *msgpack.Enc
501503
refnEnc := msgpack.NewEncoder(&refnBuf)
502504
mapLen := 0
503505

504-
// TODO: Should the refinement interface be defining the encoding? Should we export the refinement implementations?
506+
// TODO: Should the refinement interface be defining the encoding? Should we export the refinement implementation details?
507+
// - If we keep it in the interface, then we can simplify this logic
505508
for kind, refn := range val.refinements {
506509
switch kind {
507510
case refinement.KeyNullness:

0 commit comments

Comments
 (0)