-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgolang.tmpl
199 lines (165 loc) · 4.52 KB
/
golang.tmpl
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Code generated by errata; DO NOT EDIT.
// Errata Schema Version: {{ SchemaVersion }}
// Hash: {{ Hash }}
{% import "templates/golang-macros.tmpl" quote_list, quote_map, args, args_list, contexts -%}
package {{ Package }}
import (
"crypto/sha1"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"time"
{% for i in Options.Imports sorted -%}
{{- i | stringformat:"%q" }}
{%- endfor %}
)
type erratum struct {
code string
message string
categories []string
args map[string]interface{}
labels map[string]string
guide string
file string
line int
uuid string
wrapped error
}
// TODO: add documentation to all public methods
// Erratum is the public interface which indicates that a given error is an Erratum.
type Erratum interface {
// behave like a regular error
error
Unwrap() error
Code() string
Message() string
Categories() []string
Args() map[string]interface{}
Guide() string
Labels() map[string]string
UUID() string
HelpURL() string
}
func (e *erratum) Unwrap() error {
return e.wrapped
}
func (e *erratum) UUID() string {
if e.uuid == "" {
e.uuid = generateReference(e.code)
}
return e.uuid
}
// Format controls the verbosity of the printed error.
func (e *erratum) Format(f fmt.State, verb rune) {
if verb == 'v' && f.Flag('+') {
args := func() string {
if len(e.Args()) <= 0 {
return ""
}
var args []string
for k, v := range e.Args() {
args = append(args, fmt.Sprintf(`%s="%+v"`, k, v))
}
return fmt.Sprintf(` (%s)`, strings.Join(args, ", "))
}()
f.Write([]byte(fmt.Sprintf("%s%s. For more details, see %s", e.Error(), args, e.HelpURL())))
if unwrapped := e.Unwrap(); unwrapped != nil {
f.Write([]byte("\n↳ "))
if e, ok := unwrapped.(fmt.Formatter); ok {
e.Format(f, verb)
} else if e, ok := unwrapped.(error); ok {
f.Write([]byte(e.Error()))
}
}
} else {
f.Write([]byte(e.Error()))
}
}
func (e *erratum) Error() string {
return fmt.Sprintf("[%s] <%s:%v> %s", e.code, e.file, e.line, e.message)
}
func (e *erratum) HelpURL() string {
return fmt.Sprintf("{{ Options.BaseURL }}%s", e.code)
}
func (e *erratum) Code() string {
return e.code
}
func (e *erratum) Message() string {
return e.message
}
func (e *erratum) Categories() []string {
return e.categories
}
func (e *erratum) Args() map[string]interface{} {
return e.args
}
func (e *erratum) Labels() map[string]string {
return e.labels
}
func (e *erratum) Guide() string {
return e.guide
}
func (e *erratum) File() string {
return e.file
}
func (e *erratum) Line() int {
return e.line
}
const (
{%- for code, error in Errors sorted %}
{{ code | constantize }}ErrCode string = "{{ Options.Prefix }}{{ code }}"
{%- endfor %}
)
{%- for code, error in Errors sorted %}
type {{ code | constantize }}Err struct {
erratum
}
{%- endfor %}
{% for code, error in Errors sorted %}
func New{{ code | constantize }}Err(wrapped error, {{ args(error.Args) }} {{ contexts(error.Definition.context) }}) *{{ code | constantize }}Err {
err := erratum{
code: {{ code | constantize }}ErrCode,
message: `{{ error.Message | escape_backtick -}}`,
categories: []string{ {{- quote_list(error.Categories) -}} },
labels: map[string]string{ {{ quote_map(error.Labels) }} },
guide: `{{ error.Guide | escape_backtick -}}`,
args: map[string]interface{}{
{%- for arg in error.Args sorted %}
"{{ arg.Name }}": {{ arg.Name }},
{%- endfor %}
},
wrapped: wrapped,
}
addCaller(&err)
return &{{ code | constantize }}Err{err}
}
{% for arg in error.Args sorted %}
// Get{{ arg.Name | constantize }} returns the {{ arg.Name | stringformat:"%q" }} argument for a {{ code | constantize }}Err instance.
func (e *{{ code | constantize }}Err) Get{{ arg.Name | constantize }}() interface{} {
return e.args["{{ arg.Name }}"]
}
{% endfor -%}
{% for label, value in error.Labels sorted %}
// Get{{ label | constantize }} returns the {{ label|stringformat:"%q" }} label for a {{ code | constantize }}Err instance.
func (e *{{ code | constantize }}Err) Get{{ label | constantize }}() string {
return {{ value | stringformat:"%q" }}
}
{% endfor -%}
{% endfor -%}
func addCaller(err *erratum) {
_, file, line, ok := runtime.Caller(3)
if ok {
paths := strings.Split(file, string(os.PathSeparator))
segments := 2
if len(paths) < segments {
segments = 1
}
err.file = filepath.Join(paths[len(paths)-segments:]...)
err.line = line
}
}
func generateReference(code string) string {
return fmt.Sprintf("%x", sha1.Sum([]byte(code + time.Now().Format(time.RFC3339Nano))))
}