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

feat: add BigDecimal default value handling #720

Merged
merged 2 commits into from
Jul 17, 2024

Conversation

dwilkolek
Copy link
Contributor

Closes #712

Copy link
Contributor

@Emily Emily left a comment

Choose a reason for hiding this comment

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

Just a few small things! Thank you for putting this together.

private fun checkAndGetLocaleValue(value: StringValue, type: JavaTypeName): String? {
if (type.toString() == "java.util.Locale") return "Locale.forLanguageTag(\"${value.value}\")"
return null
private fun checkAndGetLocaleCodeBlock(value: Value<out Value<*>>, type: JavaTypeName): CodeBlock? {
Copy link
Contributor

Choose a reason for hiding this comment

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

This change seems mostly unrelated. I assume it prevents a crash in the event a non-string is passed in? Would be good to add a test case for.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, assuming it does prevent a crash, perhaps rather than returning null it'd be better to throw some sort of exception with a friendly error message. If someone has set someLocale: Locale = 123 in their schema, it's probably best we let them know rather than just silently not do anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think you're correct. Will do the changes.

is StringValue -> CodeBlock.of("new java.math.BigDecimal(\$S)", value.value)
is IntValue -> CodeBlock.of("new java.math.BigDecimal(\$L)", value.value)
is FloatValue -> CodeBlock.of("new java.math.BigDecimal(\$L)", value.value)
else -> null
Copy link
Contributor

Choose a reason for hiding this comment

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

Same thought here around throwing an exception rather than silently defaulting to null.

is IntValue -> CodeBlock.of("%L", value.value)
is StringValue -> {
val localeValueOverride = checkAndGetLocaleCodeBlock(value, type)
if (localeValueOverride != null) CodeBlock.of("%L", localeValueOverride)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would think this would need to change, as you changed the above method to return a CodeBlock.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh actually looks like you probably can get rid of most of this and just simplify to
is StringValue -> CodeBlock.of("%S", value.value)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, missed that one. 👍

private fun checkAndGetLocaleCodeBlock(value: Value<Value<*>>, type: TypeName): CodeBlock? {
return if (value is StringValue && type.className.canonicalName == "java.util.Locale") {
CodeBlock.of("%L", "Locale.forLanguageTag(\"${value.value}\")")
} else null
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as the java version around throwing exception.

is StringValue -> CodeBlock.of("%L", "java.math.BigDecimal(\"${value.value}\")")
is IntValue -> CodeBlock.of("%L", "java.math.BigDecimal(${value.value})")
is FloatValue -> CodeBlock.of("%L", "java.math.BigDecimal(${value.value})")
else -> null
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as the java version around throwing exception.

@@ -4832,4 +4832,36 @@ It takes a title and such.
}
assertThat(exception.message).isEqualTo("Property \"firstname\" does not exist in input type \"Person\"")
}

@Test
fun `The default value for BigDecimal should be overridden`() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for adding tests! Would you mind adding a case that would exercise the null (or exception if you make that change) route?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I'll add tests to cover handling of incompatible values 😉

@dwilkolek dwilkolek requested a review from Emily July 17, 2024 18:41
@Emily Emily merged commit ee1a27a into Netflix:master Jul 17, 2024
2 checks passed
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.

No way to provide meaningful defaults for scalar type mapping to a specific java type in inputs
3 participants