Skip to content

Commit

Permalink
types: Go pointer value constructors and getters for Bool, Float64, I…
Browse files Browse the repository at this point in the history
…nt64, and String (#689)

Reference: #649

Co-authored-by: Austin Valle <austinvalle@gmail.com>
  • Loading branch information
bflad and austinvalle committed Mar 15, 2023
1 parent 66ec355 commit 3635f4f
Show file tree
Hide file tree
Showing 16 changed files with 400 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-20230314-144231.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: ENHANCEMENTS
body: 'types/basetypes: Add `BoolValue` type `NewBoolPointerValue()` creation function
and `ValueBoolPointer()` method'
time: 2023-03-14T14:42:31.20202-04:00
custom:
Issue: "689"
6 changes: 6 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-20230314-144232.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: ENHANCEMENTS
body: 'types/basetypes: Add `Float64Value` type `NewFloat64PointerValue()` creation function
and `ValueFloat64Pointer()` method'
time: 2023-03-14T14:42:32.20202-04:00
custom:
Issue: "689"
6 changes: 6 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-20230314-144233.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: ENHANCEMENTS
body: 'types/basetypes: Add `Int64Value` type `NewInt64PointerValue()` creation function
and `ValueInt64Pointer()` method'
time: 2023-03-14T14:42:33.20202-04:00
custom:
Issue: "689"
6 changes: 6 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-20230314-144234.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: ENHANCEMENTS
body: 'types/basetypes: Add `StringValue` type `NewStringPointerValue()` creation function
and `ValueStringPointer()` method'
time: 2023-03-14T14:42:34.20202-04:00
custom:
Issue: "689"
20 changes: 20 additions & 0 deletions types/basetypes/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ func NewBoolValue(value bool) BoolValue {
}
}

// NewBoolPointerValue creates a Bool with a null value if nil or a known
// value. Access the value via the Bool type ValueBoolPointer method.
func NewBoolPointerValue(value *bool) BoolValue {
if value == nil {
return NewBoolNull()
}

return NewBoolValue(*value)
}

// BoolValue represents a boolean value.
type BoolValue struct {
// state represents whether the value is null, unknown, or known. The
Expand Down Expand Up @@ -131,6 +141,16 @@ func (b BoolValue) ValueBool() bool {
return b.value
}

// ValueBoolPointer returns a pointer to the known bool value, nil for a null
// value, or a pointer to false for an unknown value.
func (b BoolValue) ValueBoolPointer() *bool {
if b.IsNull() {
return nil
}

return &b.value
}

// ToBoolValue returns Bool.
func (b BoolValue) ToBoolValue(context.Context) (BoolValue, diag.Diagnostics) {
return b, nil
Expand Down
72 changes: 72 additions & 0 deletions types/basetypes/bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,75 @@ func TestBoolValueValueBool(t *testing.T) {
})
}
}

func TestBoolValueValueBoolPointer(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
input BoolValue
expected *bool
}{
"known-false": {
input: NewBoolValue(false),
expected: pointer(false),
},
"known-true": {
input: NewBoolValue(true),
expected: pointer(true),
},
"null": {
input: NewBoolNull(),
expected: nil,
},
"unknown": {
input: NewBoolUnknown(),
expected: pointer(false),
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

got := testCase.input.ValueBoolPointer()

if diff := cmp.Diff(got, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}

func TestNewBoolPointerValue(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
value *bool
expected BoolValue
}{
"nil": {
value: nil,
expected: NewBoolNull(),
},
"value": {
value: pointer(true),
expected: NewBoolValue(true),
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

got := NewBoolPointerValue(testCase.value)

if diff := cmp.Diff(got, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}
20 changes: 20 additions & 0 deletions types/basetypes/float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ func NewFloat64Value(value float64) Float64Value {
}
}

// NewFloat64PointerValue creates a Float64 with a null value if nil or a known
// value. Access the value via the Float64 type ValueFloat64Pointer method.
func NewFloat64PointerValue(value *float64) Float64Value {
if value == nil {
return NewFloat64Null()
}

return NewFloat64Value(*value)
}

// Float64Value represents a 64-bit floating point value, exposed as a float64.
type Float64Value struct {
// state represents whether the value is null, unknown, or known. The
Expand Down Expand Up @@ -137,6 +147,16 @@ func (f Float64Value) ValueFloat64() float64 {
return f.value
}

// ValueFloat64Pointer returns a pointer to the known float64 value, nil for a
// null value, or a pointer to 0.0 for an unknown value.
func (f Float64Value) ValueFloat64Pointer() *float64 {
if f.IsNull() {
return nil
}

return &f.value
}

// ToFloat64Value returns Float64.
func (f Float64Value) ToFloat64Value(context.Context) (Float64Value, diag.Diagnostics) {
return f, nil
Expand Down
68 changes: 68 additions & 0 deletions types/basetypes/float64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,71 @@ func TestFloat64ValueValueFloat64(t *testing.T) {
})
}
}

func TestFloat64ValueValueFloat64Pointer(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
input Float64Value
expected *float64
}{
"known": {
input: NewFloat64Value(2.4),
expected: pointer(2.4),
},
"null": {
input: NewFloat64Null(),
expected: nil,
},
"unknown": {
input: NewFloat64Unknown(),
expected: pointer(0.0),
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

got := testCase.input.ValueFloat64Pointer()

if diff := cmp.Diff(got, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}

func TestNewFloat64PointerValue(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
value *float64
expected Float64Value
}{
"nil": {
value: nil,
expected: NewFloat64Null(),
},
"value": {
value: pointer(1.2),
expected: NewFloat64Value(1.2),
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

got := NewFloat64PointerValue(testCase.value)

if diff := cmp.Diff(got, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}
20 changes: 20 additions & 0 deletions types/basetypes/int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ func NewInt64Value(value int64) Int64Value {
}
}

// NewInt64PointerValue creates a Int64 with a null value if nil or a known
// value. Access the value via the Int64 type ValueInt64Pointer method.
func NewInt64PointerValue(value *int64) Int64Value {
if value == nil {
return NewInt64Null()
}

return NewInt64Value(*value)
}

// Int64Value represents a 64-bit integer value, exposed as an int64.
type Int64Value struct {
// state represents whether the value is null, unknown, or known. The
Expand Down Expand Up @@ -131,6 +141,16 @@ func (i Int64Value) ValueInt64() int64 {
return i.value
}

// ValueInt64Pointer returns a pointer to the known int64 value, nil for a
// null value, or a pointer to 0 for an unknown value.
func (i Int64Value) ValueInt64Pointer() *int64 {
if i.IsNull() {
return nil
}

return &i.value
}

// ToInt64Value returns Int64.
func (i Int64Value) ToInt64Value(context.Context) (Int64Value, diag.Diagnostics) {
return i, nil
Expand Down
68 changes: 68 additions & 0 deletions types/basetypes/int64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,71 @@ func TestInt64ValueValueInt64(t *testing.T) {
})
}
}

func TestInt64ValueValueInt64Pointer(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
input Int64Value
expected *int64
}{
"known": {
input: NewInt64Value(24),
expected: pointer(int64(24)),
},
"null": {
input: NewInt64Null(),
expected: nil,
},
"unknown": {
input: NewInt64Unknown(),
expected: pointer(int64(0)),
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

got := testCase.input.ValueInt64Pointer()

if diff := cmp.Diff(got, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}

func TestNewInt64PointerValue(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
value *int64
expected Int64Value
}{
"nil": {
value: nil,
expected: NewInt64Null(),
},
"value": {
value: pointer(int64(123)),
expected: NewInt64Value(123),
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

got := NewInt64PointerValue(testCase.value)

if diff := cmp.Diff(got, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}
20 changes: 20 additions & 0 deletions types/basetypes/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ func NewStringValue(value string) StringValue {
}
}

// NewStringPointerValue creates a String with a null value if nil or a known
// value. Access the value via the String type ValueStringPointer method.
func NewStringPointerValue(value *string) StringValue {
if value == nil {
return NewStringNull()
}

return NewStringValue(*value)
}

// StringValue represents a UTF-8 string value.
type StringValue struct {
// state represents whether the value is null, unknown, or known. The
Expand Down Expand Up @@ -142,6 +152,16 @@ func (s StringValue) ValueString() string {
return s.value
}

// ValueStringPointer returns a pointer to the known string value, nil for a
// null value, or a pointer to "" for an unknown value.
func (s StringValue) ValueStringPointer() *string {
if s.IsNull() {
return nil
}

return &s.value
}

// ToStringValue returns String.
func (s StringValue) ToStringValue(context.Context) (StringValue, diag.Diagnostics) {
return s, nil
Expand Down
Loading

0 comments on commit 3635f4f

Please sign in to comment.