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

Override any type with string type like standard @Stringable #230

Closed
Chuckame opened this issue Jul 8, 2024 · 1 comment · Fixed by #236
Closed

Override any type with string type like standard @Stringable #230

Chuckame opened this issue Jul 8, 2024 · 1 comment · Fixed by #236
Labels
enhancement New feature or request

Comments

@Chuckame
Copy link
Contributor

Chuckame commented Jul 8, 2024

Is your feature request related to a problem? Please describe.
In the standard library we can override any type to a string when annotating it with @Stringable.

Describe the solution you'd like
The idea would be to allow defining any field to be string'ified using a similar annotation.

There is still technical limitation: kotlinx-serialization doesn't allow a data class to be serialized as a string (or any other scalar type) as it uses beginStructure and other more complex handling. To allow using encodeString from a standard data class, the user have to provide a custom KSerializer of the given data class, declare its descriptor as a PrimitiveKind.STRING and then call encodeString and decodeString according to the desired format.

Finally, there will be 2 ways to set something to a string:

  • using the new AvroScalarString annotation to change the inferred type to a string only for properties. Note that no logical type would be applied just using the annotation. If the annotated property is not compatible, the generated schema will remain to the inferred one to avoid runtime errors.
  • using a custom serializer where its descriptor is declared as PrimitiveSerialDescriptor("type name", STRING), and where its encode and decode methods handles the string type.
    • For more complex usage, like allowing a serializer to encode in fixed, string, or something else depending on the annotations, the user have to declare the descriptor as PrimitiveSerialDescriptor("type name", STRING).withSchema { /* handle here the different annotations */ }
@Chuckame Chuckame added the enhancement New feature or request label Jul 8, 2024
@Chuckame Chuckame linked a pull request Jul 11, 2024 that will close this issue
@Chuckame
Copy link
Contributor Author

Released in v2.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant