-
Notifications
You must be signed in to change notification settings - Fork 0
/
verb_index.go
75 lines (61 loc) · 1.87 KB
/
verb_index.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 IndexVerbPhrase struct {
slotVerbPhrase
}
var _ VerbPhrase = (*IndexVerbPhrase)(nil)
var _ SlotVerbPhrase = (*IndexVerbPhrase)(nil)
var _ GlobalsTemplateParameter = (*IndexVerbPhrase)(nil)
type Verb_Index struct {
slotVerbDefinition
}
var _ VerbDefinition = (*Verb_Index)(nil)
func init() {
vd := &Verb_Index{}
VerbDefinitions[vd.Tag()] = vd
}
// Verb is part of the VerbDefinition interface.
func (vd *Verb_Index) Tag() string { return "index" }
// Description is part of the VerbDefinition interface.
func (vd *Verb_Index) Description() string {
return "returns the element of the specified slice valued field at the specified (zero based) index."
}
// NewVerbPhrase is part of the VerbDefinition interface.
func (vd *Verb_Index) 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 := &IndexVerbPhrase{
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 index_method_template = template.Must(
template.New("index_method_template").Parse(`
// {{.MethodName}} is part of the {{.InterfaceName}} interface. defimpl verb {{.Verb.Tag}}.
func (x *{{.StructName}}) {{.MethodName}} (index int) {{.TypeString .SlotType.Elem}} {
return x.{{.SlotName}}[index]
}
`))
// GlobalsTemplate is part of the VerbDefinition interface.
func (vd *Verb_Index) GlobalsTemplate() *template.Template {
return index_method_template
}