generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.go
109 lines (88 loc) · 2.59 KB
/
application.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
package configkit
import (
"context"
"reflect"
"sync"
"github.com/dogmatiq/dogma"
)
// Application is an interface that represents the configuration of a Dogma
// application.
type Application interface {
Entity
// Handlers returns the handlers within this application.
Handlers() HandlerSet
}
// RichApplication is a specialization of Application that exposes information
// about the Go types used to implement the Dogma application.
type RichApplication interface {
RichEntity
// Handlers returns the handlers within this application.
Handlers() HandlerSet
// RichHandlers returns the handlers within this application.
RichHandlers() RichHandlerSet
// Application returns the underlying application.
Application() dogma.Application
}
// FromApplication returns the configuration for an application.
//
// It panics if the application is configured incorrectly. Use Recover() to
// convert configuration related panic values to errors.
func FromApplication(a dogma.Application) RichApplication {
cfg := &application{
entity: entity{
rt: reflect.TypeOf(a),
},
impl: a,
}
c := &applicationConfigurer{
entityConfigurer: entityConfigurer{
entity: &cfg.entity,
},
app: cfg,
}
a.Configure(c)
c.validate()
return cfg
}
// IsApplicationEqual compares two applications for equality.
//
// It returns true if both applications:
//
// 1. have the same identity
// 2. produce and consume the same messages, with the same roles
// 3. are implemented using the same Go types
// 4. contain equivalent handlers
//
// Point 3. refers to the type used to implement the dogma.Application interface
// (not the type used to implement the configkit.Application interface).
func IsApplicationEqual(a, b Application) bool {
return a.Identity() == b.Identity() &&
a.TypeName() == b.TypeName() &&
a.MessageNames().IsEqual(b.MessageNames()) &&
a.Handlers().IsEqual(b.Handlers())
}
// application is an implementation of RichApplication.
type application struct {
entity
handlers HandlerSet
richHandlers RichHandlerSet
foreignNames EntityMessageNames
foreignTypes EntityMessageTypes
impl dogma.Application
once sync.Once
}
func (a *application) AcceptVisitor(ctx context.Context, v Visitor) error {
return v.VisitApplication(ctx, a)
}
func (a *application) AcceptRichVisitor(ctx context.Context, v RichVisitor) error {
return v.VisitRichApplication(ctx, a)
}
func (a *application) Handlers() HandlerSet {
return a.handlers
}
func (a *application) RichHandlers() RichHandlerSet {
return a.richHandlers
}
func (a *application) Application() dogma.Application {
return a.impl
}