-
Notifications
You must be signed in to change notification settings - Fork 0
/
verb_append.go
75 lines (61 loc) · 1.85 KB
/
verb_append.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 main
import "go/ast"
import "go/types"
import "text/template"
type AppendVerbPhrase struct {
slotVerbPhrase
}
var _ VerbPhrase = (*AppendVerbPhrase)(nil)
var _ SlotVerbPhrase = (*AppendVerbPhrase)(nil)
var _ GlobalsTemplateParameter = (*AppendVerbPhrase)(nil)
type Verb_Append struct {
slotVerbDefinition
}
var _ VerbDefinition = (*Verb_Append)(nil)
func init() {
vd := &Verb_Append{}
VerbDefinitions[vd.Tag()] = vd
}
// Verb is part of the VerbDefinition interface.
func (vd *Verb_Append) Tag() string { return "append" }
// Description is part of the VerbDefinition interface.
func (vd *Verb_Append) Description() string {
return "appends the specified values to the field."
}
// NewVerbPhrase is part of the VerbDefinition interface.
func (vd *Verb_Append) NewVerbPhrase(ctx *context, idef *InterfaceDefinition, field *ast.Field, comment *ast.Comment) (VerbPhrase, error) {
slot, err := parse_slot_verb_phrase(ctx, field, comment)
if err != nil {
return nil, err
}
slot_type, err, _ := CheckSignatures(ctx, vd, idef.Package(), field, vd.GlobalsTemplate())
if err != nil {
return nil, err
}
vp := &AppendVerbPhrase{
slotVerbPhrase {
baseVerbPhrase: baseVerbPhrase {
verb: vd,
idef: idef,
field: field,
},
slot_name: slot,
slot_type: types.NewSlice(slot_type),
},
}
if err := addSlotSpec(idef, vp); err != nil {
return nil, err
}
return vp, nil
}
var append_method_template = template.Must(
template.New("append_method_template").Parse(`
// {{.MethodName}} is part of the {{.InterfaceName}} interface. defimpl verb {{.Verb.Tag}}.
func (x *{{.StructName}}) {{.MethodName}} (v ...{{.TypeString .SlotType.Elem}}) {
x.{{.SlotName}} = append(x.{{.SlotName}}, v...)
}
`))
// GlobalsTemplate is part of the VerbDefinition interface.
func (vd *Verb_Append) GlobalsTemplate() *template.Template {
return append_method_template
}