Skip to content

Commit 87b557d

Browse files
committed
refactor: update json schema type
1 parent 444980a commit 87b557d

File tree

2 files changed

+35
-49
lines changed

2 files changed

+35
-49
lines changed

plugins/sinks/stencil/sink.go

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
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

plugins/sinks/stencil/stencil.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@ type JsonType string
55
const (
66
JsonTypeObject JsonType = "object"
77
JsonTypeString JsonType = "string"
8-
JsonTypeNumber JsonType = "integer"
8+
JsonTypeNumber JsonType = "number"
99
JsonTypeArray JsonType = "array"
10-
JsonTypeBoolean JsonType = "bool"
10+
JsonTypeBoolean JsonType = "boolean"
11+
JsonTypeNull JsonType = "null"
1112
)
1213

1314
type JsonSchema struct {
14-
Id string `json:"$id"`
15-
Schema string `json:"$schema"`
16-
Title string `json:"title"`
17-
Type JsonType `json:"type"`
18-
Properties []map[string]Property `json:"properties"`
15+
Id string `json:"$id"`
16+
Schema string `json:"$schema"`
17+
Title string `json:"title"`
18+
Type JsonType `json:"type"`
19+
Properties map[string]Property `json:"properties"`
1920
}
2021

2122
type Property struct {
22-
Type JsonType `json:"type"`
23-
Description string `json:"description"`
23+
Type []JsonType `json:"type"`
24+
Description string `json:"description"`
2425
}

0 commit comments

Comments
 (0)