-
Notifications
You must be signed in to change notification settings - Fork 0
/
verb_panic.go
81 lines (65 loc) · 2.13 KB
/
verb_panic.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 main
import "go/ast"
import "text/template"
import "defimpl/util"
type PanicVerbPhrase struct {
baseVerbPhrase
MethodParameters string
MethodResults string
}
var _ VerbPhrase = (*PanicVerbPhrase)(nil)
func (vp *PanicVerbPhrase) StructBody() (string, error) {
return "", nil
}
type Verb_Panic struct {}
var _ VerbDefinition = (*Verb_Panic)(nil)
func init() {
vd := &Verb_Panic{}
VerbDefinitions[vd.Tag()] = vd
}
// Tag is part of the VerbDefinition interface.
func (vd *Verb_Panic) Tag() string { return "panic" }
// Description is part of the VerbDefinition interface.
func (vd *Verb_Panic) Description() string {
return "the method will panic if called, for when an implementation only needs to partially implement an interface."
}
// NewVerbPhrase is part of the VerbDefinition interface.
func (vd *Verb_Panic) NewVerbPhrase(ctx *context, idef *InterfaceDefinition, field *ast.Field, comment *ast.Comment) (VerbPhrase, error) {
_, err, scratchpad := CheckSignatures(ctx, vd, idef.Package(), field, vd.GlobalsTemplate())
if err != nil {
return nil, err
}
q := util.TypeStringQualifier(idef.File.AstFile)
params := ""
if p, ok := scratchpad["__PARAMETERS"].(*ast.FieldList); ok {
params, _ = util.FieldListString(p, ctx.info, q, false, false)
}
results := ""
if r, ok := scratchpad["__RESULTS"].(*ast.FieldList); ok {
results, _ = util.FieldListString(r, ctx.info, q, false, true)
}
vp := &PanicVerbPhrase{
baseVerbPhrase: baseVerbPhrase {
verb: vd,
idef: idef,
field: field,
},
MethodParameters: params,
MethodResults: results,
}
return vp, nil
}
var panic_method_template = template.Must(
template.New("panic_method_template").Parse(`
// {{.MethodName}} is part of the {{.InterfaceName}} interface. defimpl verb {{.Verb.Tag}}.
func (x *{{.StructName}}) {{.MethodName}}({{.MethodParameters}}) {{.MethodResults}} {
panic("(*{{.StructName}}).{{.MethodName}} was called")
}
`))
// GlobalsTemplate is part of the VerbDefinition interface.
func (vd *Verb_Panic) GlobalsTemplate() *template.Template {
return panic_method_template
}
func (vd *Verb_Panic) StructBody(VerbPhrase) (string, error) {
return "", nil
}