Skip to content

Commit 987af6d

Browse files
feat: Support getOrNull and getOrDefault in Struct
1 parent a97728b commit 987af6d

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,38 @@ default float getFloat(String columnName) {
176176
*/
177177
String getString(String columnName);
178178

179+
/**
180+
* @param columnIndex index of the column
181+
* @return the value of a column with type T. return value(T) can be null.
182+
*/
183+
default <T> T getOrNull(int columnIndex, Function<Integer, T> function) {
184+
return isNull(columnIndex) ? null : function.apply(columnIndex);
185+
}
186+
187+
/**
188+
* @param columnName index of the column
189+
* @return the value of a column with type T. return value(T) can be null.
190+
*/
191+
default <T> T getOrNull(String columnName, Function<String, T> function) {
192+
return isNull(columnName) ? null : function.apply(columnName);
193+
}
194+
195+
/**
196+
* @param columnIndex index of the column
197+
* @return the value of a column with type T. if column value is null, returns default value.
198+
*/
199+
default <T> T getOrDefault(int columnIndex, Function<Integer, T> function, T defaultValue) {
200+
return isNull(columnIndex) ? defaultValue : function.apply(columnIndex);
201+
}
202+
203+
/**
204+
* @param columnName name of the column
205+
* @return the value of a column with type T. if column value is null, returns default value.
206+
*/
207+
default <T> T getOrDefault(String columnName, Function<String, T> function, T defaultValue) {
208+
return isNull(columnName) ? defaultValue : function.apply(columnName);
209+
}
210+
179211
/**
180212
* @param columnIndex index of the column
181213
* @return the value of a non-{@code NULL} column with type {@link Type#json()}.

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,48 @@ public void builder() {
5757
assertThat(struct.getLong(1)).isEqualTo(2);
5858
}
5959

60+
@Test
61+
public void getOrNullTests() {
62+
Struct struct =
63+
Struct.newBuilder()
64+
.set("f1")
65+
.to("x")
66+
.set("f2")
67+
.to(2)
68+
.set("f3")
69+
.to(Value.bool(null))
70+
.build();
71+
String column1 = struct.getOrNull(0, struct::getString);
72+
assertThat(column1).isEqualTo("x");
73+
74+
Long column2 = struct.getOrNull(1, struct::getLong);
75+
assertThat(column2).isEqualTo(2);
76+
77+
String column3 = struct.getOrNull("f3", struct::getString);
78+
assertThat(column3).isNull();
79+
}
80+
81+
@Test
82+
public void getOrDefaultTests() {
83+
Struct struct =
84+
Struct.newBuilder()
85+
.set("f1")
86+
.to("x")
87+
.set("f2")
88+
.to(2)
89+
.set("f3")
90+
.to(Value.bool(null))
91+
.build();
92+
String column1 = struct.getOrDefault(0, struct::getString, "");
93+
assertThat(column1).isEqualTo("x");
94+
95+
Long column2 = struct.getOrDefault("f2", struct::getLong, -1L);
96+
assertThat(column2).isEqualTo(2);
97+
98+
String column3 = struct.getOrDefault(2, struct::getString, "");
99+
assertThat(column3).isEqualTo("");
100+
}
101+
60102
@Test
61103
public void duplicateFields() {
62104
// Duplicate fields are allowed - some SQL queries produce this type of value.

0 commit comments

Comments
 (0)