Skip to content

Commit

Permalink
Merge pull request #85 from tminaorg/searcher-cleanup
Browse files Browse the repository at this point in the history
Searcher cleanup
  • Loading branch information
aleksasiriski authored Sep 27, 2023
2 parents bcc5352 + 90a92b3 commit 88b848e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
16 changes: 9 additions & 7 deletions generate/searcher/searcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ var (
typeName = flag.String("type", "", "type name; must be set")
output = flag.String("output", "", "output file name; default srcdir/<type>_searcher.go")
trimprefix = flag.String("trimprefix", "", "trim the `prefix` from the generated constant names")
linecomment = flag.Bool("linecomment", false, "use line comment text as printed text when present")
buildTags = flag.String("tags", "", "comma-separated list of build tags to apply")
packageName = flag.String("packagename", "", "name of the package for generated code; default current package")
enginesImport = flag.String("enginesimport", "github.com/tminaorg/brzaguza/src/engines", "source of the engines import, which is prefixed to imports for consts; default github.com/tminaorg/brzaguza/src/engines")
linecomment = flag.Bool("linecomment", false, "use line comment text as printed text when present")
)

// Usage is a replacement usage function for the flags package.
Expand Down Expand Up @@ -154,7 +154,7 @@ func (g *Generator) addPackage(pkg *packages.Package) {

// generate produces imports and the NewEngineStarter method for the named type.
func (g *Generator) generate(typeName string) {
values := make(Values, 0, 100)
values := make([]Value, 0, 100)
for _, file := range g.pkg.files {
// Set the state for this run of the walker.
file.typeName = typeName
Expand All @@ -172,7 +172,7 @@ func (g *Generator) generate(typeName string) {
// Generate code for importing engines
for _, v := range values {
if validConst(v) {
g.Printf("import \"%s/%s\"\n", *enginesImport, strings.ToLower(v.originalName))
g.Printf("import \"%s/%s\"\n", *enginesImport, strings.ToLower(v.name))
}
}

Expand All @@ -184,7 +184,7 @@ func (g *Generator) generate(typeName string) {
for _, v := range values {
origName := v.originalName
if *packageName != "" {
origName = fmt.Sprintf("%s.%s", g.pkg.name, v.originalName)
origName = fmt.Sprintf("%s.%s", g.pkg.name, v.name)
}
g.Printf("\t_ = x[%s - (%s)]\n", origName, v.str)
}
Expand Down Expand Up @@ -306,14 +306,16 @@ func (f *File) genDecl(node ast.Node) bool {
}

// buildOneRun generates the variables and NewEngineStarter func for a single run of contiguous values.
func (g *Generator) buildOneRun(values Values, typeName string) {
func (g *Generator) buildOneRun(values []Value, typeName string) {
g.Printf("\n")
// The generated code is simple enough to write as a Printf format.
g.Printf("\nfunc NewEngineStarter() []EngineSearch {\n\tmm := make([]EngineSearch, %d)", len(values))
g.Printf("\nfunc NewEngineStarter() []EngineSearch {")
g.Printf("\n\tmm := make([]EngineSearch, %d)", len(values))
for _, v := range values {
if validConst(v) {
g.Printf("\n\tmm[%s.%s] = %s.Search", g.pkg.name, v.name, strings.ToLower(v.name))
}
}
g.Printf("\n\treturn mm\n}")
g.Printf("\n\treturn mm")
g.Printf("\n}")
}
6 changes: 2 additions & 4 deletions generate/searcher/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ type Value struct {
str string // The string representation given by the "go/constant" package.
}

type Values []Value

// Generator holds the state of the analysis. Primarily used to buffer
// the output for format.Source.
type Generator struct {
Expand All @@ -39,8 +37,8 @@ type File struct {
pkg *Package // Package to which this file belongs.
file *ast.File // Parsed AST.
// These fields are reset for each type being generated.
typeName string // Name of the constant type.
values Values // Accumulator for constant values of that type.
typeName string // Name of the constant type.
values []Value // Accumulator for constant values of that type.

trimPrefix string
lineComment bool
Expand Down

0 comments on commit 88b848e

Please sign in to comment.