-
Notifications
You must be signed in to change notification settings - Fork 5
/
format.go
159 lines (146 loc) · 3.98 KB
/
format.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package format
import (
"fmt"
"reflect"
"strconv"
"strings"
)
// Values is easier than map[string]interface{}
type Values = map[string]interface{}
// Format function
func Format(format string, context Context) (string, error) {
splitter := NewSplitter(format, context)
var res strings.Builder
for splitter.Split() {
res.WriteString(splitter.Text())
}
return res.String(), splitter.Err()
}
// Formatp is a formatting with positional arguments
func Formatp(format string, a ...interface{}) string {
bctx := NewContextBuilder()
for i, value := range a {
key := strconv.Itoa(i)
bctx.Add(key, value)
}
ctx, err := bctx.Build()
if err != nil {
panic(err)
}
res, err := Format(format, ctx)
if err != nil {
panic(err)
}
return res
}
// Formatm is a formatting with keys from given map
func Formatm(format string, data Values) string {
bctx := NewContextBuilder()
for key, value := range data {
bctx.Add(key, value)
}
ctx, err := bctx.Build()
if err != nil {
panic(err)
}
res, err := Format(format, ctx)
if err != nil {
panic(err)
}
return res
}
func resolveValue(prefix []string, bctx *ContextBuilder, value reflect.Value) bool {
switch value.Kind() {
case reflect.Ptr:
resolveValue(prefix, bctx, value.Elem())
case reflect.Struct:
iterateStruct(prefix, bctx, value)
return true
case reflect.Map:
valueType := value.Type().Key()
switch valueType.Kind() {
case reflect.String:
iterateStringMap(prefix, bctx, value)
return true
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
iterateIntMap(prefix, bctx, value)
return true
default:
stringerType := reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
if valueType.Implements(stringerType) {
iterateStringerMap(prefix, bctx, value)
return true
}
}
}
return false
}
func iterateStringerMap(prefix []string, builder *ContextBuilder, value reflect.Value) {
keys := value.MapKeys()
for _, keyData := range keys {
keyValue := value.MapIndex(keyData)
key := append(prefix, keyData.Interface().(fmt.Stringer).String())
builder.Add(join(key), keyValue.Interface())
resolveValue(key, builder, keyValue)
}
}
func iterateIntMap(prefix []string, builder *ContextBuilder, value reflect.Value) {
keys := value.MapKeys()
for _, keyData := range keys {
keyValue := value.MapIndex(keyData)
key := append(prefix, fmt.Sprintf("%d", keyData.Interface()))
builder.Add(join(key), keyValue.Interface())
resolveValue(key, builder, keyValue)
}
}
func iterateStringMap(prefix []string, builder *ContextBuilder, value reflect.Value) {
keys := value.MapKeys()
for _, keyData := range keys {
keyValue := value.MapIndex(keyData)
key := append(prefix, keyData.String())
builder.Add(join(key), keyValue.Interface())
resolveValue(key, builder, keyValue)
}
}
func iterateStruct(prefix []string, builder *ContextBuilder, value reflect.Value) {
for i := 0; i < value.NumField(); i++ {
fieldMeta := value.Type().Field(i)
fieldValue := value.Field(i)
if fieldMeta.Anonymous {
resolveValue(prefix, builder, fieldValue)
} else {
key := append(prefix, fieldMeta.Name)
builder.Add(join(key), fieldValue.Interface())
resolveValue(key, builder, fieldValue)
}
}
}
func join(src []string) string {
return strings.Join(src, ".")
}
// Formatg is a formatting with type guessing
func Formatg(format string, data interface{}) string {
bctx := NewContextBuilder()
value := reflect.ValueOf(data)
if !resolveValue(nil, bctx, value) {
panic(fmt.Errorf("struct or map[string | integer | Stringer]X expected, got %T", data))
}
ctx, err := bctx.Build()
if err != nil {
panic(err)
}
res, err := Format(format, ctx)
if err != nil {
panic(err)
}
return res
}
// Formatf is a formatting where values are taken form given func(string) string function
func Formatf(format string, data func(string) string) string {
ctx := contextFunc(data)
res, err := Format(format, ctx)
if err != nil {
panic(err)
}
return res
}