-
Notifications
You must be signed in to change notification settings - Fork 98
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 logical types #448
base: main
Are you sure you want to change the base?
Conversation
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.
Sorry for the delay. This is a rather large change to how schemas function, and in no way can it be turned off for people wanting spec behaviour. I am wondering if there is another way that has less impact on the "normal" behaviour of the package.
ltyp LogicalType | ||
} | ||
|
||
var customLogicalSchemas sync.Map // map[customSchemaKey]*CustomLogicalSchema |
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 being global is an issue. I should be possible to parse with and without the custom logical types.
@@ -592,6 +673,11 @@ func (s *RecordSchema) Type() Type { | |||
return Record | |||
} | |||
|
|||
// Logical returns the logical schema or nil. | |||
func (s *RecordSchema) Logical() LogicalSchema { |
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 means that every schema now implements LogicalTypeSchema
. From a spec PoV this is really confusing.
@@ -622,6 +708,12 @@ func (s *RecordSchema) String() string { | |||
fields = fields[:len(fields)-1] | |||
} | |||
|
|||
if s.logical != nil { | |||
return fmt.Sprintf("{\"name\":\"%s\", \"type\":\"%s\", \"fields\":[%s]\", %s}", |
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.
fmt.Sprintf
is slow and includes many allocations. Given this function is called often, this is a performance bottleneck.
Doc string `mapstructure:"doc"` | ||
Default any `mapstructure:"default"` | ||
Order Order `mapstructure:"order"` | ||
LogicalType string `mapstructure:"logicalType"` |
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.
A field for sure should not have a logical type, as it is not a type.
Hi @nrwiersma Thank you for the feedback! I totally get what you are saying, there ought to be a more elegant approach. Do you have another approach in mind? |
I am toying with the idea of adding something along the lines of Parser Hooks, which would allow new types to be derived from the Avro Schema types, but extended to any specific need. My hope is that the hooks would allow arb parsing of parameters into a Schema structure without having to actually change the underlying types. The thought is still a work in progress though. |
closes #435
Hi @nrwiersma
This PR adds the possibility to register custom logical types for the schema. These logicaltypes are primitive, and does not include logic for handling custom encoding/decoding, but simply adds the the custom logicalType to be referenced in the schemas.
I have added the
WithCustomLogicalType
function as a SchemaOption, but not at all schemas use this option - I saw this pattern was already used for other schema options.Let me know what you think :)