Merge pull request #97 from qmuntal/master

Support const paths
This commit is contained in:
Mark Bates 2020-06-03 14:03:53 -04:00 committed by GitHub
commit 992ea93a2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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
} }