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

Consolidate convert and generate packages for datasource, provider, and resource #98

Merged
merged 8 commits into from
Dec 14, 2023

Conversation

bendbennett
Copy link
Contributor

This PR consolidates the packages for <datasource|provider|resource>_convert, and <datasource|provider|resource>_generate, into datasource, provider, and resource, respectively.

The only purpose of the <datasource|provider|resource>_convert packages was to serve as a means to convert the types used by the Go bindings for terraform-plugin-codegen-spec into terraform-plugin-codegen-framework generator types. For example:

func convertBoolAttribute(a *datasource.BoolAttribute) (datasource_generate.GeneratorBoolAttribute, error) {
	if a == nil {
		return datasource_generate.GeneratorBoolAttribute{}, fmt.Errorf("*datasource.BoolAttribute is nil")
	}

	return datasource_generate.GeneratorBoolAttribute{
		BoolAttribute: schema.BoolAttribute{
			Required:            isRequired(a.ComputedOptionalRequired),
			Optional:            isOptional(a.ComputedOptionalRequired),
			Computed:            isComputed(a.ComputedOptionalRequired),
			Sensitive:           isSensitive(a.Sensitive),
			Description:         description(a.Description),
			MarkdownDescription: description(a.Description),
			DeprecationMessage:  deprecationMessage(a.DeprecationMessage),
		},

		AssociatedExternalType: generatorschema.NewAssocExtType(a.AssociatedExternalType),
		CustomType:             a.CustomType,
		Validators:             a.Validators,
	}, nil
}

As these functions are essentially constructors for the terraform-plugin-codegen-framework generator types, <datasource|provider|resource>_convert, and <datasource|provider|resource>_generate packages have been consolidated by adding constructors, removing the <datasource|provider|resource>_convert packages and renaming the <datasource|provider|resource>_generate packages to datasource, provider, and resource, respectively.

A typical constructor looks as follows:

func NewGeneratorBoolAttribute(a *datasource.BoolAttribute) (GeneratorBoolAttribute, error) {
	if a == nil {
		return GeneratorBoolAttribute{}, fmt.Errorf("*datasource.BoolAttribute is nil")
	}

	c := convert.NewComputedOptionalRequired(a.ComputedOptionalRequired)

	s := convert.NewSensitive(a.Sensitive)

	d := convert.NewDescription(a.Description)

	dm := convert.NewDeprecationMessage(a.DeprecationMessage)

	return GeneratorBoolAttribute{
		BoolAttribute: schema.BoolAttribute{
			Required:            c.IsRequired(),
			Optional:            c.IsOptional(),
			Computed:            c.IsComputed(),
			Sensitive:           s.IsSensitive(),
			Description:         d.Description(),
			MarkdownDescription: d.Description(),
			DeprecationMessage:  dm.DeprecationMessage(),
		},

		AssociatedExternalType: generatorschema.NewAssocExtType(a.AssociatedExternalType),
		CustomType:             a.CustomType,
		Validators:             a.Validators,
	}, nil
}

@bendbennett bendbennett added this to the v0.4.0 milestone Dec 1, 2023
@bendbennett bendbennett requested a review from a team as a code owner December 1, 2023 10:58
Copy link
Member

@austinvalle austinvalle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this refactor! 😌

LGTM 🚀

@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package datasource_generate
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sometimes I wonder how GitHub determines it's diffs 😕 lol

@bendbennett bendbennett merged commit 5e7142e into main Dec 14, 2023
4 checks passed
@bendbennett bendbennett deleted the consolidate-convert-and-generate branch December 14, 2023 07:25
Copy link

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.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants