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

Support java-class stringable property #724

Merged
merged 3 commits into from
Apr 19, 2024
Merged

Conversation

RustedBones
Copy link

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

Copy link

codecov bot commented Mar 18, 2024

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
Copy link
Contributor

benkonz commented Apr 5, 2024

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

@benkonz
Copy link
Contributor

benkonz commented Apr 9, 2024

@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.

@@ -95,18 +103,30 @@ class AvroGeneratorTest extends AnyFlatSpec with Matchers with ScalaCheckPropert
.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
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"}

@@ -95,18 +103,30 @@ class AvroGeneratorTest extends AnyFlatSpec with Matchers with ScalaCheckPropert
.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()
.name("stringableDefaultField").`type`().stringBuilder().prop(SpecificData.CLASS_PROP, classOf[StringableCustom].getName).endString().noDefault()
Copy link
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("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
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
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

Copy link
Contributor

@benkonz benkonz left a comment

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
1 check passed
@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