File: toSchema.go

package info (click to toggle)
golang-github-seandolphin-bqschema 0.0~git20150424.0.f92a08f-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 108 kB
  • sloc: makefile: 2
file content (151 lines) | stat: -rw-r--r-- 3,414 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package bqschema

import (
	"errors"
	"reflect"
	"strings"
	"time"

	"google.golang.org/api/bigquery/v2"
)

var (
	ArrayOfArray = errors.New("Array of Arrays not allowed")
	UnknownType  = errors.New("Unknown type")
	NotStruct    = errors.New("Can not convert non structs")
)

func ToSchema(src interface{}) (*bigquery.TableSchema, error) {
	value := reflect.ValueOf(src)
	t := value.Type()

	schema := &bigquery.TableSchema{}

	if t.Kind() == reflect.Struct {
		schema.Fields = make([]*bigquery.TableFieldSchema, 0, t.NumField())
		for i := 0; i < t.NumField(); i++ {
			sf := t.Field(i)
			if sf.PkgPath != "" { // unexported
				continue
			}
			v := pointerGuard(value.Field(i))

			var name string
			jsonTag := sf.Tag.Get("json")
			switch jsonTag {
			case "-":
				continue
			case "":
				name = sf.Name
			default:
				name = strings.Split(jsonTag, ",")[0]
			}

			tfs := &bigquery.TableFieldSchema{
				Mode: "required",
				Name: name,
				Type: "",
			}
			schema.Fields = append(schema.Fields, tfs)

			kind := v.Kind()
			t, isSimple := simpleType(kind)

			if isSimple {
				tfs.Type = t
			} else {
				switch kind {
				case reflect.Struct:
					tfs.Mode = "nullable"
					if t, fields, err := structConversion(v.Interface()); err == nil {
						tfs.Type = t
						if t == "string" {
							tfs.Mode = "required"
						}
						tfs.Fields = fields
					} else {
						return schema, err
					}
				case reflect.Array, reflect.Slice:
					tfs.Mode = "repeated"
					subKind := pointerGuard(v.Type().Elem()).Kind()
					t, isSimple := simpleType(subKind)
					if isSimple {
						schema.Fields[i].Type = t
					} else if subKind == reflect.Struct {
						subStruct := reflect.Zero(pointerGuard(v.Type().Elem()).Type()).Interface()
						if t, fields, err := structConversion(subStruct); err == nil {
							schema.Fields[i].Type = t
							schema.Fields[i].Fields = fields

						} else {
							return schema, err
						}
					} else {
						return schema, ArrayOfArray
					}
				default:
					return schema, UnknownType
				}
			}
		}
	} else {
		return schema, NotStruct
	}

	return schema, nil
}

func MustToSchema(src interface{}) *bigquery.TableSchema {
	schema, err := ToSchema(src)
	if err != nil {
		panic(err)
	}
	return schema
}

func simpleType(kind reflect.Kind) (string, bool) {
	isSimple := true
	t := ""
	switch kind {
	case reflect.Bool:
		t = "boolean"
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		t = "integer"
	case reflect.Float32, reflect.Float64:
		t = "float"
	case reflect.String:
		t = "string"
	default:
		isSimple = false
	}
	return t, isSimple
}

func structConversion(src interface{}) (string, []*bigquery.TableFieldSchema, error) {
	v := reflect.ValueOf(src)
	if v.Type().Name() == "Key" && strings.Contains(v.Type().PkgPath(), "appengine") {
		return "string", nil, nil
	} else if v.Type().ConvertibleTo(reflect.TypeOf(time.Time{})) {
		return "timestamp", nil, nil
	} else {
		schema, err := ToSchema(src)
		return "record", schema.Fields, err
	}
}

func pointerGuard(i interface{}) reflect.Value {
	var v reflect.Value
	var ok bool
	v, ok = i.(reflect.Value)
	if !ok {
		if t, ok := i.(reflect.Type); ok {
			v = reflect.Indirect(reflect.New(t))
		}
	}

	if v.Kind() == reflect.Ptr {
		v = reflect.Indirect(reflect.New(v.Type().Elem()))
	}
	return v
}