Skip to content

Commit

Permalink
bigquery: support BYTES data type
Browse files Browse the repository at this point in the history
Change-Id: I30d78682a67773f58ed8d07cd0cec0175d21410b
Reviewed-on: https://code-review.googlesource.com/9556
Reviewed-by: Michael Darakananda <[email protected]>
Reviewed-by: Sarah Adams <[email protected]>
  • Loading branch information
jba committed Nov 28, 2016
1 parent 7bb67ec commit 648bc87
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions bigquery/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ type FieldType string

const (
StringFieldType FieldType = "STRING"
BytesFieldType FieldType = "BYTES"
IntegerFieldType FieldType = "INTEGER"
FloatFieldType FieldType = "FLOAT"
BooleanFieldType FieldType = "BOOLEAN"
Expand Down
3 changes: 3 additions & 0 deletions bigquery/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package bigquery

import (
"encoding/base64"
"errors"
"fmt"
"strconv"
Expand Down Expand Up @@ -194,6 +195,8 @@ func convertBasicType(val string, typ FieldType) (Value, error) {
switch typ {
case StringFieldType:
return val, nil
case BytesFieldType:
return base64.StdEncoding.DecodeString(val)
case IntegerFieldType:
return strconv.ParseInt(val, 10, 64)
case FloatFieldType:
Expand Down
5 changes: 4 additions & 1 deletion bigquery/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package bigquery

import (
"encoding/base64"
"fmt"
"reflect"
"testing"
Expand All @@ -29,20 +30,22 @@ func TestConvertBasicValues(t *testing.T) {
{Type: IntegerFieldType},
{Type: FloatFieldType},
{Type: BooleanFieldType},
{Type: BytesFieldType},
}
row := &bq.TableRow{
F: []*bq.TableCell{
{V: "a"},
{V: "1"},
{V: "1.2"},
{V: "true"},
{V: base64.StdEncoding.EncodeToString([]byte("foo"))},
},
}
got, err := convertRow(row, schema)
if err != nil {
t.Fatalf("error converting: %v", err)
}
want := []Value{"a", int64(1), 1.2, true}
want := []Value{"a", int64(1), 1.2, true, []byte("foo")}
if !reflect.DeepEqual(got, want) {
t.Errorf("converting basic values: got:\n%v\nwant:\n%v", got, want)
}
Expand Down

0 comments on commit 648bc87

Please sign in to comment.