Skip to content

Commit 7d11d8d

Browse files
committed
small adjustments to string prefix
1 parent 4218773 commit 7d11d8d

File tree

3 files changed

+39
-27
lines changed

3 files changed

+39
-27
lines changed

tftypes/refinement/nullness.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,38 @@ package refinement
22

33
import "github.com/vmihailenco/msgpack/v5"
44

5-
type nullness struct {
6-
Value bool
5+
type Nullness struct {
6+
value bool
77
}
88

9-
func (n nullness) Encode(enc *msgpack.Encoder) error {
9+
func (n Nullness) Encode(enc *msgpack.Encoder) error {
1010
err := enc.EncodeInt(int64(KeyNullness))
1111
if err != nil {
1212
return err
1313
}
1414

15-
// A value that is definitely null cannot be unknown
16-
return enc.EncodeBool(false)
15+
// It shouldn't be possible for an unknown value to be definitely null (i.e. nullness.value = true),
16+
// as that should be represented by a known null value instead. This encoding is in place to be compliant
17+
// with Terraform's encoding which uses a definitely null refinement to collapse into a known null value.
18+
return enc.EncodeBool(n.value)
1719
}
1820

19-
func (n nullness) Equal(Refinement) bool {
21+
func (n Nullness) Equal(Refinement) bool {
2022
return false
2123
}
2224

23-
func (n nullness) String() string {
24-
return "todo - nullness"
25+
func (n Nullness) String() string {
26+
return "todo - Nullness"
2527
}
2628

27-
func (n nullness) unimplementable() {}
29+
func (n Nullness) NotNull() bool {
30+
return !n.value
31+
}
32+
33+
func (n Nullness) unimplementable() {}
2834

29-
// TODO: Should this accept a value? If a value is unknown and the it's refined to be null
30-
// then the value should be a known value of null instead.
31-
func Nullness(value bool) Refinement {
32-
return nullness{
33-
Value: value,
35+
func NewNullness(value bool) Refinement {
36+
return Nullness{
37+
value: value,
3438
}
3539
}

tftypes/refinement/string_prefix.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ package refinement
22

33
import "github.com/vmihailenco/msgpack/v5"
44

5-
type stringPrefix struct {
6-
Value string
5+
type StringPrefix struct {
6+
value string
77
}
88

99
// TODO: What if the prefix is empty? Should we skip encoding? Throw an error earlier? Throw an error here?
1010
// - I think an empty prefix string is valid, empty string is still more information then wholly unknown?
1111
// - I wonder what Terraform does in this situation today
12-
func (s stringPrefix) Encode(enc *msgpack.Encoder) error {
12+
func (s StringPrefix) Encode(enc *msgpack.Encoder) error {
1313
// Matching go-cty for the max prefix length allowed here
1414
//
1515
// This ensures the total size of the refinements blob does not exceed the limit
1616
// set by the decoder (1024).
1717
maxPrefixLength := 256
18-
prefix := s.Value
19-
if len(s.Value) > maxPrefixLength {
18+
prefix := s.value
19+
if len(s.value) > maxPrefixLength {
2020
prefix = prefix[:maxPrefixLength-1]
2121
}
2222

@@ -28,18 +28,22 @@ func (s stringPrefix) Encode(enc *msgpack.Encoder) error {
2828
return enc.EncodeString(prefix)
2929
}
3030

31-
func (s stringPrefix) Equal(Refinement) bool {
31+
func (s StringPrefix) Equal(Refinement) bool {
3232
return false
3333
}
3434

35-
func (s stringPrefix) String() string {
35+
func (s StringPrefix) String() string {
3636
return "todo - stringPrefix"
3737
}
3838

39-
func (s stringPrefix) unimplementable() {}
39+
func (s StringPrefix) PrefixValue() string {
40+
return s.value
41+
}
42+
43+
func (s StringPrefix) unimplementable() {}
4044

41-
func StringPrefix(value string) Refinement {
42-
return stringPrefix{
43-
Value: value,
45+
func NewStringPrefix(value string) Refinement {
46+
return StringPrefix{
47+
value: value,
4448
}
4549
}

tftypes/value_msgpack.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,11 @@ func msgpackUnmarshalUnknown(dec *msgpack.Decoder, typ Type, path *AttributePath
421421
// The presence of this key means we're refining the null-ness one
422422
// way or another. If nullness is unknown then this key should not
423423
// be present at all.
424-
newRefinements[keyCode] = refinement.Nullness(isNull)
424+
//
425+
// isNull should always be false if this refinement is present, but to match Terraform's support
426+
// of this encoding, we will pass along the value. If isNull is true, then the value should not be
427+
// unknown with refinements, it should be a known null value.
428+
newRefinements[keyCode] = refinement.NewNullness(isNull)
425429
case refinement.KeyStringPrefix:
426430
if !typ.Is(String) {
427431
return Value{}, path.NewErrorf("failed to decode msgpack extension body: string prefix refinement for non-string type")
@@ -436,7 +440,7 @@ func msgpackUnmarshalUnknown(dec *msgpack.Decoder, typ Type, path *AttributePath
436440

437441
// TODO: If terraform doesn't support an empty string prefix, then neither should we, potentially return an error here.
438442

439-
newRefinements[keyCode] = refinement.StringPrefix(prefix)
443+
newRefinements[keyCode] = refinement.NewStringPrefix(prefix)
440444
default:
441445
// We don't want to error here, as go-cty could introduce new refinements that we'd
442446
// want to just ignore until this logic is updated

0 commit comments

Comments
 (0)