Skip to content

Commit

Permalink
Add handleString
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed Jun 8, 2024
1 parent 1f1656d commit 83bff02
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
69 changes: 60 additions & 9 deletions bake/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ func ListAst(dir string) error {
if len(x.Type.Params.List) != 1 {
return false
}
var optFieldName string
for _, param := range x.Type.Params.List {
name := param.Names[0].Name
var buf bytes.Buffer
Expand All @@ -207,6 +208,7 @@ func ListAst(dir string) error {
if buf.String() != "*getoptions.GetOpt" {
return false
}
optFieldName = name
}

// Check Results
Expand All @@ -228,14 +230,15 @@ func ListAst(dir string) error {
switch x := n.(type) {
case *ast.BlockStmt:
for i, stmt := range x.List {
Logger.Printf("i: %d\n", i)
Logger.Printf("stmt: %T\n", stmt)
var buf bytes.Buffer
printer.Fprint(&buf, fset, stmt)
// We are expecting the expression before the return function
_, ok := stmt.(*ast.ReturnStmt)
if ok {
return false
}
Logger.Printf("inspect stmt: %T\n", stmt)
Logger.Printf("i: %d\n", i)
Logger.Printf("stmt: %s\n", buf.String())
exprStmt, ok := stmt.(*ast.ExprStmt)
if !ok {
continue
Expand All @@ -254,14 +257,16 @@ func ListAst(dir string) error {
if !ok {
return false
}
xSel := fun.Sel.Name
Logger.Printf("X: %s, Selector: %s\n", xIdent.Name, xSel)
if xIdent.Name != optFieldName {
return false
}
Logger.Printf("handling %s.%s\n", xIdent.Name, fun.Sel.Name)

// Check for args
for _, arg := range x.Args {
Logger.Printf("arg: %T\n", arg)
spew.Dump(arg)
switch fun.Sel.Name {
case "String":
handleString(optFieldName, n)
}

return false
}
return true
Expand All @@ -278,3 +283,49 @@ func ListAst(dir string) error {
}
return nil
}

func handleString(optFieldName string, n ast.Node) error {
x := n.(*ast.CallExpr)
// Check for args
for i, arg := range x.Args {
Logger.Printf("i: %d, arg: %T\n", i, arg)
if i == 0 {
// First argument is the Name
Logger.Printf("Name: %s\n", arg.(*ast.BasicLit).Value)
} else if i == 1 {
// Second argument is the Default
Logger.Printf("Default: %s\n", arg.(*ast.BasicLit).Value)
} else {
// Remaining arguments are option modifiers
// Start with support for:
// Description
// ValidValues
// Alias

callE, ok := arg.(*ast.CallExpr)
if !ok {
continue
}
fun, ok := callE.Fun.(*ast.SelectorExpr)
if !ok {
continue
}
xIdent, ok := fun.X.(*ast.Ident)
if !ok {
continue
}
if xIdent.Name != optFieldName {
continue
}
Logger.Printf("\t%s.%s\n", xIdent.Name, fun.Sel.Name)
if fun.Sel.Name == "SetCalled" {
// TODO: SetCalled function receives a bool
continue
}
for _, arg := range callE.Args {
Logger.Printf("Value: %s\n", arg.(*ast.BasicLit).Value)
}
}
}
return nil
}
1 change: 1 addition & 0 deletions bake/examples/website/bake/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func Build(opt *getoptions.GetOpt) getoptions.CommandFn {
func Asciidoc(opt *getoptions.GetOpt) getoptions.CommandFn {
opt.String("lang", "en", opt.ValidValues("en", "es"), opt.Description("Language"))
opt.String("hello", "world")
Logger.Println("Running Asciidoc")
opt.String("hola", "mundo")
return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
Logger.Println("Running build:diagram")
Expand Down

0 comments on commit 83bff02

Please sign in to comment.