Skip to content

Commit

Permalink
Merge pull request #9 from lovoo/bbq
Browse files Browse the repository at this point in the history
bbq: use schema filter for value export
  • Loading branch information
Jan Bickel authored Apr 27, 2020
2 parents 906d4a6 + 110aad5 commit 6f7e1dc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
5 changes: 3 additions & 2 deletions bbq/bbq.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ func convertToMapReflect(value reflect.Value) map[string]bigquery.Value {
values := make(map[string]bigquery.Value, value.NumField())

for i := 0; i < value.NumField(); i++ {
// field is ignored for marshalling, so let's not export it
if value.Type().Field(i).Tag.Get("json") == "-" {

if !useFieldForExport(value.Type().Field(i)) {
continue
}

switch value.Field(i).Kind() {
case reflect.Ptr:
if !value.Field(i).IsNil() && value.Field(i).Elem().Kind() == reflect.Struct {
Expand Down
28 changes: 19 additions & 9 deletions bbq/infer_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,8 @@ func inferFields(rt reflect.Type) (bigquery.Schema, error) {

for i := 0; i < rt.NumField(); i++ {
field := rt.Field(i)
// field is ignored for marshalling, so let's not export it
if field.Tag.Get("json") == "-" {
continue
}
if field.Name == "" {
continue
}

// if the field is not exported, drop it
if strings.ToUpper(field.Name[:1]) != field.Name[:1] {
if !useFieldForExport(field) {
continue
}

Expand All @@ -154,6 +146,24 @@ func isSupportedIntType(t reflect.Type) bool {
}
}

// Checks whether a field should be exported. This is both used for the mapping and for
// inferring the schema
func useFieldForExport(field reflect.StructField) bool {
// field is ignored for marshalling, so let's not export it
if field.Tag.Get("json") == "-" {
return false
}
if field.Name == "" {
return false
}

// if the field is not exported, drop it
if strings.ToUpper(field.Name[:1]) != field.Name[:1] {
return false
}
return true
}

// typeList is a linked list of reflect.Types.
type typeList struct {
t reflect.Type
Expand Down

0 comments on commit 6f7e1dc

Please sign in to comment.