Skip to content
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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

jwtryg
Copy link

@jwtryg jwtryg commented Sep 10, 2024

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 :)

Copy link
Member

@nrwiersma nrwiersma left a 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
Copy link
Member

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 {
Copy link
Member

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}",
Copy link
Member

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"`
Copy link
Member

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.

@jwtryg
Copy link
Author

jwtryg commented Oct 10, 2024

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?

@nrwiersma
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

logicalType - keep property
2 participants