This repository has been archived by the owner on Jun 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaststr.go
97 lines (88 loc) · 1.79 KB
/
aststr.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
cmd "cli/controllers"
"fmt"
"path"
"strings"
)
type strLeaf struct {
val string
}
func (l strLeaf) getStr() (string, error) {
return l.val, nil
}
func (l strLeaf) execute() (interface{}, error) {
return l.getStr()
}
type pathNode struct {
path node
}
func (n pathNode) getStr() (string, error) {
val, err := n.path.execute()
if err != nil {
return "", err
}
p, ok := val.(string)
if !ok {
return "", fmt.Errorf("path should be a string")
}
if p == "" {
p = "."
}
if p == "_" {
return "_", nil
}
if p == "-" {
return cmd.State.PrevPath, nil
}
var output_words []string
if p[0] != '/' {
output_words = strings.Split(cmd.State.CurrPath, "/")[1:]
if len(output_words) == 1 && output_words[0] == "" {
output_words = output_words[0:0]
}
} else {
p = p[1:]
}
input_words := strings.Split(p, "/")
for _, word := range input_words {
if word == "." {
continue
} else if word == ".." {
if len(output_words) > 0 {
output_words = output_words[:len(output_words)-1]
}
} else {
output_words = append(output_words, word)
}
}
if len(output_words) > 0 {
if output_words[0] == "P" {
output_words[0] = "Physical"
} else if output_words[0] == "L" {
output_words[0] = "Logical"
}
}
return path.Clean("/" + strings.Join(output_words, "/")), nil
}
func (n pathNode) execute() (interface{}, error) {
return n.getStr()
}
type formatStringNode struct {
str string
varsDeref []symbolReferenceNode
}
func (n *formatStringNode) getStr() (string, error) {
vals := []any{}
for _, varDeref := range n.varsDeref {
val, err := varDeref.execute()
if err != nil {
return "", err
}
vals = append(vals, val)
}
return fmt.Sprintf(n.str, vals...), nil
}
func (n *formatStringNode) execute() (interface{}, error) {
return n.getStr()
}