generated from dogmatiq/template-go
/
entity.go
183 lines (152 loc) · 4.42 KB
/
entity.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package lint
import (
"fmt"
"go/ast"
"go/types"
"golang.org/x/tools/go/ssa"
)
type (
// Entity is an application or handler.
Entity struct {
Type types.Type
ConfigureMethod ConfigureMethod
}
// Application contains information about a type that implements
// [dogma.Application].
Application struct{ Entity }
// Handler contains information about a type that implements one of Dogma's
// handler interfaces.
Handler = struct{ Entity }
// Aggregate contains information about a type that implements
// [dogma.AggregateMessageHandler].
Aggregate struct{ Handler }
// Process contains information about a type that implements
// [dogma.ProcessMessageHandler].
Process struct{ Handler }
// Integration contains information about a type that implements
// [dogma.IntegrationMessageHandler].
Integration struct{ Handler }
// Projection contains information about a type that implements
// [dogma.ProjectionMessageHandler].
Projection struct{ Handler }
)
// ConfigureMethod describes the Configure() method of an application or
// handler.
type ConfigureMethod struct {
Declaration *ast.FuncDecl
Implementation *ssa.Function
Configurer *ssa.Parameter
}
// IsConfigurerCall returns true if c is a call to a specific method of the
// entity's configurer.
func (m *ConfigureMethod) IsConfigurerCall(c *ssa.Call, method string) bool {
return c.Call.Value == m.Configurer && c.Common().Method.Name() == method
}
// buildEntities populates the entities fields of ctx.
func buildEntities(ctx *Context) {
// If the package does not import dogma, then there is nothing to do.
if ctx.Dogma == nil {
return
}
for _, m := range ctx.SSAPackage.Members {
if m, ok := m.(*ssa.Type); ok {
t := m.Type()
if impl, ok := implements(t, ctx.Dogma.Application); ok {
buildApplication(ctx, impl)
} else if impl, ok := implements(t, ctx.Dogma.AggregateMessageHandler); ok {
buildAggregate(ctx, impl)
} else if impl, ok := implements(t, ctx.Dogma.ProcessMessageHandler); ok {
buildProcess(ctx, impl)
} else if impl, ok := implements(t, ctx.Dogma.IntegrationMessageHandler); ok {
buildIntegration(ctx, impl)
} else if impl, ok := implements(t, ctx.Dogma.ProjectionMessageHandler); ok {
buildProjection(ctx, impl)
}
}
}
}
// implements returns true if t implements i.
//
// It returns the actual type that implements the interface, either t or *t.
func implements(t types.Type, i *types.Interface) (types.Type, bool) {
// TODO: skip type aliases
// If t is itself an interface, then it cannot implement i.
if _, ok := t.Underlying().(*types.Interface); ok {
return nil, false
}
// The sequence of the if-blocks below is important as a type
// implements an interface only if the methods in the interface's
// method set have non-pointer receivers. Hence the implementation
// check for the "raw" (non-pointer) type is made first.
//
// A pointer to the type, on the other hand, implements the
// interface regardless of whether pointer receivers are used or
// not.
if types.Implements(t, i) {
return t, true
}
return t, types.Implements(t, i)
}
func buildEntity(ctx *Context, t types.Type) Entity {
fn := ctx.SSAProgram.LookupMethod(t, ctx.Package.Types, "Configure")
if fn == nil {
panic(fmt.Sprintf("could not find Configure() method for %s", t))
}
e := Entity{
Type: t,
ConfigureMethod: ConfigureMethod{
Declaration: fn.Syntax().(*ast.FuncDecl),
Implementation: fn,
Configurer: fn.Params[1],
},
}
ctx.Entities = append(ctx.Entities, e)
return e
}
func buildApplication(ctx *Context, t types.Type) {
ctx.Applications = append(
ctx.Applications,
Application{
Entity: buildEntity(ctx, t),
},
)
}
func buildHandler(ctx *Context, t types.Type) Handler {
h := Handler{
Entity: buildEntity(ctx, t),
}
ctx.Handlers = append(ctx.Handlers, h)
return h
}
func buildAggregate(ctx *Context, t types.Type) {
ctx.Aggregates = append(
ctx.Aggregates,
Aggregate{
Handler: buildHandler(ctx, t),
},
)
}
func buildProcess(ctx *Context, t types.Type) {
ctx.Processes = append(
ctx.Processes,
Process{
Handler: buildHandler(ctx, t),
},
)
}
func buildIntegration(ctx *Context, t types.Type) {
ctx.Integrations = append(
ctx.Integrations,
Integration{
Handler: buildHandler(ctx, t),
},
)
}
func buildProjection(ctx *Context, t types.Type) {
ctx.Projections = append(
ctx.Projections,
Projection{
Handler: buildHandler(ctx, t),
},
)
}