Skip to content

Commit

Permalink
feat: Add getStringOrDefault method to FieldValue (#3255)
Browse files Browse the repository at this point in the history
* feat: Add getStringOrDefault method to FieldValue

* feat: Add getStringOrDefault method to FieldValue

* Add null case to FieldValueTest unit test

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
PhongChuong and gcf-owl-bot[bot] committed Apr 29, 2024
1 parent c2e3283 commit 8bac33a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
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

0 comments on commit 8bac33a

Please sign in to comment.