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

fix(serde): nested form-url struct with list or map serialization #407

Merged
merged 2 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,13 @@ private class FormUrlStructSerializer(
}

override fun listField(descriptor: SdkFieldDescriptor, block: ListSerializer.() -> Unit) {
FormUrlListSerializer(parent, descriptor).apply(block)
val childDescriptor = descriptor.copyWithNewSerialName("${prefix}${descriptor.serialName}")
FormUrlListSerializer(parent, childDescriptor).apply(block)
}

override fun mapField(descriptor: SdkFieldDescriptor, block: MapSerializer.() -> Unit) {
FormUrlMapSerializer(parent, descriptor).apply(block)
val childDescriptor = descriptor.copyWithNewSerialName("${prefix}${descriptor.serialName}")
FormUrlMapSerializer(parent, childDescriptor).apply(block)
}

override fun rawField(descriptor: SdkFieldDescriptor, value: String) = writeField(descriptor) {
Expand Down Expand Up @@ -321,3 +323,9 @@ private fun QueryLiteral.toDescriptor(): SdkFieldDescriptor = SdkFieldDescriptor

private val SdkFieldDescriptor.serialName: String
get() = expectTrait<FormUrlSerialName>().name

private fun SdkFieldDescriptor.copyWithNewSerialName(newName: String): SdkFieldDescriptor {
val newTraits = traits.filterNot { it is FormUrlSerialName }.toMutableSet()
newTraits.add(FormUrlSerialName(newName))
return SdkFieldDescriptor(kind, newTraits)
}
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,87 @@ class FormUrlSerializerTest {
assertEquals(expected, actual)
}

data class NestedStruct(
val mapInput: Map<String, String>? = null,
val listInput: List<String>? = null
) : SdkSerializable {
override fun serialize(serializer: Serializer) {
val listDescriptor = SdkFieldDescriptor(SerialKind.Map, FormUrlSerialName("ListArg"))
val mapDescriptor = SdkFieldDescriptor(SerialKind.Map, FormUrlSerialName("MapArg"))
val objectDescriptor = SdkObjectDescriptor.build {
trait(FormUrlSerialName("NestedStruct"))
field(listDescriptor)
field(mapDescriptor)
}
serializer.serializeStruct(objectDescriptor) {
if (listInput != null) {
listField(listDescriptor) {
listInput.forEach { serializeString(it) }
}
}

if (mapInput != null) {
mapField(mapDescriptor) {
mapInput.forEach { (key, value) ->
entry(key, value)
}
}
}
}
}
}

data class NestedStructureInput(val nested: NestedStruct) {
companion object {
val NESTED_DESCRIPTOR = SdkFieldDescriptor(SerialKind.Struct, FormUrlSerialName("Nested"))
val OBJ_DESCRIPTOR = SdkObjectDescriptor.build {
field(NESTED_DESCRIPTOR)
}
}

fun serialize(serializer: Serializer) {
serializer.serializeStruct(OBJ_DESCRIPTOR) {
field(NESTED_DESCRIPTOR, nested)
}
}
}

@Test
fun itSerializesNestedMaps() {
val input = NestedStructureInput(
nested = NestedStruct(mapInput = mapOf("k1" to "v1", "k2" to "v2"))
)

val expected = """
Nested.MapArg.entry.1.key=k1
&Nested.MapArg.entry.1.value=v1
&Nested.MapArg.entry.2.key=k2
&Nested.MapArg.entry.2.value=v2
""".trimIndent().replace("\n", "")

val serializer = FormUrlSerializer()
input.serialize(serializer)
val actual = serializer.toByteArray().decodeToString()
assertEquals(expected, actual)
}

@Test
fun itSerializesNestedLists() {
val input = NestedStructureInput(
nested = NestedStruct(listInput = listOf("v1", "v2"))
)

val expected = """
Nested.ListArg.member.1=v1
&Nested.ListArg.member.2=v2
""".trimIndent().replace("\n", "")

val serializer = FormUrlSerializer()
input.serialize(serializer)
val actual = serializer.toByteArray().decodeToString()
assertEquals(expected, actual)
}

@Test
fun itSerializesRenamedMaps() {
// map with xmlName key/value overrides
Expand Down