Skip to content

Commit d8a5711

Browse files
authored
feat!: Remove withComment for codegen (#108)
reading comments from source code is super slow and this is public api which we dont want to support. The best practice/guideline should be just pointing to the original API and not duplicating go documentation to our docs.
1 parent 444466f commit d8a5711

File tree

2 files changed

+3
-45
lines changed

2 files changed

+3
-45
lines changed

codegen/golang.go

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@ package codegen
33
import (
44
"embed"
55
"fmt"
6-
"go/ast"
76
"io"
87
"reflect"
9-
"strings"
108
"text/template"
119
"unicode"
1210

1311
"github.com/cloudquery/plugin-sdk/schema"
1412
"github.com/iancoleman/strcase"
15-
"golang.org/x/tools/go/packages"
1613
)
1714

1815
type TableOptions func(*TableDefinition)
@@ -75,12 +72,6 @@ func WithExtraColumns(columns []ColumnDefinition) TableOptions {
7572
}
7673
}
7774

78-
func WithDescriptionsEnabled() TableOptions {
79-
return func(t *TableDefinition) {
80-
t.descriptionsEnabled = true
81-
}
82-
}
83-
8475
func defaultTransformer(name string) string {
8576
return strcase.ToSnake(name)
8677
}
@@ -111,11 +102,6 @@ func NewTableFromStruct(name string, obj interface{}, opts ...TableOptions) (*Ta
111102
return nil, fmt.Errorf("expected struct, got %s", e.Kind())
112103
}
113104

114-
comments := make(map[string]string)
115-
if t.descriptionsEnabled {
116-
comments = readStructComments(e.Type().PkgPath(), e.Type().Name())
117-
}
118-
119105
t.Columns = append(t.Columns, t.extraColumns...)
120106

121107
for i := 0; i < e.NumField(); i++ {
@@ -139,10 +125,9 @@ func NewTableFromStruct(name string, obj interface{}, opts ...TableOptions) (*Ta
139125
// generate a PathResolver to use by default
140126
pathResolver := fmt.Sprintf("schema.PathResolver(%q)", field.Name)
141127
column := ColumnDefinition{
142-
Name: t.nameTransformer(field.Name),
143-
Type: columnType,
144-
Resolver: pathResolver,
145-
Description: strings.ReplaceAll(comments[field.Name], "`", "'"),
128+
Name: t.nameTransformer(field.Name),
129+
Type: columnType,
130+
Resolver: pathResolver,
146131
}
147132
t.Columns = append(t.Columns, column)
148133
}
@@ -161,29 +146,3 @@ func (t *TableDefinition) GenerateTemplate(wr io.Writer) error {
161146
}
162147
return nil
163148
}
164-
165-
func readStructComments(pkgPath string, structName string) map[string]string {
166-
cfg := &packages.Config{Mode: packages.NeedFiles | packages.NeedSyntax}
167-
pkgs, err := packages.Load(cfg, pkgPath)
168-
if err != nil {
169-
panic(err)
170-
}
171-
comments := make(map[string]string, 0)
172-
for _, p := range pkgs {
173-
for _, f := range p.Syntax {
174-
ast.Inspect(f, func(n ast.Node) bool {
175-
if st, ok := n.(*ast.TypeSpec); ok {
176-
if st.Name.Name == structName {
177-
for _, field := range st.Type.(*ast.StructType).Fields.List {
178-
if len(field.Names) > 0 {
179-
comments[field.Names[0].Name] = field.Doc.Text()
180-
}
181-
}
182-
}
183-
}
184-
return true
185-
})
186-
}
187-
}
188-
return comments
189-
}

codegen/table.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ type TableDefinition struct {
2323
nameTransformer func(string) string
2424
skipFields []string
2525
extraColumns ColumnDefinitions
26-
descriptionsEnabled bool
2726
}
2827

2928
type ColumnDefinitions []ColumnDefinition

0 commit comments

Comments
 (0)