-
Notifications
You must be signed in to change notification settings - Fork 2
/
interactor.go
296 lines (243 loc) · 6.38 KB
/
interactor.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package usecase
import (
"context"
"path"
"runtime"
"strings"
"unicode"
"unicode/utf8"
)
// Interactor orchestrates the flow of data to and from the entities,
// and direct those entities to use their enterprise
// wide business rules to achieve the goals of the use case.
type Interactor interface {
// Interact sets output port value with regards to input port value or fails.
Interact(ctx context.Context, input, output interface{}) error
}
// Interact makes use case interactor from function.
type Interact func(ctx context.Context, input, output interface{}) error
// Interact implements Interactor.
func (i Interact) Interact(ctx context.Context, input, output interface{}) error {
return i(ctx, input, output)
}
// HasInputPort declares input port.
type HasInputPort interface {
// InputPort returns sample of input value, e.g. new(MyInput).
InputPort() interface{}
}
// WithInput is an embeddable implementation of HasInputPort.
type WithInput struct {
Input interface{}
}
// InputPort implements HasInputPort.
func (wi WithInput) InputPort() interface{} {
return wi.Input
}
// HasOutputPort declares output port.
type HasOutputPort interface {
// OutputPort returns sample of output value, e.g. new(MyOutput).
OutputPort() interface{}
}
// WithOutput is an embeddable implementation of HasOutputPort.
type WithOutput struct {
Output interface{}
}
// OutputPort implements HasOutputPort.
func (wi WithOutput) OutputPort() interface{} {
return wi.Output
}
// HasTitle declares title.
type HasTitle interface {
Title() string
}
// HasName declares title.
type HasName interface {
Name() string
}
// HasDescription declares description.
type HasDescription interface {
Description() string
}
// HasTags declares tags of use cases group.
type HasTags interface {
Tags() []string
}
// HasExpectedErrors declares errors that are expected to cause use case failure.
type HasExpectedErrors interface {
ExpectedErrors() []error
}
// HasIsDeprecated declares status of deprecation.
type HasIsDeprecated interface {
IsDeprecated() bool
}
// Info exposes information about use case.
type Info struct {
name string
title string
description string
tags []string
expectedErrors []error
isDeprecated bool
}
var (
_ HasTags = Info{}
_ HasTitle = Info{}
_ HasName = Info{}
_ HasDescription = Info{}
_ HasIsDeprecated = Info{}
_ HasExpectedErrors = Info{}
)
// IsDeprecated implements HasIsDeprecated.
func (i Info) IsDeprecated() bool {
return i.isDeprecated
}
// SetIsDeprecated sets status of deprecation.
func (i *Info) SetIsDeprecated(isDeprecated bool) {
i.isDeprecated = isDeprecated
}
// ExpectedErrors implements HasExpectedErrors.
func (i Info) ExpectedErrors() []error {
return i.expectedErrors
}
// SetExpectedErrors sets errors that are expected to cause use case failure.
func (i *Info) SetExpectedErrors(expectedErrors ...error) {
i.expectedErrors = expectedErrors
}
// Tags implements HasTag.
func (i Info) Tags() []string {
return i.tags
}
// SetTags sets tags of use cases group.
func (i *Info) SetTags(tags ...string) {
i.tags = tags
}
// Description implements HasDescription.
func (i Info) Description() string {
return i.description
}
// SetDescription sets use case description.
func (i *Info) SetDescription(description string) {
i.description = description
}
// Title implements HasTitle.
func (i Info) Title() string {
return i.title
}
// SetTitle sets use case title.
func (i *Info) SetTitle(title string) {
i.title = title
}
// Name implements HasName.
func (i Info) Name() string {
return i.name
}
// SetName sets use case title.
func (i *Info) SetName(name string) {
i.name = name
}
// IOInteractor is an interactor with input and output.
type IOInteractor struct {
Interactor
Info
WithInput
WithOutput
}
// NewIOI creates use case interactor with input, output and interact action function.
//
// It pre-fills name and title with caller function.
func NewIOI(input, output interface{}, interact Interact, options ...func(i *IOInteractor)) IOInteractor {
u := IOInteractor{}
u.Input = input
u.Output = output
u.Interactor = interact
u.name, u.title = callerFunc()
u.name = filterName(u.name)
for _, o := range options {
o(&u)
}
return u
}
var titleReplacer = strings.NewReplacer(
"(", "",
".", "",
"*", "",
")", "",
)
func filterName(name string) string {
name = strings.TrimPrefix(name, "internal/")
name = strings.TrimPrefix(name, "usecase.")
name = strings.TrimPrefix(name, "usecase/")
name = strings.TrimPrefix(name, "./main.")
return name
}
// callerFunc returns trimmed path and name of parent function.
func callerFunc() (string, string) {
skipFrames := 2
pc, _, _, ok := runtime.Caller(skipFrames)
if !ok {
return "", ""
}
f := runtime.FuncForPC(pc)
pathName := path.Base(path.Dir(f.Name())) + "/" + path.Base(f.Name())
title := path.Base(f.Name())
parts := strings.SplitN(title, ".", 2)
if len(parts) != 1 {
title = parts[len(parts)-1]
if len(title) == 0 {
return pathName, ""
}
// Uppercase first character of title.
r := []rune(title)
r[0] = unicode.ToUpper(r[0])
title = string(r)
title = titleReplacer.Replace(title)
title = splitCamelcase(title)
}
return pathName, title
}
// borrowed from https://pkg.go.dev/github.com/fatih/camelcase#Split to avoid external dependency.
func splitCamelcase(src string) string { //nolint:cyclop
// don't split invalid utf8
if !utf8.ValidString(src) {
return src
}
var (
entries []string
runes [][]rune
)
var class, lastClass int
// split into fields based on class of unicode character
for _, r := range src {
switch {
case unicode.IsLower(r):
class = 1
case unicode.IsUpper(r):
class = 2
case unicode.IsDigit(r):
class = 3
default:
class = 4
}
if class == lastClass && runes != nil {
runes[len(runes)-1] = append(runes[len(runes)-1], r)
} else {
runes = append(runes, []rune{r})
}
lastClass = class
}
// handle upper case -> lower case sequences, e.g.
// "PDFL", "oader" -> "PDF", "Loader"
for i := 0; i < len(runes)-1; i++ {
if unicode.IsUpper(runes[i][0]) && unicode.IsLower(runes[i+1][0]) {
runes[i+1] = append([]rune{runes[i][len(runes[i])-1]}, runes[i+1]...)
runes[i] = runes[i][:len(runes[i])-1]
}
}
// construct []string from results
for _, s := range runes {
if len(s) > 0 {
entries = append(entries, string(s))
}
}
return strings.Join(entries, " ")
}