Skip to content

Commit

Permalink
wrapper for localdate from stringvalue (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewparmet authored Jul 14, 2020
1 parent 57544bd commit 78b9515
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 com.toasttab.protokt.StringValue
import java.time.LocalDate

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

override val wrapped = StringValue::class

override fun wrap(unwrapped: StringValue) =
LocalDateConverter.wrap(unwrapped.value)

override fun unwrap(wrapped: LocalDate) =
StringValue { value = LocalDateConverter.unwrap(wrapped) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ message Wrappers {
string local_date = 8 [
(.protokt.property).wrap = "java.time.LocalDate"
];

google.protobuf.BytesValue nullable_uuid = 9 [
(.protokt.property).wrap = "java.util.UUID"
];

google.protobuf.StringValue nullable_local_date = 10 [
(.protokt.property).wrap = "java.time.LocalDate"
];
}

message OneofWrappers {
Expand Down Expand Up @@ -95,6 +103,14 @@ message OneofWrappers {
string local_date_oneof = 8 [
(.protokt.property).wrap = "java.time.LocalDate"
];

google.protobuf.BytesValue nullable_uuid_oneof = 9 [
(.protokt.property).wrap = "java.util.UUID"
];

google.protobuf.StringValue nullable_local_date_oneof = 10 [
(.protokt.property).wrap = "java.time.LocalDate"
];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class WrapperTypesTest {
instant = Instant.now()
duration = Duration.ofSeconds(5)
localDate = LocalDate.of(1950, 10, 4)
nullableUuid = UUID.randomUUID()
nullableLocalDate = LocalDate.of(1950, 10, 4)
}

@Test
Expand Down Expand Up @@ -93,6 +95,36 @@ class WrapperTypesTest {
assertThat(deserialized.localDate).isEqualTo(model.localDate)
}

@Test
fun `round trip should preserve nullable uuid`() {
val deserialized = Wrappers.deserialize(model.serialize())

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

@Test
fun `round trip should preserve nullable uuid when null`() {
val deserialized =
Wrappers.deserialize(model.copy { nullableUuid = null }.serialize())

assertThat(deserialized.nullableUuid).isNull()
}

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

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

@Test
fun `round trip should preserve nullable localdate when null`() {
val deserialized =
Wrappers.deserialize(model.copy { nullableLocalDate = null }.serialize())

assertThat(deserialized.nullableLocalDate).isNull()
}

@Test
fun `round trip should preserve id oneof`() {
val deserialized = OneofWrappers.deserialize(
Expand Down

0 comments on commit 78b9515

Please sign in to comment.