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 adding schema definition string method on types #23

Open
bendbennett opened this issue Jun 20, 2023 · 1 comment
Open

Consider adding schema definition string method on types #23

bendbennett opened this issue Jun 20, 2023 · 1 comment

Comments

@bendbennett
Copy link
Contributor

In the terraform-plugin-codegen-framework code strings for the types for use in generated schema are produced along the following lines:

switch {
case v.Bool != nil:
	if v.Bool.CustomType != nil {
		aTypes.WriteString(v.Bool.CustomType.Type)
	} else {
		aTypes.WriteString("types.BoolType")
	}
	// ...
}

A method could be created for each of the types so that the logic for determining which string to return would be come the responsibility of the type itself rather than the caller.

For instance:

switch {
case v.Bool != nil:
	aTypes.WriteString(v.Bool.SchemaString)
	// ...
}
@bflad
Copy link
Contributor

bflad commented Jun 20, 2023

This feels like more of a framework code generation issue than something to solve within specification code as I don't think the specification should know or understand SDK-specific details (e.g. types.BoolType). Generating terraform-plugin-go code, for another example, would be concerned with understanding tftypes package implementation details.

One potential way to envision this on the framework side of things is having a wrapper type, similar to the schema wrapper types, that knows how to handle the framework-specific details:

// In terraform-plugin-codegen-framework/internal/schema package since
// it should theoretically be generic across any schema type.
package schema

type BoolType struct {
  CustomType *schemaspec.CustomType // or its own type, if there's shared framework-specific details worth abstracting
}

func (t BoolType) SchemaDefinition() string {
  if t.CustomType != nil {
    return t.CustomType.Type
  }

  return "types.BoolType"
}

func (t BoolType) SchemaImport() string {
  if t.CustomType != nil {
    if t.CustomType.HasImport() {
      return t.CustomType.Import
    }

    return ""
  }

  return "github.com/hashicorp/terraform-plugin-framework/types"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants