schema.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2015 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package bigquery
  15. import bq "google.golang.org/api/bigquery/v2"
  16. // Schema describes the fields in a table or query result.
  17. type Schema []*FieldSchema
  18. // TODO(mcgreevy): add a function to generate a schema from a struct.
  19. type FieldSchema struct {
  20. // The field name.
  21. // Must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_),
  22. // and must start with a letter or underscore.
  23. // The maximum length is 128 characters.
  24. Name string
  25. // A description of the field. The maximum length is 16,384 characters.
  26. Description string
  27. // Whether the field may contain multiple values.
  28. Repeated bool
  29. // Whether the field is required. Ignored if Repeated is true.
  30. Required bool
  31. // The field data type. If Type is Record, then this field contains a nested schema,
  32. // which is described by Schema.
  33. Type FieldType
  34. // Describes the nested schema if Type is set to Record.
  35. Schema Schema
  36. }
  37. func (fs *FieldSchema) asTableFieldSchema() *bq.TableFieldSchema {
  38. tfs := &bq.TableFieldSchema{
  39. Description: fs.Description,
  40. Name: fs.Name,
  41. Type: string(fs.Type),
  42. }
  43. if fs.Repeated {
  44. tfs.Mode = "REPEATED"
  45. } else if fs.Required {
  46. tfs.Mode = "REQUIRED"
  47. } // else leave as default, which is interpreted as NULLABLE.
  48. for _, f := range fs.Schema {
  49. tfs.Fields = append(tfs.Fields, f.asTableFieldSchema())
  50. }
  51. return tfs
  52. }
  53. func (s Schema) asTableSchema() *bq.TableSchema {
  54. var fields []*bq.TableFieldSchema
  55. for _, f := range s {
  56. fields = append(fields, f.asTableFieldSchema())
  57. }
  58. return &bq.TableSchema{Fields: fields}
  59. }
  60. func convertTableFieldSchema(tfs *bq.TableFieldSchema) *FieldSchema {
  61. fs := &FieldSchema{
  62. Description: tfs.Description,
  63. Name: tfs.Name,
  64. Repeated: tfs.Mode == "REPEATED",
  65. Required: tfs.Mode == "REQUIRED",
  66. Type: FieldType(tfs.Type),
  67. }
  68. for _, f := range tfs.Fields {
  69. fs.Schema = append(fs.Schema, convertTableFieldSchema(f))
  70. }
  71. return fs
  72. }
  73. func convertTableSchema(ts *bq.TableSchema) Schema {
  74. var s Schema
  75. for _, f := range ts.Fields {
  76. s = append(s, convertTableFieldSchema(f))
  77. }
  78. return s
  79. }
  80. type FieldType string
  81. const (
  82. StringFieldType FieldType = "STRING"
  83. IntegerFieldType FieldType = "INTEGER"
  84. FloatFieldType FieldType = "FLOAT"
  85. BooleanFieldType FieldType = "BOOLEAN"
  86. TimestampFieldType FieldType = "TIMESTAMP"
  87. RecordFieldType FieldType = "RECORD"
  88. )