Skip to content
This repository has been archived by the owner on Aug 22, 2022. It is now read-only.

Commit

Permalink
Merge pull request #97 from qmuntal/master
Browse files Browse the repository at this point in the history
Support const paths
  • Loading branch information
markbates authored Jun 3, 2020
2 parents d84a13e + c7908c9 commit 992ea93
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
29 changes: 26 additions & 3 deletions parser/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,39 @@ func (p *ParsedSource) Parse() error {
return p.err
}

func (p *ParsedSource) value(node ast.Node) (string, error) {
var s string
func (p *ParsedSource) valueIdent(node *ast.Ident) (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) {
case *ast.BasicLit:
s = x.Value
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)
}

Expand Down
6 changes: 5 additions & 1 deletion pkging/pkgtest/testdata/ref/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ func main() {
}
}

const (
unused, pathAsset = "", "/assets"
)

func run() error {
if err := actions.WalkTemplates(os.Stdout); err != nil {
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 {
return err
}
Expand Down

0 comments on commit 992ea93

Please sign in to comment.