-
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
Framework doesn't appear to support a computed list of objects. #713
Comments
Hi @liamawhite 👋 Thank you for raising this and sorry you ran into trouble here. No additional custom types are necessarily needed -- what the framework error diagnostic is trying to relay to you as the provider developer is that: type ServiceAccountModel struct {
Id types.String `tfsdk:"id"`
Keys []*KeyModel `tfsdk:"keys"`
} Should be: type ServiceAccountModel struct {
Id types.String `tfsdk:"id"`
Keys types.List `tfsdk:"keys"`
} Because a var model ServiceAccountModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &model)...)
if resp.Diagnostics.HasError() {
return
} Which is generally correct when the model is implemented using all Does this make sense? If you have suggestions for how to improve documentation, the error messaging, etc., please let us know. |
Let me give this a try and I'll get back to you. |
Could you point to any examples on how to handle this with nested objects? For now I have something like this: keys, err := types.ListValueFrom(ctx, req.Config.Schema.GetAttributes()["keys"].(schema.ListNestedAttribute).NestedObject.Type(), []*KeyModel{{Token: types.StringValue("some-token")}})
if err != nil {
resp.Diagnostics.Append(err...)
}
model.Keys = keys But it seems a little tedious, especially if there are further levels of nesting. Any guidance? |
In my world, every layer of nesting is a struct with I'd probably wind up with something like: type key struct {
ThingOne types.String `tfsdk:"thing_one"`
ThingTwo types.Int64 `tfsdk:"thing_two"`
}
func (o key) attrTypes() map[string]attr.Type {
return map[string]attr.Type{
"thing_one": types.StringType,
"thing_two": types.Int64Type,
}
}
func keySliceToList (ctx context.Context, keysSliceIn []key, diags *diag.Diagnostics) types.List {
keys, d := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: key{}.attrTypes()}, keysSliceIn)
diags.Append(d...)
return keys
} This might not be the right way of doing things, but it's been a big improvement on what I had before. |
Since the framework is working as expected for the original report, I'm going to close this issue. If you have documentation/error messaging suggestions or feature request proposals in this space, please open a new issue. We do have #695 pending to make the data handling documentation a lot more prescriptive for each schema type already. 👍 The approach above seems to be relatively popular though and some of the maintainer team's forward-looking code generation efforts to convert framework data to/from other Go types (typically an API SDK's types) are similar in nature. If you have general questions about implementation details when working with the framework, it might be good to check out or post in HashiCorp Discuss where the maintainers and other provider developers typically answer those. |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. |
Module version
Relevant provider source code
https://github.com/liamawhite/tf-issue-repro
Debug Output
Expected Behavior
To populate the model without having to define a custom type just for computed lists of objects.
The text was updated successfully, but these errors were encountered: