Skip to content

Commit 78699f4

Browse files
authored
fix: Better handling for Arrow type strings in docs (#896)
This fixes the rendering of some Arrow types in the docs, as well as in the JSON
1 parent 2884bed commit 78699f4

File tree

7 files changed

+53
-13
lines changed

7 files changed

+53
-13
lines changed

plugins/source/docs.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"sort"
1212
"text/template"
1313

14-
"github.com/apache/arrow/go/v13/arrow"
1514
"github.com/cloudquery/plugin-sdk/v3/caser"
1615
"github.com/cloudquery/plugin-sdk/v3/schema"
1716
)
@@ -128,12 +127,16 @@ type jsonColumn struct {
128127

129128
func (p *Plugin) renderTablesAsJSON(dir string, tables schema.Tables) error {
130129
jsonTables := p.jsonifyTables(tables)
131-
b, err := json.MarshalIndent(jsonTables, "", " ")
130+
buffer := &bytes.Buffer{}
131+
m := json.NewEncoder(buffer)
132+
m.SetIndent("", " ")
133+
m.SetEscapeHTML(false)
134+
err := m.Encode(jsonTables)
132135
if err != nil {
133-
return fmt.Errorf("failed to marshal tables as json: %v", err)
136+
return err
134137
}
135138
outputPath := filepath.Join(dir, "__tables.json")
136-
return os.WriteFile(outputPath, b, 0644)
139+
return os.WriteFile(outputPath, buffer.Bytes(), 0644)
137140
}
138141

139142
func (p *Plugin) jsonifyTables(tables schema.Tables) []jsonTable {
@@ -200,8 +203,7 @@ func (p *Plugin) renderAllTables(t *schema.Table, dir string) error {
200203

201204
func (p *Plugin) renderTable(table *schema.Table, dir string) error {
202205
t := template.New("").Funcs(map[string]any{
203-
"formatType": formatType,
204-
"title": p.titleTransformer,
206+
"title": p.titleTransformer,
205207
})
206208
t, err := t.New("table.md.go.tpl").ParseFS(templatesFS, "templates/table.md.go.tpl")
207209
if err != nil {
@@ -228,10 +230,6 @@ func formatMarkdown(s string) string {
228230
return reMatchHeaders.ReplaceAllString(s, `$1`+"\n\n")
229231
}
230232

231-
func formatType(v arrow.DataType) string {
232-
return v.String()
233-
}
234-
235233
func indentToDepth(table *schema.Table) string {
236234
s := ""
237235
t := table

plugins/source/docs_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/apache/arrow/go/v13/arrow"
1111
"github.com/bradleyjkemp/cupaloy/v2"
1212
"github.com/cloudquery/plugin-sdk/v3/schema"
13+
"github.com/cloudquery/plugin-sdk/v3/types"
1314
"github.com/stretchr/testify/require"
1415
)
1516

@@ -32,6 +33,22 @@ var testTables = []*schema.Table{
3233
Type: arrow.PrimitiveTypes.Int64,
3334
PrimaryKey: true,
3435
},
36+
{
37+
Name: "json_col",
38+
Type: types.ExtensionTypes.JSON,
39+
},
40+
{
41+
Name: "list_col",
42+
Type: arrow.ListOf(arrow.PrimitiveTypes.Int64),
43+
},
44+
{
45+
Name: "map_col",
46+
Type: arrow.MapOf(arrow.BinaryTypes.String, arrow.PrimitiveTypes.Int64),
47+
},
48+
{
49+
Name: "struct_col",
50+
Type: arrow.StructOf(arrow.Field{Name: "string_field", Type: arrow.BinaryTypes.String}, arrow.Field{Name: "int_field", Type: arrow.PrimitiveTypes.Int64}),
51+
},
3552
},
3653
Relations: []*schema.Table{
3754
{

plugins/source/templates/table.md.go.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ The following tables depend on {{.Name}}:
4040
| Name | Type |
4141
| ------------- | ------------- |
4242
{{- range $column := $.Columns }}
43-
|{{$column.Name}}{{if $column.PrimaryKey}} (PK){{end}}{{if $column.IncrementalKey}} (Incremental Key){{end}}|{{$column.Type | formatType}}|
43+
|{{$column.Name}}{{if $column.PrimaryKey}} (PK){{end}}{{if $column.IncrementalKey}} (Incremental Key){{end}}|{{$column.Type}}|
4444
{{- end }}

plugins/source/testdata/TestGeneratePluginDocs-JSON-__tables.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@
7272
"name": "id_col2",
7373
"type": "int64",
7474
"is_primary_key": true
75+
},
76+
{
77+
"name": "json_col",
78+
"type": "json"
79+
},
80+
{
81+
"name": "list_col",
82+
"type": "list<item: int64, nullable>"
83+
},
84+
{
85+
"name": "map_col",
86+
"type": "map<utf8, int64, items_nullable>"
87+
},
88+
{
89+
"name": "struct_col",
90+
"type": "struct<string_field: utf8, int_field: int64>"
7591
}
7692
],
7793
"relations": [
@@ -195,3 +211,4 @@
195211
]
196212
}
197213
]
214+

plugins/source/testdata/TestGeneratePluginDocs-Markdown-test_table.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ The following tables depend on test_table:
2323
|int_col|int64|
2424
|id_col (PK)|int64|
2525
|id_col2 (PK)|int64|
26+
|json_col|json|
27+
|list_col|list<item: int64, nullable>|
28+
|map_col|map<utf8, int64, items_nullable>|
29+
|struct_col|struct<string_field: utf8, int_field: int64>|

types/inet.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ func (*InetType) ExtensionName() string {
207207
return "inet"
208208
}
209209

210+
func (*InetType) String() string {
211+
return "inet"
212+
}
213+
210214
// Serialize returns "inet-serialized" for testing proper metadata passing
211215
func (*InetType) Serialize() string {
212216
return "inet-serialized"

types/json.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ func (*JSONType) ExtensionName() string {
209209
return "json"
210210
}
211211

212-
func (e *JSONType) String() string {
213-
return fmt.Sprintf("extension_type<storage=%s>", e.Storage)
212+
func (*JSONType) String() string {
213+
return "json"
214214
}
215215

216216
func (e *JSONType) MarshalJSON() ([]byte, error) {

0 commit comments

Comments
 (0)