Skip to content

Commit 4fbbd19

Browse files
committed
Add NetConn() net.Conn support for cast
1 parent 875ad36 commit 4fbbd19

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

common/upstream.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
package common
22

3+
import "net"
4+
35
type WithUpstream interface {
46
Upstream() any
57
}
68

9+
type stdWithUpstreamNetConn interface {
10+
NetConn() net.Conn
11+
}
12+
713
func Cast[T any](obj any) (T, bool) {
814
if c, ok := obj.(T); ok {
915
return c, true
1016
}
1117
if u, ok := obj.(WithUpstream); ok {
1218
return Cast[T](u.Upstream())
1319
}
20+
if u, ok := obj.(stdWithUpstreamNetConn); ok {
21+
return Cast[T](u.NetConn())
22+
}
1423
return DefaultValue[T](), false
1524
}
1625

@@ -27,5 +36,8 @@ func Top(obj any) any {
2736
if u, ok := obj.(WithUpstream); ok {
2837
return Top(u.Upstream())
2938
}
39+
if u, ok := obj.(stdWithUpstreamNetConn); ok {
40+
return Top(u.NetConn())
41+
}
3042
return obj
3143
}

0 commit comments

Comments
 (0)