Skip to content

Commit f84a3b1

Browse files
committed
Handle unset of Field's mode and description
1 parent 79001cf commit f84a3b1

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

  • gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Field.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
package com.google.gcloud.bigquery;
1818

19+
import static com.google.common.base.MoreObjects.firstNonNull;
1920
import static com.google.common.base.Preconditions.checkArgument;
2021
import static com.google.common.base.Preconditions.checkNotNull;
2122

23+
import com.google.api.client.util.Data;
2224
import com.google.api.services.bigquery.model.TableFieldSchema;
2325
import com.google.common.base.Function;
2426
import com.google.common.base.MoreObjects;
@@ -180,7 +182,7 @@ public boolean equals(Object obj) {
180182
* than one value.
181183
*/
182184
public enum Mode {
183-
NULLABLE, REQUIRED, REPEATED
185+
NULLABLE, REQUIRED, REPEATED, NOT_SET
184186
}
185187

186188
private final String name;
@@ -197,6 +199,13 @@ public static final class Builder {
197199

198200
private Builder() {}
199201

202+
private Builder(Field field) {
203+
this.name = field.name;
204+
this.type = field.type;
205+
this.mode = field.mode;
206+
this.description = field.description;
207+
}
208+
200209
/**
201210
* Sets the field name. The name must contain only letters (a-z, A-Z), numbers (0-9), or
202211
* underscores (_), and must start with a letter or underscore. The maximum length is 128
@@ -222,15 +231,15 @@ public Builder type(Type type) {
222231
* Sets the mode of the field. When not specified {@link Mode#NULLABLE} is used.
223232
*/
224233
public Builder mode(Mode mode) {
225-
this.mode = mode;
234+
this.mode = firstNonNull(mode, Mode.NOT_SET);
226235
return this;
227236
}
228237

229238
/**
230239
* Sets the field description. The maximum length is 16K characters.
231240
*/
232241
public Builder description(String description) {
233-
this.description = description;
242+
this.description = firstNonNull(description, Data.<String>nullOf(String.class));
234243
return this;
235244
}
236245

@@ -270,14 +279,14 @@ public Type type() {
270279
* Returns the field mode. By default {@link Mode#NULLABLE} is used.
271280
*/
272281
public Mode mode() {
273-
return mode;
282+
return mode == Mode.NOT_SET ? null : mode;
274283
}
275284

276285
/**
277286
* Returns the field description.
278287
*/
279288
public String description() {
280-
return description;
289+
return Data.isNull(description) ? null : description;
281290
}
282291

283292
/**
@@ -292,11 +301,7 @@ public List<Field> fields() {
292301
* Returns a builder for the {@code Field} object.
293302
*/
294303
public Builder toBuilder() {
295-
return new Builder()
296-
.name(this.name)
297-
.type(this.type)
298-
.mode(this.mode)
299-
.description(this.description);
304+
return new Builder(this);
300305
}
301306

302307
@Override
@@ -324,7 +329,7 @@ TableFieldSchema toPb() {
324329
fieldSchemaPb.setName(name);
325330
fieldSchemaPb.setType(type.value().name());
326331
if (mode != null) {
327-
fieldSchemaPb.setMode(mode.name());
332+
fieldSchemaPb.setMode(mode == Mode.NOT_SET ? Data.<String>nullOf(String.class) : mode.name());
328333
}
329334
if (description != null) {
330335
fieldSchemaPb.setDescription(description);

0 commit comments

Comments
 (0)