-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtable_options.go
More file actions
52 lines (48 loc) · 1.6 KB
/
table_options.go
File metadata and controls
52 lines (48 loc) · 1.6 KB
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
package docs
import (
"encoding/json"
"errors"
"reflect"
"regexp"
"strings"
schemaDocs "github.com/cloudquery/codegen/jsonschema/docs"
"github.com/cloudquery/plugin-sdk/v4/schema"
invoschema "github.com/invopop/jsonschema"
)
func TableOptionsDescriptionTransformer(tableOptions any, jsonSchema string) (schema.Transform, error) {
var sc invoschema.Schema
if err := json.Unmarshal([]byte(jsonSchema), &sc); err != nil {
return nil, err
}
tableNamesToOptionsDocs := make(map[string]string)
tableOptionsType := reflect.ValueOf(tableOptions).Elem().Type()
for i := range tableOptionsType.NumField() {
field := tableOptionsType.Field(i)
fieldType := field.Type.String()
if strings.Contains(fieldType, ".") {
fieldType = strings.Split(fieldType, ".")[1]
}
defValue, ok := sc.Definitions[fieldType]
if !ok {
return nil, errors.New("definition not found for " + field.Name)
}
tableName := strings.Split(field.Tag.Get("json"), ",")[0]
if tableName == "" {
return nil, errors.New("json tag not found for table " + field.Name)
}
newRoot := sc
newRoot.ID = "Table Options"
newRoot.Ref = "#/$defs/" + "Table Options"
newRoot.Definitions["Table Options"] = defValue
sch, _ := json.Marshal(newRoot)
doc, _ := schemaDocs.Generate(sch, 1)
tocRegex := regexp.MustCompile(`# Table of contents[\s\S]+?##`)
tableNamesToOptionsDocs[tableName] = tocRegex.ReplaceAllString(doc, "##")
}
return func(table *schema.Table) error {
if tableNamesToOptionsDocs[table.Name] != "" {
table.Description = table.Description + "\n\n" + tableNamesToOptionsDocs[table.Name]
}
return nil
}, nil
}