Skip to content

Support java-class stringable property#724

Merged
RustedBones merged 3 commits into
masterfrom
java-class-stringable
Apr 19, 2024
Merged

Support java-class stringable property#724
RustedBones merged 3 commits into
masterfrom
java-class-stringable

Conversation

@RustedBones

Copy link
Copy Markdown

Attempt to support java-class and java-key-class as defined in the avro specification

java-element-class is only used in the reflect API

@codecov

codecov Bot commented Mar 18, 2024

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 69.76744% with 13 lines in your changes are missing coverage. Please review.

Project coverage is 70.61%. Comparing base (ce4ce4d) to head (242e244).

Files Patch % Lines
...om/spotify/ratatool/scalacheck/AvroGenerator.scala 71.42% 12 Missing ⚠️
...spotify/ratatool/scalacheck/HashMapBuildable.scala 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #724      +/-   ##
==========================================
- Coverage   70.74%   70.61%   -0.13%     
==========================================
  Files          44       44              
  Lines        1839     1865      +26     
  Branches      317      317              
==========================================
+ Hits         1301     1317      +16     
- Misses        538      548      +10     
Flag Coverage Δ
ratatoolCli 2.87% <ø> (ø)
ratatoolCommon 0.00% <ø> (ø)
ratatoolDiffy 32.17% <34.88%> (-0.34%) ⬇️
ratatoolExamples 17.14% <32.55%> (-0.09%) ⬇️
ratatoolSampling 60.63% <32.55%> (-1.16%) ⬇️
ratatoolScalacheck 77.94% <67.44%> (-1.40%) ⬇️
ratatoolShapeless 4.14% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@RustedBones
RustedBones marked this pull request as ready for review March 19, 2024 09:00
@benkonz

benkonz commented Apr 5, 2024

Copy link
Copy Markdown
Contributor

@RustedBones I'll take a look on Monday, thanks for opening!

@benkonz

benkonz commented Apr 9, 2024

Copy link
Copy Markdown
Contributor

@RustedBones the tests make sense, although I don't fully understand all of the details. Maybe you could explain a little more in detail about what your change fixes? Other than that, I don't have a problem just merging.

.name("javaStringField").`type`().stringBuilder().prop(GenericData.STRING_PROP, "String").endString().noDefault()
.name("charSequenceField").`type`().stringBuilder().prop(GenericData.STRING_PROP, "CharSequence").endString().noDefault()
.name("utf8Field").`type`().stringBuilder().prop(GenericData.STRING_PROP, "CharSequence").endString().noDefault()
.name("stringableBigDecimalField").`type`().stringBuilder().prop(SpecificData.CLASS_PROP, classOf[java.math.BigDecimal].getName).endString().noDefault()

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avsc equivalent for

{"type": "string", "java-class": "java.math.BigDecimal"}

.name("charSequenceField").`type`().stringBuilder().prop(GenericData.STRING_PROP, "CharSequence").endString().noDefault()
.name("utf8Field").`type`().stringBuilder().prop(GenericData.STRING_PROP, "CharSequence").endString().noDefault()
.name("stringableBigDecimalField").`type`().stringBuilder().prop(SpecificData.CLASS_PROP, classOf[java.math.BigDecimal].getName).endString().noDefault()
.name("stringableDefaultField").`type`().stringBuilder().prop(SpecificData.CLASS_PROP, classOf[StringableCustom].getName).endString().noDefault()

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avsc equivalent for

{"type": "string", "java-class": "com.spotify.ratatool.scalacheck.StringableCustom"}

Called default because no custom stringableGen passed for this class

.name("stringableDefaultField").`type`().stringBuilder().prop(SpecificData.CLASS_PROP, classOf[StringableCustom].getName).endString().noDefault()
.name("mapDefaultKeyField").`type`().map().values().longType().noDefault()
.name("mapJavaStringKeyField").`type`().map().prop(GenericData.STRING_PROP, "String").values().longType().noDefault()
.name("mapStringableBigIntegerKeyField").`type`().map().prop(SpecificData.KEY_CLASS_PROP, classOf[java.math.BigInteger].getName).values().longType().noDefault()

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avsc equivalent for

{"type": "string", "java-class": "java.math.BigInteger"}

Comment on lines +115 to +121
val stringableGens = Map[Class[_], Gen[_]](
classOf[java.math.BigDecimal] -> Gen.choose(BigDecimal(-1), BigDecimal(1)).map(_.bigDecimal),
classOf[java.math.BigInteger] -> Gen
.choose(BigInt(0), BigInt(Long.MaxValue))
.map(_.bigInteger)
)
val gen = avroOf(schema, stringableGens)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without those custom stringableGens, random strings would be generated to create BigDecimal and BigInteger that most likely creates a parse exception.
Here we give custom generators for those types and bypass the conversion from string.

The StringableCustom type not being part of the map will be constructed with random string

@benkonz benkonz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for helping me understand these changes, LGTM!

@RustedBones
RustedBones merged commit e9c3902 into master Apr 19, 2024
@RustedBones
RustedBones deleted the java-class-stringable branch April 19, 2024 07:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants