Skip to content

Commit 78bf4f0

Browse files
authored
Utility to convert type proto to field type (if possible). (#4814)
1 parent e587539 commit 78bf4f0

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

java/arcs/core/data/proto/TypeProtoDecoders.kt

+24-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111

1212
package arcs.core.data.proto
1313

14+
import arcs.core.data.FieldType
1415
import arcs.core.data.PrimitiveType
1516

1617
/**
17-
* Converts a [PrimitiveTypeProto] protobuf instance into a native kotlin [PrimitiveType] instance.
18+
* Converts a [PrimitiveTypeProto] protobuf instance into a Kotlin [PrimitiveType] instance.
1819
*/
1920
fun PrimitiveTypeProto.decode(): PrimitiveType =
2021
when (this) {
@@ -24,3 +25,25 @@ fun PrimitiveTypeProto.decode(): PrimitiveType =
2425
PrimitiveTypeProto.UNRECOGNIZED ->
2526
throw IllegalArgumentException("Unknown PrimitiveTypeProto value.")
2627
}
28+
29+
/**
30+
* Converts a [PrimitiveTypeProto] protobuf instance into a Kotlin [FieldType] instance.
31+
*/
32+
fun PrimitiveTypeProto.decodeAsFieldType(): FieldType.Primitive = FieldType.Primitive(decode())
33+
34+
/**
35+
* Converts a [TypeProto] protobuf instance into a Kotlin [FieldType] instance.
36+
*
37+
* @throws [IllegalArgumentexception] if the type cannot be converted to [FieldType].
38+
*/
39+
fun TypeProto.decodeAsFieldType(): FieldType =
40+
when (getDataCase()) {
41+
TypeProto.DataCase.PRIMITIVE -> getPrimitive().decodeAsFieldType()
42+
// TODO: Handle FieldType.EntityRef. It is not clear how it is
43+
// represented in the proto.
44+
TypeProto.DataCase.DATA_NOT_SET ->
45+
throw IllegalArgumentException("Unknown data field in TypeProto.")
46+
else ->
47+
throw IllegalArgumentException(
48+
"Cannot decode a ${getDataCase().name} type to a [FieldType].")
49+
}

javatests/arcs/core/data/proto/TypeProtoDecodersTest.kt

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package arcs.core.data.proto
22

3-
import arcs.core.data.PrimitiveType
3+
import arcs.core.data.*
44
import arcs.core.testutil.assertThrows
5+
import arcs.core.testutil.fail
56
import arcs.repoutils.runfilesDir
67
import com.google.common.truth.Truth.assertThat
78
import com.google.protobuf.Message.Builder
@@ -12,6 +13,15 @@ import org.junit.Test
1213
import org.junit.runner.RunWith
1314
import org.junit.runners.JUnit4
1415

16+
/**
17+
* Parses a given proto text as [TypeProto].
18+
*/
19+
fun parseTypeProtoText(protoText: String): TypeProto {
20+
val builder = TypeProto.newBuilder()
21+
TextFormat.getParser().merge(protoText, builder)
22+
return builder.build()
23+
}
24+
1525
@RunWith(JUnit4::class)
1626
class TypeProtoDecodersTest {
1727
@Test
@@ -23,4 +33,33 @@ class TypeProtoDecodersTest {
2333
PrimitiveTypeProto.UNRECOGNIZED.decode()
2434
}
2535
}
36+
37+
@Test
38+
fun decodesPrimitiveTypeAsFieldType() {
39+
val textField = PrimitiveTypeProto.TEXT.decodeAsFieldType()
40+
assertThat(textField.primitiveType).isEqualTo(PrimitiveType.Text)
41+
val numberField = PrimitiveTypeProto.NUMBER.decodeAsFieldType()
42+
assertThat(numberField.primitiveType).isEqualTo(PrimitiveType.Number)
43+
val booleanField = PrimitiveTypeProto.BOOLEAN.decodeAsFieldType()
44+
assertThat(booleanField.primitiveType).isEqualTo(PrimitiveType.Boolean)
45+
}
46+
47+
@Test
48+
fun decodesTypeProtoAsFieldType() {
49+
fun checkPrimitive(textProto: String, expected: PrimitiveType) {
50+
val primitiveTypeProto = parseTypeProtoText(textProto)
51+
val field = primitiveTypeProto.decodeAsFieldType()
52+
when (field) {
53+
is FieldType.Primitive ->
54+
assertThat(field.primitiveType).isEqualTo(expected)
55+
else -> fail("TypeProto should have been decoded to [FieldType.Primitive].")
56+
}
57+
}
58+
checkPrimitive("primitive: TEXT", PrimitiveType.Text)
59+
checkPrimitive("primitive: BOOLEAN", PrimitiveType.Boolean)
60+
checkPrimitive("primitive: NUMBER", PrimitiveType.Number)
61+
assertThrows(IllegalArgumentException::class) {
62+
checkPrimitive("""variable: { name: "Blah"}""", PrimitiveType.Text)
63+
}
64+
}
2665
}

0 commit comments

Comments
 (0)