Skip to content

Commit

Permalink
Rename GenGoFile.ErrIfAdd to IgnoreDiscoveredImports
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Aug 1, 2023
1 parent 131d936 commit f0dba70
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
16 changes: 8 additions & 8 deletions encoding/gocode/gen.go
Expand Up @@ -123,10 +123,10 @@ func GenerateTypesOpenAPI(sch thema.Schema, cfg *TypeConfigOpenAPI) ([]byte, err
}

return PostprocessGoFile(GenGoFile{
Path: fmt.Sprintf("%s_type_gen.go", sch.Lineage().Name()),
Appliers: applyFuncs,
In: []byte(gostr),
ErrIfAdd: !cfg.IgnoreDiscoveredImports,
Path: fmt.Sprintf("%s_type_gen.go", sch.Lineage().Name()),
Appliers: applyFuncs,
In: []byte(gostr),
IgnoreDiscoveredImports: cfg.IgnoreDiscoveredImports,
})
}

Expand Down Expand Up @@ -343,10 +343,10 @@ func GenerateLineageBinding(lin thema.Lineage, cfg *BindingConfig) ([]byte, erro
}

return PostprocessGoFile(GenGoFile{
Path: fmt.Sprintf("%s_binding_gen.go", strings.ToLower(lin.Name())),
Appliers: cfg.ApplyFuncs,
In: buf.Bytes(),
ErrIfAdd: !cfg.IgnoreDiscoveredImports,
Path: fmt.Sprintf("%s_binding_gen.go", strings.ToLower(lin.Name())),
Appliers: cfg.ApplyFuncs,
In: buf.Bytes(),
IgnoreDiscoveredImports: cfg.IgnoreDiscoveredImports,
})
}

Expand Down
10 changes: 8 additions & 2 deletions encoding/gocode/postprocessing.go
Expand Up @@ -16,10 +16,16 @@ import (
)

type GenGoFile struct {
ErrIfAdd bool
Path string
Appliers []dstutil.ApplyFunc
In []byte

// IgnoreDiscoveredImports causes the processing not to fail with an error in the
// event that goimports adds additional import statements. (The default behavior
// is to fail because adding imports entails a search, which can slow down
// codegen by multiple orders of magnitude. Succeeding silently but slowly is a bad
// default behavior when the fix is usually quite easy.)
IgnoreDiscoveredImports bool
}

func PostprocessGoFile(cfg GenGoFile) ([]byte, error) {
Expand All @@ -45,7 +51,7 @@ func PostprocessGoFile(cfg GenGoFile) ([]byte, error) {
return nil, fmt.Errorf("goimports processing of generated file failed: %w", err)
}

if cfg.ErrIfAdd {
if !cfg.IgnoreDiscoveredImports {
// Compare imports before and after; warn about performance if some were added
gfa, _ := parser.ParseFile(fset, fname, string(byt), parser.ParseComments)
imap := make(map[string]bool)
Expand Down

0 comments on commit f0dba70

Please sign in to comment.