forked from voxelbrain/jsonformat
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfuncs.go
More file actions
59 lines (53 loc) · 1017 Bytes
/
funcs.go
File metadata and controls
59 lines (53 loc) · 1017 Bytes
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
package main
import (
"fmt"
"strings"
)
var defaultFuncs = map[string]interface{}{
"str": String,
"dec": Decimal,
"eq": Equal,
"eq_igncase": EqualIgnoreCase,
}
func String(v ...interface{}) string {
if v == nil || len(v) == 0 {
return "\"\""
}
switch x := v[0].(type) {
case string:
x = strings.Replace(x, `\`, `\\`, -1)
x = strings.Replace(x, `"`, `\"`, -1)
return fmt.Sprintf("\"%s\"", x)
case float64:
return fmt.Sprintf("\"%f\"", x)
}
return "\"\""
}
func Decimal(dec int, v ...interface{}) string {
if v == nil || len(v) == 0 {
return ""
}
if f, ok := v[0].(float64); ok {
fmtstr := fmt.Sprintf("%%.%df", dec)
return fmt.Sprintf(fmtstr, f)
}
return ""
}
func Equal(v ...interface{}) interface{} {
if v == nil || len(v) < 2 {
return nil
}
if v[0] == v[1] {
return v[0]
}
return nil
}
func EqualIgnoreCase(s ...string) string {
if len(s) < 2 {
return ""
}
if strings.ToLower(s[0]) == strings.ToLower(s[1]) {
return s[0]
}
return ""
}