-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(values): support float64 values
- Loading branch information
Showing
7 changed files
with
177 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters