hashicorp / terraform-plugin-codegen-spec

Terraform Provider Code Generation Specification and Go Bindings
Mozilla Public License 2.0
7 stars 4 forks source link

Add validation to prevent duplicate names #13

Closed bendbennett closed 1 year ago

bendbennett commented 1 year ago

It is never valid for a plugin Framework schema to have duplicated names for data sources or resources. Similarly, it is never valid for attributes or blocks to have duplicated names within a schema at the same nesting level.

The proposal is to introduce additional validation to ensure that duplicate names are not present within spec.Specification.

An additional function will be added that performs validation that duplicates are not present prior to returning a spec.Specification. For instance:

func Generate(ctx context.Context, document []byte) (Specification, error) {
    if err := Validate(ctx, document); err != nil {
        return Specification{}, err
    }

    var s Specification
    if err = json.Unmarshal(src, &s); err != nil {
        return s, err
    } 

    if err = s.Validate(ctx); err != nil {
        return s, err
    }

    return s, nil
}

The Generate() function will delegate to the relevant Validate() functions on the spec.Specification and subsequently on the nested data sources, provider and resources. For example:

package resource

type Resources []Resource

func (rs Resources) Validate(ctx context.Context) error {
  // Resource name duplication detection
  // Delegate to new (Resource).Validate() method on each for Schema, etc. validation
}

package spec

type Specification struct {
  // ...
  Resources resource.Resources `json:"resources,omitempty"`
}

func (s Specification) Validate(ctx context.Context) error {
  // Delegate to (resource.Resources).Validate(), etc. methods
}
github-actions[bot] commented 3 months ago

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