Skip to content

Commit 75fefd8

Browse files
authored
Merge pull request #650 from k1LoW/fix-jsonschema
Fix JSON Schema
2 parents f542f95 + d7d5854 commit 75fefd8

File tree

18 files changed

+62
-29
lines changed

18 files changed

+62
-29
lines changed

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ test_ext_driver: build
152152

153153
test_jsonschema:
154154
cd scripts/jsonschema && go run main.go | diff -u ../../spec/tbls.schema.json_schema.json -
155+
jv spec/tbls.schema.json_schema.json --assert-content sample/mysql/schema.json
156+
jv spec/tbls.schema.json_schema.json --assert-content sample/postgres/schema.json
155157

156158
generate_jsonschema:
157159
cd scripts/jsonschema && go run main.go > ../../spec/tbls.schema.json_schema.json
@@ -172,6 +174,7 @@ depsdev:
172174
go install github.com/xo/usql@latest
173175
go install github.com/Songmu/gocredits/cmd/gocredits@latest
174176
go install github.com/securego/gosec/v2/cmd/gosec@latest
177+
go install github.com/santhosh-tekuri/jsonschema/cmd/jv@latest
175178

176179
prerelease:
177180
git pull origin --tag

sample/dict/schema.json

+1-1
Large diffs are not rendered by default.

sample/exclude/schema.json

+1-1
Large diffs are not rendered by default.

sample/font/schema.json

+1-1
Large diffs are not rendered by default.

sample/hide/schema.json

+1-1
Large diffs are not rendered by default.

sample/hide_not_related_column/schema.json

+1-1
Large diffs are not rendered by default.

sample/mermaid/schema.json

+1-1
Large diffs are not rendered by default.

sample/mssql/schema.json

+1-1
Large diffs are not rendered by default.

sample/mysql/schema.json

+1-1
Large diffs are not rendered by default.

sample/number/schema.json

+1-1
Large diffs are not rendered by default.

sample/png/schema.json

+1-1
Large diffs are not rendered by default.

sample/viewpoints/schema.json

+1-1
Large diffs are not rendered by default.

schema/json.go

+39-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type SchemaJSON struct {
1212
Relations []*RelationJSON `json:"relations,omitempty"`
1313
Functions []*Function `json:"functions,omitempty"`
1414
Enums []*Enum `json:"enums,omitempty"`
15-
Driver *Driver `json:"driver,omitempty"`
15+
Driver *DriverJSON `json:"driver,omitempty"`
1616
Labels Labels `json:"labels,omitempty"`
1717
Viewpoints Viewpoints `json:"viewpoints,omitempty"`
1818
}
@@ -54,6 +54,18 @@ type RelationJSON struct {
5454
Virtual bool `json:"virtual,omitempty"`
5555
}
5656

57+
type DriverJSON struct {
58+
Name string `json:"name"`
59+
DatabaseVersion string `json:"database_version,omitempty" yaml:"databaseVersion,omitempty"`
60+
Meta *DriverMetaJSON `json:"meta,omitempty"`
61+
}
62+
63+
type DriverMetaJSON struct {
64+
CurrentSchema string `json:"current_schema,omitempty" yaml:"currentSchema,omitempty"`
65+
SearchPaths []string `json:"search_paths,omitempty" yaml:"searchPaths,omitempty"`
66+
Dict map[string]string `json:"dict,omitempty"`
67+
}
68+
5769
// ToJSONObjct convert schema.Schema to JSON object
5870
func (s Schema) ToJSONObject() SchemaJSON {
5971
var tables []*TableJSON
@@ -73,7 +85,7 @@ func (s Schema) ToJSONObject() SchemaJSON {
7385
Relations: relations,
7486
Functions: s.Functions,
7587
Enums: s.Enums,
76-
Driver: s.Driver,
88+
Driver: s.Driver.ToJSONObject(),
7789
Labels: s.Labels,
7890
Viewpoints: s.Viewpoints,
7991
}
@@ -140,6 +152,31 @@ func (r Relation) ToJSONObject() RelationJSON {
140152
}
141153
}
142154

155+
func (d *Driver) ToJSONObject() *DriverJSON {
156+
if d == nil {
157+
return nil
158+
}
159+
return &DriverJSON{
160+
Name: d.Name,
161+
DatabaseVersion: d.DatabaseVersion,
162+
Meta: d.Meta.ToJSONObject(),
163+
}
164+
}
165+
166+
func (d *DriverMeta) ToJSONObject() *DriverMetaJSON {
167+
if d == nil {
168+
return nil
169+
}
170+
m := &DriverMetaJSON{
171+
CurrentSchema: d.CurrentSchema,
172+
SearchPaths: d.SearchPaths,
173+
}
174+
if d.Dict != nil {
175+
m.Dict = d.Dict.Dump()
176+
}
177+
return m
178+
}
179+
143180
// MarshalJSON return custom JSON byte
144181
func (s Schema) MarshalJSON() ([]byte, error) {
145182
ss := s.ToJSONObject()

schema/schema.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ type Constraint struct {
102102
Def string `json:"def"`
103103
Table *string `json:"table"`
104104
ReferencedTable *string `json:"referenced_table,omitempty" yaml:"referencedTable,omitempty"`
105-
Columns []string `json:"columns"`
105+
Columns []string `json:"columns,omitempty"`
106106
ReferencedColumns []string `json:"referenced_columns,omitempty" yaml:"referencedColumns,omitempty"`
107107
Comment string `json:"comment,omitempty"`
108108
}

scripts/jsonschema/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ func _main() error {
3131
if err != nil {
3232
return err
3333
}
34-
fmt.Println(string(b))
35-
fmt.Println()
34+
fmt.Print(string(b))
3635
return nil
3736
}

spec/tbls.schema.json_schema.json

+6-10
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,9 @@
8181
"name",
8282
"type",
8383
"def",
84-
"table",
85-
"columns"
84+
"table"
8685
]
8786
},
88-
"Dict": {
89-
"properties": {},
90-
"additionalProperties": false,
91-
"type": "object"
92-
},
9387
"Driver": {
9488
"properties": {
9589
"name": {
@@ -120,7 +114,10 @@
120114
"type": "array"
121115
},
122116
"dict": {
123-
"$ref": "#/$defs/Dict"
117+
"additionalProperties": {
118+
"type": "string"
119+
},
120+
"type": "object"
124121
}
125122
},
126123
"additionalProperties": false,
@@ -475,5 +472,4 @@
475472
"type": "array"
476473
}
477474
}
478-
}
479-
475+
}

testdata/json_output_schema.golden

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434
"name": "PRIMARY",
3535
"type": "",
3636
"def": "PRIMARY KEY (a)",
37-
"table": "a",
38-
"columns": null
37+
"table": "a"
3938
}
4039
],
4140
"triggers": [

testdata/yaml_output_schema.golden

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ tables:
2323
type: ""
2424
def: PRIMARY KEY (a)
2525
table: a
26-
columns: []
2726
triggers:
2827
- name: update_a_a2
2928
def: CREATE CONSTRAINT TRIGGER update_a_a2 AFTER INSERT OR UPDATE ON a

0 commit comments

Comments
 (0)