Skip to content

Commit

Permalink
Handle unset of Field's mode and description
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard committed Dec 3, 2015
1 parent 79001cf commit f84a3b1
Showing 1 changed file with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -222,15 +231,15 @@ 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;
}

/**
* Sets the field description. The maximum length is 16K characters.
*/
public Builder description(String description) {
this.description = description;
this.description = firstNonNull(description, Data.<String>nullOf(String.class));
return this;
}

Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -292,11 +301,7 @@ public List<Field> 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
Expand Down Expand Up @@ -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.<String>nullOf(String.class) : mode.name());
}
if (description != null) {
fieldSchemaPb.setDescription(description);
Expand Down

0 comments on commit f84a3b1

Please sign in to comment.