Skip to content

Commit 57544bd

Browse files
authored
add LocalDate wrapper (#84)
1 parent 4c2fa88 commit 57544bd

File tree

3 files changed

+109
-16
lines changed

3 files changed

+109
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 java.time.LocalDate
20+
21+
@AutoService(Converter::class)
22+
object LocalDateConverter : Converter<LocalDate, String> {
23+
override val wrapper = LocalDate::class
24+
25+
override val wrapped = String::class
26+
27+
override fun wrap(unwrapped: String): LocalDate =
28+
LocalDate.parse(unwrapped)
29+
30+
override fun unwrap(wrapped: LocalDate) =
31+
wrapped.toString()
32+
}

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

+15-3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ message Wrappers {
5454
(.protokt.property).wrap = "java.time.Duration",
5555
(.protokt.property).non_null = true
5656
];
57+
58+
string local_date = 8 [
59+
(.protokt.property).wrap = "java.time.LocalDate"
60+
];
5761
}
5862

5963
message OneofWrappers {
@@ -76,12 +80,20 @@ message OneofWrappers {
7680
(.protokt.property).wrap = "CachingId"
7781
];
7882

79-
google.protobuf.Timestamp instant_oneof = 5 [
83+
.protokt.ext.InetSocketAddress socket_address_oneof = 5 [
84+
(.protokt.property).wrap = "java.net.InetSocketAddress"
85+
];
86+
87+
google.protobuf.Timestamp instant_oneof = 6 [
8088
(.protokt.property).wrap = "java.time.Instant"
8189
];
8290

83-
.protokt.ext.InetSocketAddress socket_address_oneof = 6 [
84-
(.protokt.property).wrap = "java.net.InetSocketAddress"
91+
google.protobuf.Duration duration_oneof = 7 [
92+
(.protokt.property).wrap = "java.time.Duration"
93+
];
94+
95+
string local_date_oneof = 8 [
96+
(.protokt.property).wrap = "java.time.LocalDate"
8597
];
8698
}
8799
}

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

+62-13
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
package com.toasttab.protokt.testing.options
1717

1818
import com.google.common.truth.Truth.assertThat
19+
import com.toasttab.protokt.testing.options.OneofWrappers.WrappedOneof
1920
import java.net.InetAddress
2021
import java.net.InetSocketAddress
2122
import java.time.Duration
2223
import java.time.Instant
24+
import java.time.LocalDate
2325
import java.util.UUID
2426
import org.junit.jupiter.api.Test
2527
import org.junit.jupiter.api.assertThrows
@@ -34,6 +36,7 @@ class WrapperTypesTest {
3436
socketAddress = InetSocketAddress(InetAddress.getLocalHost(), 8080)
3537
instant = Instant.now()
3638
duration = Duration.ofSeconds(5)
39+
localDate = LocalDate.of(1950, 10, 4)
3740
}
3841

3942
@Test
@@ -84,70 +87,116 @@ class WrapperTypesTest {
8487
}
8588

8689
@Test
87-
fun `round trip should preserve generic wrapper oneof`() {
90+
fun `round trip should preserve localdate`() {
91+
val deserialized = Wrappers.deserialize(model.serialize())
92+
93+
assertThat(deserialized.localDate).isEqualTo(model.localDate)
94+
}
95+
96+
@Test
97+
fun `round trip should preserve id oneof`() {
8898
val deserialized = OneofWrappers.deserialize(
8999
OneofWrappers {
90-
wrappedOneof = OneofWrappers.WrappedOneof.IdOneof(model.id)
100+
wrappedOneof = WrappedOneof.IdOneof(model.id)
91101
}.serialize()
92102
)
93103

94104
assertThat(
95-
(deserialized.wrappedOneof as OneofWrappers.WrappedOneof.IdOneof).idOneof
105+
(deserialized.wrappedOneof as WrappedOneof.IdOneof).idOneof
96106
).isEqualTo(model.id)
97107
}
98108

99109
@Test
100110
fun `round trip should preserve uuid oneof`() {
101111
val deserialized = OneofWrappers.deserialize(
102112
OneofWrappers {
103-
wrappedOneof = OneofWrappers.WrappedOneof.UuidOneof(model.uuid)
113+
wrappedOneof = WrappedOneof.UuidOneof(model.uuid)
104114
}.serialize()
105115
)
106116

107117
assertThat(
108-
(deserialized.wrappedOneof as OneofWrappers.WrappedOneof.UuidOneof).uuidOneof
118+
(deserialized.wrappedOneof as WrappedOneof.UuidOneof).uuidOneof
109119
).isEqualTo(model.uuid)
110120
}
111121

112122
@Test
113123
fun `round trip should preserve ip address oneof`() {
114124
val deserialized = OneofWrappers.deserialize(
115125
OneofWrappers {
116-
wrappedOneof = OneofWrappers.WrappedOneof.IpAddressOneof(model.ipAddress)
126+
wrappedOneof = WrappedOneof.IpAddressOneof(model.ipAddress)
117127
}.serialize()
118128
)
119129

120130
assertThat(
121-
(deserialized.wrappedOneof as OneofWrappers.WrappedOneof.IpAddressOneof).ipAddressOneof
131+
(deserialized.wrappedOneof as WrappedOneof.IpAddressOneof).ipAddressOneof
122132
).isEqualTo(model.ipAddress)
123133
}
124134

125135
@Test
126-
fun `round trip should preserve instant oneof`() {
136+
fun `round trip should preserve caching id oneof`() {
127137
val deserialized = OneofWrappers.deserialize(
128138
OneofWrappers {
129-
wrappedOneof = OneofWrappers.WrappedOneof.InstantOneof(model.instant)
139+
wrappedOneof = WrappedOneof.CachingIdOneof(model.cachingId)
130140
}.serialize()
131141
)
132142

133143
assertThat(
134-
(deserialized.wrappedOneof as OneofWrappers.WrappedOneof.InstantOneof).instantOneof
135-
).isEqualTo(model.instant)
144+
(deserialized.wrappedOneof as WrappedOneof.CachingIdOneof).cachingIdOneof
145+
).isEqualTo(model.cachingId)
136146
}
137147

138148
@Test
139149
fun `round trip should preserve socket address oneof`() {
140150
val deserialized = OneofWrappers.deserialize(
141151
OneofWrappers {
142-
wrappedOneof = OneofWrappers.WrappedOneof.SocketAddressOneof(model.socketAddress)
152+
wrappedOneof = WrappedOneof.SocketAddressOneof(model.socketAddress)
143153
}.serialize()
144154
)
145155

146156
assertThat(
147-
(deserialized.wrappedOneof as OneofWrappers.WrappedOneof.SocketAddressOneof).socketAddressOneof
157+
(deserialized.wrappedOneof as WrappedOneof.SocketAddressOneof).socketAddressOneof
148158
).isEqualTo(model.socketAddress)
149159
}
150160

161+
@Test
162+
fun `round trip should preserve instant oneof`() {
163+
val deserialized = OneofWrappers.deserialize(
164+
OneofWrappers {
165+
wrappedOneof = WrappedOneof.InstantOneof(model.instant)
166+
}.serialize()
167+
)
168+
169+
assertThat(
170+
(deserialized.wrappedOneof as WrappedOneof.InstantOneof).instantOneof
171+
).isEqualTo(model.instant)
172+
}
173+
174+
@Test
175+
fun `round trip should preserve duration oneof`() {
176+
val deserialized = OneofWrappers.deserialize(
177+
OneofWrappers {
178+
wrappedOneof = WrappedOneof.DurationOneof(model.duration)
179+
}.serialize()
180+
)
181+
182+
assertThat(
183+
(deserialized.wrappedOneof as WrappedOneof.DurationOneof).durationOneof
184+
).isEqualTo(model.duration)
185+
}
186+
187+
@Test
188+
fun `round trip should preserve localdate oneof`() {
189+
val deserialized = OneofWrappers.deserialize(
190+
OneofWrappers {
191+
wrappedOneof = WrappedOneof.LocalDateOneof(model.localDate)
192+
}.serialize()
193+
)
194+
195+
assertThat(
196+
(deserialized.wrappedOneof as WrappedOneof.LocalDateOneof).localDateOneof
197+
).isEqualTo(model.localDate)
198+
}
199+
151200
@Test
152201
fun `wrapped message should not be nullable`() {
153202
val thrown = assertThrows<IllegalArgumentException> {

0 commit comments

Comments
 (0)