Open bflad opened 1 year ago
NestedAttributeDefaults
looks like a good proposal to me and will be useful.
Below is an example that contains nested properties that are default null and unknown. I just want to make sure this edgecase is covered during the implementation.
"backlog_workflow_state": schema.SingleNestedAttribute{
MarkdownDescription: "Settings for the `backlog` workflow state that is created by default for the team. *Position is always `0`. This can not be deleted.*",
Optional: true,
Computed: true,
Default: objectdefault.StaticValue(
types.ObjectValueMust(
workflowStateAttrTypes,
map[string]attr.Value{
"id": types.StringUnknown(),
"position": types.Float64Unknown(),
"name": types.StringValue("Backlog"),
"color": types.StringValue("#bec2c8"),
"description": types.StringNull(),
},
),
),
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "Identifier of the workflow state.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"position": schema.Float64Attribute{
MarkdownDescription: "Position of the workflow state.",
Computed: true,
PlanModifiers: []planmodifier.Float64{
float64planmodifier.UseStateForUnknown(),
},
},
"name": schema.StringAttribute{
MarkdownDescription: "Name of the workflow state. **Default** `Backlog`.",
Optional: true,
Computed: true,
Default: stringdefault.StaticString("Backlog"),
},
"color": schema.StringAttribute{
MarkdownDescription: "Color of the workflow state. **Default** `#bec2c8`.",
Optional: true,
Computed: true,
Default: stringdefault.StaticString("#bec2c8"),
Validators: []validator.String{
stringvalidator.RegexMatches(colorRegex(), "must be a hex color"),
},
},
"description": schema.StringAttribute{
MarkdownDescription: "Description of the workflow state.",
Optional: true,
},
},
},
Another edgecase I have is, a single nested attribute inside single nested attribute.
Module version
Use-cases
When working with nested attributes, it may be desirable to automatically have an object default based on all the nested attribute default values for when the object is null. Today, this is a manual process to re-create the object default value based on the nested attribute defaults.
Proposal
NOTE:
NestedAttributes
is not possible at the moment due to import cycle.Add an
AttributeTypes
field andNestedAttributes
field todefaults.ObjectRequest
:In
internal/fwschemadata
, update the object handling to something like:Create a new
resource/schema/objectdefault
implementation for instantiating the object using all the nested attribute defaults, e.g.It may also be worth considering whether there should be a simplified object value default function that only takes in the
map[string]attr.Value
, since we can theoretically have themap[string]attr.Type
already available.References