-
Notifications
You must be signed in to change notification settings - Fork 54
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 YamlNamingStrategy #400
Add YamlNamingStrategy #400
Conversation
6165cdc
to
ca673fb
Compare
Thank you for the PR @russellbanks! |
any idea when this will be merged? |
This is currently an incomplete draft - it'll be merged when it's ready :) If you'd like to see it merged sooner, I'm sure @russellbanks would love some help. |
It's the linking up of the algorithms to the serial names that would be helpful to have some pointers for :) |
This is where property names are emitted - so that should be the place to perform the transformation. |
f163627
to
566a6a2
Compare
@charleskorn This is ready for review I think! One thing that I should note is that applying a naming strategy will ignore the casing of all |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for putting this together @russellbanks! Overall it's looking good.
@charleskorn I've made the requested changes! Let me know if there's anything else. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking great, thanks again for this @russellbanks.
I've just noticed one more case that is missing a test: could you please add a test for the case where an unknown property is included in the YAML document being read? I want to make sure the exception includes the transformed names (from the naming strategy) rather than the property in the descriptor.
Well thought @charleskorn! I've been able to map that but this test case fails and I'm not sure how to map the YamlMap's entries. context("given some input representing an object with an unknown key, using a naming strategy") {
val input = "oneTwoThree: something".trimIndent()
context("parsing that input") {
it("throws an exception with the naming strategy applied") {
val exception = shouldThrow<UnknownPropertyException> {
Yaml(
configuration = YamlConfiguration(yamlNamingStrategy = YamlNamingStrategy.SnakeCase)
).decodeFromString(ComplexStructure.serializer(), input)
}
exception.asClue {
it.message shouldBe "Unknown property 'one_two_three'. Known properties are: boolean, byte, char, double, enum, float, int, long, nullable, short, string"
it.line shouldBe 1
it.column shouldBe 1
it.propertyName shouldBe "one_two_three"
it.validPropertyNames shouldBe setOf("boolean", "byte", "char", "double", "enum", "float", "int", "long", "nullable", "short", "string")
it.path shouldBe YamlPath.root.withMapElementKey("one_two_three", Location(1, 1))
}
}
}
} This is the test case that fails it.path shouldBe YamlPath.root.withMapElementKey("one_two_three", Location(1, 1))
|
I think the test is not quite right - the exception should contain the properties as they appear / would appear in YAML. So this: it.path shouldBe YamlPath.root.withMapElementKey("one_two_three", Location(1, 1)) should be: it.path shouldBe YamlPath.root.withMapElementKey("oneTwoThree", Location(1, 1)) And the exception message should be something like |
aa0c5be
to
73c6af6
Compare
@charleskorn I've done that and I have also tested this PR by publishing to Maven Local and it works well in my project :) |
Thanks again for the PR @russellbanks! |
Adds YamlNamingStrategy from #398 with pre-defined snake_case, kebab-case, PascalCase, and camelCase.
The SnakeCase implementation has been taken and modified from JsonNamingStrategy.kt in KotlinX Serialization.
Usage
Example data class:
Example Yaml:
This will successfully serialize and deserialize without the need for
@SerialName
on every value in the data class.Custom naming strategy
Converts to lowercase and capitalises first letter:
Converts to uppercase
Note
As
@SerialName
is resolved at compile time rather than runtime, using a YamlNamingStrategy will ignore the casing of all@SerialName
annotations.Closes #398