-
Notifications
You must be signed in to change notification settings - Fork 572
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1877 from square/jwilson.0105.boxed_oneofs_json
Implement JSON for boxed oneofs.
- Loading branch information
Showing
11 changed files
with
248 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...library/wire-runtime/src/jvmMain/kotlin/com/squareup/wire/internal/FieldOrOneOfBinding.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2021 Square Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.squareup.wire.internal | ||
|
||
import com.squareup.wire.Message | ||
import com.squareup.wire.ProtoAdapter | ||
import com.squareup.wire.Syntax | ||
import com.squareup.wire.WireField | ||
|
||
interface FieldOrOneOfBinding<M : Message<M, B>, B : Message.Builder<M, B>> { | ||
val tag: Int | ||
|
||
val label: WireField.Label | ||
|
||
val redacted: Boolean | ||
|
||
val isMap: Boolean | ||
|
||
/** | ||
* The name of the field in generated code. If the declared name is a keyword like `fun`, this | ||
* will be a transformed name like `fun_`. | ||
*/ | ||
val name: String | ||
|
||
/** | ||
* The name of the field as declared in the `.proto` file. | ||
*/ | ||
val declaredName: String | ||
|
||
/** | ||
* The JSON name as determined at code-generation name. This is usually camelCase even if the | ||
* field is declared in snake_case. | ||
*/ | ||
val wireFieldJsonName: String | ||
|
||
fun keyAdapter(): ProtoAdapter<*> | ||
|
||
fun adapter(): ProtoAdapter<Any> | ||
|
||
fun value(builder: B, value: Any) | ||
|
||
fun set(builder: B, value: Any?) | ||
|
||
operator fun get(message: M): Any? | ||
|
||
fun getFromBuilder(builder: B): Any? | ||
|
||
fun singleAdapter(): ProtoAdapter<*> | ||
|
||
fun omitFromJson(syntax: Syntax, value: Any?): Boolean | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
wire-library/wire-runtime/src/jvmMain/kotlin/com/squareup/wire/internal/OneOfBinding.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2021 Square Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.squareup.wire.internal | ||
|
||
import com.squareup.wire.Message | ||
import com.squareup.wire.OneOf | ||
import com.squareup.wire.ProtoAdapter | ||
import com.squareup.wire.Syntax | ||
import com.squareup.wire.WireField | ||
import java.lang.reflect.Field | ||
|
||
internal class OneOfBinding<M : Message<M, B>, B : Message.Builder<M, B>> internal constructor( | ||
private val messageField: Field, | ||
builderType: Class<B>, | ||
private val key: OneOf.Key<*> | ||
) : FieldOrOneOfBinding<M, B> { | ||
private val builderField: Field = builderType.getDeclaredField(messageField.name) | ||
|
||
override val tag: Int | ||
get() = key.tag | ||
|
||
override val label: WireField.Label | ||
get() = WireField.Label.OPTIONAL | ||
|
||
override val redacted: Boolean | ||
get() = key.redacted | ||
|
||
override val wireFieldJsonName: String | ||
get() = key.jsonName | ||
|
||
override val name: String | ||
get() = key.declaredName | ||
|
||
override val declaredName: String | ||
get() = key.declaredName | ||
|
||
override val isMap: Boolean | ||
get() = false | ||
|
||
override fun keyAdapter(): ProtoAdapter<*> { | ||
error("not a map") | ||
} | ||
|
||
override fun adapter(): ProtoAdapter<Any> { | ||
return key.adapter as ProtoAdapter<Any> | ||
} | ||
|
||
override fun value(builder: B, value: Any) { | ||
set(builder, value) | ||
} | ||
|
||
override fun set(builder: B, value: Any?) { | ||
builderField.set(builder, OneOf(key as OneOf.Key<Any>, value!!)) | ||
} | ||
|
||
override fun get(message: M): Any? { | ||
val oneOfOrNull = messageField.get(message) as OneOf<*, *>? | ||
return oneOfOrNull?.getOrNull(key) | ||
} | ||
|
||
override fun getFromBuilder(builder: B): Any? { | ||
val oneOfOrNull = builderField.get(builder) as OneOf<*, *>? | ||
return oneOfOrNull?.getOrNull(key) | ||
} | ||
|
||
override fun singleAdapter(): ProtoAdapter<*> { | ||
return adapter() | ||
} | ||
|
||
override fun omitFromJson(syntax: Syntax, value: Any?): Boolean { | ||
return value == null | ||
} | ||
} |
Oops, something went wrong.