From f84a3b115196ca702d3c4a420870edcbb3f86596 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Thu, 3 Dec 2015 22:31:58 +0100 Subject: [PATCH] Handle unset of Field's mode and description --- .../com/google/gcloud/bigquery/Field.java | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Field.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Field.java index 73ca87640ff8..d28e5eb9fed7 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Field.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Field.java @@ -16,9 +16,11 @@ package com.google.gcloud.bigquery; +import static com.google.common.base.MoreObjects.firstNonNull; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; +import com.google.api.client.util.Data; import com.google.api.services.bigquery.model.TableFieldSchema; import com.google.common.base.Function; import com.google.common.base.MoreObjects; @@ -180,7 +182,7 @@ public boolean equals(Object obj) { * than one value. */ public enum Mode { - NULLABLE, REQUIRED, REPEATED + NULLABLE, REQUIRED, REPEATED, NOT_SET } private final String name; @@ -197,6 +199,13 @@ public static final class Builder { private Builder() {} + private Builder(Field field) { + this.name = field.name; + this.type = field.type; + this.mode = field.mode; + this.description = field.description; + } + /** * Sets the field name. The name must contain only letters (a-z, A-Z), numbers (0-9), or * underscores (_), and must start with a letter or underscore. The maximum length is 128 @@ -222,7 +231,7 @@ public Builder type(Type type) { * Sets the mode of the field. When not specified {@link Mode#NULLABLE} is used. */ public Builder mode(Mode mode) { - this.mode = mode; + this.mode = firstNonNull(mode, Mode.NOT_SET); return this; } @@ -230,7 +239,7 @@ public Builder mode(Mode mode) { * Sets the field description. The maximum length is 16K characters. */ public Builder description(String description) { - this.description = description; + this.description = firstNonNull(description, Data.nullOf(String.class)); return this; } @@ -270,14 +279,14 @@ public Type type() { * Returns the field mode. By default {@link Mode#NULLABLE} is used. */ public Mode mode() { - return mode; + return mode == Mode.NOT_SET ? null : mode; } /** * Returns the field description. */ public String description() { - return description; + return Data.isNull(description) ? null : description; } /** @@ -292,11 +301,7 @@ public List fields() { * Returns a builder for the {@code Field} object. */ public Builder toBuilder() { - return new Builder() - .name(this.name) - .type(this.type) - .mode(this.mode) - .description(this.description); + return new Builder(this); } @Override @@ -324,7 +329,7 @@ TableFieldSchema toPb() { fieldSchemaPb.setName(name); fieldSchemaPb.setType(type.value().name()); if (mode != null) { - fieldSchemaPb.setMode(mode.name()); + fieldSchemaPb.setMode(mode == Mode.NOT_SET ? Data.nullOf(String.class) : mode.name()); } if (description != null) { fieldSchemaPb.setDescription(description);