Skip to content
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
Expand Up @@ -65,11 +65,8 @@ public static Object convertToObject(final Any any) throws InvalidProtocolBuffer
if (any.is(Int64Value.class)) {
return any.unpack(Int64Value.class).getValue();
}
if (any.is(Int64Value.class)) {
return any.unpack(Int64Value.class).getValue();
}
if (any.is(UInt32Value.class)) {
return any.unpack(UInt64Value.class).getValue();
return any.unpack(UInt32Value.class).getValue();
}
if (any.is(UInt64Value.class)) {
return any.unpack(UInt64Value.class).getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,50 +32,66 @@
import com.google.protobuf.StringValue;
import com.google.protobuf.Struct;
import com.google.protobuf.Struct.Builder;
import com.google.protobuf.UInt32Value;
import com.google.protobuf.UInt64Value;
import com.google.protobuf.Value;
import com.google.protobuf.util.JsonFormat;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.sql.Timestamp;
import java.time.OffsetDateTime;
import java.util.stream.Stream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;

class ProtobufAnyValueConverterTest {

@Test
void assertConvertToObject() throws InvalidProtocolBufferException {
Object actual = ProtobufAnyValueConverter.convertToObject(Any.pack(Int32Value.of(123)));
assertThat(actual, is(123));
actual = ProtobufAnyValueConverter.convertToObject(Any.pack(Int64Value.of(Long.MAX_VALUE)));
assertThat(actual, is(Long.MAX_VALUE));
OffsetDateTime now = OffsetDateTime.now();
actual = ProtobufAnyValueConverter.convertToObject(Any.pack(com.google.protobuf.Timestamp.newBuilder().setSeconds(now.toEpochSecond()).setNanos(now.getNano()).build()));
assertThat(actual, is(Timestamp.valueOf(now.toLocalDateTime())));
actual = ProtobufAnyValueConverter.convertToObject(Any.pack(FloatValue.of(1.23F)));
assertThat(actual, is(1.23F));
actual = ProtobufAnyValueConverter.convertToObject(Any.pack(DoubleValue.of(4.56D)));
assertThat(actual, is(4.56D));
actual = ProtobufAnyValueConverter.convertToObject(Any.pack(StringValue.of("Hello")));
assertThat(actual, is("Hello"));
actual = ProtobufAnyValueConverter.convertToObject(Any.pack(BoolValue.of(true)));
assertTrue((Boolean) actual);
actual = ProtobufAnyValueConverter.convertToObject(Any.pack(BytesValue.of(ByteString.copyFrom(new byte[]{1, 2, 3}))));
assertThat(actual, is(new byte[]{1, 2, 3}));
actual = ProtobufAnyValueConverter.convertToObject(Any.pack(UInt64Value.of(101010L)));
assertThat(actual, is(101010L));
actual = ProtobufAnyValueConverter.convertToObject(Any.pack(Empty.getDefaultInstance()));
assertNull(actual);
actual = Struct.newBuilder().putFields("str", Value.newBuilder().setStringValue("ABC defg").build())
void assertConvertUnsupportedTypeThrowsUnsupportedOperationException() {
Any any = Any.pack(Value.newBuilder().setStringValue("unsupported").build());
UnsupportedOperationException actual = assertThrows(UnsupportedOperationException.class, () -> ProtobufAnyValueConverter.convertToObject(any));
assertThat(actual.getMessage(), is(String.format("not support unpack the type %s", any.getTypeUrl())));
}

@ParameterizedTest
@MethodSource("supportedValues")
void assertConvertToObject(final Any any, final Object expected) throws InvalidProtocolBufferException {
Object actual = ProtobufAnyValueConverter.convertToObject(any);
if (expected instanceof Struct) {
Builder actualStruct = Struct.newBuilder();
JsonFormat.parser().merge((String) actual, actualStruct);
assertThat(actualStruct.build().toString(), is(expected.toString()));
} else {
assertThat(actual, is(expected));
}
}

private static Stream<Arguments> supportedValues() {
long seconds = 1577830861L;
int nanos = 123456789;
Timestamp expectedTimestamp = new Timestamp(seconds * 1000);
expectedTimestamp.setNanos(nanos);
Struct struct = Struct.newBuilder().putFields("str", Value.newBuilder().setStringValue("ABC defg").build())
.putFields("null", Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build())
.putFields("number", Value.newBuilder().setNumberValue(123.45D).build())
.putFields("list", Value.newBuilder().setListValue(ListValue.newBuilder().addValues(Value.newBuilder().setNumberValue(1)).build()).build()).build();
Builder expected = Struct.newBuilder();
JsonFormat.parser().merge((String) ProtobufAnyValueConverter.convertToObject(Any.pack((Struct) actual)), expected);
assertThat(actual.toString(), is(expected.toString()));
return Stream.of(
Arguments.of(null, null),
Arguments.of(Any.pack(Int32Value.of(123)), 123),
Arguments.of(Any.pack(Int64Value.of(Long.MAX_VALUE)), Long.MAX_VALUE),
Arguments.of(Any.pack(UInt32Value.of(7)), 7),
Arguments.of(Any.pack(com.google.protobuf.Timestamp.newBuilder().setSeconds(seconds).setNanos(nanos).build()), expectedTimestamp),
Arguments.of(Any.pack(FloatValue.of(1.23F)), 1.23F),
Arguments.of(Any.pack(DoubleValue.of(4.56D)), 4.56D),
Arguments.of(Any.pack(StringValue.of("Hello")), "Hello"),
Arguments.of(Any.pack(BoolValue.of(true)), true),
Arguments.of(Any.pack(BytesValue.of(ByteString.copyFrom(new byte[]{1, 2, 3}))), new byte[]{1, 2, 3}),
Arguments.of(Any.pack(UInt64Value.of(101010L)), 101010L),
Arguments.of(Any.pack(Empty.getDefaultInstance()), null),
Arguments.of(Any.pack(struct), struct));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public final class ListExpressionConverter {
public static Optional<SqlNode> convert(final ListExpression segment) {
Collection<SqlNode> sqlNodes = new LinkedList<>();
for (ExpressionSegment each : segment.getItems()) {
Optional<SqlNode> sqlNode = ExpressionConverter.convert(each);
sqlNode.ifPresent(sqlNodes::add);
ExpressionConverter.convert(each).ifPresent(sqlNodes::add);
}
return sqlNodes.isEmpty() ? Optional.empty() : Optional.of(new SqlNodeList(sqlNodes, SqlParserPos.ZERO));
}
Expand Down