Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: Support Float64, Float64Type, Int64, and Int64Type #166

Merged
merged 3 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/166.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
types: Support for `Float64`, `Float64Type`, `Int64`, and `Int64Type`
```
20 changes: 20 additions & 0 deletions tfsdk/serve_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ func (t *testServeProvider) GetSchema(_ context.Context) (Schema, diag.Diagnosti
Type: types.BoolType,
Optional: true,
},
"int64": {
Type: types.Int64Type,
Optional: true,
},
"float64": {
Type: types.Float64Type,
Optional: true,
},
"list-string": {
Type: types.ListType{
ElemType: types.StringType,
Expand Down Expand Up @@ -282,6 +290,16 @@ var testServeProviderProviderSchema = &tfprotov6.Schema{
},
Optional: true,
},
{
Name: "float64",
Type: tftypes.Number,
Optional: true,
},
{
Name: "int64",
Type: tftypes.Number,
Optional: true,
},
{
Name: "list-list-string",
Type: tftypes.List{
Expand Down Expand Up @@ -489,6 +507,8 @@ var testServeProviderProviderType = tftypes.Object{
"string": tftypes.String,
"number": tftypes.Number,
"bool": tftypes.Bool,
"int64": tftypes.Number,
"float64": tftypes.Number,
"list-string": tftypes.List{ElementType: tftypes.String},
"list-list-string": tftypes.List{ElementType: tftypes.List{ElementType: tftypes.String}},
"list-object": tftypes.List{ElementType: tftypes.Object{AttributeTypes: map[string]tftypes.Type{
Expand Down
6 changes: 6 additions & 0 deletions tfsdk/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ func TestServerValidateProviderConfig(t *testing.T) {
"string": tftypes.NewValue(tftypes.String, "a new string value"),
"number": tftypes.NewValue(tftypes.Number, 1234),
"bool": tftypes.NewValue(tftypes.Bool, true),
"int64": tftypes.NewValue(tftypes.Number, 1234),
"float64": tftypes.NewValue(tftypes.Number, 1234),
"list-string": tftypes.NewValue(tftypes.List{ElementType: tftypes.String}, []tftypes.Value{
tftypes.NewValue(tftypes.String, "hello"),
tftypes.NewValue(tftypes.String, "world"),
Expand Down Expand Up @@ -779,6 +781,8 @@ func TestServerConfigureProvider(t *testing.T) {
"string": tftypes.NewValue(tftypes.String, "a new string value"),
"number": tftypes.NewValue(tftypes.Number, 1234),
"bool": tftypes.NewValue(tftypes.Bool, true),
"int64": tftypes.NewValue(tftypes.Number, 1234),
"float64": tftypes.NewValue(tftypes.Number, 1234),
"list-string": tftypes.NewValue(tftypes.List{ElementType: tftypes.String}, []tftypes.Value{
tftypes.NewValue(tftypes.String, "hello"),
tftypes.NewValue(tftypes.String, "world"),
Expand Down Expand Up @@ -958,6 +962,8 @@ func TestServerConfigureProvider(t *testing.T) {
"string": tftypes.NewValue(tftypes.String, "a new string value"),
"number": tftypes.NewValue(tftypes.Number, 1234),
"bool": tftypes.NewValue(tftypes.Bool, true),
"int64": tftypes.NewValue(tftypes.Number, 1234),
"float64": tftypes.NewValue(tftypes.Number, 1234),
"list-string": tftypes.NewValue(tftypes.List{ElementType: tftypes.String}, []tftypes.Value{
tftypes.NewValue(tftypes.String, "hello"),
tftypes.NewValue(tftypes.String, "world"),
Expand Down
135 changes: 135 additions & 0 deletions types/float64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package types

import (
"context"
"fmt"
"math/big"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

func float64Validate(ctx context.Context, in tftypes.Value, path *tftypes.AttributePath) diag.Diagnostics {
var diags diag.Diagnostics

if !in.Type().Is(tftypes.Number) {
diags.AddAttributeError(
path,
"Float64 Type Validation Error",
"An unexpected error was encountered trying to validate an attribute value. This is always an error in the provider. Please report the following to the provider developer:\n\n"+
fmt.Sprintf("Expected Number value, received %T with value: %v", in, in),
)
return diags
}

if !in.IsKnown() || in.IsNull() {
return diags
}

var value *big.Float
err := in.As(&value)

if err != nil {
diags.AddAttributeError(
path,
"Float64 Type Validation Error",
"An unexpected error was encountered trying to validate an attribute value. This is always an error in the provider. Please report the following to the provider developer:\n\n"+
fmt.Sprintf("Cannot convert value to big.Float: %s", err),
)
return diags
}

_, accuracy := value.Float64()

if accuracy != 0 {
diags.AddAttributeError(
path,
"Float64 Type Validation Error",
fmt.Sprintf("Value %s cannot be represented as a 64-bit floating point.", value),
)
return diags
}

return diags
}

func float64ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
if !in.IsKnown() {
return Float64{Unknown: true}, nil
}

if in.IsNull() {
return Float64{Null: true}, nil
}

var bigF *big.Float
err := in.As(&bigF)

if err != nil {
return nil, err
}

f, accuracy := bigF.Float64()

if accuracy != 0 {
return nil, fmt.Errorf("Value %s cannot be represented as a 64-bit floating point.", bigF)
}

return Float64{Value: f}, nil
}

var _ attr.Value = Float64{}

// Float64 represents a 64-bit floating point value, exposed as an float64.
type Float64 struct {
// Unknown will be true if the value is not yet known.
Unknown bool

// Null will be true if the value was not set, or was explicitly set to
// null.
Null bool

// Value contains the set value, as long as Unknown and Null are both
// false.
Value float64
}

// Equal returns true if `other` is an Float64 and has the same value as `i`.
func (f Float64) Equal(other attr.Value) bool {
o, ok := other.(Float64)

if !ok {
return false
}

if f.Unknown != o.Unknown {
return false
}

if f.Null != o.Null {
return false
}

return f.Value == o.Value
}

// ToTerraformValue returns the data contained in the Float64 as a float64.
// If Unknown is true, it returns a tftypes.UnknownValue. If Null is true, it
// returns nil.
func (f Float64) ToTerraformValue(ctx context.Context) (interface{}, error) {
if f.Null {
return nil, nil
}

if f.Unknown {
return tftypes.UnknownValue, nil
}

return big.NewFloat(f.Value), nil
}

// Type returns a NumberType.
func (f Float64) Type(ctx context.Context) attr.Type {
return Int64Type
}
Loading