-
Notifications
You must be signed in to change notification settings - Fork 52
/
serialization.go
52 lines (47 loc) · 1.03 KB
/
serialization.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
package serialization
import (
"crypto/md5"
"embed"
"encoding/json"
"fmt"
"regexp"
"strings"
"text/template"
"gopkg.in/yaml.v2"
)
var (
//go:embed templates/*.tmpl
templateFiles embed.FS
Templates *template.Template
)
func init() {
Templates = template.New("output").Funcs(template.FuncMap{
"json": func(i interface{}) string {
b, err := json.Marshal(i)
if err != nil {
return err.Error()
}
return string(b)
},
"yaml": func(i interface{}) string {
b, err := yaml.Marshal(i)
if err != nil {
return err.Error()
}
return strings.Trim(string(b), "\n")
},
"underscore": func(s string) string {
re := regexp.MustCompile(`[^A-Za-z0-9]+`)
return re.ReplaceAllString(strings.ToLower(s), "_")
},
"color": func(s string) string {
hash := md5.Sum([]byte(s))
return fmt.Sprintf("#%x", hash[:3])
},
})
template.Must(Templates.ParseFS(templateFiles, "templates/*.tmpl"))
}
type GraphSeralization interface {
Serialize([]byte) ([]byte, error)
Deserialize([]byte) ([]byte, error)
}