Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add getStringOrDefault method to FieldValue #3255

Merged
merged 4 commits into from Apr 29, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -60,13 +60,13 @@ implementation 'com.google.cloud:google-cloud-bigquery'
If you are using Gradle without BOM, add this to your dependencies:

```Groovy
implementation 'com.google.cloud:google-cloud-bigquery:2.38.2'
implementation 'com.google.cloud:google-cloud-bigquery:2.39.0'
```

If you are using SBT, add this to your dependencies:

```Scala
libraryDependencies += "com.google.cloud" % "google-cloud-bigquery" % "2.38.2"
libraryDependencies += "com.google.cloud" % "google-cloud-bigquery" % "2.39.0"
```
<!-- {x-version-update-end} -->

Expand Down Expand Up @@ -351,7 +351,7 @@ Java is a registered trademark of Oracle and/or its affiliates.
[kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/java-bigquery/java11.html
[stability-image]: https://img.shields.io/badge/stability-stable-green
[maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-bigquery.svg
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-bigquery/2.38.2
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-bigquery/2.39.0
[authentication]: https://github.com/googleapis/google-cloud-java#authentication
[auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes
[predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles
Expand Down
Expand Up @@ -123,6 +123,20 @@ public String getStringValue() {
return (String) value;
}

/**
* Returns this field's value as a {@link String}, or defaultValue if {@link #isNull()} returns
* {@code true}. See {@link #getStringValue()} for more details.
*
* @throws ClassCastException if the field is not a primitive type
*/
@SuppressWarnings("unchecked")
public String getStringValueOrDefault(String defaultValue) {
if (isNull()) {
return defaultValue;
}
return getStringValue();
}

/**
* Returns this field's value as a byte array. This method should only be used if the
* corresponding field has primitive type ({@link LegacySQLTypeName#BYTES}.
Expand Down
Expand Up @@ -20,6 +20,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import com.google.api.client.util.Data;
import com.google.api.services.bigquery.model.TableCell;
Expand Down Expand Up @@ -77,12 +78,14 @@ public void testFromPb() {
value = FieldValue.fromPb(GEOGRAPHY_FIELD);
assertEquals(FieldValue.Attribute.PRIMITIVE, value.getAttribute());
assertEquals("POINT(-122.350220 47.649154)", value.getStringValue());
assertEquals("POINT(-122.350220 47.649154)", value.getStringValueOrDefault(null));
value = FieldValue.fromPb(NUMERIC_FIELD);
assertEquals(FieldValue.Attribute.PRIMITIVE, value.getAttribute());
assertEquals(new BigDecimal("123456789.123456789"), value.getNumericValue());
value = FieldValue.fromPb(STRING_FIELD);
assertEquals(FieldValue.Attribute.PRIMITIVE, value.getAttribute());
assertEquals("string", value.getStringValue());
assertEquals("string", value.getStringValueOrDefault(null));
value = FieldValue.fromPb(TIMESTAMP_FIELD);
assertEquals(FieldValue.Attribute.PRIMITIVE, value.getAttribute());
assertEquals(42000000, value.getTimestampValue());
Expand All @@ -92,11 +95,13 @@ public void testFromPb() {
PeriodDuration.of(Period.of(3, 2, 1), Duration.parse("PT12H34M56.789S"));
assertEquals(periodDuration, value.getPeriodDuration());
assertEquals("P3Y2M1DT12H34M56.789S", value.getStringValue());
assertEquals("P3Y2M1DT12H34M56.789S", value.getStringValueOrDefault(null));
value = FieldValue.fromPb(INTERVAL_FIELD_2);
assertEquals(FieldValue.Attribute.PRIMITIVE, value.getAttribute());
periodDuration = PeriodDuration.of(Period.of(3, 2, 1), Duration.parse("PT12H34M56.789S"));
assertEquals(periodDuration, value.getPeriodDuration());
assertEquals("3-2 1 12:34:56.789", value.getStringValue());
assertEquals("3-2 1 12:34:56.789", value.getStringValueOrDefault(null));
value = FieldValue.fromPb(BYTES_FIELD);
assertEquals(FieldValue.Attribute.PRIMITIVE, value.getAttribute());
assertArrayEquals(BYTES, value.getBytesValue());
Expand All @@ -113,6 +118,10 @@ public void testFromPb() {
assertEquals(FieldValue.Attribute.RECORD, value.getAttribute());
assertEquals(FieldValue.fromPb(FLOAT_FIELD), value.getRepeatedValue().get(0));
assertEquals(FieldValue.fromPb(TIMESTAMP_FIELD), value.getRepeatedValue().get(1));
value = FieldValue.fromPb(NULL_FIELD);
assertTrue(value.isNull());
assertEquals(null, value.getStringValueOrDefault(null));
assertEquals("defaultValue", value.getStringValueOrDefault("defaultValue"));
}

@Test
Expand Down