hashicorp / hashicorp/terraform-plugin-codegen-framework

Consider adding comments to model field structs

Open
#19 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
57
Forks
29
Avg merge
2d 13h
Merged PRs (30d)
1

Description

During the generation of data models, there can be a significant number of models that are generated for nested attributes and/or nested blocks.

For example, using the following intermediate representation (IR):

```json
{
"datasources": [
{
"name": "datasource",
"schema": {
"attributes": [
{
"name": "list_nested_list_nested_bool_attribute",
"list_nested": {
"computed_optional_required": "computed",
"nested_object": {
"attributes": [
{
"name": "list_nested_bool_attribute",
"list_nested": {
"computed_optional_required": "computed",
"nested_object": {
"attributes": [
{
"name": "bool_attribute",
"bool": {
"computed_optional_required": "computed"
}
}
]
}
}
}
]
}
}
},
{
"name": "list_nested_list_nested_list_attribute",
"list_nested": {
"computed_optional_required": "computed",
"nested_object": {
"attributes": [
{
"name": "list_nested_list_attribute",
"list_nested": {
"computed_optional_required": "computed",
"nested_object": {
"attributes": [
{
"name": "list_attribute",
"list": {
"computed_optional_required": "computed",
"element_type": {
"string": {}
}
}
}
]
}
}
}
]
}
}
}
]
}
}
],
"provider": {
"name": "provider"
}
}
```

Results in the generation of the following models:

```go
type datasourceModel struct {
ListNestedListNestedBoolAttribute types.List `tfsdk:"list_nested_list_nested_bool_attribute"`
ListNestedListNestedListAttribute types.List `tfsdk:"list_nested_list_nested_list_attribute"`
}

type listNestedListNestedBoolAttributeModel struct {
ListNestedBoolAttribute types.List `tfsdk:"list_nested_bool_attribute"`
}

type listNestedBoolAttributeModel struct {
BoolAttribute types.Bool `tfsdk:"bool_attribute"`
}

type listNestedListNestedListAttributeModel struct {
ListNestedListAttribute types.List `tfsdk:"list_nested_list_attribute"`
}

type listNestedListAttributeModel struct {
ListAttribute types.List `tfsdk:"list_attribute"`
}
```

Within other providers, for instance [TLS](https://github.com/hashicorp/terraform-provider-tls/blob/db1c1933f5410d86c2c40fedabe10d0942a71341/internal/provider/models.go#L25), comments have been added to the models to make it more clear which nested model "belongs" to which model struct field.

In this instance, autogenerates comments indicating the relationships between the models would look something like the following:

```go
type datasourceModel struct {
ListNestedListNestedBoolAttribute types.List `tfsdk:"list_nested_list_nested_bool_attribute"` //< listNestedListNestedBoolAttributeModel
ListNestedListNestedListAttribute types.List `tfsdk:"list_nested_list_nested_list_attribute"` //< listNestedListNestedListAttributeModel
}

type listNestedListNestedBoolAttributeModel struct {
ListNestedBoolAttribute types.List `tfsdk:"list_nested_bool_attribute"` // < listNestedBoolAttributeModel
}

type listNestedBoolAttributeModel struct {
BoolAttribute types.Bool `tfsdk:"bool_attribute"`
}

type listNestedListNestedListAttributeModel struct {
ListNestedListAttribute types.List `tfsdk:"list_nested_list_attribute"` // < listNestedListAttributeModel
}

type listNestedListAttributeModel struct {
ListAttribute types.List `tfsdk:"list_attribute"`
}
```

We may want to consider adding these comments to help with the navigation between and association of models and nested models.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.