-
Notifications
You must be signed in to change notification settings - Fork 0
/
verb_delete.go
84 lines (70 loc) · 1.98 KB
/
verb_delete.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
81
82
83
package main
import "go/ast"
import "go/types"
import "text/template"
type DeleteVerbPhrase struct {
slotVerbPhrase
}
var _ VerbPhrase = (*DeleteVerbPhrase)(nil)
var _ SlotVerbPhrase = (*DeleteVerbPhrase)(nil)
var _ GlobalsTemplateParameter = (*DeleteVerbPhrase)(nil)
type Verb_Delete struct {
slotVerbDefinition
}
var _ VerbDefinition = (*Verb_Delete)(nil)
func init() {
vd := &Verb_Delete{}
VerbDefinitions[vd.Tag()] = vd
}
// Verb is part of the VerbDefinition interface.
func (vd *Verb_Delete) Tag() string { return "delete" }
// Description is part of the VerbDefinition interface.
func (vd *Verb_Delete) Description() string {
return "deletes the specified item from the filed."
}
// NewVerbPhrase is part of the VerbDefinition interface.
func (vd *Verb_Delete) 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 := &DeleteVerbPhrase{
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 delete_method_template = template.Must(
template.New("delete_method_template").Parse(`
// {{.MethodName}} is part of the {{.InterfaceName}} interface. defimpl verb {{.Verb.Tag}}.
func (x *{{.StructName}}) {{.MethodName}} (item {{.TypeString .SlotType.Elem}}) {
i := -1
for j, v := range x.{{.SlotName}} {
if v == item {
i = j
break
}
}
if i >= 0 {
x.{{.SlotName}} = append(x.{{.SlotName}}[:i], x.{{.SlotName}}[i+1:]...)
}
}
`))
// GlobalsTemplate is part of the VerbDefinition interface.
func (vd *Verb_Delete) GlobalsTemplate() *template.Template {
return delete_method_template
}