Skip to content

Commit

Permalink
refactor: remove more stale code and
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Nov 30, 2024
1 parent bd97b8e commit a4e0227
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 90 deletions.
2 changes: 1 addition & 1 deletion cmd/muxt/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

const CodeGenerationComment = "// Code generated by muxt. DO NOT EDIT."

func generateCommand(args []string, workingDirectory string, getEnv func(string) string, stdout, stderr io.Writer) error {
func generateCommand(args []string, workingDirectory string, stdout, stderr io.Writer) error {
config, err := configuration.NewRoutesFileConfiguration(args, stderr)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions cmd/muxt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func main() {
os.Exit(handleError(command(wd, flag.Args(), os.Getenv, os.Stdout, os.Stderr)))
}

func command(wd string, args []string, getEnv func(string) string, stdout, stderr io.Writer) error {
func command(wd string, args []string, _ func(string) string, stdout, stderr io.Writer) error {
if len(args) > 0 {
switch cmd, cmdArgs := args[0], args[1:]; cmd {
case "generate", "gen", "g":
return generateCommand(cmdArgs, wd, getEnv, stdout, stderr)
return generateCommand(cmdArgs, wd, stdout, stderr)
case "version", "v":
return versionCommand(stdout)
}
Expand Down
66 changes: 0 additions & 66 deletions internal/source/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,8 @@ import (
"net/http"
"strconv"
"strings"

"golang.org/x/tools/go/packages"
)

func Load(dir string, patterns ...string) ([]*packages.Package, error) {
return packages.Load(&packages.Config{
Mode: packages.NeedModule | packages.NeedName | packages.NeedFiles | packages.NeedTypes | packages.NeedSyntax | packages.NeedEmbedPatterns | packages.NeedEmbedFiles,
Dir: dir,
Tests: false,
}, patterns...)
}

func IterateGenDecl(files []*ast.File, tok token.Token) func(func(*ast.File, *ast.GenDecl) bool) {
return func(yield func(*ast.File, *ast.GenDecl) bool) {
for _, file := range files {
Expand Down Expand Up @@ -48,62 +38,6 @@ func IterateValueSpecs(files []*ast.File) func(func(*ast.File, *ast.ValueSpec) b
}
}

//func IterateTypes(files []*ast.File) func(func(*ast.File, *ast.TypeSpec) bool) {
// return func(yield func(*ast.File, *ast.TypeSpec) bool) {
// for _, file := range files {
// for _, decl := range file.Decls {
// spec, ok := decl.(*ast.GenDecl)
// if !ok || spec.Tok != token.TYPE {
// continue
// }
// for _, s := range spec.Specs {
// t, ok := s.(*ast.TypeSpec)
// if !ok {
// continue
// }
// if !yield(file, t) {
// return
// }
// }
// }
// }
// }
//}

func IterateFunctions(files []*ast.File) func(func(*ast.File, *ast.FuncDecl) bool) {
return func(yield func(*ast.File, *ast.FuncDecl) bool) {
for _, file := range files {
for _, decl := range file.Decls {
fn, ok := decl.(*ast.FuncDecl)
if !ok {
continue
}
if !yield(file, fn) {
return
}
}
}
}
}

//func IterateImports(files []*ast.File) func(func(*ast.File, *ast.ImportSpec) bool) {
// return func(yield func(*ast.File, *ast.ImportSpec) bool) {
// for _, file := range files {
// for _, decl := range file.Decls {
// genDecl, ok := decl.(*ast.GenDecl)
// if !ok || genDecl.Tok != token.IMPORT {
// continue
// }
// for _, s := range genDecl.Specs {
// if !yield(file, s.(*ast.ImportSpec)) {
// return
// }
// }
// }
// }
// }
//}

func Format(node ast.Node) string {
var buf strings.Builder
if err := printer.Fprint(&buf, token.NewFileSet(), node); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/source/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
"go/parser"
"go/token"
"go/types"
"golang.org/x/tools/go/packages"
"log"
"path"
"path/filepath"
"slices"
"strconv"
"strings"

"golang.org/x/tools/go/packages"
)

type Imports struct {
Expand Down
20 changes: 0 additions & 20 deletions internal/source/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,3 @@ func (val PatternValidation) GenerateValidation(imports *Imports, variable ast.E
},
}
}

func parseBlock(tmpIdent string, parseCall ast.Expr, validations []ast.Stmt, handleErr, handleResult func(out ast.Expr) ast.Stmt) []ast.Stmt {
const errIdent = "err"
parse := &ast.AssignStmt{
Lhs: []ast.Expr{ast.NewIdent(tmpIdent), ast.NewIdent(errIdent)},
Tok: token.DEFINE,
Rhs: []ast.Expr{parseCall},
}
errCheck := ErrorCheckReturn(errIdent, handleErr(&ast.CallExpr{
Fun: &ast.SelectorExpr{
X: ast.NewIdent(errIdent),
Sel: ast.NewIdent("Error"),
},
Args: []ast.Expr{},
}))
block := &ast.BlockStmt{List: []ast.Stmt{parse, errCheck}}
block.List = append(block.List, validations...)
block.List = append(block.List, handleResult(ast.NewIdent(tmpIdent)))
return block.List
}

0 comments on commit a4e0227

Please sign in to comment.