-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverb_delegate.go
123 lines (104 loc) · 3.58 KB
/
verb_delegate.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
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
// The delegate verb is meant as an aid to developing an an interface
// impleementation that can delegate some interface methods to some
// data member.
//
// The parameter of the delegate verb is the sewcond half of a
// selector expression that will be combined with the receiver to
// identify where to delegate the operation to.
package main
import "fmt"
import "reflect"
import "strings"
import "go/ast"
import "text/template"
import "defimpl/util"
type DelegateVerbPhrase struct {
baseVerbPhrase
MethodParameters string
ParameterNames string
MethodResults string
DelegateTo string
}
var _ VerbPhrase = (*DelegateVerbPhrase)(nil)
func (vp *DelegateVerbPhrase) StructBody() (string, error) {
return "", nil
}
type Verb_Delegate struct {}
var _ VerbDefinition = (*Verb_Delegate)(nil)
func init() {
vd := &Verb_Delegate{}
VerbDefinitions[vd.Tag()] = vd
}
// Tag is part of the VerbDefinition interface.
func (vd *Verb_Delegate) Tag() string { return "delegate" }
// Description is part of the VerbDefinition interface.
func (vd *Verb_Delegate) Description() string {
return "the method will delegate to another object."
}
// NewVerbPhrase is part of the VerbDefinition interface.
func (vd *Verb_Delegate) 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
}
delegate_to, err := parse_DelegateVerbPhrase(ctx, field, comment)
if err != nil {
return nil, err
}
q := util.TypeStringQualifier(idef.File.AstFile)
// For method parameters we need two forms:
// * the parameter list declaration
/// * the parameter names to pass to the delegate.
params := ""
actual := ""
if p, ok := scratchpad["__PARAMETERS"].(*ast.FieldList); ok {
params, actual = util.FieldListString(p, ctx.info, q, true, false)
}
results := ""
if r, ok := scratchpad["__RESULTS"].(*ast.FieldList); ok {
results, _ = util.FieldListString(r, ctx.info, q, false, true)
}
vp := &DelegateVerbPhrase{
baseVerbPhrase: baseVerbPhrase {
verb: vd,
idef: idef,
field: field,
},
MethodParameters: params,
ParameterNames: actual,
MethodResults: results,
DelegateTo: delegate_to,
}
return vp, nil
}
func parse_DelegateVerbPhrase(ctx *context, field *ast.Field, comment *ast.Comment) (string, error) {
val, ok := reflect.StructTag(comment.Text[2:]).Lookup("defimpl")
if !ok {
// Shouldn't happen. To get here we should already have found a defimpl comment.
panic("Can't construct VerbPhrase from a non-defimpl comment.")
}
// Everything after the verb itself is captured as the
// delation target, in case the string representation of that
// target contains a space.
split := strings.SplitN(val, " ", 2)
if len(split) != 2 {
pos := ctx.fset.Position(comment.Slash)
return "", fmt.Errorf("defimpl verb %q expects 1 parameter, the right hand side of a selector expression: %s %q",
split[0], pos, comment.Text)
}
return split[1], nil
}
var delegate_method_template = template.Must(
template.New("delegate_method_template").Parse(`
// {{.MethodName}} is part of the {{.InterfaceName}} interface. defimpl verb {{.Verb.Tag}}.
func (x *{{.StructName}}) {{.MethodName}}({{.MethodParameters}}) ({{.MethodResults}}) {
return x.{{.DelegateTo}}.{{.MethodName}}({{.ParameterNames}})
}
`))
// GlobalsTemplate is part of the VerbDefinition interface.
func (vd *Verb_Delegate) GlobalsTemplate() *template.Template {
return delegate_method_template
}
func (vd *Verb_Delegate) StructBody(VerbPhrase) (string, error) {
return "", nil
}