Skip to content

Commit 91f6f37

Browse files
authored
feat: add GoParser.ExcludeCustom method (#39)
1 parent 886677e commit 91f6f37

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

convert.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type GoParser struct {
2828
Pkgs map[string]*packages.Package
2929
Reference map[string]bool
3030
Prefix map[string]string
31+
Skips map[string]struct{}
3132

3233
// referencedTypes is a map of all types that are referenced by the generated
3334
// packages. This is to generated referenced types on demand.
@@ -66,9 +67,10 @@ func NewGolangParser() (*GoParser, error) {
6667
fileSet: fileSet,
6768
config: config,
6869
Pkgs: make(map[string]*packages.Package),
69-
Reference: map[string]bool{},
70+
Reference: make(map[string]bool),
7071
referencedTypes: newReferencedTypes(),
71-
Prefix: map[string]string{},
72+
Prefix: make(map[string]string),
73+
Skips: make(map[string]struct{}),
7274
typeOverrides: map[string]TypeOverride{
7375
// Some hard coded defaults
7476
"error": func() bindings.ExpressionType {
@@ -116,6 +118,14 @@ func (p *GoParser) IncludeCustom(mappings map[GolangType]GolangType) error {
116118
return nil
117119
}
118120

121+
// ExcludeCustom flags golang types to not be generated in the Typescript output.
122+
func (p *GoParser) ExcludeCustom(fqnames ...string) error {
123+
for _, fqname := range fqnames {
124+
p.Skips[fqname] = struct{}{}
125+
}
126+
return nil
127+
}
128+
119129
// IncludeGenerate parses a directory and adds the parsed package to the list of packages.
120130
// These package's types will be generated.
121131
func (p *GoParser) IncludeGenerate(directory string) error {
@@ -166,6 +176,7 @@ func (p *GoParser) ToTypescript() (*Typescript, error) {
166176
typescript := &Typescript{
167177
typescriptNodes: make(map[string]*typescriptNode),
168178
parsed: p,
179+
skip: p.Skips,
169180
}
170181

171182
// Parse all go types to the typescript AST
@@ -200,6 +211,7 @@ type Typescript struct {
200211
// TODO: the key "string" should be replaced with "Identifier"
201212
typescriptNodes map[string]*typescriptNode
202213
parsed *GoParser
214+
skip map[string]struct{}
203215
// Do not allow calling serialize more than once.
204216
// The call affects the state.
205217
serialized bool
@@ -251,6 +263,11 @@ func (ts *Typescript) parseGolangIdentifiers() error {
251263
continue
252264
}
253265

266+
// Skip by qualified name
267+
if _, ok := ts.skip[obj.Type().String()]; ok {
268+
continue
269+
}
270+
254271
if ts.parsed.Reference[pkg.PkgPath] {
255272
if !ts.parsed.referencedTypes.IsReferenced(obj) {
256273
continue

convert_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ func TestGeneration(t *testing.T) {
8484
case "testdata/anyreference":
8585
err = gen.IncludeReference("github.com/coder/guts/testdata/prefix", "Prefix")
8686
require.NoErrorf(t, err, "include %q", dir)
87+
case "testdata/excludecustom":
88+
err = gen.ExcludeCustom("github.com/coder/guts/testdata/excludecustom.Secret")
89+
require.NoErrorf(t, err, "exclude %q", dir)
8790
}
8891

8992
gen.IncludeCustomDeclaration(config.StandardMappings())

example/simple/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ type SimpleType[T comparable] struct {
1616
FieldTime time.Time
1717
}
1818

19+
type SecondaryType struct {
20+
FieldString string
21+
}
22+
1923
func main() {
2024
golang, _ := guts.NewGolangParser()
2125
// Generate the typescript types for this package
@@ -24,6 +28,8 @@ func main() {
2428
_ = golang.IncludeCustom(map[string]string{
2529
"time.Time": "string",
2630
})
31+
// Exclude SecondaryType from output
32+
_ = golang.ExcludeCustom("github.com/coder/guts/example/simple.SecondaryType")
2733

2834
// Convert the golang types to typescript AST
2935
ts, _ := golang.ToTypescript()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package excludecustom
2+
3+
type Secret struct {
4+
SecretString string
5+
}
6+
7+
type Public struct {
8+
PublicString string
9+
PublicInt int
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Code generated by 'guts'. DO NOT EDIT.
2+
3+
// From excludecustom/excludecustom.go
4+
export interface Public {
5+
readonly PublicString: string;
6+
readonly PublicInt: number;
7+
}

0 commit comments

Comments
 (0)