|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 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 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.spanner; |
| 18 | + |
| 19 | +import com.google.cloud.Date; |
| 20 | +import com.google.protobuf.ListValue; |
| 21 | +import java.text.SimpleDateFormat; |
| 22 | +import java.time.LocalDate; |
| 23 | +import java.time.LocalDateTime; |
| 24 | +import java.time.OffsetDateTime; |
| 25 | +import java.time.ZoneId; |
| 26 | +import java.time.ZonedDateTime; |
| 27 | +import java.time.format.DateTimeFormatter; |
| 28 | +import java.time.temporal.TemporalAccessor; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.Iterator; |
| 31 | +import java.util.List; |
| 32 | +import java.util.function.Function; |
| 33 | +import java.util.stream.Collectors; |
| 34 | +import java.util.stream.Stream; |
| 35 | + |
| 36 | +final class SpannerTypeConverter { |
| 37 | + |
| 38 | + private static final String DATE_PATTERN = "yyyy-MM-dd"; |
| 39 | + private static final SimpleDateFormat SIMPLE_DATE_FORMATTER = new SimpleDateFormat(DATE_PATTERN); |
| 40 | + private static final ZoneId UTC_ZONE = ZoneId.of("UTC"); |
| 41 | + private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern(DATE_PATTERN); |
| 42 | + private static final DateTimeFormatter ISO_8601_DATE_FORMATTER = |
| 43 | + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX"); |
| 44 | + |
| 45 | + static <T> Value createUntypedArrayValue(Stream<T> stream) { |
| 46 | + List<com.google.protobuf.Value> values = |
| 47 | + stream |
| 48 | + .map( |
| 49 | + val -> |
| 50 | + com.google.protobuf.Value.newBuilder() |
| 51 | + .setStringValue(String.valueOf(val)) |
| 52 | + .build()) |
| 53 | + .collect(Collectors.toList()); |
| 54 | + return Value.untyped( |
| 55 | + com.google.protobuf.Value.newBuilder() |
| 56 | + .setListValue(ListValue.newBuilder().addAllValues(values).build()) |
| 57 | + .build()); |
| 58 | + } |
| 59 | + |
| 60 | + static <T extends TemporalAccessor> String convertToISO8601(T dateTime) { |
| 61 | + return ISO_8601_DATE_FORMATTER.format(dateTime); |
| 62 | + } |
| 63 | + |
| 64 | + static <T> Value createUntypedValue(T value) { |
| 65 | + return Value.untyped( |
| 66 | + com.google.protobuf.Value.newBuilder().setStringValue(String.valueOf(value)).build()); |
| 67 | + } |
| 68 | + |
| 69 | + @SuppressWarnings("unchecked") |
| 70 | + static <T, U> Iterable<U> convertToTypedIterable( |
| 71 | + Function<T, U> func, T val, Iterator<?> iterator) { |
| 72 | + List<U> values = new ArrayList<>(); |
| 73 | + values.add(func.apply(val)); |
| 74 | + iterator.forEachRemaining(value -> values.add(func.apply((T) value))); |
| 75 | + return values; |
| 76 | + } |
| 77 | + |
| 78 | + static <T> Iterable<T> convertToTypedIterable(T val, Iterator<?> iterator) { |
| 79 | + return convertToTypedIterable(v -> v, val, iterator); |
| 80 | + } |
| 81 | + |
| 82 | + static Date convertUtilDateToSpannerDate(java.util.Date date) { |
| 83 | + return Date.parseDate(SIMPLE_DATE_FORMATTER.format(date)); |
| 84 | + } |
| 85 | + |
| 86 | + static Date convertLocalDateToSpannerDate(LocalDate date) { |
| 87 | + return Date.parseDate(DATE_FORMATTER.format(date)); |
| 88 | + } |
| 89 | + |
| 90 | + static <T> Value createUntypedIterableValue( |
| 91 | + T value, Iterator<?> iterator, Function<T, String> func) { |
| 92 | + return Value.untyped( |
| 93 | + com.google.protobuf.Value.newBuilder() |
| 94 | + .setListValue( |
| 95 | + ListValue.newBuilder() |
| 96 | + .addAllValues( |
| 97 | + SpannerTypeConverter.convertToTypedIterable( |
| 98 | + (val) -> |
| 99 | + com.google.protobuf.Value.newBuilder() |
| 100 | + .setStringValue(func.apply(val)) |
| 101 | + .build(), |
| 102 | + value, |
| 103 | + iterator))) |
| 104 | + .build()); |
| 105 | + } |
| 106 | + |
| 107 | + static ZonedDateTime convertToUTCTimezone(LocalDateTime localDateTime) { |
| 108 | + return localDateTime.atZone(UTC_ZONE); |
| 109 | + } |
| 110 | + |
| 111 | + static ZonedDateTime convertToUTCTimezone(OffsetDateTime localDateTime) { |
| 112 | + return localDateTime.atZoneSameInstant(UTC_ZONE); |
| 113 | + } |
| 114 | + |
| 115 | + static ZonedDateTime convertToUTCTimezone(ZonedDateTime localDateTime) { |
| 116 | + return localDateTime.withZoneSameInstant(UTC_ZONE); |
| 117 | + } |
| 118 | +} |
0 commit comments