This repository was archived by the owner on Dec 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstuber.go
More file actions
180 lines (158 loc) · 5.01 KB
/
stuber.go
File metadata and controls
180 lines (158 loc) · 5.01 KB
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
package stuber
import (
"github.com/bavix/features"
"github.com/google/uuid"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
// MethodTitle is a feature flag for using title casing in the method field
// of a Query struct.
const MethodTitle features.Flag = iota
// Budgerigar is the main struct for the stuber package. It contains a
// searcher and toggles.
type Budgerigar struct {
searcher *searcher
toggles features.Toggles
}
// NewBudgerigar creates a new Budgerigar with the given features.Toggles.
//
// Parameters:
// - toggles: The features.Toggles to use.
//
// Returns:
// - A new Budgerigar.
func NewBudgerigar(toggles features.Toggles) *Budgerigar {
return &Budgerigar{
searcher: newSearcher(),
toggles: toggles,
}
}
// PutMany inserts the given Stub values into the Budgerigar. If a Stub value
// does not have a key, a new UUID is generated for its key.
//
// Parameters:
// - values: The Stub values to insert.
//
// Returns:
// - []uuid.UUID: The keys of the inserted Stub values.
func (b *Budgerigar) PutMany(values ...*Stub) []uuid.UUID {
for _, value := range values {
if value.Key() == uuid.Nil {
value.ID = uuid.New()
}
}
return b.searcher.upsert(values...)
}
// UpdateMany updates the given Stub values in the Budgerigar. Only Stub values
// with non-nil keys are updated.
//
// Parameters:
// - values: The Stub values to update.
//
// Returns:
// - []uuid.UUID: The keys of the updated values.
func (b *Budgerigar) UpdateMany(values ...*Stub) []uuid.UUID {
updates := make([]*Stub, 0, len(values))
for _, value := range values {
if value.Key() != uuid.Nil {
updates = append(updates, value)
}
}
return b.searcher.upsert(updates...)
}
// DeleteByID deletes the Stub values with the given IDs from the Budgerigar's searcher.
//
// Parameters:
// - ids: The UUIDs of the Stub values to delete.
//
// Returns:
// - int: The number of Stub values that were successfully deleted.
func (b *Budgerigar) DeleteByID(ids ...uuid.UUID) int {
return b.searcher.del(ids...)
}
// FindByID retrieves the Stub value associated with the given ID from the Budgerigar's searcher.
//
// Parameters:
// - id: The UUID of the Stub value to retrieve.
//
// Returns:
// - *Stub: The Stub value associated with the given ID, or nil if not found.
func (b *Budgerigar) FindByID(id uuid.UUID) *Stub {
return b.searcher.findByID(id)
}
// FindByQuery retrieves the Stub value associated with the given Query from the Budgerigar's searcher.
//
// Parameters:
// - query: The Query used to search for a Stub value.
//
// Returns:
// - *Result: The Result containing the found Stub value (if any), or nil.
// - error: An error if the search fails.
func (b *Budgerigar) FindByQuery(query Query) (*Result, error) {
if b.toggles.Has(MethodTitle) {
query.Method = cases.
Title(language.English, cases.NoLower).
String(query.Method)
}
return b.searcher.find(query)
}
// FindByQueryV2 retrieves the Stub value associated with the given QueryV2 from the Budgerigar's searcher.
//
// Parameters:
// - query: The QueryV2 used to search for a Stub value.
//
// Returns:
// - *Result: The Result containing the found Stub value (if any), or nil.
// - error: An error if the search fails.
func (b *Budgerigar) FindByQueryV2(query QueryV2) (*Result, error) {
if b.toggles.Has(MethodTitle) {
query.Method = cases.Title(language.English).String(query.Method)
}
return b.searcher.findV2(query)
}
// FindByQueryBidi retrieves a BidiResult for bidirectional streaming with the given QueryBidi.
// For bidirectional streaming, each message is treated as a separate unary request.
// The server can respond with multiple messages for each request.
//
// Parameters:
// - query: The QueryBidi used to search for bidirectional streaming stubs.
//
// Returns:
// - *BidiResult: The BidiResult for finding matching stubs for each message.
// - error: An error if the search fails.
func (b *Budgerigar) FindByQueryBidi(query QueryBidi) (*BidiResult, error) {
if b.toggles.Has(MethodTitle) {
query.Method = cases.Title(language.English).String(query.Method)
}
return b.searcher.findBidi(query)
}
// FindBy retrieves all Stub values that match the given service and method
// from the Budgerigar's searcher, sorted by priority score in descending order.
func (b *Budgerigar) FindBy(service, method string) ([]*Stub, error) {
return b.searcher.findBy(service, method)
}
// All returns all Stub values from the Budgerigar's searcher.
//
// Returns:
// - []*Stub: All Stub values.
func (b *Budgerigar) All() []*Stub {
return b.searcher.all()
}
// Used returns all Stub values that have been used from the Budgerigar's searcher.
//
// Returns:
// - []*Stub: All used Stub values.
func (b *Budgerigar) Used() []*Stub {
return b.searcher.used()
}
// Unused returns all Stub values that have not been used from the Budgerigar's searcher.
//
// Returns:
// - []*Stub: All unused Stub values.
func (b *Budgerigar) Unused() []*Stub {
return b.searcher.unused()
}
// Clear clears all Stub values from the Budgerigar's searcher.
func (b *Budgerigar) Clear() {
b.searcher.clear()
}