Skip to content

Commit bd66111

Browse files
committed
package docs
1 parent 54bed95 commit bd66111

File tree

9 files changed

+70
-28
lines changed

9 files changed

+70
-28
lines changed

tftypes/refinement/collection_length_lower_bound.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ package refinement
55

66
import "fmt"
77

8-
// TODO: doc
8+
// CollectionLengthLowerBound represents an unknown value refinement which indicates the length of the final collection value will be
9+
// at least the specified int64 value. This refinement can only be applied to List, Map, and Set types.
910
type CollectionLengthLowerBound struct {
1011
value int64
1112
}
@@ -23,14 +24,15 @@ func (n CollectionLengthLowerBound) String() string {
2324
return fmt.Sprintf("length lower bound = %d", n.LowerBound())
2425
}
2526

26-
// TODO: doc
27+
// LowerBound returns the int64 value that the final value's collection length will be at least.
2728
func (n CollectionLengthLowerBound) LowerBound() int64 {
2829
return n.value
2930
}
3031

3132
func (n CollectionLengthLowerBound) unimplementable() {}
3233

33-
// TODO: doc
34+
// NewCollectionLengthLowerBound returns the CollectionLengthLowerBound unknown value refinement which indicates the length of the final
35+
// collection value will be at least the specified int64 value. This refinement can only be applied to List, Map, and Set types.
3436
func NewCollectionLengthLowerBound(value int64) Refinement {
3537
return CollectionLengthLowerBound{
3638
value: value,

tftypes/refinement/collection_length_upper_bound.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ package refinement
55

66
import "fmt"
77

8-
// TODO: doc
8+
// CollectionLengthUpperBound represents an unknown value refinement which indicates the length of the final collection value will be
9+
// at most the specified int64 value. This refinement can only be applied to List, Map, and Set types.
910
type CollectionLengthUpperBound struct {
1011
value int64
1112
}
@@ -23,14 +24,15 @@ func (n CollectionLengthUpperBound) String() string {
2324
return fmt.Sprintf("length upper bound = %d", n.UpperBound())
2425
}
2526

26-
// TODO: doc
27+
// UpperBound returns the int64 value that the final value's collection length will be at most.
2728
func (n CollectionLengthUpperBound) UpperBound() int64 {
2829
return n.value
2930
}
3031

3132
func (n CollectionLengthUpperBound) unimplementable() {}
3233

33-
// TODO: doc
34+
// NewCollectionLengthUpperBound returns the CollectionLengthUpperBound unknown value refinement which indicates the length of the final
35+
// collection value will be at most the specified int64 value. This refinement can only be applied to List, Map, and Set types.
3436
func NewCollectionLengthUpperBound(value int64) Refinement {
3537
return CollectionLengthUpperBound{
3638
value: value,

tftypes/refinement/doc.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
// Copyright (c) HashiCorp, Inc.
22
// SPDX-License-Identifier: MPL-2.0
33

4-
// TODO: docs
4+
// The refinement package contains the interfaces and structs that represent unknown value refinement data. Refinements contain
5+
// additional constraints about unknown values and what their eventual known values can be. In certain scenarios, Terraform can
6+
// use these constraints to produce known results from unknown values. (like evaluating a count expression comparing an unknown
7+
// value to "null")
8+
//
9+
// Unknown value refinements can be added to a `tftypes.Value` via the `(tftypes.Value).Refine` method. Refinements on an unknown
10+
// `tftypes.Value` can be retrieved via the `(tftypes.Value).Refinements()` method.
511
package refinement

tftypes/refinement/nullness.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33

44
package refinement
55

6-
// TODO: doc
6+
// Nullness represents an unknown value refinement that indicates the final value will definitely not be null (Nullness = false). This refinement
7+
// can be applied to a value of any type (excluding DynamicPseudoType).
8+
//
9+
// While an unknown value can be refined to indicate that the final value will definitely be null (Nullness = true), there is no practical reason
10+
// to do this. This option is exposed to maintain parity with Terraform's type system, while all practical usages of this refinement should collapse
11+
// to known null values instead.
712
type Nullness struct {
813
value bool
914
}
@@ -27,14 +32,25 @@ func (n Nullness) String() string {
2732
return "not null"
2833
}
2934

30-
// TODO: doc
35+
// Nullness returns the underlying refinement data indicating:
36+
// - When "false", the final value will definitely not be null.
37+
// - When "true", the final value will definitely be null.
38+
//
39+
// While an unknown value can be refined to indicate that the final value will definitely be null (Nullness = true), there is no practical reason
40+
// to do this. This option is exposed to maintain parity with Terraform's type system, while all practical usages of this refinement should collapse
41+
// to known null values instead.
3142
func (n Nullness) Nullness() bool {
3243
return n.value
3344
}
3445

3546
func (n Nullness) unimplementable() {}
3647

37-
// TODO: doc
48+
// NewNullness returns the Nullness unknown value refinement that indicates the final value will definitely not be null (Nullness = false). This refinement
49+
// can be applied to a value of any type (excluding DynamicPseudoType).
50+
//
51+
// While an unknown value can be refined to indicate that the final value will definitely be null (Nullness = true), there is no practical reason
52+
// to do this. This option is exposed to maintain parity with Terraform's type system, while all practical usages of this refinement should collapse
53+
// to known null values instead.
3854
func NewNullness(value bool) Refinement {
3955
return Nullness{
4056
value: value,

tftypes/refinement/number_lower_bound.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88
"math/big"
99
)
1010

11-
// TODO: doc
11+
// NumberLowerBound represents an unknown value refinement that indicates the final value will not be less than the specified
12+
// *big.Float value, as well as whether that bound is inclusive or exclusive. This refinement can only be applied to the Number type.
1213
type NumberLowerBound struct {
1314
inclusive bool
1415
value *big.Float
@@ -32,19 +33,21 @@ func (n NumberLowerBound) String() string {
3233
return fmt.Sprintf("lower bound = %s (%s)", n.LowerBound().String(), rangeDescription)
3334
}
3435

35-
// TODO: doc
36+
// IsInclusive returns whether the bound returned by the `LowerBound` method is inclusive or exclusive.
3637
func (n NumberLowerBound) IsInclusive() bool {
3738
return n.inclusive
3839
}
3940

40-
// TODO: doc
41+
// LowerBound returns the *big.Float value that the final value will not be less than. The `IsInclusive` method must also be used during
42+
// comparison to determine whether the bound is inclusive or exclusive.
4143
func (n NumberLowerBound) LowerBound() *big.Float {
4244
return n.value
4345
}
4446

4547
func (n NumberLowerBound) unimplementable() {}
4648

47-
// TODO: doc
49+
// NewNumberLowerBound returns the NumberLowerBound unknown value refinement that indicates the final value will not be less than the specified
50+
// *big.Float value, as well as whether that bound is inclusive or exclusive. This refinement can only be applied to the Number type.
4851
func NewNumberLowerBound(value *big.Float, inclusive bool) Refinement {
4952
return NumberLowerBound{
5053
value: value,

tftypes/refinement/number_upper_bound.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88
"math/big"
99
)
1010

11-
// TODO: doc
11+
// NumberUpperBound represents an unknown value refinement that indicates the final value will not be greater than the specified
12+
// *big.Float value, as well as whether that bound is inclusive or exclusive. This refinement can only be applied to the Number type.
1213
type NumberUpperBound struct {
1314
inclusive bool
1415
value *big.Float
@@ -32,19 +33,21 @@ func (n NumberUpperBound) String() string {
3233
return fmt.Sprintf("upper bound = %s (%s)", n.UpperBound().String(), rangeDescription)
3334
}
3435

35-
// TODO: doc
36+
// IsInclusive returns whether the bound returned by the `UpperBound` method is inclusive or exclusive.
3637
func (n NumberUpperBound) IsInclusive() bool {
3738
return n.inclusive
3839
}
3940

40-
// TODO: doc
41+
// UpperBound returns the *big.Float value that the final value will not be greater than. The `IsInclusive` method must also be used during
42+
// comparison to determine whether the bound is inclusive or exclusive.
4143
func (n NumberUpperBound) UpperBound() *big.Float {
4244
return n.value
4345
}
4446

4547
func (n NumberUpperBound) unimplementable() {}
4648

47-
// TODO: doc
49+
// NewNumberUpperBound returns the NumberUpperBound unknown value refinement that indicates the final value will not be greater than the specified
50+
// *big.Float value, as well as whether that bound is inclusive or exclusive. This refinement can only be applied to the Number type.
4851
func NewNumberUpperBound(value *big.Float, inclusive bool) Refinement {
4952
return NumberUpperBound{
5053
value: value,

tftypes/refinement/refinement.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
type Key int64
1313

1414
func (k Key) String() string {
15-
// TODO: Not sure when this is used, double check the names
1615
switch k {
1716
case KeyNullness:
1817
return "nullness"
@@ -22,6 +21,10 @@ func (k Key) String() string {
2221
return "number_lower_bound"
2322
case KeyNumberUpperBound:
2423
return "number_upper_bound"
24+
case KeyCollectionLengthLowerBound:
25+
return "collection_length_lower_bound"
26+
case KeyCollectionLengthUpperBound:
27+
return "collection_length_upper_bound"
2528
default:
2629
return fmt.Sprintf("unsupported refinement: %d", k)
2730
}
@@ -65,7 +68,8 @@ const (
6568
KeyCollectionLengthUpperBound = Key(6)
6669
)
6770

68-
// TODO: docs
71+
// Refinement represents an unknown value refinement with data constraints relevant to the final value. This interface can be asserted further
72+
// with the associated structs in the `refinement` package to extract underlying refinement data.
6973
type Refinement interface {
7074
// Equal should return true if the Refinement is considered equivalent to the
7175
// Refinement passed as an argument.
@@ -77,7 +81,7 @@ type Refinement interface {
7781
unimplementable() // prevents external implementations, all refinements are defined in the Terraform/HCL type system go-cty.
7882
}
7983

80-
// TODO: docs
84+
// Refinements represents a map of unknown value refinement data.
8185
type Refinements map[Key]Refinement
8286

8387
func (r Refinements) Equal(other Refinements) bool {

tftypes/refinement/string_prefix.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ package refinement
55

66
import "fmt"
77

8-
// TODO: doc
8+
// StringPrefix represents an unknown value refinement that indicates the final value will be prefixed with the specified string value.
9+
// String prefixes that exceed 256 characters in length will be truncated and empty string prefixes will not be encoded. This refinement can
10+
// only be applied to the String type.
911
type StringPrefix struct {
1012
value string
1113
}
@@ -23,14 +25,16 @@ func (s StringPrefix) String() string {
2325
return fmt.Sprintf("prefix = %q", s.PrefixValue())
2426
}
2527

26-
// TODO: doc
28+
// PrefixValue returns the string value that the final value will be prefixed with.
2729
func (s StringPrefix) PrefixValue() string {
2830
return s.value
2931
}
3032

3133
func (s StringPrefix) unimplementable() {}
3234

33-
// TODO: doc
35+
// NewStringPrefix returns the StringPrefix unknown value refinement that indicates the final value will be prefixed with the specified
36+
// string value. String prefixes that exceed 256 characters in length will be truncated and empty string prefixes will not be encoded. This
37+
// refinement can only be applied to the String type.
3438
func NewStringPrefix(value string) Refinement {
3539
return StringPrefix{
3640
value: value,

tftypes/value.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ 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
4745
type Value struct {
4846
typ Type
4947
value interface{}
@@ -628,7 +626,10 @@ func unexpectedValueTypeError(p *AttributePath, expected, got interface{}, typ T
628626
return p.NewErrorf("unexpected value type %T, %s values must be of type %T", got, typ, expected)
629627
}
630628

631-
// TODO: doc, mention returning value if not unknown
629+
// Refine is used to apply unknown refinement data to a Value. This method will return a copy of the Value, fully
630+
// replacing all the existing refinement data with the provided refinements.
631+
//
632+
// If the Value is not unknown, then a copy of the Value will be returned with no refinements.
632633
func (val Value) Refine(refinements refinement.Refinements) Value {
633634
newVal := val.Copy()
634635

@@ -648,8 +649,9 @@ func (val Value) Refine(refinements refinement.Refinements) Value {
648649
return newVal
649650
}
650651

651-
// TODO: doc, mention copy
652+
// Refinements returns the unknown refinement data for the Value.
652653
func (val Value) Refinements() refinement.Refinements {
654+
// Copy the data first to prevent any mutation of the refinement map data.
653655
valCopy := val.Copy()
654656

655657
return valCopy.refinements

0 commit comments

Comments
 (0)