lang: Add logging and debug info via a new Init method

This expands the Stmt and Expr interfaces to add an Init method. This
is used to pass in Debug and Logf values, but is also used to validate
the AST. This gets rid of standalone use of the "log" package.
This commit is contained in:
James Shubin
2018-06-19 16:49:02 -04:00
parent 75633817a7
commit 3f42e5f702
6 changed files with 417 additions and 65 deletions

View File

@@ -19,7 +19,6 @@ package lang // TODO: move this into a sub package of lang/$name?
import (
"fmt"
"log"
"github.com/purpleidea/mgmt/lang/interfaces"
@@ -36,11 +35,21 @@ type Pos struct {
Filename string // optional source filename, if known
}
// InterpolateInfo contains some information passed around during interpolation.
// TODO: rename to Info if this is moved to its own package.
type InterpolateInfo struct {
// Debug represents if we're running in debug mode or not.
Debug bool
// Logf is a logger which should be used.
Logf func(format string, v ...interface{})
}
// InterpolateStr interpolates a string and returns the representative AST. This
// particular implementation uses the hashicorp hil library and syntax to do so.
func InterpolateStr(str string, pos *Pos) (interfaces.Expr, error) {
if interfaces.Debug {
log.Printf("%s: interpolate: %s", Name, str)
func InterpolateStr(str string, pos *Pos, info *InterpolateInfo) (interfaces.Expr, error) {
if info.Debug {
info.Logf("interpolating: %s", str)
}
var line, column int = -1, -1
var filename string
@@ -59,39 +68,50 @@ func InterpolateStr(str string, pos *Pos) (interfaces.Expr, error) {
if err != nil {
return nil, errwrap.Wrapf(err, "can't parse string interpolation: `%s`", str)
}
if interfaces.Debug {
log.Printf("%s: interpolate: tree: %+v", Name, tree)
if info.Debug {
info.Logf("tree: %+v", tree)
}
result, err := hilTransform(tree)
transformInfo := &InterpolateInfo{
Debug: info.Debug,
Logf: func(format string, v ...interface{}) {
info.Logf("transform: "+format, v...)
},
}
result, err := hilTransform(tree, transformInfo)
if err != nil {
return nil, errwrap.Wrapf(err, "error running AST map: `%s`", str)
}
if interfaces.Debug {
log.Printf("%s: interpolate: transform: %+v", Name, result)
if info.Debug {
info.Logf("transform: %+v", result)
}
return result, err
// make sure to run the Init on the new expression
return result, errwrap.Wrapf(result.Init(&interfaces.Data{
Debug: info.Debug,
Logf: info.Logf,
}), "init failed")
}
// hilTransform returns the AST equivalent of the hil AST.
func hilTransform(root hilast.Node) (interfaces.Expr, error) {
func hilTransform(root hilast.Node, info *InterpolateInfo) (interfaces.Expr, error) {
switch node := root.(type) {
case *hilast.Output: // common root node
if interfaces.Debug {
log.Printf("%s: interpolate: transform: got output type: %+v", Name, node)
if info.Debug {
info.Logf("got output type: %+v", node)
}
if len(node.Exprs) == 0 {
return nil, fmt.Errorf("no expressions found")
}
if len(node.Exprs) == 1 {
return hilTransform(node.Exprs[0])
return hilTransform(node.Exprs[0], info)
}
// assumes len > 1
args := []interfaces.Expr{}
for _, n := range node.Exprs {
expr, err := hilTransform(n)
expr, err := hilTransform(n, info)
if err != nil {
return nil, errwrap.Wrapf(err, "root failed")
}
@@ -107,12 +127,12 @@ func hilTransform(root hilast.Node) (interfaces.Expr, error) {
return result, nil
case *hilast.Call:
if interfaces.Debug {
log.Printf("%s: interpolate: transform: got function type: %+v", Name, node)
if info.Debug {
info.Logf("got function type: %+v", node)
}
args := []interfaces.Expr{}
for _, n := range node.Args {
arg, err := hilTransform(n)
arg, err := hilTransform(n, info)
if err != nil {
return nil, fmt.Errorf("call failed: %+v", err)
}
@@ -125,8 +145,8 @@ func hilTransform(root hilast.Node) (interfaces.Expr, error) {
}, nil
case *hilast.LiteralNode: // string, int, etc...
if interfaces.Debug {
log.Printf("%s: interpolate: transform: got literal type: %+v", Name, node)
if info.Debug {
info.Logf("got literal type: %+v", node)
}
switch node.Typex {
@@ -160,8 +180,8 @@ func hilTransform(root hilast.Node) (interfaces.Expr, error) {
}
case *hilast.VariableAccess: // variable lookup
if interfaces.Debug {
log.Printf("%s: interpolate: transform: got variable access type: %+v", Name, node)
if info.Debug {
info.Logf("got variable access type: %+v", node)
}
return &ExprVar{
Name: node.Name,