|
| 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.time.LocalDate; |
| 22 | +import java.time.LocalDateTime; |
| 23 | +import java.time.OffsetDateTime; |
| 24 | +import java.time.ZoneId; |
| 25 | +import java.time.ZonedDateTime; |
| 26 | +import java.time.format.DateTimeFormatter; |
| 27 | +import java.time.temporal.TemporalAccessor; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Iterator; |
| 30 | +import java.util.List; |
| 31 | +import java.util.function.Consumer; |
| 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 ZoneId UTC_ZONE = ZoneId.of("UTC"); |
| 39 | + private static final DateTimeFormatter ISO_8601_DATE_FORMATTER = |
| 40 | + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX"); |
| 41 | + |
| 42 | + static <T> Value createUntypedArrayValue(Stream<T> stream) { |
| 43 | + List<com.google.protobuf.Value> values = |
| 44 | + stream |
| 45 | + .map( |
| 46 | + val -> |
| 47 | + com.google.protobuf.Value.newBuilder() |
| 48 | + .setStringValue(String.valueOf(val)) |
| 49 | + .build()) |
| 50 | + .collect(Collectors.toList()); |
| 51 | + return Value.untyped( |
| 52 | + com.google.protobuf.Value.newBuilder() |
| 53 | + .setListValue(ListValue.newBuilder().addAllValues(values).build()) |
| 54 | + .build()); |
| 55 | + } |
| 56 | + |
| 57 | + static <T extends TemporalAccessor> String convertToISO8601(T dateTime) { |
| 58 | + return ISO_8601_DATE_FORMATTER.format(dateTime); |
| 59 | + } |
| 60 | + |
| 61 | + static <T> Value createUntypedStringValue(T value) { |
| 62 | + return Value.untyped( |
| 63 | + com.google.protobuf.Value.newBuilder().setStringValue(String.valueOf(value)).build()); |
| 64 | + } |
| 65 | + |
| 66 | + static <T, U> Iterable<U> convertToTypedIterable( |
| 67 | + Function<T, U> func, T val, Iterator<?> iterator) { |
| 68 | + List<U> values = new ArrayList<>(); |
| 69 | + SpannerTypeConverter.processIterable(val, iterator, func, values::add); |
| 70 | + return values; |
| 71 | + } |
| 72 | + |
| 73 | + static <T> Iterable<T> convertToTypedIterable(T val, Iterator<?> iterator) { |
| 74 | + return convertToTypedIterable(v -> v, val, iterator); |
| 75 | + } |
| 76 | + |
| 77 | + @SuppressWarnings("unchecked") |
| 78 | + static <T, U> void processIterable( |
| 79 | + T val, Iterator<?> iterator, Function<T, U> func, Consumer<U> consumer) { |
| 80 | + consumer.accept(func.apply(val)); |
| 81 | + iterator.forEachRemaining(values -> consumer.accept(func.apply((T) values))); |
| 82 | + } |
| 83 | + |
| 84 | + static Date convertLocalDateToSpannerDate(LocalDate date) { |
| 85 | + return Date.fromYearMonthDay(date.getYear(), date.getMonthValue(), date.getDayOfMonth()); |
| 86 | + } |
| 87 | + |
| 88 | + static <T> Value createUntypedIterableValue( |
| 89 | + T value, Iterator<?> iterator, Function<T, String> func) { |
| 90 | + ListValue.Builder listValueBuilder = ListValue.newBuilder(); |
| 91 | + SpannerTypeConverter.processIterable( |
| 92 | + value, |
| 93 | + iterator, |
| 94 | + (val) -> com.google.protobuf.Value.newBuilder().setStringValue(func.apply(val)).build(), |
| 95 | + listValueBuilder::addValues); |
| 96 | + return Value.untyped( |
| 97 | + com.google.protobuf.Value.newBuilder().setListValue(listValueBuilder.build()).build()); |
| 98 | + } |
| 99 | + |
| 100 | + static ZonedDateTime atUTC(LocalDateTime localDateTime) { |
| 101 | + return atUTC(localDateTime.atZone(ZoneId.systemDefault())); |
| 102 | + } |
| 103 | + |
| 104 | + static ZonedDateTime atUTC(OffsetDateTime localDateTime) { |
| 105 | + return localDateTime.atZoneSameInstant(UTC_ZONE); |
| 106 | + } |
| 107 | + |
| 108 | + static ZonedDateTime atUTC(ZonedDateTime localDateTime) { |
| 109 | + return localDateTime.withZoneSameInstant(UTC_ZONE); |
| 110 | + } |
| 111 | +} |
0 commit comments