support const expressions

This commit is contained in:
Quim Muntal Diaz 2020-05-21 17:19:19 +02:00
parent d97c60c346
commit c7908c9bfc
2 changed files with 31 additions and 4 deletions

View File

@ -33,16 +33,39 @@ func (p *ParsedSource) Parse() error {
return p.err return p.err
} }
func (p *ParsedSource) value(node ast.Node) (string, error) { func (p *ParsedSource) valueIdent(node *ast.Ident) (s string) {
var s string s = node.Name
if node.Obj.Kind != ast.Con {
return
}
// As per ast package a Con object is always a *ValueSpec,
// but double-checking to avoid panics
if x, ok := node.Obj.Decl.(*ast.ValueSpec); ok {
// The const var can be defined inline with other vars,
// as in `const a, b = "a", "b"`.
for i, v := range x.Names {
if v.Name == node.Name {
s = p.valueNode(x.Values[i])
break
}
}
}
return
}
func (p *ParsedSource) valueNode(node ast.Node) string {
var s string
switch x := node.(type) { switch x := node.(type) {
case *ast.BasicLit: case *ast.BasicLit:
s = x.Value s = x.Value
case *ast.Ident: case *ast.Ident:
s = x.Name s = p.valueIdent(x)
}
return s
} }
func (p *ParsedSource) value(node ast.Node) (string, error) {
s := p.valueNode(node)
return strconv.Unquote(s) return strconv.Unquote(s)
} }

View File

@ -18,12 +18,16 @@ func main() {
} }
} }
const (
unused, pathAsset = "", "/assets"
)
func run() error { func run() error {
if err := actions.WalkTemplates(os.Stdout); err != nil { if err := actions.WalkTemplates(os.Stdout); err != nil {
return err return err
} }
err := pkger.Walk("/assets", func(path string, info os.FileInfo, err error) error { err := pkger.Walk(pathAsset, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
} }