Skip to content

Commit a51973b

Browse files
thiagotnunesolavloitegcf-owl-bot[bot]
authored
feat: allows for getting json columns using getValue (#1699)
* feat: allow for retrieving json using getValue Allows for getting JSON values using resultSet.getValue(). * feat: allows for value.getString on json values Overrides the getString method in json values to allow for getString and getStringArray on json values. * fix: fix test (wrong method called) Co-authored-by: Knut Olav Løite <koloite@gmail.com> * fix: fix test (wrong method called) Co-authored-by: Knut Olav Løite <koloite@gmail.com> * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Knut Olav Løite <koloite@gmail.com> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 0550d5a commit a51973b

File tree

5 files changed

+64
-24
lines changed

5 files changed

+64
-24
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ implementation 'com.google.cloud:google-cloud-spanner'
5656
If you are using Gradle without BOM, add this to your dependencies
5757

5858
```Groovy
59-
implementation 'com.google.cloud:google-cloud-spanner:6.19.0'
59+
implementation 'com.google.cloud:google-cloud-spanner:6.19.1'
6060
```
6161

6262
If you are using SBT, add this to your dependencies
6363

6464
```Scala
65-
libraryDependencies += "com.google.cloud" % "google-cloud-spanner" % "6.19.0"
65+
libraryDependencies += "com.google.cloud" % "google-cloud-spanner" % "6.19.1"
6666
```
6767

6868
## Authentication

google-cloud-spanner/src/main/java/com/google/cloud/spanner/AbstractResultSet.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,8 @@ protected Value getValueInternal(int columnIndex) {
713713
return Value.float64(isNull ? null : getDoubleInternal(columnIndex));
714714
case STRING:
715715
return Value.string(isNull ? null : getStringInternal(columnIndex));
716+
case JSON:
717+
return Value.json(isNull ? null : getJsonInternal(columnIndex));
716718
case BYTES:
717719
return Value.bytes(isNull ? null : getBytesInternal(columnIndex));
718720
case TIMESTAMP:
@@ -736,6 +738,8 @@ protected Value getValueInternal(int columnIndex) {
736738
return Value.float64Array(isNull ? null : getDoubleListInternal(columnIndex));
737739
case STRING:
738740
return Value.stringArray(isNull ? null : getStringListInternal(columnIndex));
741+
case JSON:
742+
return Value.jsonArray(isNull ? null : getJsonListInternal(columnIndex));
739743
case BYTES:
740744
return Value.bytesArray(isNull ? null : getBytesListInternal(columnIndex));
741745
case TIMESTAMP:

google-cloud-spanner/src/main/java/com/google/cloud/spanner/Value.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,11 @@ public String getJson() {
11511151
return value;
11521152
}
11531153

1154+
@Override
1155+
public String getString() {
1156+
return getJson();
1157+
}
1158+
11541159
@Override
11551160
void valueToString(StringBuilder b) {
11561161
if (value.length() > MAX_DEBUG_STRING_LENGTH) {
@@ -1587,6 +1592,11 @@ public List<String> getJsonArray() {
15871592
return value;
15881593
}
15891594

1595+
@Override
1596+
public List<String> getStringArray() {
1597+
return getJsonArray();
1598+
}
1599+
15901600
@Override
15911601
void appendElement(StringBuilder b, String element) {
15921602
b.append(element);

google-cloud-spanner/src/test/java/com/google/cloud/spanner/ValueTest.java

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.testing.SerializableTester.reserializeAndAssert;
2020
import static com.google.common.truth.Truth.assertThat;
2121
import static com.google.common.truth.Truth.assertWithMessage;
22+
import static org.junit.Assert.assertArrayEquals;
2223
import static org.junit.Assert.assertEquals;
2324
import static org.junit.Assert.assertFalse;
2425
import static org.junit.Assert.assertNull;
@@ -42,6 +43,7 @@
4243
import java.util.Arrays;
4344
import java.util.Collections;
4445
import java.util.List;
46+
import java.util.function.Supplier;
4547
import org.junit.Test;
4648
import org.junit.runner.RunWith;
4749
import org.junit.runners.JUnit4;
@@ -416,6 +418,7 @@ public void json() {
416418
assertEquals(Type.json(), v.getType());
417419
assertFalse(v.isNull());
418420
assertEquals(json, v.getJson());
421+
assertEquals(json, v.getString());
419422
}
420423

421424
@Test
@@ -424,12 +427,8 @@ public void jsonNull() {
424427
assertEquals(Type.json(), v.getType());
425428
assertTrue(v.isNull());
426429
assertEquals(NULL_STRING, v.toString());
427-
try {
428-
v.getJson();
429-
fail("Expected exception");
430-
} catch (IllegalStateException e) {
431-
assertThat(e.getMessage().contains("null value"));
432-
}
430+
assertThrowsWithMessage(v::getJson, "null value");
431+
assertThrowsWithMessage(v::getString, "null value");
433432
}
434433

435434
@Test
@@ -834,21 +833,18 @@ public void jsonArray() {
834833
String three = "{\"color\":\"red\",\"value\":\"#f00\"}";
835834
Value v = Value.jsonArray(Arrays.asList(one, two, three));
836835
assertFalse(v.isNull());
837-
assertThat(v.getJsonArray()).containsExactly(one, two, three).inOrder();
836+
assertArrayEquals(new String[] {one, two, three}, v.getJsonArray().toArray());
838837
assertEquals("[{},NULL,{\"color\":\"red\",\"value\":\"#f00\"}]", v.toString());
838+
assertArrayEquals(new String[] {one, two, three}, v.getStringArray().toArray());
839839
}
840840

841841
@Test
842842
public void jsonArrayNull() {
843843
Value v = Value.jsonArray(null);
844844
assertTrue(v.isNull());
845845
assertEquals(NULL_STRING, v.toString());
846-
try {
847-
v.getJsonArray();
848-
fail("Expected exception");
849-
} catch (IllegalStateException e) {
850-
assertThat(e.getMessage().contains("null value"));
851-
}
846+
assertThrowsWithMessage(v::getJsonArray, "null value");
847+
assertThrowsWithMessage(v::getStringArray, "null value");
852848
}
853849

854850
@Test
@@ -863,14 +859,9 @@ public void jsonArrayTryGetBytesArray() {
863859
}
864860

865861
@Test
866-
public void jsonArrayTryGetStringArray() {
867-
Value value = Value.jsonArray(Arrays.asList("{}"));
868-
try {
869-
value.getStringArray();
870-
fail("Expected exception");
871-
} catch (IllegalStateException e) {
872-
assertThat(e.getMessage().contains("Expected: ARRAY<STRING> actual: ARRAY<JSON>"));
873-
}
862+
public void jsonArrayTryGetFloat64Array() {
863+
Value value = Value.jsonArray(Collections.singletonList("{}"));
864+
assertThrowsWithMessage(value::getFloat64Array, "Expected: ARRAY<FLOAT64> actual: ARRAY<JSON>");
874865
}
875866

876867
@Test
@@ -1810,4 +1801,19 @@ private void writeObject(@SuppressWarnings("unused") java.io.ObjectOutputStream
18101801
throw new IllegalStateException("Serialization disabled");
18111802
}
18121803
}
1804+
1805+
private void assertThrowsWithMessage(Supplier<?> supplier, String message) {
1806+
try {
1807+
supplier.get();
1808+
fail("Expected exception");
1809+
} catch (Exception e) {
1810+
assertTrue(
1811+
"Expected exception message to contain: \""
1812+
+ message
1813+
+ "\", actual: \""
1814+
+ e.getMessage()
1815+
+ "\"",
1816+
e.getMessage().contains(message));
1817+
}
1818+
}
18131819
}

google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITResultSetGetValue.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,16 @@ public static void beforeClass()
101101
+ "bytes BYTES(MAX),"
102102
+ "timestamp TIMESTAMP,"
103103
+ "date DATE,"
104+
+ "json JSON,"
104105
+ "boolArray ARRAY<BOOL>,"
105106
+ "int64Array ARRAY<INT64>,"
106107
+ "float64Array ARRAY<FLOAT64>,"
107108
+ "numericArray ARRAY<NUMERIC>,"
108109
+ "stringArray ARRAY<STRING(MAX)>,"
109110
+ "bytesArray ARRAY<BYTES(MAX)>,"
110111
+ "timestampArray ARRAY<TIMESTAMP>,"
111-
+ "dateArray ARRAY<DATE>"
112+
+ "dateArray ARRAY<DATE>,"
113+
+ "jsonArray ARRAY<JSON>"
112114
+ ") PRIMARY KEY (Id)");
113115
googleStandardSQLClient = env.getTestHelper().getDatabaseClient(googleStandardSqlDatabase);
114116
if (!EmulatorSpannerHelper.isUsingEmulator()) {
@@ -167,6 +169,8 @@ public void testReadNonNullValuesGoogleStandardSQL() {
167169
.to(Timestamp.ofTimeSecondsAndNanos(1, 0))
168170
.set("date")
169171
.to(Date.fromYearMonthDay(2021, 1, 2))
172+
.set("json")
173+
.to(Value.json("{\"key\":\"value\"}"))
170174
.set("boolArray")
171175
.toBoolArray(new boolean[] {false, true})
172176
.set("int64Array")
@@ -189,6 +193,8 @@ public void testReadNonNullValuesGoogleStandardSQL() {
189193
.toDateArray(
190194
Arrays.asList(
191195
Date.fromYearMonthDay(2021, 2, 3), Date.fromYearMonthDay(2021, 3, 4)))
196+
.set("jsonArray")
197+
.toJsonArray(Arrays.asList("{\"key1\":\"value1\"}", "{\"key2\":\"value2\"}"))
192198
.build()));
193199
try (ResultSet resultSet =
194200
databaseClient
@@ -206,6 +212,7 @@ public void testReadNonNullValuesGoogleStandardSQL() {
206212
assertEquals(
207213
Value.timestamp(Timestamp.ofTimeSecondsAndNanos(1, 0)), resultSet.getValue("timestamp"));
208214
assertEquals(Value.date(Date.fromYearMonthDay(2021, 1, 2)), resultSet.getValue("date"));
215+
assertEquals(Value.json("{\"key\":\"value\"}"), resultSet.getValue("json"));
209216
assertEquals(Value.boolArray(new boolean[] {false, true}), resultSet.getValue("boolArray"));
210217
assertEquals(Value.int64Array(new long[] {100L, 200L}), resultSet.getValue("int64Array"));
211218
assertArrayEquals(
@@ -231,6 +238,9 @@ public void testReadNonNullValuesGoogleStandardSQL() {
231238
Value.dateArray(
232239
Arrays.asList(Date.fromYearMonthDay(2021, 2, 3), Date.fromYearMonthDay(2021, 3, 4))),
233240
resultSet.getValue("dateArray"));
241+
assertEquals(
242+
Value.jsonArray(Arrays.asList("{\"key1\":\"value1\"}", "{\"key2\":\"value2\"}")),
243+
resultSet.getValue("jsonArray"));
234244
}
235245
}
236246

@@ -300,6 +310,8 @@ public void testReadNullValuesGoogleStandardSQL() {
300310
IllegalStateException.class, () -> resultSet.getValue("timestamp").getTimestamp());
301311
assertTrue(resultSet.getValue("date").isNull());
302312
assertThrows(IllegalStateException.class, () -> resultSet.getValue("date").getDate());
313+
assertTrue(resultSet.getValue("json").isNull());
314+
assertThrows(IllegalStateException.class, () -> resultSet.getValue("json").getJson());
303315
assertTrue(resultSet.getValue("boolArray").isNull());
304316
assertThrows(
305317
IllegalStateException.class, () -> resultSet.getValue("boolArray").getBoolArray());
@@ -325,6 +337,9 @@ public void testReadNullValuesGoogleStandardSQL() {
325337
assertTrue(resultSet.getValue("dateArray").isNull());
326338
assertThrows(
327339
IllegalStateException.class, () -> resultSet.getValue("dateArray").getDateArray());
340+
assertTrue(resultSet.getValue("jsonArray").isNull());
341+
assertThrows(
342+
IllegalStateException.class, () -> resultSet.getValue("jsonArray").getJsonArray());
328343
}
329344
}
330345

@@ -379,6 +394,8 @@ public void testReadNullValuesInArrays() {
379394
.toTimestampArray(Arrays.asList(null, Timestamp.ofTimeSecondsAndNanos(20, 0)))
380395
.set("dateArray")
381396
.toDateArray(Arrays.asList(Date.fromYearMonthDay(2021, 2, 3), null))
397+
.set("jsonArray")
398+
.toJsonArray(Arrays.asList("{\"key1\":\"value1\"}", null))
382399
.build()));
383400

384401
try (ResultSet resultSet =
@@ -406,6 +423,9 @@ public void testReadNullValuesInArrays() {
406423
assertEquals(
407424
Value.dateArray(Arrays.asList(Date.fromYearMonthDay(2021, 2, 3), null)),
408425
resultSet.getValue("dateArray"));
426+
assertEquals(
427+
Value.jsonArray(Arrays.asList("{\"key1\":\"value1\"}", null)),
428+
resultSet.getValue("jsonArray"));
409429
}
410430
}
411431

0 commit comments

Comments
 (0)