-
Notifications
You must be signed in to change notification settings - Fork 93
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
datasource/schema: Initial package #546
Conversation
One aesthetic choice we could make here is to omit "Attribute" everywhere and only call out "Block", e.g. func (d ThingDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"string_attribute": schema.String{
Required: true,
Validators: []validator.String{
stringvalidator.LengthBetween(3, 256),
},
},
"custom_string_attribute": schema.String{
CustomType: timetypes.RFC3339Type,
Optional: true,
},
"list_attribute": schema.List{
ElementType: types.StringType,
Optional: true,
},
"list_nested_attribute": schema.ListNested{
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"bool_attribute": schema.Bool{
Optional: true,
},
},
Validators: []validator.Object{ /*...*/ },
},
Optional: true,
Validators: []validator.List{
listvalidator.SizeAtMost(2),
},
},
"single_nested_attribute": schema.SingleNested{
Attributes: map[string]schema.Attribute{
"int64_attribute": schema.Int64{
Optional: true,
},
},
Optional: true,
},
},
Blocks: map[string]schema.Block{
"list_block": schema.ListBlock{
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"float64_attribute": schema.Float64{
Optional: true,
Validators: []validator.Float64{
float64validator.OneOf(1.2, 2.4),
},
},
},
Validators: []validator.Object{ /*...*/ },
},
Validators: []validator.List{
listvalidator.SizeAtMost(2),
},
},
},
}
} Another option would be further breaking apart the package into func (d ThingDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]attribute.Attribute{
"string_attribute": attribute.String{
Required: true,
Validators: []validator.String{
stringvalidator.LengthBetween(3, 256),
},
},
"custom_string_attribute": attribute.String{
CustomType: timetypes.RFC3339Type,
Optional: true,
},
"list_attribute": attribute.List{
ElementType: types.StringType,
Optional: true,
},
"list_nested_attribute": attribute.ListNested{
NestedObject: attribute.NestedObject{
Attributes: map[string]attribute.Attribute{
"bool_attribute": attribute.Bool{
Optional: true,
},
},
Validators: []validator.Object{ /*...*/ },
},
Optional: true,
Validators: []validator.List{
listvalidator.SizeAtMost(2),
},
},
"single_nested_attribute": attribute.SingleNested{
Attributes: map[string]attribute.Attribute{
"int64_attribute": attribute.Int64{
Optional: true,
},
},
Optional: true,
},
},
Blocks: map[string]block.Block{
"list_block": block.List{
NestedObject: block.NestedObject{
Attributes: map[string]attribute.Attribute{
"float64_attribute": attribute.Float64{
Optional: true,
Validators: []validator.Float64{
float64validator.OneOf(1.2, 2.4),
},
},
},
Validators: []validator.Object{ /*...*/ },
},
Validators: []validator.List{
listvalidator.SizeAtMost(2),
},
},
},
}
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
LGTM
9d51a1f
to
8df3de9
Compare
Reference: #132 Reference: #326 Reference: #437 Reference: #491 Reference: #508 Reference: #532 This change introduces a new `datasource/schema` package, which contains schema interfaces and types relevant to data sources, such as omitting plan modifiers and schema versioning. This new schema implementation also provides strongly typed attributes, nested attributes, and blocks with customizable types. Nested attributes and blocks are exposed with a separate nested object for customization and validation. The implementation leans heavily on the design choice of the framework being responsible for preventing provider developer runtime errors. The tailored fields no longer expose functionality that is not available for data sources. The framework design will also raise compiler-time errors for errant typing of validators.
8df3de9
to
e3b212d
Compare
The changes here do not conflict with #552, so pulling this in to continue the effort on |
I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions. |
Reference: #132
Reference: #326
Reference: #437
Reference: #491
Reference: #508
Reference: #532
This change introduces a new
datasource/schema
package, which contains schema interfaces and types relevant to data sources, such as omitting plan modifiers and schema versioning. This new schema implementation also provides strongly typed attributes, nested attributes, and blocks with customizable types. Nested attributes and blocks are exposed with a separate nested object for customization and validation.The implementation leans heavily on the design choice of the framework being responsible for preventing provider developer runtime errors. The tailored fields no longer expose functionality that is not available for data sources. The framework design will also raise compiler-time errors for errant typing of validators.
No changes are required for data handling in the
Read
method.Example definition:
To migrate a data source schema:
github.com/hashicorp/terraform-plugin-framework/datasource/schema
to theimport
statementdatasource.DataSource
implementationGetSchema
method toSchema
whose response includes aschema.Schema
from the new package.Prior implementation:
Migrated implementation:
map[string]tfsdk.Attribute
withmap[string]schema.Attribute
map[string]tfsdk.Block
withmap[string]schema.Block
Type
field will be removed and the map entries must declare the typed implementation, e.g. atfsdk.Attribute
withType: types.StringType
is equivalent toschema.StringAttribute
. Custom attribute types can be specified via theCustomType
field in each of the implementations.Prior primitive type (
types.BoolType
,types.Float64Type
,types.Int64Type
,types.NumberType
,types.StringType
) attribute implementation:Migrated implementation:
Prior collection type (
types.ListType
,types.MapType
,types.SetType
) attribute implementation:Migrated implementation:
Prior single nested attributes type (
tfsdk.SingleNestedAttributes()
) attribute implementation:Migrated implementation:
Prior collection nested attributes type (
tfsdk.ListNestedAttributes()
,tfsdk.MapNestedAttributes()
,tfsdk.SetNestedAttributes()
) attribute implementation:Migrated implementation:
Prior collection blocks type (
tfsdk.Block
) attribute implementation:Migrated implementation: