Add getExtensionOrNull utility for protobuf messages

This commit is contained in:
Alexander Udalov
2018-02-07 15:31:21 +01:00
parent 65a6171782
commit 7123202670
11 changed files with 51 additions and 55 deletions
@@ -90,8 +90,7 @@ class AnnotationAndConstantLoaderImpl(
proto: ProtoBuf.Property,
expectedType: KotlinType
): ConstantValue<*>? {
if (!proto.hasExtension(protocol.compileTimeValue)) return null
val value = proto.getExtension(protocol.compileTimeValue)
val value = proto.getExtensionOrNull(protocol.compileTimeValue) ?: return null
return deserializer.resolveValue(expectedType, value, container.nameResolver)
}
}
@@ -0,0 +1,12 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.serialization.deserialization
import org.jetbrains.kotlin.protobuf.GeneratedMessageLite
fun <M : GeneratedMessageLite.ExtendableMessage<M>, T> GeneratedMessageLite.ExtendableMessage<M>.getExtensionOrNull(
extension: GeneratedMessageLite.GeneratedExtension<M, T>
): T? = if (hasExtension(extension)) getExtension(extension) else null