-
Notifications
You must be signed in to change notification settings - Fork 28
Add validation to ensure duplicate names are not present within the schema #2
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
Conversation
…, attributes and blocks are not duplicated at the same nesting level
| as the `all` command only generates schema for data sources, provider and resources at | ||
| present. | ||
|
|
||
| ## Further Considerations |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ## Further Considerations |
| specValidator := validate.NewSpecValidator() | ||
|
|
||
| commands := map[string]cli.CommandFactory{ | ||
| "schema": func() (cli.Command, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the future I'm hoping we can still support terraform-plugin-codegen-framework schema -data-source=examplecloud_thing (or something along those lines) so existing providers can inject //go:generate directives to replace manually written code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although looking at this again, I'm guessing we'd be looking for a command structure like:
|- all
|- data-source TYPE (flags like -schema)
|- provider (flags like -schema)
\ resource TYPE (flags like -schema)
But hopefully you get the idea 😄
| // validateSchemaNames determines whether any of the names used for data sources or | ||
| // resources are duplicated. SchemaNames also determines whether any of the names used | ||
| // for attributes or blocks at the same level of nesting within a schema are duplicated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this particular validation logic is undebatable (e.g. it is never valid for a provider to have duplicated resource names) and may be desired for any code implementing the spec on either side of it, I wonder if we should uplift this logic into the spec Go module? It's a bummer that it seems like this cannot be introduced into the JSON schema itself.
We could introduce something like the following there:
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
}At this point we could debate the merits of the spec.Validate() function doing this automatically (with the unfortunate part of not returning the then-already JSON unmarshalled spec.Specification that would be needed to call the (spec.Specification).Validate() method), introducing a separate function that does the JSON Schema validate and JSON unmarshal with this additional validation logic, or whatever suits our fancy.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'd be in favour of the latter, having something like the following available on the spec Go module:
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
}|
Validation for ensuring that there are not duplicate names in use for data sources, resources, attributes, blocks and object attribute types has been added directly to the codegen.spec repo. Other changes have been rolled into [WIP] Add custom type handling for elements and schema generation for provider and resources or in the case of the command structure an issue has been created. |
|
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. |
This PR: