Skip to content

Commit 7613216

Browse files
blgmonsi
authored andcommitted
chore: replace interface{} with any
- See discussion in: onsi/ginkgo#1203 - Updating Gomega for consistency with Ginkgo
1 parent 9fe5259 commit 7613216

File tree

106 files changed

+712
-704
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+712
-704
lines changed

docs/index.md

+59-59
Large diffs are not rendered by default.

format/format.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ If the CustomFormatter does not want to handle the object it should return ("",
7373
7474
Strings returned by CustomFormatters are not truncated
7575
*/
76-
type CustomFormatter func(value interface{}) (string, bool)
76+
type CustomFormatter func(value any) (string, bool)
7777
type CustomFormatterKey uint
7878

7979
var customFormatterKey CustomFormatterKey = 1
@@ -125,7 +125,7 @@ If expected is omitted, then the message looks like:
125125
<pretty printed actual>
126126
<message>
127127
*/
128-
func Message(actual interface{}, message string, expected ...interface{}) string {
128+
func Message(actual any, message string, expected ...any) string {
129129
if len(expected) == 0 {
130130
return fmt.Sprintf("Expected\n%s\n%s", Object(actual, 1), message)
131131
}
@@ -255,7 +255,7 @@ recursing into the object.
255255
256256
Set PrintContextObjects to true to print the content of objects implementing context.Context
257257
*/
258-
func Object(object interface{}, indentation uint) string {
258+
func Object(object any, indentation uint) string {
259259
indent := strings.Repeat(Indent, int(indentation))
260260
value := reflect.ValueOf(object)
261261
commonRepresentation := ""
@@ -392,7 +392,7 @@ func formatValue(value reflect.Value, indentation uint) string {
392392
}
393393
}
394394

395-
func formatString(object interface{}, indentation uint) string {
395+
func formatString(object any, indentation uint) string {
396396
if indentation == 1 {
397397
s := fmt.Sprintf("%s", object)
398398
components := strings.Split(s, "\n")

format/format_test.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ type SecretiveStruct struct {
6262
byteArrValue [3]byte
6363
mapValue map[string]int
6464
structValue AStruct
65-
interfaceValue interface{}
65+
interfaceValue any
6666
}
6767

6868
type CustomFormatted struct {
@@ -84,7 +84,7 @@ func (c *CustomError) Error() string {
8484
return c.Details
8585
}
8686

87-
func customFormatter(obj interface{}) (string, bool) {
87+
func customFormatter(obj any) (string, bool) {
8888
cf, ok := obj.(CustomFormatted)
8989
if !ok {
9090
return "", false
@@ -132,14 +132,14 @@ func (g gomegaStringerMultiline) GomegaString() string {
132132
}
133133

134134
var _ = Describe("Format", func() {
135-
match := func(typeRepresentation string, valueRepresentation string, args ...interface{}) types.GomegaMatcher {
135+
match := func(typeRepresentation string, valueRepresentation string, args ...any) types.GomegaMatcher {
136136
if len(args) > 0 {
137137
valueRepresentation = fmt.Sprintf(valueRepresentation, args...)
138138
}
139139
return Equal(fmt.Sprintf("%s<%s>: %s", Indent, typeRepresentation, valueRepresentation))
140140
}
141141

142-
matchRegexp := func(typeRepresentation string, valueRepresentation string, args ...interface{}) types.GomegaMatcher {
142+
matchRegexp := func(typeRepresentation string, valueRepresentation string, args ...any) types.GomegaMatcher {
143143
if len(args) > 0 {
144144
valueRepresentation = fmt.Sprintf(valueRepresentation, args...)
145145
}
@@ -599,18 +599,18 @@ var _ = Describe("Format", func() {
599599
})
600600
})
601601

602-
Describe("formatting nested interface{} types", func() {
602+
Describe("formatting nested any types", func() {
603603
It("should print out the types of the container and value", func() {
604-
Expect(Object([]interface{}{"foo"}, 1)).
604+
Expect(Object([]any{"foo"}, 1)).
605605
To(match("[]interface {} | len:1, cap:1", `[<string>"foo"]`))
606606

607-
Expect(Object(map[string]interface{}{"foo": true}, 1)).
607+
Expect(Object(map[string]any{"foo": true}, 1)).
608608
To(match("map[string]interface {} | len:1", `{"foo": <bool>true}`))
609609

610-
Expect(Object(struct{ A interface{} }{A: 1}, 1)).
610+
Expect(Object(struct{ A any }{A: 1}, 1)).
611611
To(match("struct { A interface {} }", "{A: <int>1}"))
612612

613-
v := struct{ A interface{} }{A: struct{ B string }{B: "foo"}}
613+
v := struct{ A any }{A: struct{ B string }{B: "foo"}}
614614
Expect(Object(v, 1)).To(match(`struct { A interface {} }`, `{
615615
A: <struct { B string }>{B: "foo"},
616616
}`))
@@ -694,7 +694,7 @@ var _ = Describe("Format", func() {
694694

695695
Describe("Handling interfaces", func() {
696696
It("should unpack the interface", func() {
697-
outerHash := map[string]interface{}{}
697+
outerHash := map[string]any{}
698698
innerHash := map[string]int{}
699699

700700
innerHash["inner"] = 3
@@ -708,19 +708,19 @@ var _ = Describe("Format", func() {
708708

709709
Describe("Handling recursive things", func() {
710710
It("should not go crazy...", func() {
711-
m := map[string]interface{}{}
711+
m := map[string]any{}
712712
m["integer"] = 2
713713
m["map"] = m
714714
Expect(Object(m, 1)).Should(ContainSubstring("..."))
715715
})
716716

717717
It("really should not go crazy...", func() {
718718
type complexKey struct {
719-
Value map[interface{}]int
719+
Value map[any]int
720720
}
721721

722722
complexObject := complexKey{}
723-
complexObject.Value = make(map[interface{}]int)
723+
complexObject.Value = make(map[any]int)
724724

725725
complexObject.Value[&complexObject] = 2
726726
Expect(Object(complexObject, 1)).Should(ContainSubstring("..."))
@@ -784,7 +784,7 @@ var _ = Describe("Format", func() {
784784

785785
It("indents CustomFormatter output correctly", func() {
786786
cf := CustomFormatted{"hey\nbob", 17}
787-
DeferCleanup(UnregisterCustomFormatter, RegisterCustomFormatter(func(value interface{}) (string, bool) {
787+
DeferCleanup(UnregisterCustomFormatter, RegisterCustomFormatter(func(value any) (string, bool) {
788788
cf, ok := value.(CustomFormatted)
789789
if !ok {
790790
return "", false

gbytes/buffer.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ Subsequent matches against the buffer will only operate against data that appear
77
88
The read cursor is an opaque implementation detail that you cannot access. You should use the Say matcher to sift through the buffer. You can always
99
access the entire buffer's contents with Contents().
10-
1110
*/
1211
package gbytes
1312

@@ -29,7 +28,7 @@ type Buffer struct {
2928
contents []byte
3029
readCursor uint64
3130
lock *sync.Mutex
32-
detectCloser chan interface{}
31+
detectCloser chan any
3332
closed bool
3433
}
3534

@@ -167,19 +166,25 @@ You could do something like:
167166
168167
select {
169168
case <-buffer.Detect("You are not logged in"):
169+
170170
//log in
171+
171172
case <-buffer.Detect("Success"):
173+
172174
//carry on
175+
173176
case <-time.After(time.Second):
174-
//welp
175-
}
177+
178+
//welp
179+
}
180+
176181
buffer.CancelDetects()
177182
178183
You should always call CancelDetects after using Detect. This will close any channels that have not detected and clean up the goroutines that were spawned to support them.
179184
180185
Finally, you can pass detect a format string followed by variadic arguments. This will construct the regexp using fmt.Sprintf.
181186
*/
182-
func (b *Buffer) Detect(desired string, args ...interface{}) chan bool {
187+
func (b *Buffer) Detect(desired string, args ...any) chan bool {
183188
formattedRegexp := desired
184189
if len(args) > 0 {
185190
formattedRegexp = fmt.Sprintf(desired, args...)
@@ -190,7 +195,7 @@ func (b *Buffer) Detect(desired string, args ...interface{}) chan bool {
190195
defer b.lock.Unlock()
191196

192197
if b.detectCloser == nil {
193-
b.detectCloser = make(chan interface{})
198+
b.detectCloser = make(chan any)
194199
}
195200

196201
closer := b.detectCloser

gbytes/say_matcher.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/onsi/gomega/format"
1010
)
1111

12-
//Objects satisfying the BufferProvider can be used with the Say matcher.
12+
// Objects satisfying the BufferProvider can be used with the Say matcher.
1313
type BufferProvider interface {
1414
Buffer() *Buffer
1515
}
@@ -37,7 +37,7 @@ In such cases, Say simply operates on the *gbytes.Buffer returned by Buffer()
3737
3838
If the buffer is closed, the Say matcher will tell Eventually to abort.
3939
*/
40-
func Say(expected string, args ...interface{}) *sayMatcher {
40+
func Say(expected string, args ...any) *sayMatcher {
4141
if len(args) > 0 {
4242
expected = fmt.Sprintf(expected, args...)
4343
}
@@ -51,7 +51,7 @@ type sayMatcher struct {
5151
receivedSayings []byte
5252
}
5353

54-
func (m *sayMatcher) buffer(actual interface{}) (*Buffer, bool) {
54+
func (m *sayMatcher) buffer(actual any) (*Buffer, bool) {
5555
var buffer *Buffer
5656

5757
switch x := actual.(type) {
@@ -66,7 +66,7 @@ func (m *sayMatcher) buffer(actual interface{}) (*Buffer, bool) {
6666
return buffer, true
6767
}
6868

69-
func (m *sayMatcher) Match(actual interface{}) (success bool, err error) {
69+
func (m *sayMatcher) Match(actual any) (success bool, err error) {
7070
buffer, ok := m.buffer(actual)
7171
if !ok {
7272
return false, fmt.Errorf("Say must be passed a *gbytes.Buffer or BufferProvider. Got:\n%s", format.Object(actual, 1))
@@ -78,23 +78,23 @@ func (m *sayMatcher) Match(actual interface{}) (success bool, err error) {
7878
return didSay, nil
7979
}
8080

81-
func (m *sayMatcher) FailureMessage(actual interface{}) (message string) {
81+
func (m *sayMatcher) FailureMessage(actual any) (message string) {
8282
return fmt.Sprintf(
8383
"Got stuck at:\n%s\nWaiting for:\n%s",
8484
format.IndentString(string(m.receivedSayings), 1),
8585
format.IndentString(m.re.String(), 1),
8686
)
8787
}
8888

89-
func (m *sayMatcher) NegatedFailureMessage(actual interface{}) (message string) {
89+
func (m *sayMatcher) NegatedFailureMessage(actual any) (message string) {
9090
return fmt.Sprintf(
9191
"Saw:\n%s\nWhich matches the unexpected:\n%s",
9292
format.IndentString(string(m.receivedSayings), 1),
9393
format.IndentString(m.re.String(), 1),
9494
)
9595
}
9696

97-
func (m *sayMatcher) MatchMayChangeInTheFuture(actual interface{}) bool {
97+
func (m *sayMatcher) MatchMayChangeInTheFuture(actual any) bool {
9898
switch x := actual.(type) {
9999
case *Buffer:
100100
return !x.Closed()

gcustom/make_matcher.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/onsi/gomega/format"
1313
)
1414

15-
var interfaceType = reflect.TypeOf((*interface{})(nil)).Elem()
15+
var interfaceType = reflect.TypeOf((*any)(nil)).Elem()
1616
var errInterface = reflect.TypeOf((*error)(nil)).Elem()
1717

1818
var defaultTemplate = template.Must(ParseTemplate("{{if .Failure}}Custom matcher failed for:{{else}}Custom matcher succeeded (but was expected to fail) for:{{end}}\n{{.FormattedActual}}"))

gexec/exit_matcher.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type Exiter interface {
4343
ExitCode() int
4444
}
4545

46-
func (m *exitMatcher) Match(actual interface{}) (success bool, err error) {
46+
func (m *exitMatcher) Match(actual any) (success bool, err error) {
4747
exiter, ok := actual.(Exiter)
4848
if !ok {
4949
return false, fmt.Errorf("Exit must be passed a gexec.Exiter (Missing method ExitCode() int) Got:\n%s", format.Object(actual, 1))
@@ -61,14 +61,14 @@ func (m *exitMatcher) Match(actual interface{}) (success bool, err error) {
6161
return m.exitCode == m.actualExitCode, nil
6262
}
6363

64-
func (m *exitMatcher) FailureMessage(actual interface{}) (message string) {
64+
func (m *exitMatcher) FailureMessage(actual any) (message string) {
6565
if m.actualExitCode == -1 {
6666
return "Expected process to exit. It did not."
6767
}
6868
return format.Message(m.actualExitCode, "to match exit code:", m.exitCode)
6969
}
7070

71-
func (m *exitMatcher) NegatedFailureMessage(actual interface{}) (message string) {
71+
func (m *exitMatcher) NegatedFailureMessage(actual any) (message string) {
7272
if m.actualExitCode == -1 {
7373
return "you really shouldn't be able to see this!"
7474
} else {
@@ -79,7 +79,7 @@ func (m *exitMatcher) NegatedFailureMessage(actual interface{}) (message string)
7979
}
8080
}
8181

82-
func (m *exitMatcher) MatchMayChangeInTheFuture(actual interface{}) bool {
82+
func (m *exitMatcher) MatchMayChangeInTheFuture(actual any) bool {
8383
session, ok := actual.(*Session)
8484
if ok {
8585
return session.ExitCode() == -1

gexec/session.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ will wait for the command to exit then return the entirety of Out's contents.
140140
141141
Wait uses eventually under the hood and accepts the same timeout/polling intervals that eventually does.
142142
*/
143-
func (s *Session) Wait(timeout ...interface{}) *Session {
143+
func (s *Session) Wait(timeout ...any) *Session {
144144
EventuallyWithOffset(1, s, timeout...).Should(Exit())
145145
return s
146146
}
@@ -225,7 +225,7 @@ The timeout specified is applied to each process killed.
225225
226226
If any of the processes already exited, KillAndWait returns silently.
227227
*/
228-
func KillAndWait(timeout ...interface{}) {
228+
func KillAndWait(timeout ...any) {
229229
trackedSessionsMutex.Lock()
230230
defer trackedSessionsMutex.Unlock()
231231
for _, session := range trackedSessions {
@@ -240,7 +240,7 @@ The timeout specified is applied to each process killed.
240240
241241
If any of the processes already exited, TerminateAndWait returns silently.
242242
*/
243-
func TerminateAndWait(timeout ...interface{}) {
243+
func TerminateAndWait(timeout ...any) {
244244
trackedSessionsMutex.Lock()
245245
defer trackedSessionsMutex.Unlock()
246246
for _, session := range trackedSessions {

0 commit comments

Comments
 (0)