77 "encoding/json"
88 "fmt"
99 "github.com/odpf/meteor/models"
10+ facetsv1beta1 "github.com/odpf/meteor/models/odpf/assets/facets/v1beta1"
1011 assetsv1beta1 "github.com/odpf/meteor/models/odpf/assets/v1beta1"
1112 "github.com/odpf/meteor/plugins"
1213 "github.com/odpf/meteor/registry"
@@ -15,7 +16,6 @@ import (
1516 "github.com/pkg/errors"
1617 "io/ioutil"
1718 "net/http"
18- "strconv"
1919 "strings"
2020)
2121
@@ -155,60 +155,45 @@ func (s *Sink) buildJsonStencilPayload(table *assetsv1beta1.Table) (JsonSchema,
155155 return record , nil
156156}
157157
158- func (s * Sink ) buildJsonProperties (table * assetsv1beta1.Table ) (properties [] map [string ]Property ) {
158+ func (s * Sink ) buildJsonProperties (table * assetsv1beta1.Table ) (columnRecord map [string ]Property ) {
159159 columns := table .GetSchema ().GetColumns ()
160160 if columns == nil {
161161 return
162162 }
163163
164164 for _ , column := range columns {
165- var dataType JsonType
166- columnRecord := make ( map [ string ] Property )
165+ dataType := s . typeToJsonSchemaType ( table , column )
166+ columnType := [] JsonType { dataType }
167167
168+ if column .IsNullable {
169+ columnType = []JsonType {dataType , JsonTypeNull }
170+ }
171+
172+ columnRecord [column .Name ] = Property {
173+ Type : columnType ,
174+ Description : column .GetDescription (),
175+ }
176+ }
177+
178+ return
179+ }
180+
181+ func (s * Sink ) typeToJsonSchemaType (table * assetsv1beta1.Table , column * facetsv1beta1.Column ) (dataType JsonType ) {
182+ if table .GetResource ().GetService () == "bigquery" {
168183 switch column .DataType {
169- case "varchar " , "text " , "char " :
184+ case "STRING " , "DATE " , "DATETIME" , "TIME" , "TIMESTAMP" , "GEOGRAPHY " :
170185 dataType = JsonTypeString
171- case "bigint " , "int " , "tinyint " :
186+ case "INT64 " , "NUMERIC " , "FLOAT64" , "INT" , "FLOAT" , "BIGNUMERIC " :
172187 dataType = JsonTypeNumber
173- case "array " :
188+ case "BYTES " :
174189 dataType = JsonTypeArray
175- case "object" :
176- dataType = JsonTypeObject
177- case "bool" , "boolean" :
190+ case "BOOLEAN" :
178191 dataType = JsonTypeBoolean
192+ case "RECORD" :
193+ dataType = JsonTypeObject
179194 default :
180195 dataType = JsonTypeString
181196 }
182-
183- columnRecord [column .Profile .String ()] = Property {
184- Type : JsonTypeString ,
185- Description : "The profile of the column" ,
186- }
187- columnRecord [column .Name ] = Property {
188- Type : JsonTypeString ,
189- Description : "The name of the column" ,
190- }
191- //columnRecord[column.Properties.String()] = Property{
192- // Type: "",
193- // Description: "",
194- //}
195- columnRecord [column .Description ] = Property {
196- Type : JsonTypeString ,
197- Description : "The description of the column" ,
198- }
199- columnRecord [strconv .Itoa (int (column .Length ))] = Property {
200- Type : JsonTypeNumber ,
201- Description : "The length of the column" ,
202- }
203- columnRecord [strconv .FormatBool (column .IsNullable )] = Property {
204- Type : JsonTypeBoolean ,
205- Description : "The format of the column" ,
206- }
207- columnRecord [column .DataType ] = Property {
208- Type : dataType ,
209- Description : "The type of the column" ,
210- }
211- properties = append (properties , columnRecord )
212197 }
213198
214199 return
0 commit comments