-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtemplate.go
142 lines (123 loc) · 2.59 KB
/
template.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// Copyright (c) 2022 Markku Rossi
//
// All rights reserved.
//
package circuit
import (
"fmt"
"regexp"
"strconv"
"strings"
)
var reVar = regexp.MustCompilePOSIX(`{(.?){([^\}]+)}}`)
// Template defines an expandable text template.
type Template struct {
parts []*part
FloatCvt FloatCvt
IntCvt IntCvt
StringCvt StringCvt
}
// FloatCvt converts a float64 value to float64 value.
type FloatCvt func(v float64) float64
// IntCvt converts an integer value to float64 value.
type IntCvt func(v int) float64
// StringCvt converts a string value to a string value.
type StringCvt func(v string) string
const (
partFloat = iota
partInt
partString
)
type part struct {
t int
fv float64
iv int
sv string
}
func (p part) String() string {
switch p.t {
case partFloat:
return fmt.Sprintf("float64:%v", p.fv)
case partInt:
return fmt.Sprintf("int:%v", p.iv)
case partString:
return p.sv
default:
return fmt.Sprintf("{part %d}", p.t)
}
}
// NewTemplate parses the input string and returns the parsed
// Template.
func NewTemplate(input string) *Template {
t := &Template{
FloatCvt: func(v float64) float64 { return v },
IntCvt: func(v int) float64 { return float64(v) },
StringCvt: func(v string) string { return v },
}
matches := reVar.FindAllStringSubmatchIndex(input, -1)
if matches == nil {
return t
}
var start int
var err error
for _, m := range matches {
if m[0] > start {
t.parts = append(t.parts, &part{
t: partString,
sv: input[start:m[0]],
})
}
content := input[m[4]:m[5]]
part := &part{
t: partFloat,
sv: content,
}
t.parts = append(t.parts, part)
start = m[1]
if m[2] != m[3] {
switch input[m[2]:m[3]] {
default:
panic(fmt.Sprintf("unknown template variable conversion: %s",
input[m[2]:m[3]]))
}
} else {
part.iv, err = strconv.Atoi(content)
if err == nil {
part.t = partInt
} else {
part.fv, err = strconv.ParseFloat(content, 64)
if err == nil {
part.t = partFloat
} else {
part.sv = content
part.t = partString
}
}
}
}
if start < len(input) {
t.parts = append(t.parts, &part{
t: partString,
sv: input[start:],
})
}
return t
}
// Expand expands the template.
func (t *Template) Expand() string {
var b strings.Builder
for _, part := range t.parts {
switch part.t {
case partFloat:
b.WriteString(fmt.Sprintf("%v", t.FloatCvt(part.fv)))
case partInt:
b.WriteString(fmt.Sprintf("%v", t.IntCvt(part.iv)))
case partString:
b.WriteString(part.sv)
default:
panic(fmt.Sprintf("invalid part type: %v", part.t))
}
}
return b.String()
}