Skip to content

Commit

Permalink
feat(values): support float64 values
Browse files Browse the repository at this point in the history
  • Loading branch information
MStreet3 committed Sep 24, 2024
1 parent 1be7384 commit e6792d2
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 28 deletions.
38 changes: 38 additions & 0 deletions pkg/values/float64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package values

import (
"errors"

"github.com/smartcontractkit/chainlink-common/pkg/values/pb"
)

type Float64 struct {
Underlying float64
}

func NewFloat64(f float64) *Float64 {
return &Float64{Underlying: f}
}

func (f *Float64) proto() *pb.Value {
return pb.NewFloat64(f.Underlying)
}

func (f *Float64) Unwrap() (any, error) {
var to float64
return to, f.UnwrapTo(&to)
}

func (f *Float64) UnwrapTo(to any) error {
if f == nil {
return errors.New("cannot unwrap nil values.Float64")
}
return unwrapTo(f.Underlying, to)
}

func (f *Float64) copy() Value {
if f == nil {
return f
}
return &Float64{Underlying: f.Underlying}
}
75 changes: 75 additions & 0 deletions pkg/values/float64_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package values

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_Float64UnwrapTo(t *testing.T) {
expected := 1.1
v := NewFloat64(expected)

var got float64
err := v.UnwrapTo(&got)
require.NoError(t, err)

assert.Equal(t, expected, got)

gotn := (*float64)(nil)
err = v.UnwrapTo(gotn)
assert.ErrorContains(t, err, "cannot unwrap to nil pointer")

var varAny any
err = v.UnwrapTo(&varAny)
require.NoError(t, err)
assert.Equal(t, expected, varAny)

fn := (*Float64)(nil)
_, err = fn.Unwrap()
assert.ErrorContains(t, err, "cannot unwrap nil")

var f float64
err = fn.UnwrapTo(&f)
assert.ErrorContains(t, err, "cannot unwrap nil")

// handle alias
type myFloat64 float64
var mf myFloat64
err = v.UnwrapTo(&mf)
require.NoError(t, err)
assert.Equal(t, myFloat64(expected), mf)
}

// Test_Float64 tests that Float64 values can converted to and from protobuf representations.
func Test_Float64(t *testing.T) {
testCases := []struct {
name string
f float64
}{
{
name: "positive",
f: 1.1,
},
{
name: "0",
f: 0,
},
{
name: "negative",
f: -1.1,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(st *testing.T) {
v := NewFloat64(tc.f)

vp := Proto(v)
got, err := FromProto(vp)
assert.NoError(t, err)
assert.Equal(t, tc.f, got)
})
}
}
8 changes: 8 additions & 0 deletions pkg/values/pb/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,11 @@ func NewTime(t time.Time) *Value {
},
}
}

func NewFloat64(f float64) *Value {
return &Value{
Value: &Value_Float64Value{
Float64Value: f,
},
}
}
73 changes: 45 additions & 28 deletions pkg/values/pb/values.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/values/pb/values.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ message Value {
int64 int64_value = 7;
BigInt bigint_value = 9;
google.protobuf.Timestamp time_value = 10;
double float64_value = 11;
}
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/values/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ func FromProto(val *pb.Value) (Value, error) {
return fromBigIntValueProto(val.GetBigintValue()), nil
case *pb.Value_TimeValue:
return NewTime(val.GetTimeValue().AsTime()), nil
case *pb.Value_Float64Value:
return NewFloat64(val.GetFloat64Value()), nil
}

return nil, fmt.Errorf("unsupported type %T: %+v", val, val)
Expand Down
8 changes: 8 additions & 0 deletions pkg/values/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ func Test_Value(t *testing.T) {
return t, tv, err
},
},
{
name: "float64",
newValue: func() (any, Value, error) {
f := 1.0
fv := NewFloat64(f)
return f, fv, nil
},
},
{
name: "recursive map",
newValue: func() (any, Value, error) {
Expand Down

0 comments on commit e6792d2

Please sign in to comment.