Parcourir la source

Replace 'ConnectionTarget' enum with a struct (#861)

Motivation:

Public enums are bad for API evolution.

Modifications:

Turn the 'ConnectionTarget' enum into a struct.

Result:

API is less prone to broken in a major way.
George Barnett il y a 5 ans
Parent
commit
823b0b7895
2 fichiers modifiés avec 26 ajouts et 7 suppressions
  1. 25 6
      Sources/GRPC/ClientConnection.swift
  2. 1 1
      Sources/GRPC/Server.swift

+ 25 - 6
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<Channel> {
-    switch target {
+    switch target.wrapped {
     case .hostAndPort(let host, let port):
       return self.connect(host: host, port: port)
 

+ 1 - 1
Sources/GRPC/Server.swift

@@ -262,7 +262,7 @@ fileprivate extension Channel {
 
 fileprivate extension ServerBootstrapProtocol {
   func bind(to target: BindTarget) -> EventLoopFuture<Channel> {
-    switch target {
+    switch target.wrapped {
     case .hostAndPort(let host, let port):
       return self.bind(host: host, port: port)