-
Notifications
You must be signed in to change notification settings - Fork 22
/
json.go
64 lines (58 loc) · 1.77 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package docs
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"github.com/cloudquery/plugin-sdk/v4/schema"
)
type jsonTable struct {
Name string `json:"name"`
Title string `json:"title"`
Description string `json:"description"`
Columns []jsonColumn `json:"columns"`
Relations []jsonTable `json:"relations"`
}
type jsonColumn struct {
Name string `json:"name"`
Type string `json:"type"`
IsPrimaryKey bool `json:"is_primary_key,omitempty"`
IsPrimaryKeyComponent bool `json:"is_primary_key_component,omitempty"`
IsIncrementalKey bool `json:"is_incremental_key,omitempty"`
}
func (g *Generator) renderTablesAsJSON(dir string) error {
jsonTables := g.jsonifyTables(g.tables)
buffer := &bytes.Buffer{}
m := json.NewEncoder(buffer)
m.SetIndent("", " ")
m.SetEscapeHTML(false)
err := m.Encode(jsonTables)
if err != nil {
return err
}
outputPath := filepath.Join(dir, "__tables.json")
return os.WriteFile(outputPath, buffer.Bytes(), 0644)
}
func (g *Generator) jsonifyTables(tables schema.Tables) []jsonTable {
jsonTables := make([]jsonTable, len(tables))
for i, table := range tables {
jsonColumns := make([]jsonColumn, len(table.Columns))
for c, col := range table.Columns {
jsonColumns[c] = jsonColumn{
Name: col.Name,
Type: col.Type.String(),
IsPrimaryKey: col.PrimaryKey,
IsPrimaryKeyComponent: col.PrimaryKeyComponent,
IsIncrementalKey: col.IncrementalKey,
}
}
jsonTables[i] = jsonTable{
Name: table.Name,
Title: g.titleTransformer(table),
Description: table.Description,
Columns: jsonColumns,
Relations: g.jsonifyTables(table.Relations),
}
}
return jsonTables
}