generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
176 lines (140 loc) · 3.23 KB
/
string.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
package configkit
import (
"context"
"io"
"sort"
"strings"
"github.com/dogmatiq/configkit/message"
"github.com/dogmatiq/iago/indent"
"github.com/dogmatiq/iago/must"
)
// ToString returns a human-readable string representation of the given entity.
func ToString(e Entity) string {
var b strings.Builder
if err := e.AcceptVisitor(
context.Background(),
&stringer{w: &b},
); err != nil {
panic(err)
}
return b.String()
}
// stringer is a Visitor that builds string representations of entities.
type stringer struct {
w io.Writer
}
func (s *stringer) VisitApplication(ctx context.Context, cfg Application) error {
id := cfg.Identity()
must.Fprintf(
s.w,
"application %s (%s) %s\n",
id.Name,
id.Key,
cfg.TypeName(),
)
v := &stringer{
w: indent.NewIndenter(s.w, nil),
}
for _, h := range sortHandlers(cfg.Handlers()) {
must.WriteByte(s.w, '\n')
must.WriteString(v.w, "- ")
if err := h.AcceptVisitor(ctx, v); err != nil {
return err
}
}
return nil
}
func (s *stringer) visitHandler(cfg Handler) error {
id := cfg.Identity()
var flags []string
if cfg.IsDisabled() {
flags = append(flags, "disabled")
}
flagString := ""
if len(flags) > 0 {
flagString = " [" + strings.Join(flags, ", ") + "]"
}
must.Fprintf(
s.w,
"%s %s (%s) %s%s\n",
cfg.HandlerType(),
id.Name,
id.Key,
cfg.TypeName(),
flagString,
)
for _, p := range sortNameRoles(cfg.MessageNames().Consumed) {
if p.Role == message.TimeoutRole {
break
}
must.Fprintf(
s.w,
" handles %s%s\n",
p.Name,
p.Role.Marker(),
)
}
for _, p := range sortNameRoles(cfg.MessageNames().Produced) {
verb := ""
switch p.Role {
case message.CommandRole:
verb = "executes"
case message.EventRole:
verb = "records"
case message.TimeoutRole:
verb = "schedules"
}
must.Fprintf(
s.w,
" %s %s%s\n",
verb,
p.Name,
p.Role.Marker(),
)
}
return nil
}
func (s *stringer) VisitAggregate(_ context.Context, cfg Aggregate) error {
return s.visitHandler(cfg)
}
func (s *stringer) VisitProcess(_ context.Context, cfg Process) error {
return s.visitHandler(cfg)
}
func (s *stringer) VisitIntegration(_ context.Context, cfg Integration) error {
return s.visitHandler(cfg)
}
func (s *stringer) VisitProjection(_ context.Context, cfg Projection) error {
return s.visitHandler(cfg)
}
// sortHandlers returns a set of handlers sorted by their name.
func sortHandlers(handlers HandlerSet) []Handler {
sorted := make([]Handler, 0, len(handlers))
for _, h := range handlers {
sorted = append(sorted, h)
}
sort.Slice(sorted, func(i, j int) bool {
return sorted[i].Identity().Name < sorted[j].Identity().Name
})
return sorted
}
type pair struct {
Name message.Name
Role message.Role
}
// sortNameRoles returns a list of name/role pairs, sorted by name.
// Timeout messages are always sorted towards the end.
func sortNameRoles(names message.NameRoles) []pair {
sorted := make([]pair, 0, len(names))
for n, r := range names {
sorted = append(sorted, pair{n, r})
}
sort.Slice(sorted, func(i, j int) bool {
pi := sorted[i]
pj := sorted[j]
if pi.Role == message.TimeoutRole && pj.Role != message.TimeoutRole {
return false
}
return pi.Name.String() < pj.Name.String()
})
return sorted
}