Skip to content

Commit

Permalink
Merge pull request #650 from k1LoW/fix-jsonschema
Browse files Browse the repository at this point in the history
Fix JSON Schema
  • Loading branch information
k1LoW authored Jan 26, 2025
2 parents f542f95 + d7d5854 commit 75fefd8
Show file tree
Hide file tree
Showing 18 changed files with 62 additions and 29 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ test_ext_driver: build

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

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

prerelease:
git pull origin --tag
Expand Down
2 changes: 1 addition & 1 deletion sample/dict/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/exclude/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/font/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/hide/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/hide_not_related_column/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/mermaid/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/mssql/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/mysql/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/number/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/png/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/viewpoints/schema.json

Large diffs are not rendered by default.

41 changes: 39 additions & 2 deletions schema/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type SchemaJSON struct {
Relations []*RelationJSON `json:"relations,omitempty"`
Functions []*Function `json:"functions,omitempty"`
Enums []*Enum `json:"enums,omitempty"`
Driver *Driver `json:"driver,omitempty"`
Driver *DriverJSON `json:"driver,omitempty"`
Labels Labels `json:"labels,omitempty"`
Viewpoints Viewpoints `json:"viewpoints,omitempty"`
}
Expand Down Expand Up @@ -54,6 +54,18 @@ type RelationJSON struct {
Virtual bool `json:"virtual,omitempty"`
}

type DriverJSON struct {
Name string `json:"name"`
DatabaseVersion string `json:"database_version,omitempty" yaml:"databaseVersion,omitempty"`
Meta *DriverMetaJSON `json:"meta,omitempty"`
}

type DriverMetaJSON struct {
CurrentSchema string `json:"current_schema,omitempty" yaml:"currentSchema,omitempty"`
SearchPaths []string `json:"search_paths,omitempty" yaml:"searchPaths,omitempty"`
Dict map[string]string `json:"dict,omitempty"`
}

// ToJSONObjct convert schema.Schema to JSON object
func (s Schema) ToJSONObject() SchemaJSON {
var tables []*TableJSON
Expand All @@ -73,7 +85,7 @@ func (s Schema) ToJSONObject() SchemaJSON {
Relations: relations,
Functions: s.Functions,
Enums: s.Enums,
Driver: s.Driver,
Driver: s.Driver.ToJSONObject(),
Labels: s.Labels,
Viewpoints: s.Viewpoints,
}
Expand Down Expand Up @@ -140,6 +152,31 @@ func (r Relation) ToJSONObject() RelationJSON {
}
}

func (d *Driver) ToJSONObject() *DriverJSON {
if d == nil {
return nil
}
return &DriverJSON{
Name: d.Name,
DatabaseVersion: d.DatabaseVersion,
Meta: d.Meta.ToJSONObject(),
}
}

func (d *DriverMeta) ToJSONObject() *DriverMetaJSON {
if d == nil {
return nil
}
m := &DriverMetaJSON{
CurrentSchema: d.CurrentSchema,
SearchPaths: d.SearchPaths,
}
if d.Dict != nil {
m.Dict = d.Dict.Dump()
}
return m
}

// MarshalJSON return custom JSON byte
func (s Schema) MarshalJSON() ([]byte, error) {
ss := s.ToJSONObject()
Expand Down
2 changes: 1 addition & 1 deletion schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ type Constraint struct {
Def string `json:"def"`
Table *string `json:"table"`
ReferencedTable *string `json:"referenced_table,omitempty" yaml:"referencedTable,omitempty"`
Columns []string `json:"columns"`
Columns []string `json:"columns,omitempty"`
ReferencedColumns []string `json:"referenced_columns,omitempty" yaml:"referencedColumns,omitempty"`
Comment string `json:"comment,omitempty"`
}
Expand Down
3 changes: 1 addition & 2 deletions scripts/jsonschema/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func _main() error {
if err != nil {
return err
}
fmt.Println(string(b))
fmt.Println()
fmt.Print(string(b))
return nil
}
16 changes: 6 additions & 10 deletions spec/tbls.schema.json_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,9 @@
"name",
"type",
"def",
"table",
"columns"
"table"
]
},
"Dict": {
"properties": {},
"additionalProperties": false,
"type": "object"
},
"Driver": {
"properties": {
"name": {
Expand Down Expand Up @@ -120,7 +114,10 @@
"type": "array"
},
"dict": {
"$ref": "#/$defs/Dict"
"additionalProperties": {
"type": "string"
},
"type": "object"
}
},
"additionalProperties": false,
Expand Down Expand Up @@ -475,5 +472,4 @@
"type": "array"
}
}
}

}
3 changes: 1 addition & 2 deletions testdata/json_output_schema.golden
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
"name": "PRIMARY",
"type": "",
"def": "PRIMARY KEY (a)",
"table": "a",
"columns": null
"table": "a"
}
],
"triggers": [
Expand Down
1 change: 0 additions & 1 deletion testdata/yaml_output_schema.golden
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ tables:
type: ""
def: PRIMARY KEY (a)
table: a
columns: []
triggers:
- name: update_a_a2
def: CREATE CONSTRAINT TRIGGER update_a_a2 AFTER INSERT OR UPDATE ON a
Expand Down

0 comments on commit 75fefd8

Please sign in to comment.