pkger/parser/decl.go

59 lines
891 B
Go
Raw Permalink Normal View History

2019-10-21 17:27:04 +03:00
package parser
import (
"go/token"
"sort"
)
type Decl interface {
File() (*File, error)
Pos() (token.Pos, error)
Value() (string, error)
}
type Filer interface {
2019-10-23 20:19:39 +03:00
Files(map[string]string) ([]*File, error)
}
type Virtualer interface {
VirtualPaths() []string
2019-10-21 17:27:04 +03:00
}
type Decls []Decl
func (decls Decls) Files() ([]*File, error) {
m := map[string]*File{}
2019-10-23 20:19:39 +03:00
v := map[string]string{}
2019-10-21 17:27:04 +03:00
for _, d := range decls {
2019-10-23 20:19:39 +03:00
if vt, ok := d.(Virtualer); ok {
for _, s := range vt.VirtualPaths() {
v[s] = s
}
}
2019-10-21 17:27:04 +03:00
fl, ok := d.(Filer)
if !ok {
continue
}
2019-10-23 20:19:39 +03:00
files, err := fl.Files(v)
2019-10-21 17:27:04 +03:00
if err != nil {
return nil, err
}
for _, f := range files {
m[f.Path.String()] = f
}
}
var files []*File
for _, f := range m {
files = append(files, f)
}
sort.Slice(files, func(i, j int) bool {
return files[i].Path.String() < files[j].Path.String()
})
return files, nil
}