Simplified the template generator logging code.

Renamed a few of the functions in the template generator.
This commit is contained in:
Azareal 2017-11-08 00:12:50 +00:00
parent ed2293ac0e
commit 3b0fba1b17
1 changed files with 106 additions and 225 deletions

View File

@ -99,9 +99,7 @@ func (c *CTemplateSet) compileTemplate(name string, dir string, expects string,
if err != nil { if err != nil {
return "", err return "", err
} }
if dev.TemplateDebug { c.log(name)
fmt.Println(name)
}
out = "" out = ""
fname := strings.TrimSuffix(name, filepath.Ext(name)) fname := strings.TrimSuffix(name, filepath.Ext(name))
@ -109,9 +107,7 @@ func (c *CTemplateSet) compileTemplate(name string, dir string, expects string,
c.tlist[fname] = tree c.tlist[fname] = tree
varholder := "tmpl_" + fname + "_vars" varholder := "tmpl_" + fname + "_vars"
if dev.TemplateDebug { c.log(c.tlist)
fmt.Println(c.tlist)
}
c.localVars = make(map[string]map[string]VarItemReflect) c.localVars = make(map[string]map[string]VarItemReflect)
c.localVars[fname] = make(map[string]VarItemReflect) c.localVars[fname] = make(map[string]VarItemReflect)
c.localVars[fname]["."] = VarItemReflect{".", varholder, holdreflect} c.localVars[fname]["."] = VarItemReflect{".", varholder, holdreflect}
@ -154,18 +150,14 @@ w.Write([]byte(`, " + ", -1)
fmt.Println(" ") fmt.Println(" ")
} }
if dev.TemplateDebug { c.log("Output!")
fmt.Println("Output!") c.log(fout)
fmt.Println(fout)
}
//log.Fatal("remove the log.Fatal line") //log.Fatal("remove the log.Fatal line")
return fout, nil return fout, nil
} }
func (c *CTemplateSet) rootIterate(tree *parse.Tree, varholder string, holdreflect reflect.Value, fname string) (out string) { func (c *CTemplateSet) rootIterate(tree *parse.Tree, varholder string, holdreflect reflect.Value, fname string) (out string) {
if dev.TemplateDebug { c.log(tree.Root)
fmt.Println(tree.Root)
}
treeLength := len(tree.Root.Nodes) treeLength := len(tree.Root.Nodes)
for index, node := range tree.Root.Nodes { for index, node := range tree.Root.Nodes {
if dev.TemplateDebug { if dev.TemplateDebug {
@ -182,14 +174,10 @@ func (c *CTemplateSet) rootIterate(tree *parse.Tree, varholder string, holdrefle
} }
func (c *CTemplateSet) compileSwitch(varholder string, holdreflect reflect.Value, templateName string, node parse.Node) (out string) { func (c *CTemplateSet) compileSwitch(varholder string, holdreflect reflect.Value, templateName string, node parse.Node) (out string) {
if dev.TemplateDebug { c.log("in compileSwitch")
fmt.Println("in compileSwitch")
}
switch node := node.(type) { switch node := node.(type) {
case *parse.ActionNode: case *parse.ActionNode:
if dev.TemplateDebug { c.log("Action Node")
fmt.Println("Action Node")
}
if node.Pipe == nil { if node.Pipe == nil {
break break
} }
@ -197,44 +185,30 @@ func (c *CTemplateSet) compileSwitch(varholder string, holdreflect reflect.Value
out += c.compileSubswitch(varholder, holdreflect, templateName, cmd) out += c.compileSubswitch(varholder, holdreflect, templateName, cmd)
} }
case *parse.IfNode: case *parse.IfNode:
if dev.TemplateDebug { c.log("If Node:")
fmt.Println("If Node:") c.log("node.Pipe", node.Pipe)
fmt.Println("node.Pipe", node.Pipe)
}
var expr string var expr string
for _, cmd := range node.Pipe.Cmds { for _, cmd := range node.Pipe.Cmds {
if dev.TemplateDebug { c.log("If Node Bit:", cmd)
fmt.Println("If Node Bit:", cmd) c.log("If Node Bit Type:", reflect.ValueOf(cmd).Type().Name())
fmt.Println("If Node Bit Type:", reflect.ValueOf(cmd).Type().Name())
}
expr += c.compileVarswitch(varholder, holdreflect, templateName, cmd) expr += c.compileVarswitch(varholder, holdreflect, templateName, cmd)
if dev.TemplateDebug { c.log("If Node Expression Step:", c.compileVarswitch(varholder, holdreflect, templateName, cmd))
fmt.Println("If Node Expression Step:", c.compileVarswitch(varholder, holdreflect, templateName, cmd))
}
} }
if dev.TemplateDebug { c.log("If Node Expression:", expr)
fmt.Println("If Node Expression:", expr)
}
c.previousNode = c.currentNode c.previousNode = c.currentNode
c.currentNode = parse.NodeList c.currentNode = parse.NodeList
c.nextNode = -1 c.nextNode = -1
if node.ElseList == nil { if node.ElseList == nil {
if dev.TemplateDebug { c.log("Selected Branch 1")
fmt.Println("Selected Branch 1")
}
return "if " + expr + " {\n" + c.compileSwitch(varholder, holdreflect, templateName, node.List) + "}\n" return "if " + expr + " {\n" + c.compileSwitch(varholder, holdreflect, templateName, node.List) + "}\n"
} }
if dev.TemplateDebug { c.log("Selected Branch 2")
fmt.Println("Selected Branch 2")
}
return "if " + expr + " {\n" + c.compileSwitch(varholder, holdreflect, templateName, node.List) + "} else {\n" + c.compileSwitch(varholder, holdreflect, templateName, node.ElseList) + "}\n" return "if " + expr + " {\n" + c.compileSwitch(varholder, holdreflect, templateName, node.List) + "} else {\n" + c.compileSwitch(varholder, holdreflect, templateName, node.ElseList) + "}\n"
case *parse.ListNode: case *parse.ListNode:
if dev.TemplateDebug { c.log("List Node")
fmt.Println("List Node")
}
for _, subnode := range node.Nodes { for _, subnode := range node.Nodes {
out += c.compileSwitch(varholder, holdreflect, templateName, subnode) out += c.compileSwitch(varholder, holdreflect, templateName, subnode)
} }
@ -266,22 +240,18 @@ func (c *CTemplateSet) compileSwitch(varholder string, holdreflect reflect.Value
} }
func (c *CTemplateSet) compileRangeNode(varholder string, holdreflect reflect.Value, templateName string, node *parse.RangeNode) (out string) { func (c *CTemplateSet) compileRangeNode(varholder string, holdreflect reflect.Value, templateName string, node *parse.RangeNode) (out string) {
if dev.TemplateDebug { c.log("Range Node!")
fmt.Println("Range Node!") c.log(node.Pipe)
fmt.Println(node.Pipe)
}
var outVal reflect.Value var outVal reflect.Value
for _, cmd := range node.Pipe.Cmds { for _, cmd := range node.Pipe.Cmds {
if dev.TemplateDebug { if dev.TemplateDebug {
fmt.Println("Range Bit:", cmd) fmt.Println("Range Bit:", cmd)
} }
out, outVal = c.compileReflectswitch(varholder, holdreflect, templateName, cmd) out, outVal = c.compileReflectSwitch(varholder, holdreflect, templateName, cmd)
}
if dev.TemplateDebug {
fmt.Println("Returned:", out)
fmt.Println("Range Kind Switch!")
} }
c.log("Returned:", out)
c.log("Range Kind Switch!")
switch outVal.Kind() { switch outVal.Kind() {
case reflect.Map: case reflect.Map:
@ -318,15 +288,11 @@ func (c *CTemplateSet) compileRangeNode(varholder string, holdreflect reflect.Va
} }
func (c *CTemplateSet) compileSubswitch(varholder string, holdreflect reflect.Value, templateName string, node *parse.CommandNode) (out string) { func (c *CTemplateSet) compileSubswitch(varholder string, holdreflect reflect.Value, templateName string, node *parse.CommandNode) (out string) {
if dev.TemplateDebug { c.log("in compileSubswitch")
fmt.Println("in compileSubswitch")
}
firstWord := node.Args[0] firstWord := node.Args[0]
switch n := firstWord.(type) { switch n := firstWord.(type) {
case *parse.FieldNode: case *parse.FieldNode:
if dev.TemplateDebug { c.log("Field Node:", n.Ident)
fmt.Println("Field Node:", n.Ident)
}
/* Use reflect to determine if the field is for a method, otherwise assume it's a variable. Variable declarations are coming soon! */ /* Use reflect to determine if the field is for a method, otherwise assume it's a variable. Variable declarations are coming soon! */
cur := holdreflect cur := holdreflect
@ -338,22 +304,16 @@ func (c *CTemplateSet) compileSubswitch(varholder string, holdreflect reflect.Va
} }
for _, id := range n.Ident { for _, id := range n.Ident {
if dev.TemplateDebug { c.log("Data Kind:", cur.Kind().String())
fmt.Println("Data Kind:", cur.Kind().String()) c.log("Field Bit:", id)
fmt.Println("Field Bit:", id)
}
if cur.Kind() == reflect.Ptr { if cur.Kind() == reflect.Ptr {
if dev.TemplateDebug { c.log("Looping over pointer")
fmt.Println("Looping over pointer")
}
for cur.Kind() == reflect.Ptr { for cur.Kind() == reflect.Ptr {
cur = cur.Elem() cur = cur.Elem()
} }
if dev.TemplateDebug { c.log("Data Kind:", cur.Kind().String())
fmt.Println("Data Kind:", cur.Kind().String()) c.log("Field Bit:", id)
fmt.Println("Field Bit:", id)
}
} }
if !cur.IsValid() { if !cur.IsValid() {
@ -386,9 +346,7 @@ func (c *CTemplateSet) compileSubswitch(varholder string, holdreflect reflect.Va
} else { } else {
varbit += "." + id varbit += "." + id
} }
if dev.TemplateDebug { c.log("End Cycle")
fmt.Println("End Cycle")
}
} }
out = c.compileVarsub(varholder+varbit, cur) out = c.compileVarsub(varholder+varbit, cur)
@ -399,36 +357,28 @@ func (c *CTemplateSet) compileSubswitch(varholder string, holdreflect reflect.Va
} }
return out return out
case *parse.DotNode: case *parse.DotNode:
if dev.TemplateDebug { c.log("Dot Node:", node.String())
fmt.Println("Dot Node:", node.String())
}
return c.compileVarsub(varholder, holdreflect) return c.compileVarsub(varholder, holdreflect)
case *parse.NilNode: case *parse.NilNode:
panic("Nil is not a command x.x") panic("Nil is not a command x.x")
case *parse.VariableNode: case *parse.VariableNode:
if dev.TemplateDebug { c.log("Variable Node:", n.String())
fmt.Println("Variable Node:", n.String()) c.log(n.Ident)
fmt.Println(n.Ident)
}
varname, reflectVal := c.compileIfVarsub(n.String(), varholder, templateName, holdreflect) varname, reflectVal := c.compileIfVarsub(n.String(), varholder, templateName, holdreflect)
return c.compileVarsub(varname, reflectVal) return c.compileVarsub(varname, reflectVal)
case *parse.StringNode: case *parse.StringNode:
return n.Quoted return n.Quoted
case *parse.IdentifierNode: case *parse.IdentifierNode:
if dev.TemplateDebug { c.log("Identifier Node:", node)
fmt.Println("Identifier Node:", node) c.log("Identifier Node Args:", node.Args)
fmt.Println("Identifier Node Args:", node.Args) return c.compileVarsub(c.compileIdentSwitch(varholder, holdreflect, templateName, node))
}
return c.compileVarsub(c.compileIdentswitch(varholder, holdreflect, templateName, node))
default: default:
return c.unknownNode(node) return c.unknownNode(node)
} }
} }
func (c *CTemplateSet) compileVarswitch(varholder string, holdreflect reflect.Value, templateName string, node *parse.CommandNode) (out string) { func (c *CTemplateSet) compileVarswitch(varholder string, holdreflect reflect.Value, templateName string, node *parse.CommandNode) (out string) {
if dev.TemplateDebug { c.log("in compileVarswitch")
fmt.Println("in compileVarswitch")
}
firstWord := node.Args[0] firstWord := node.Args[0]
switch n := firstWord.(type) { switch n := firstWord.(type) {
case *parse.FieldNode: case *parse.FieldNode:
@ -442,37 +392,27 @@ func (c *CTemplateSet) compileVarswitch(varholder string, holdreflect reflect.Va
/* Use reflect to determine if the field is for a method, otherwise assume it's a variable. Coming Soon. */ /* Use reflect to determine if the field is for a method, otherwise assume it's a variable. Coming Soon. */
return c.compileBoolsub(n.String(), varholder, templateName, holdreflect) return c.compileBoolsub(n.String(), varholder, templateName, holdreflect)
case *parse.ChainNode: case *parse.ChainNode:
if dev.TemplateDebug { c.log("Chain Node:", n.Node)
fmt.Println("Chain Node:", n.Node) c.log("Chain Node Args:", node.Args)
fmt.Println("Chain Node Args:", node.Args)
}
case *parse.IdentifierNode: case *parse.IdentifierNode:
if dev.TemplateDebug { c.log("Identifier Node:", node)
fmt.Println("Identifier Node:", node) c.log("Identifier Node Args:", node.Args)
fmt.Println("Identifier Node Args:", node.Args) return c.compileIdentSwitchN(varholder, holdreflect, templateName, node)
}
return c.compileIdentswitchN(varholder, holdreflect, templateName, node)
case *parse.DotNode: case *parse.DotNode:
return varholder return varholder
case *parse.VariableNode: case *parse.VariableNode:
if dev.TemplateDebug { c.log("Variable Node:", n.String())
fmt.Println("Variable Node:", n.String()) c.log("Variable Node Identifier:", n.Ident)
fmt.Println("Variable Node Identifier:", n.Ident)
}
out, _ = c.compileIfVarsub(n.String(), varholder, templateName, holdreflect) out, _ = c.compileIfVarsub(n.String(), varholder, templateName, holdreflect)
case *parse.NilNode: case *parse.NilNode:
panic("Nil is not a command x.x") panic("Nil is not a command x.x")
case *parse.PipeNode: case *parse.PipeNode:
if dev.TemplateDebug { c.log("Pipe Node!")
fmt.Println("Pipe Node!") c.log(n)
fmt.Println(n) c.log("Args:", node.Args)
fmt.Println("Args:", node.Args) out += c.compileIdentSwitchN(varholder, holdreflect, templateName, node)
}
out += c.compileIdentswitchN(varholder, holdreflect, templateName, node)
if dev.TemplateDebug { c.log("Out:", out)
fmt.Println("Out:", out)
}
default: default:
return c.unknownNode(firstWord) return c.unknownNode(firstWord)
} }
@ -483,23 +423,22 @@ func (c *CTemplateSet) unknownNode(node parse.Node) (out string) {
fmt.Println("Unknown Kind:", reflect.ValueOf(node).Elem().Kind()) fmt.Println("Unknown Kind:", reflect.ValueOf(node).Elem().Kind())
fmt.Println("Unknown Type:", reflect.ValueOf(node).Elem().Type().Name()) fmt.Println("Unknown Type:", reflect.ValueOf(node).Elem().Type().Name())
panic("I don't know what node this is! Grr...") panic("I don't know what node this is! Grr...")
return ""
} }
func (c *CTemplateSet) compileIdentswitchN(varholder string, holdreflect reflect.Value, templateName string, node *parse.CommandNode) (out string) { func (c *CTemplateSet) compileIdentSwitchN(varholder string, holdreflect reflect.Value, templateName string, node *parse.CommandNode) (out string) {
if dev.TemplateDebug { c.log("in compileIdentSwitchN")
fmt.Println("in compile_identswitch_n") out, _ = c.compileIdentSwitch(varholder, holdreflect, templateName, node)
}
out, _ = c.compileIdentswitch(varholder, holdreflect, templateName, node)
return out return out
} }
func (c *CTemplateSet) compareFunc(varholder string, holdreflect reflect.Value, templateName string, pos int, node *parse.CommandNode, compare string) (out string) { func (c *CTemplateSet) dumpSymbol(pos int, node *parse.CommandNode, symbol string) {
if dev.TemplateDebug { c.log("symbol: ", symbol)
fmt.Println("symbol: ", compare) c.log("node.Args[pos + 1]", node.Args[pos+1])
fmt.Println("node.Args[pos + 1]", node.Args[pos+1]) c.log("node.Args[pos + 2]", node.Args[pos+2])
fmt.Println("node.Args[pos + 2]", node.Args[pos+2])
} }
func (c *CTemplateSet) compareFunc(varholder string, holdreflect reflect.Value, templateName string, pos int, node *parse.CommandNode, compare string) (out string) {
c.dumpSymbol(pos, node, compare)
return c.compileIfVarsubN(node.Args[pos+1].String(), varholder, templateName, holdreflect) + " " + compare + " " + c.compileIfVarsubN(node.Args[pos+2].String(), varholder, templateName, holdreflect) return c.compileIfVarsubN(node.Args[pos+1].String(), varholder, templateName, holdreflect) + " " + compare + " " + c.compileIfVarsubN(node.Args[pos+2].String(), varholder, templateName, holdreflect)
} }
@ -517,19 +456,12 @@ func (c *CTemplateSet) simpleMath(varholder string, holdreflect reflect.Value, t
val = reflect.ValueOf(numSample) val = reflect.ValueOf(numSample)
} }
if dev.TemplateDebug { c.dumpSymbol(pos, node, symbol)
fmt.Println("symbol: " + symbol)
fmt.Println("node.Args[pos + 1]", node.Args[pos+1])
fmt.Println("node.Args[pos + 2]", node.Args[pos+2])
}
return leftParam + " " + symbol + " " + rightParam, val return leftParam + " " + symbol + " " + rightParam, val
} }
func (c *CTemplateSet) compareJoin(varholder string, holdreflect reflect.Value, templateName string, pos int, node *parse.CommandNode, symbol string) (pos2 int, out string) { func (c *CTemplateSet) compareJoin(varholder string, holdreflect reflect.Value, templateName string, pos int, node *parse.CommandNode, symbol string) (pos2 int, out string) {
if dev.TemplateDebug { c.log("Building " + symbol + " function")
fmt.Println("Building " + symbol + " function")
}
if pos == 0 { if pos == 0 {
fmt.Println("pos:", pos) fmt.Println("pos:", pos)
panic(symbol + " is missing a left operand") panic(symbol + " is missing a left operand")
@ -548,47 +480,32 @@ func (c *CTemplateSet) compareJoin(varholder string, holdreflect reflect.Value,
right = c.compileBoolsub(node.Args[pos+1].String(), varholder, templateName, holdreflect) right = c.compileBoolsub(node.Args[pos+1].String(), varholder, templateName, holdreflect)
} }
out = left + " " + symbol + " " + right out = left + " " + symbol + " " + right
//log.Print("left: ", left)
//log.Print("right: ", right)
if dev.TemplateDebug { c.log("Left operand:", node.Args[pos-1])
fmt.Println("Left operand:", node.Args[pos-1]) c.log("Right operand:", node.Args[pos+1])
fmt.Println("Right operand:", node.Args[pos+1])
}
if !funcExists { if !funcExists {
pos++ pos++
} }
if dev.TemplateDebug { c.log("pos:", pos)
fmt.Println("pos:", pos) c.log("len(node.Args):", len(node.Args))
fmt.Println("len(node.Args):", len(node.Args))
}
return pos, out return pos, out
} }
func (c *CTemplateSet) compileIdentswitch(varholder string, holdreflect reflect.Value, templateName string, node *parse.CommandNode) (out string, val reflect.Value) { func (c *CTemplateSet) compileIdentSwitch(varholder string, holdreflect reflect.Value, templateName string, node *parse.CommandNode) (out string, val reflect.Value) {
if dev.TemplateDebug { c.log("in compileIdentSwitch")
fmt.Println("in compileIdentswitch")
}
//var outbuf map[int]string //var outbuf map[int]string
ArgLoop: ArgLoop:
for pos := 0; pos < len(node.Args); pos++ { for pos := 0; pos < len(node.Args); pos++ {
id := node.Args[pos] id := node.Args[pos]
if dev.TemplateDebug { c.log("pos:", pos)
fmt.Println("pos:", pos) c.log("ID:", id)
fmt.Println("ID:", id)
}
switch id.String() { switch id.String() {
case "not": case "not":
out += "!" out += "!"
case "or": case "or", "and":
var rout string var rout string
pos, rout = c.compareJoin(varholder, holdreflect, templateName, pos, node, "||") // TODO: Test this pos, rout = c.compareJoin(varholder, holdreflect, templateName, pos, node, c.funcMap[id.String()].(string)) // TODO: Test this
out += rout
case "and":
var rout string
pos, rout = c.compareJoin(varholder, holdreflect, templateName, pos, node, "&&") // TODO: Test this
out += rout out += rout
case "le": // TODO: Can we condense these comparison cases down into one? case "le": // TODO: Can we condense these comparison cases down into one?
out += c.compareFunc(varholder, holdreflect, templateName, pos, node, "<=") out += c.compareFunc(varholder, holdreflect, templateName, pos, node, "<=")
@ -629,9 +546,7 @@ ArgLoop:
val = rval val = rval
break ArgLoop break ArgLoop
default: default:
if dev.TemplateDebug { c.log("Variable!")
fmt.Println("Variable!")
}
if len(node.Args) > (pos + 1) { if len(node.Args) > (pos + 1) {
nextNode := node.Args[pos+1].String() nextNode := node.Args[pos+1].String()
if nextNode == "or" || nextNode == "and" { if nextNode == "or" || nextNode == "and" {
@ -641,18 +556,11 @@ ArgLoop:
out += c.compileIfVarsubN(id.String(), varholder, templateName, holdreflect) out += c.compileIfVarsubN(id.String(), varholder, templateName, holdreflect)
} }
} }
//for _, outval := range outbuf {
// out += outval
//}
//log.Print("outbit: ", out)
return out, val return out, val
} }
func (c *CTemplateSet) compileReflectswitch(varholder string, holdreflect reflect.Value, templateName string, node *parse.CommandNode) (out string, outVal reflect.Value) { func (c *CTemplateSet) compileReflectSwitch(varholder string, holdreflect reflect.Value, templateName string, node *parse.CommandNode) (out string, outVal reflect.Value) {
if dev.TemplateDebug { c.log("in compileReflectSwitch")
fmt.Println("in compileReflectswitch")
}
firstWord := node.Args[0] firstWord := node.Args[0]
switch n := firstWord.(type) { switch n := firstWord.(type) {
case *parse.FieldNode: case *parse.FieldNode:
@ -665,11 +573,8 @@ func (c *CTemplateSet) compileReflectswitch(varholder string, holdreflect reflec
/* Use reflect to determine if the field is for a method, otherwise assume it's a variable. Coming Soon. */ /* Use reflect to determine if the field is for a method, otherwise assume it's a variable. Coming Soon. */
return c.compileIfVarsub(n.String(), varholder, templateName, holdreflect) return c.compileIfVarsub(n.String(), varholder, templateName, holdreflect)
case *parse.ChainNode: case *parse.ChainNode:
if dev.TemplateDebug { c.log("Chain Node:", n.Node)
fmt.Println("Chain Node:", n.Node) c.log("node.Args:", node.Args)
fmt.Println("node.Args:", node.Args)
}
return "", outVal
case *parse.DotNode: case *parse.DotNode:
return varholder, holdreflect return varholder, holdreflect
case *parse.NilNode: case *parse.NilNode:
@ -681,17 +586,13 @@ func (c *CTemplateSet) compileReflectswitch(varholder string, holdreflect reflec
} }
func (c *CTemplateSet) compileIfVarsubN(varname string, varholder string, templateName string, cur reflect.Value) (out string) { func (c *CTemplateSet) compileIfVarsubN(varname string, varholder string, templateName string, cur reflect.Value) (out string) {
if dev.TemplateDebug { c.log("in compileIfVarsubN")
fmt.Println("in compileIfVarsubN")
}
out, _ = c.compileIfVarsub(varname, varholder, templateName, cur) out, _ = c.compileIfVarsub(varname, varholder, templateName, cur)
return out return out
} }
func (c *CTemplateSet) compileIfVarsub(varname string, varholder string, templateName string, cur reflect.Value) (out string, val reflect.Value) { func (c *CTemplateSet) compileIfVarsub(varname string, varholder string, templateName string, cur reflect.Value) (out string, val reflect.Value) {
if dev.TemplateDebug { c.log("in compileIfVarsub")
fmt.Println("in compileIfVarsub")
}
if varname[0] != '.' && varname[0] != '$' { if varname[0] != '.' && varname[0] != '$' {
return varname, cur return varname, cur
} }
@ -711,65 +612,49 @@ func (c *CTemplateSet) compileIfVarsub(varname string, varholder string, templat
cur = cur.Elem() cur = cur.Elem()
} }
} else { } else {
out += varholder
if cur.Kind() == reflect.Interface { if cur.Kind() == reflect.Interface {
cur = cur.Elem() cur = cur.Elem()
out += varholder + ".(" + cur.Type().Name() + ")" out += ".(" + cur.Type().Name() + ")"
} else {
out += varholder
} }
} }
bits[0] = strings.TrimPrefix(bits[0], "$") bits[0] = strings.TrimPrefix(bits[0], "$")
if dev.TemplateDebug { c.log("Cur Kind:", cur.Kind())
fmt.Println("Cur Kind:", cur.Kind()) c.log("Cur Type:", cur.Type().Name())
fmt.Println("Cur Type:", cur.Type().Name())
}
for _, bit := range bits { for _, bit := range bits {
if dev.TemplateDebug { c.log("Variable Field:", bit)
fmt.Println("Variable Field:", bit)
}
if bit == "" { if bit == "" {
continue continue
} }
// TODO: Fix this up so that it works for regular pointers and not just struct pointers. Ditto for the other cur.Kind() == reflect.Ptr we have in this file // TODO: Fix this up so that it works for regular pointers and not just struct pointers. Ditto for the other cur.Kind() == reflect.Ptr we have in this file
if cur.Kind() == reflect.Ptr { if cur.Kind() == reflect.Ptr {
if dev.TemplateDebug { c.log("Looping over pointer")
fmt.Println("Looping over pointer")
}
for cur.Kind() == reflect.Ptr { for cur.Kind() == reflect.Ptr {
cur = cur.Elem() cur = cur.Elem()
} }
c.log("Data Kind:", cur.Kind().String())
if dev.TemplateDebug { c.log("Field Bit:", bit)
fmt.Println("Data Kind:", cur.Kind().String())
fmt.Println("Field Bit:", bit)
}
} }
cur = cur.FieldByName(bit) cur = cur.FieldByName(bit)
out += "." + bit
if cur.Kind() == reflect.Interface { if cur.Kind() == reflect.Interface {
cur = cur.Elem() cur = cur.Elem()
out += "." + bit + ".(" + cur.Type().Name() + ")" out += ".(" + cur.Type().Name() + ")"
} else {
out += "." + bit
} }
if !cur.IsValid() { if !cur.IsValid() {
panic(out + "^\n" + "Invalid value. Maybe, it doesn't exist?") panic(out + "^\n" + "Invalid value. Maybe, it doesn't exist?")
} }
if dev.TemplateDebug { c.log("Data Kind:", cur.Kind())
fmt.Println("Data Kind:", cur.Kind()) c.log("Data Type:", cur.Type().Name())
fmt.Println("Data Type:", cur.Type().Name())
}
} }
if dev.TemplateDebug { c.log("Out Value:", out)
fmt.Println("Out Value:", out) c.log("Out Kind:", cur.Kind())
fmt.Println("Out Kind:", cur.Kind()) c.log("Out Type:", cur.Type().Name())
fmt.Println("Out Type:", cur.Type().Name())
}
for _, varItem := range c.varList { for _, varItem := range c.varList {
if strings.HasPrefix(out, varItem.Destination) { if strings.HasPrefix(out, varItem.Destination) {
@ -777,11 +662,9 @@ func (c *CTemplateSet) compileIfVarsub(varname string, varholder string, templat
} }
} }
if dev.TemplateDebug { c.log("Out Value:", out)
fmt.Println("Out Value:", out) c.log("Out Kind:", cur.Kind())
fmt.Println("Out Kind:", cur.Kind()) c.log("Out Type:", cur.Type().Name())
fmt.Println("Out Type:", cur.Type().Name())
}
_, ok := c.stats[out] _, ok := c.stats[out]
if ok { if ok {
@ -794,9 +677,7 @@ func (c *CTemplateSet) compileIfVarsub(varname string, varholder string, templat
} }
func (c *CTemplateSet) compileBoolsub(varname string, varholder string, templateName string, val reflect.Value) string { func (c *CTemplateSet) compileBoolsub(varname string, varholder string, templateName string, val reflect.Value) string {
if dev.TemplateDebug { c.log("in compileBoolsub")
fmt.Println("in compileBoolsub")
}
out, val := c.compileIfVarsub(varname, varholder, templateName, val) out, val := c.compileIfVarsub(varname, varholder, templateName, val)
// TODO: What if it's a pointer or an interface? I *think* we've got pointers handled somewhere, but not interfaces which we don't know the types of at compile time // TODO: What if it's a pointer or an interface? I *think* we've got pointers handled somewhere, but not interfaces which we don't know the types of at compile time
switch val.Kind() { switch val.Kind() {
@ -817,9 +698,7 @@ func (c *CTemplateSet) compileBoolsub(varname string, varholder string, template
} }
func (c *CTemplateSet) compileVarsub(varname string, val reflect.Value) string { func (c *CTemplateSet) compileVarsub(varname string, val reflect.Value) string {
if dev.TemplateDebug { c.log("in compileVarsub")
fmt.Println("in compileVarsub")
}
for _, varItem := range c.varList { for _, varItem := range c.varList {
if strings.HasPrefix(varname, varItem.Destination) { if strings.HasPrefix(varname, varItem.Destination) {
varname = strings.Replace(varname, varItem.Destination, varItem.Name, 1) varname = strings.Replace(varname, varItem.Destination, varItem.Name, 1)
@ -863,10 +742,8 @@ func (c *CTemplateSet) compileVarsub(varname string, val reflect.Value) string {
} }
func (c *CTemplateSet) compileSubtemplate(pvarholder string, pholdreflect reflect.Value, node *parse.TemplateNode) (out string) { func (c *CTemplateSet) compileSubtemplate(pvarholder string, pholdreflect reflect.Value, node *parse.TemplateNode) (out string) {
if dev.TemplateDebug { c.log("in compileSubtemplate")
fmt.Println("in compileSubtemplate") c.log("Template Node:", node.Name)
fmt.Println("Template Node:", node.Name)
}
fname := strings.TrimSuffix(node.Name, filepath.Ext(node.Name)) fname := strings.TrimSuffix(node.Name, filepath.Ext(node.Name))
varholder := "tmpl_" + fname + "_vars" varholder := "tmpl_" + fname + "_vars"
@ -907,9 +784,7 @@ func (c *CTemplateSet) compileSubtemplate(pvarholder string, pholdreflect reflec
c.tlist[fname] = tree c.tlist[fname] = tree
subtree := c.tlist[fname] subtree := c.tlist[fname]
if dev.TemplateDebug { c.log("subtree.Root", subtree.Root)
fmt.Println("subtree.Root", subtree.Root)
}
c.localVars[fname] = make(map[string]VarItemReflect) c.localVars[fname] = make(map[string]VarItemReflect)
c.localVars[fname]["."] = VarItemReflect{".", varholder, holdreflect} c.localVars[fname]["."] = VarItemReflect{".", varholder, holdreflect}
@ -919,6 +794,12 @@ func (c *CTemplateSet) compileSubtemplate(pvarholder string, pholdreflect reflec
return out return out
} }
func (c *CTemplateSet) log(args ...interface{}) {
if dev.TemplateDebug {
fmt.Println(args...)
}
}
func (c *CTemplateSet) compileCommand(*parse.CommandNode) (out string) { func (c *CTemplateSet) compileCommand(*parse.CommandNode) (out string) {
panic("Uh oh! Something went wrong!") panic("Uh oh! Something went wrong!")
} }