Skip to content

Commit

Permalink
Add NetConn() net.Conn support for cast
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Nov 8, 2023
1 parent 875ad36 commit 4fbbd19
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions common/upstream.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package common

import "net"

type WithUpstream interface {
Upstream() any
}

type stdWithUpstreamNetConn interface {
NetConn() net.Conn
}

func Cast[T any](obj any) (T, bool) {
if c, ok := obj.(T); ok {
return c, true
}
if u, ok := obj.(WithUpstream); ok {
return Cast[T](u.Upstream())
}
if u, ok := obj.(stdWithUpstreamNetConn); ok {
return Cast[T](u.NetConn())
}
return DefaultValue[T](), false
}

Expand All @@ -27,5 +36,8 @@ func Top(obj any) any {
if u, ok := obj.(WithUpstream); ok {
return Top(u.Upstream())
}
if u, ok := obj.(stdWithUpstreamNetConn); ok {
return Top(u.NetConn())
}
return obj
}

0 comments on commit 4fbbd19

Please sign in to comment.