-
Notifications
You must be signed in to change notification settings - Fork 547
/
engine.go
74 lines (62 loc) 路 1.82 KB
/
engine.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
package render
import (
"fmt"
"html/template"
"io"
"reflect"
"regexp"
"strings"
"github.com/go-echarts/go-echarts/v2/types"
)
const (
ModChart = "chart"
ModPage = "page"
// EchartsInstancePrefix the default prefix for each echarts instance
EchartsInstancePrefix = "goecharts_"
// EchartsInstancePlaceholder a placeholder for types.FuncStr inject echarts instance
EchartsInstancePlaceholder = "%MY_ECHARTS%"
)
var pat = regexp.MustCompile(`(__f__")|("__f__)|(__f__)`)
type ChartSnippet struct {
Element string
Script string
Option string
}
// Renderer
// Any kinds of charts have their render implementation and
// you can define your own render logic easily.
type Renderer interface {
Render(w io.Writer) error
RenderContent() []byte
RenderSnippet() ChartSnippet
}
// isSet check if the field exist in the chart instance
// Shamed copy from https://stackoverflow.com/questions/44675087/golang-template-variable-isset
func isSet(name string, data interface{}) bool {
v := reflect.ValueOf(data)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if v.Kind() != reflect.Struct {
return false
}
return v.FieldByName(name).IsValid()
}
// MustTemplate creates a new template with the given name and parsed contents.
func MustTemplate(name string, contents []string) *template.Template {
tpl := template.New(name).Funcs(template.FuncMap{
"safeJS": func(s interface{}) template.JS {
return template.JS(fmt.Sprint(s))
},
"isSet": isSet,
"injectInstance": func(funcStr types.FuncStr, echartsInstancePlaceholder string, chartID string) string {
instance := EchartsInstancePrefix + chartID
return strings.Replace(string(funcStr), echartsInstancePlaceholder, instance, -1)
},
})
tpl = template.Must(tpl.Parse(contents[0]))
for _, cont := range contents[1:] {
tpl = template.Must(tpl.Parse(cont))
}
return tpl
}