diff --git a/Sources/GRPC/ClientConnection.swift b/Sources/GRPC/ClientConnection.swift index 69cc55bf0..2a5427c1b 100644 --- a/Sources/GRPC/ClientConnection.swift +++ b/Sources/GRPC/ClientConnection.swift @@ -225,16 +225,35 @@ extension ClientConnection: GRPCChannel { // MARK: - Configuration structures /// A target to connect to. -public enum ConnectionTarget { +public struct ConnectionTarget { + internal enum Wrapped { + case hostAndPort(String, Int) + case unixDomainSocket(String) + case socketAddress(SocketAddress) + } + + internal var wrapped: Wrapped + private init(_ wrapped: Wrapped) { + self.wrapped = wrapped + } + /// The host and port. - case hostAndPort(String, Int) + public static func hostAndPort(_ host: String, _ port: Int) -> ConnectionTarget { + return ConnectionTarget(.hostAndPort(host, port)) + } + /// The path of a Unix domain socket. - case unixDomainSocket(String) + public static func unixDomainSocket(_ path: String) -> ConnectionTarget { + return ConnectionTarget(.unixDomainSocket(path)) + } + /// A NIO socket address. - case socketAddress(SocketAddress) + public static func socketAddress(_ address: SocketAddress) -> ConnectionTarget { + return ConnectionTarget(.socketAddress(address)) + } var host: String { - switch self { + switch self.wrapped { case .hostAndPort(let host, _): return host case .socketAddress(.v4(let address)): @@ -335,7 +354,7 @@ extension ClientBootstrapProtocol { /// /// - Parameter target: The target to connect to. func connect(to target: ConnectionTarget) -> EventLoopFuture { - switch target { + switch target.wrapped { case .hostAndPort(let host, let port): return self.connect(host: host, port: port) diff --git a/Sources/GRPC/Server.swift b/Sources/GRPC/Server.swift index 86b9965c9..9f3281a5e 100644 --- a/Sources/GRPC/Server.swift +++ b/Sources/GRPC/Server.swift @@ -262,7 +262,7 @@ fileprivate extension Channel { fileprivate extension ServerBootstrapProtocol { func bind(to target: BindTarget) -> EventLoopFuture { - switch target { + switch target.wrapped { case .hostAndPort(let host, let port): return self.bind(host: host, port: port)