forked from Murray-LIANG/gounity
/
errors.go
80 lines (70 loc) · 1.53 KB
/
errors.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
package pkg
import (
"io/ioutil"
"sort"
"text/template"
"time"
"github.com/sirupsen/logrus"
yaml "gopkg.in/yaml.v2"
)
type errors struct {
errors []*errorEntry
tmplFilePath string
fieldsFilePath string
packageName string
}
type errorEntry struct {
Name string
ErrorCode int
}
// NewErrors constructs a `errors`.
func NewErrors(tmplFilePath, fieldsFilePath, packageName string) *errors {
return &errors{
tmplFilePath: tmplFilePath,
fieldsFilePath: fieldsFilePath,
packageName: packageName,
}
}
func (e *errors) parseFields() *errors {
log := logrus.WithField("errors", e)
rawFields, err := ioutil.ReadFile(e.fieldsFilePath)
if err != nil {
log.WithError(err).Fatal("read errors field file failed")
}
ef := make(map[int]string)
err = yaml.Unmarshal(rawFields, ef)
if err != nil {
log.WithError(err).Fatal("unmarshal errors failed")
}
codes := make([]int, 0)
for c := range ef {
codes = append(codes, c)
}
sort.Ints(codes)
tmpErrors := make([]*errorEntry, 0)
for _, c := range codes {
e := &errorEntry{
Name: ef[c],
ErrorCode: c,
}
tmpErrors = append(tmpErrors, e)
logrus.Infof("modelgen: error %s parsed", ef[c])
}
e.errors = tmpErrors
return e
}
func (e *errors) GetTemplate() *template.Template {
return template.Must(template.ParseFiles(e.tmplFilePath))
}
func (e *errors) PrepareData() interface{} {
e = e.parseFields()
return struct {
Timestamp time.Time
PackageName string
Errors []*errorEntry
}{
time.Now().UTC(),
e.packageName,
e.errors,
}
}