Skip to content

Commit 78b9515

Browse files
authored
wrapper for localdate from stringvalue (#85)
1 parent 57544bd commit 78b9515

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2020 Toast Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package com.toasttab.protokt.ext
17+
18+
import com.google.auto.service.AutoService
19+
import com.toasttab.protokt.StringValue
20+
import java.time.LocalDate
21+
22+
@AutoService(Converter::class)
23+
object LocalDateStringValueConverter : Converter<LocalDate, StringValue> {
24+
override val wrapper = LocalDate::class
25+
26+
override val wrapped = StringValue::class
27+
28+
override fun wrap(unwrapped: StringValue) =
29+
LocalDateConverter.wrap(unwrapped.value)
30+
31+
override fun unwrap(wrapped: LocalDate) =
32+
StringValue { value = LocalDateConverter.unwrap(wrapped) }
33+
}

testing/options/src/main/proto/com/toasttab/protokt/testing/options/wrapper_types.proto

+16
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ message Wrappers {
5858
string local_date = 8 [
5959
(.protokt.property).wrap = "java.time.LocalDate"
6060
];
61+
62+
google.protobuf.BytesValue nullable_uuid = 9 [
63+
(.protokt.property).wrap = "java.util.UUID"
64+
];
65+
66+
google.protobuf.StringValue nullable_local_date = 10 [
67+
(.protokt.property).wrap = "java.time.LocalDate"
68+
];
6169
}
6270

6371
message OneofWrappers {
@@ -95,6 +103,14 @@ message OneofWrappers {
95103
string local_date_oneof = 8 [
96104
(.protokt.property).wrap = "java.time.LocalDate"
97105
];
106+
107+
google.protobuf.BytesValue nullable_uuid_oneof = 9 [
108+
(.protokt.property).wrap = "java.util.UUID"
109+
];
110+
111+
google.protobuf.StringValue nullable_local_date_oneof = 10 [
112+
(.protokt.property).wrap = "java.time.LocalDate"
113+
];
98114
}
99115
}
100116

testing/options/src/test/kotlin/com/toasttab/protokt/testing/options/WrapperTypesTest.kt

+32
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class WrapperTypesTest {
3737
instant = Instant.now()
3838
duration = Duration.ofSeconds(5)
3939
localDate = LocalDate.of(1950, 10, 4)
40+
nullableUuid = UUID.randomUUID()
41+
nullableLocalDate = LocalDate.of(1950, 10, 4)
4042
}
4143

4244
@Test
@@ -93,6 +95,36 @@ class WrapperTypesTest {
9395
assertThat(deserialized.localDate).isEqualTo(model.localDate)
9496
}
9597

98+
@Test
99+
fun `round trip should preserve nullable uuid`() {
100+
val deserialized = Wrappers.deserialize(model.serialize())
101+
102+
assertThat(deserialized.nullableUuid).isEqualTo(model.nullableUuid)
103+
}
104+
105+
@Test
106+
fun `round trip should preserve nullable uuid when null`() {
107+
val deserialized =
108+
Wrappers.deserialize(model.copy { nullableUuid = null }.serialize())
109+
110+
assertThat(deserialized.nullableUuid).isNull()
111+
}
112+
113+
@Test
114+
fun `round trip should preserve nullable localdate`() {
115+
val deserialized = Wrappers.deserialize(model.serialize())
116+
117+
assertThat(deserialized.nullableLocalDate).isEqualTo(model.nullableLocalDate)
118+
}
119+
120+
@Test
121+
fun `round trip should preserve nullable localdate when null`() {
122+
val deserialized =
123+
Wrappers.deserialize(model.copy { nullableLocalDate = null }.serialize())
124+
125+
assertThat(deserialized.nullableLocalDate).isNull()
126+
}
127+
96128
@Test
97129
fun `round trip should preserve id oneof`() {
98130
val deserialized = OneofWrappers.deserialize(

0 commit comments

Comments
 (0)