-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathexpression.go
executable file
·66 lines (50 loc) · 1.56 KB
/
expression.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"go/ast"
"regexp"
"strings"
"github.com/elliotchance/pie/pie"
"golang.org/x/tools/go/ast/astutil"
)
type Expression string
func (e Expression) DependencyNames() (deps []string) {
for _, v := range regexp.MustCompile(`@{(.*?)}`).FindAllStringSubmatch(string(e), -1) {
parts := strings.Split(v[1], "(")
deps = append(deps, parts[0])
}
return pie.Strings(deps).Unique()
}
func (e Expression) Dependencies() (deps []string) {
for _, v := range regexp.MustCompile(`@{(.*?)}`).FindAllStringSubmatch(string(e), -1) {
deps = append(deps, v[1])
}
return pie.Strings(deps).Unique()
}
func (e Expression) performSubstitutions(file *File, services Services, fromArgs bool) string {
stmt := string(e)
// Replace environment variables.
stmt = replaceAllStringSubmatchFunc(
regexp.MustCompile(`\${(.*?)}`), stmt, func(i []string) string {
astutil.AddImport(file.fset, file.file, "os")
return fmt.Sprintf("os.Getenv(\"%s\")", i[1])
})
// Replace service names.
stmt = replaceAllStringSubmatchFunc(
regexp.MustCompile(`@{(.*?)}`), stmt, func(i []string) string {
if fromArgs {
return strings.Split(i[1], "(")[0]
}
if strings.Contains(i[1], "(") {
return fmt.Sprintf("container.Get%s", i[1])
}
if _, existsService := services[i[1]]; !existsService {
panic(fmt.Sprintf("service does not exist: %s", i[1]))
}
if _, ok := services[i[1]].ContainerFieldType(services).(*ast.FuncType); ok {
return fmt.Sprintf("container.%s", i[1])
}
return fmt.Sprintf("container.Get%s()", i[1])
})
return stmt
}