Skip to content
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

Consider mapping for use with associated_external_type #51

Open
bendbennett opened this issue Sep 21, 2023 · 0 comments
Open

Consider mapping for use with associated_external_type #51

bendbennett opened this issue Sep 21, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@bendbennett
Copy link
Contributor

Currently, the generation of To<...>()/From<...>() methods (refer to Consider handling of associated_external_type for all types) uses a direct mapping from the Terraform Plugin Framework types to the API SDK types.

For example, usage of associated_external_type in the following specification results in the generation of the To<...>()/From<...>() methods.

{
  "datasources": [
    {
      "name": "example",
      "schema": {
        "attributes": [
          {
            "name": "list_nested_attribute",
            "list_nested": {
              "nested_object": {
                "associated_external_type": {
                  "import": {
                    "path": "example.com/apisdk"
                  },
                  "type": "*apisdk.Type"
                },
                "attributes": [
                  {
                    "name": "int64_attribute",
                    "int64": {
                      "computed_optional_required": "optional"
                    }
                  }
                ]
              },
              "computed_optional_required": "optional"
            }
          }
        ]
      }
    }
  ],
  "provider": {
    "name": "example"
  }
}
func (v ListNestedAttributeValue) ToApisdkType(ctx context.Context) (*apisdk.Type, diag.Diagnostics) {
	var diags diag.Diagnostics

	if v.IsNull() {
		return nil, diags
	}

	if v.IsUnknown() {
		diags.Append(diag.NewErrorDiagnostic(
			"ListNestedAttributeValue Value Is Unknown",
			`"ListNestedAttributeValue" is unknown.`,
		))

		return nil, diags
	}

	return &apisdk.Type{
		Int64Attribute: v.Int64Attribute.ValueInt64Pointer(),
	}, diags
}

func (v ListNestedAttributeValue) FromApisdkType(ctx context.Context, apiObject *apisdk.Type) (ListNestedAttributeValue, diag.Diagnostics) {
	var diags diag.Diagnostics

	if apiObject == nil {
		return NewListNestedAttributeValueNull(), diags
	}

	return ListNestedAttributeValue{
		Int64Attribute: types.Int64PointerValue(apiObject.Int64Attribute),
		state:          attr.ValueStateKnown,
	}, diags
}

The following assumptions are made during the generation of the To<...>()/From<...>() methods based on the specified associated_external_type:

  • The API SDK type is a struct with fields which are named with the camel-cased attribute name (i.e., int64_attribute => Int64Attribute).
  • The values held within the API SDK type are pointer values of the attribute value (i.e., int64 => *int64).

Proposal

Additional flexibility could be introduced by allowing the specification of "mapping" between the Terraform Plugin Framework types and the types specified by associated_external_type. This would increase the usefulness of the generated To<...>()/From<...>() methods and accommodate cases where a direct 1-2-1 mapping between the Terraform Plugin Framework types and the API SDK types was not present.

@bendbennett bendbennett added the enhancement New feature or request label Sep 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant