Skip to content

Commit

Permalink
Use indexing syntax with FieldBinding
Browse files Browse the repository at this point in the history
  • Loading branch information
Egor Andreevici authored and Egorand committed Apr 22, 2019
1 parent 6b7ab72 commit efe3d1d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal class MessageTypeAdapter<M : Message<M, B>, B : Message.Builder<M, B>>(

out.beginObject()
for (tagBinding in messageAdapter.fieldBindings.values) {
val value = tagBinding.get(message) ?: continue
val value = tagBinding[message] ?: continue
out.name(tagBinding.name)
emitJson(out, value, tagBinding.singleAdapter(), tagBinding.label)
}
Expand Down Expand Up @@ -102,7 +102,7 @@ internal class MessageTypeAdapter<M : Message<M, B>, B : Message.Builder<M, B>>(
} else {
val element = elementAdapter.read(input)
val value = parseValue(fieldBinding, element)
fieldBinding.set(builder, value)
fieldBinding[builder] = value
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal class MessageJsonAdapter<M : Message<M, B>, B : Message.Builder<M, B>>(
out.beginObject()
fieldBindings.forEachIndexed { index, fieldBinding ->
out.name(fieldBinding.name)
val value = fieldBinding.get(message)
val value = fieldBinding[message]
jsonAdapters[index]?.toJson(out, value)
}
out.endObject()
Expand Down Expand Up @@ -87,7 +87,7 @@ internal class MessageJsonAdapter<M : Message<M, B>, B : Message.Builder<M, B>>(
// If the value was explicitly null we ignore it rather than forcing null into the field.
// Otherwise malformed JSON that sets a list to null will create a malformed message, and
// we'd rather just ignore that problem.
fieldBinding.set(builder, value)
fieldBinding[builder] = value
}
input.endObject()
return builder.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class RuntimeMessageAdapter<M : Message<M, B>, B : Builder<M, B>>(

var size = 0
for (fieldBinding in fieldBindings.values) {
val binding = fieldBinding.get(value) ?: continue
val binding = fieldBinding[value] ?: continue
size += fieldBinding.adapter().encodedSizeWithTag(fieldBinding.tag, binding)
}
size += value.unknownFields().size
Expand All @@ -51,7 +51,7 @@ class RuntimeMessageAdapter<M : Message<M, B>, B : Builder<M, B>>(
@Throws(IOException::class)
override fun encode(writer: ProtoWriter, value: M) {
for (fieldBinding in fieldBindings.values) {
val binding = fieldBinding.get(value) ?: continue
val binding = fieldBinding[value] ?: continue
fieldBinding.adapter().encodeWithTag(writer, fieldBinding.tag, binding)
}
writer.writeBytes(value.unknownFields())
Expand All @@ -69,7 +69,7 @@ class RuntimeMessageAdapter<M : Message<M, B>, B : Builder<M, B>>(
val builderValue = fieldBinding.getFromBuilder(builder)
if (builderValue != null) {
val redactedValue = fieldBinding.adapter().redact(builderValue)
fieldBinding.set(builder, redactedValue!!)
fieldBinding[builder] = redactedValue!!
}
} else if (isMessage && fieldBinding.label.isRepeated) {
@Suppress("UNCHECKED_CAST")
Expand All @@ -91,7 +91,7 @@ class RuntimeMessageAdapter<M : Message<M, B>, B : Builder<M, B>>(

override fun toString(value: M): String = buildString {
for (fieldBinding in fieldBindings.values) {
val binding = fieldBinding.get(value)
val binding = fieldBinding[value]
if (binding != null) {
append(", ")
append(fieldBinding.name)
Expand Down

0 comments on commit efe3d1d

Please sign in to comment.