-
Notifications
You must be signed in to change notification settings - Fork 18
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
Add custom type handling for collection elements and object attribute types #9
Conversation
…d spec generation
…import is included in the top-level schema.gotmpl
…ObjectAttributeType
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.
Overall I think this really looks good. Just a few little things to potentially followup on. 😄
return "types.Int64Type" | ||
case e.List != nil: | ||
if e.List.CustomType != nil { | ||
return fmt.Sprintf("%s{\nElemType: %s,\n}", e.List.CustomType.Type, GetElementType(e.List.ElementType)) |
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.
This is okay as a placeholder for now, but we may need a way in the specification to ensure that developers can fully specify how this should be created as code since its not guaranteed that the custom type will be implemented with an ElemType
field. Similarly with object types.
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've created 2 issues for this:
- Consider providing a way to fully specify custom type (codegen-spec).
- Fully specify custom type (codegen-framework follow-up issue).
} | ||
return "types.NumberType" | ||
case e.Object != nil: | ||
return fmt.Sprintf("types.ObjectType{\nAttrTypes: map[string]attr.Type{\n%s\n},\n}", GetAttrTypes(e.Object)) |
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.
Should this also be implementing e.Object.CustomType
logic similar to the collection types?
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.
e.Object
is a []ObjectAttributeType
but there's no e.Object.CustomType
available.
Perhaps this needs to be added to the spec?
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.
Ah ha, yes it would 👍
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've added an issue for Add CustomType to the Specification Go Bindings.
internal/schema/schema.go
Outdated
switch { | ||
case v.Bool != nil: | ||
if v.Bool.CustomType != nil { | ||
aTypes.WriteString(fmt.Sprintf("\"%s\": %s,", v.Name, v.Bool.CustomType.Type)) |
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.
Nit: These can use %q
to remove the \"%s\"
handling. It may also be worth moving the v.Name
writing logic above the switch since branches require the logic.
aTypes.WriteString(fmt.Sprintf("%q: ", v.Name))
switch {
case v.Bool != nil:
if v.Bool.CustomType != nil {
aTypes.WriteString(v.Bool.CustomType.Type)
} else {
aTypes.WriteString("types.BoolType")
}
// ...
}
aTypes.WriteString(",")
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.
(We could also consider making a "schema definition string" method on each of the types so calling logic doesn't need to handle the implementation details like the switch statement here 😄 )
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've added an issue to Consider adding schema definition string method on types.
) | ||
|
||
type GeneratorListAttribute struct { | ||
schema.ListAttribute | ||
|
||
CustomType *specschema.CustomType | ||
Validators []specschema.ListValidator | ||
CustomType *specschema.CustomType |
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.
Nit: It might be good to throw a quick comment in these generator types about why we're preferring the "specschema" types here, rather than using the "real" schema types -- e.g. because we need to support extracting custom import information later. 👍
} | ||
|
||
for a := range imports { | ||
s.WriteString(fmt.Sprintf("\"%s\"\n", a)) |
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.
Nit: 😎
s.WriteString(fmt.Sprintf("\"%s\"\n", a)) | |
s.WriteString(fmt.Sprintf("%q\n", a)) |
} | ||
|
||
for a := range imports { | ||
s.WriteString(fmt.Sprintf("\"%s\"\n", a)) |
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.
Nit: 😎
s.WriteString(fmt.Sprintf("\"%s\"\n", a)) | |
s.WriteString(fmt.Sprintf("%q\n", a)) |
@@ -198,3 +230,93 @@ func customTypeEqual(x, y *specschema.CustomType) bool { | |||
|
|||
return true | |||
} | |||
|
|||
func objectTypeEqual(x, y []specschema.ObjectAttributeType) bool { |
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.
Would you mind raising a feature request issue for consideration of this in terraform-plugin-codegen-spec? Thanks!
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've created an issue for Consider adding equality functions in the terraform-plugin-codegen-spec repo.
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.
Looks good to me 🚀
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. |
Within the terraform-plugin-codegen-spec repository the JSON schema and the Go bindings for the schema define the intermediate representation.
The schema and Go bindings allow for the usage of custom types in conjunction with element types within collections (i.e., list, map, set) and attribute types within objects.
The changes in this PR include the following: