-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
251 additions
and
1 deletion.
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
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
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,41 @@ | ||
package tfsdk | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"regexp" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
tfReflect "github.com/hashicorp/terraform-plugin-framework/internal/reflect" | ||
"github.com/hashicorp/terraform-plugin-framework/schema" | ||
"github.com/hashicorp/terraform-plugin-go/tftypes" | ||
) | ||
|
||
var attributeValueReflectType = reflect.TypeOf(new(attr.Value)).Elem() | ||
|
||
type State struct { | ||
Raw tftypes.Value | ||
Schema schema.Schema | ||
} | ||
|
||
func isValidFieldName(name string) bool { | ||
re := regexp.MustCompile("^[a-z][a-z0-9_]*$") | ||
return re.MatchString(name) | ||
} | ||
|
||
// Get populates the struct passed as `target` with the entire state. No type assertion necessary. | ||
func (s State) Get(ctx context.Context, target interface{}) error { | ||
return tfReflect.Into(ctx, s.Schema.AttributeType(), s.Raw, target, tfReflect.Options{}) | ||
} | ||
|
||
// // GetAttribute retrieves the attribute found at `path` and returns it as an attr.Value, | ||
// // which provider developers need to assert the type of | ||
// func (s State) GetAttribute(ctx context.Context, path tftypes.AttributePath) (attr.Value, error) { | ||
|
||
// } | ||
|
||
// // MustGetAttribute retrieves the attribute as GetAttribute does, but populates target using As, | ||
// // using the simplified representation without Unknown. Errors if Unknown present | ||
// func (s State) MustGetAttribute(ctx context.Context, path tftypes.AttributePath, target interface{}) error { | ||
// return nil | ||
// } |
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,81 @@ | ||
package tfsdk | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-plugin-go/tftypes" | ||
) | ||
|
||
func TestStateGet(t *testing.T) { | ||
schema := schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"foo": { | ||
Type: types.StringType, | ||
Required: true, | ||
}, | ||
"bar": { | ||
Type: types.ListType{ | ||
ElemType: types.StringType, | ||
}, | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
state := State{ | ||
Raw: tftypes.NewValue(tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"foo": tftypes.String, | ||
"bar": tftypes.List{ElementType: tftypes.String}, | ||
}, | ||
}, map[string]tftypes.Value{ | ||
"foo": tftypes.NewValue(tftypes.String, "hello, world"), | ||
"bar": tftypes.NewValue(tftypes.List{ | ||
ElementType: tftypes.String, | ||
}, []tftypes.Value{ | ||
tftypes.NewValue(tftypes.String, "red"), | ||
tftypes.NewValue(tftypes.String, "blue"), | ||
tftypes.NewValue(tftypes.String, "green"), | ||
}), | ||
}), | ||
Schema: schema, | ||
} | ||
type myType struct { | ||
Foo types.String `tfsdk:"foo"` | ||
Bar types.List `tfsdk:"bar"` | ||
} | ||
var val myType | ||
err := state.Get(context.Background(), &val) | ||
if err != nil { | ||
t.Errorf("Error running As: %s", err) | ||
} | ||
if val.Foo.Unknown { | ||
t.Error("Expected Foo to be known") | ||
} | ||
if val.Foo.Null { | ||
t.Error("Expected Foo to be non-null") | ||
} | ||
if val.Foo.Value != "hello, world" { | ||
t.Errorf("Expected Foo to be %q, got %q", "hello, world", val.Foo.Value) | ||
} | ||
if val.Bar.Unknown { | ||
t.Error("Expected Bar to be known") | ||
} | ||
if val.Bar.Null { | ||
t.Errorf("Expected Bar to be non-null") | ||
} | ||
if len(val.Bar.Elems) != 3 { | ||
t.Errorf("Expected Bar to have 3 elements, had %d", len(val.Bar.Elems)) | ||
} | ||
if val.Bar.Elems[0].(types.String).Value != "red" { | ||
t.Errorf("Expected Bar's first element to be %q, got %q", "red", val.Bar.Elems[0].(types.String).Value) | ||
} | ||
if val.Bar.Elems[1].(types.String).Value != "blue" { | ||
t.Errorf("Expected Bar's second element to be %q, got %q", "blue", val.Bar.Elems[1].(types.String).Value) | ||
} | ||
if val.Bar.Elems[2].(types.String).Value != "green" { | ||
t.Errorf("Expected Bar's third element to be %q, got %q", "green", val.Bar.Elems[2].(types.String).Value) | ||
} | ||
} |
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
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