Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LocalDate wrapper #84

Merged
merged 1 commit into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2020 Toast Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.toasttab.protokt.ext

import com.google.auto.service.AutoService
import java.time.LocalDate

@AutoService(Converter::class)
object LocalDateConverter : Converter<LocalDate, String> {
override val wrapper = LocalDate::class

override val wrapped = String::class

override fun wrap(unwrapped: String): LocalDate =
LocalDate.parse(unwrapped)

override fun unwrap(wrapped: LocalDate) =
wrapped.toString()
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ message Wrappers {
(.protokt.property).wrap = "java.time.Duration",
(.protokt.property).non_null = true
];

string local_date = 8 [
(.protokt.property).wrap = "java.time.LocalDate"
];
}

message OneofWrappers {
Expand All @@ -76,12 +80,20 @@ message OneofWrappers {
(.protokt.property).wrap = "CachingId"
];

google.protobuf.Timestamp instant_oneof = 5 [
.protokt.ext.InetSocketAddress socket_address_oneof = 5 [
(.protokt.property).wrap = "java.net.InetSocketAddress"
];

google.protobuf.Timestamp instant_oneof = 6 [
(.protokt.property).wrap = "java.time.Instant"
];

.protokt.ext.InetSocketAddress socket_address_oneof = 6 [
(.protokt.property).wrap = "java.net.InetSocketAddress"
google.protobuf.Duration duration_oneof = 7 [
(.protokt.property).wrap = "java.time.Duration"
];

string local_date_oneof = 8 [
(.protokt.property).wrap = "java.time.LocalDate"
];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
package com.toasttab.protokt.testing.options

import com.google.common.truth.Truth.assertThat
import com.toasttab.protokt.testing.options.OneofWrappers.WrappedOneof
import java.net.InetAddress
import java.net.InetSocketAddress
import java.time.Duration
import java.time.Instant
import java.time.LocalDate
import java.util.UUID
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
Expand All @@ -34,6 +36,7 @@ class WrapperTypesTest {
socketAddress = InetSocketAddress(InetAddress.getLocalHost(), 8080)
instant = Instant.now()
duration = Duration.ofSeconds(5)
localDate = LocalDate.of(1950, 10, 4)
}

@Test
Expand Down Expand Up @@ -84,70 +87,116 @@ class WrapperTypesTest {
}

@Test
fun `round trip should preserve generic wrapper oneof`() {
fun `round trip should preserve localdate`() {
val deserialized = Wrappers.deserialize(model.serialize())

assertThat(deserialized.localDate).isEqualTo(model.localDate)
}

@Test
fun `round trip should preserve id oneof`() {
val deserialized = OneofWrappers.deserialize(
OneofWrappers {
wrappedOneof = OneofWrappers.WrappedOneof.IdOneof(model.id)
wrappedOneof = WrappedOneof.IdOneof(model.id)
}.serialize()
)

assertThat(
(deserialized.wrappedOneof as OneofWrappers.WrappedOneof.IdOneof).idOneof
(deserialized.wrappedOneof as WrappedOneof.IdOneof).idOneof
).isEqualTo(model.id)
}

@Test
fun `round trip should preserve uuid oneof`() {
val deserialized = OneofWrappers.deserialize(
OneofWrappers {
wrappedOneof = OneofWrappers.WrappedOneof.UuidOneof(model.uuid)
wrappedOneof = WrappedOneof.UuidOneof(model.uuid)
}.serialize()
)

assertThat(
(deserialized.wrappedOneof as OneofWrappers.WrappedOneof.UuidOneof).uuidOneof
(deserialized.wrappedOneof as WrappedOneof.UuidOneof).uuidOneof
).isEqualTo(model.uuid)
}

@Test
fun `round trip should preserve ip address oneof`() {
val deserialized = OneofWrappers.deserialize(
OneofWrappers {
wrappedOneof = OneofWrappers.WrappedOneof.IpAddressOneof(model.ipAddress)
wrappedOneof = WrappedOneof.IpAddressOneof(model.ipAddress)
}.serialize()
)

assertThat(
(deserialized.wrappedOneof as OneofWrappers.WrappedOneof.IpAddressOneof).ipAddressOneof
(deserialized.wrappedOneof as WrappedOneof.IpAddressOneof).ipAddressOneof
).isEqualTo(model.ipAddress)
}

@Test
fun `round trip should preserve instant oneof`() {
fun `round trip should preserve caching id oneof`() {
val deserialized = OneofWrappers.deserialize(
OneofWrappers {
wrappedOneof = OneofWrappers.WrappedOneof.InstantOneof(model.instant)
wrappedOneof = WrappedOneof.CachingIdOneof(model.cachingId)
}.serialize()
)

assertThat(
(deserialized.wrappedOneof as OneofWrappers.WrappedOneof.InstantOneof).instantOneof
).isEqualTo(model.instant)
(deserialized.wrappedOneof as WrappedOneof.CachingIdOneof).cachingIdOneof
).isEqualTo(model.cachingId)
}

@Test
fun `round trip should preserve socket address oneof`() {
val deserialized = OneofWrappers.deserialize(
OneofWrappers {
wrappedOneof = OneofWrappers.WrappedOneof.SocketAddressOneof(model.socketAddress)
wrappedOneof = WrappedOneof.SocketAddressOneof(model.socketAddress)
}.serialize()
)

assertThat(
(deserialized.wrappedOneof as OneofWrappers.WrappedOneof.SocketAddressOneof).socketAddressOneof
(deserialized.wrappedOneof as WrappedOneof.SocketAddressOneof).socketAddressOneof
).isEqualTo(model.socketAddress)
}

@Test
fun `round trip should preserve instant oneof`() {
val deserialized = OneofWrappers.deserialize(
OneofWrappers {
wrappedOneof = WrappedOneof.InstantOneof(model.instant)
}.serialize()
)

assertThat(
(deserialized.wrappedOneof as WrappedOneof.InstantOneof).instantOneof
).isEqualTo(model.instant)
}

@Test
fun `round trip should preserve duration oneof`() {
val deserialized = OneofWrappers.deserialize(
OneofWrappers {
wrappedOneof = WrappedOneof.DurationOneof(model.duration)
}.serialize()
)

assertThat(
(deserialized.wrappedOneof as WrappedOneof.DurationOneof).durationOneof
).isEqualTo(model.duration)
}

@Test
fun `round trip should preserve localdate oneof`() {
val deserialized = OneofWrappers.deserialize(
OneofWrappers {
wrappedOneof = WrappedOneof.LocalDateOneof(model.localDate)
}.serialize()
)

assertThat(
(deserialized.wrappedOneof as WrappedOneof.LocalDateOneof).localDateOneof
).isEqualTo(model.localDate)
}

@Test
fun `wrapped message should not be nullable`() {
val thrown = assertThrows<IllegalArgumentException> {
Expand Down