-
Notifications
You must be signed in to change notification settings - Fork 95
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
gen: Allow cutom go types via annotations #462
Comments
This is an interesting concept. I wonder if any other Go lib has implemented this, to compare the proposed annotations against. I also wonder if the number of annotations could be reduced to just |
I'm not aware of other libs doing type annotation for Avro. However I did not come up with it. I just took from the example for Java in the Avro specs, and adapted it for Go. Regarding reducing the number of annotations, you can in most cases but it has some caveats which requires those extra ones for edge cases. According to the Go spec, an import path can be any character. Thus it is a bit complicated to separate the import path from the rest without coming up with a mini markup language. I find it easier to have different annotations. However a compiler "may also exclude the characters This would look like: an optional
This should work in most cases. However if for any reason, the package name is not the suffix of the import path, if we need to use an import alias (for example if we use data types from two "avro" libs) or any other weird edge case which may come up; we need to be able to specify everything (import path, pkg name, type, alias, ptr) explicitly. |
The schema proposed for |
Somewhat similar to #429 but more flexible and with keeping type annotations within the Avro schema, it would be great to generate a Go struct with custom types using annotations.
Specifically: add the ability to support specific annotations (
go-type
,go-key-type
) similar to Java's but for go. The Go types would be expected to implementencoding.TextMarshaler
/encoding.TextUnmarshaler
(taking advantage of #68 and #327).Example
Schema
Which results in the following schema
Gen cmd
Actual Output
Desired Output
Notes
Potential pain points are:
go-type
annotation.For complex cases, extra annotations should be considered... SQLC which also allows go type override handles this decently. Taking inspiration from their doc, this would define:
go-type-import
: Import path of the package.go-type-pkg
: Package name if it doesn't match the import path.go-type-name
: The actual Go type namego-type-ptr
: Whether to use a pointer or the type directly.go-key-type-import
,go-key-type-pkg
,go-key-type-name
,go-key-type-ptr
annotations.goimports
.@go-type("github.com/google/btree.BTreeG[int]") array<string> totals;
) is a bit more tricky as it is overwriting the array type. Marshaling to and from that type is not supported as it is not a string type supportingencoding.TextMarshaler
/encoding.TextUnmarshaler
. Overriding array (and map!) types be ignored (potentially with a warning/error) until marshaling can be handled for array, map or even arbitrary types.The text was updated successfully, but these errors were encountered: