Browse Source

Use SwiftProtobufNamer from SwiftProtobufPluginLibrary to name messages.

Tim Burks 8 years ago
parent
commit
464e26791a
1 changed files with 46 additions and 62 deletions
  1. 46 62
      Plugin/Sources/protoc-gen-swiftgrpc/filters.swift

+ 46 - 62
Plugin/Sources/protoc-gen-swiftgrpc/filters.swift

@@ -37,92 +37,76 @@ extension String {
   }
 }
 
+// error-generating helpers
+
+func invalidArgumentCount(filter:String, expected:Int) -> TemplateSyntaxError {
+  return TemplateSyntaxError("\(filter): expects \(expected) arguments")
+}
+
+func invalidArgument(filter:String, value:Any?) -> TemplateSyntaxError {
+  return TemplateSyntaxError("\(filter): invalid argument \(String(describing:value))")
+}
+
+func invalidArgumentType(filter: String, required: String, received: Any?) -> TemplateSyntaxError {
+  return TemplateSyntaxError("\(filter): invalid argument type: required \(required) received \(String(describing:received))")
+}
+
 // functions for use in templates
 
 // Transform .some.package_name.FooBarRequest -> Some_PackageName_FooBarRequest
 func protoMessageName(_ descriptor :SwiftProtobufPluginLibrary.Descriptor) -> String {
-  let name = descriptor.fullName 
-
-  var parts : [String] = []
-  for dotComponent in name.components(separatedBy:".") {
-    var part = ""
-    if dotComponent == "" {
-      continue
-    }
-    for underscoreComponent in dotComponent.components(separatedBy:"_") {
-      part.append(underscoreComponent.uppercasedFirst)
-    }
-    parts.append(part)
-  }
-
-  return parts.joined(separator:"_")
+  return namer.fullName(message:descriptor)
 }
 
-func pathName(_ arguments: [Any?]) throws -> String {
+func pathName(filter:String, arguments: [Any?]) throws -> String {
   if arguments.count != 3 {
-    throw TemplateSyntaxError("path expects 3 arguments")
+    throw invalidArgumentCount(filter:"path", expected:3)
   }
   guard let protoFile = arguments[0] as? SwiftProtobufPluginLibrary.FileDescriptor
     else {
-      throw TemplateSyntaxError("tag must be called with a " +
-        "FileDescriptor" +
-        " argument, received \(String(describing:arguments[0]))")
+      throw invalidArgumentType(filter:"path", required:"FileDescriptor", received:arguments[0])
   }
   guard let service = arguments[1] as? SwiftProtobufPluginLibrary.ServiceDescriptor
     else {
-      throw TemplateSyntaxError("tag must be called with a " +
-        "ServiceDescriptor" +
-        " argument, received \(String(describing:arguments[1]))")
+      throw invalidArgumentType(filter:"path", required:"ServiceDescriptor", received:arguments[1])
   }
   guard let method = arguments[2] as? SwiftProtobufPluginLibrary.MethodDescriptor
     else {
-      throw TemplateSyntaxError("tag must be called with a " +
-        "MethodDescriptor" +
-        " argument, received \(String(describing:arguments[2]))")
+      throw invalidArgumentType(filter:"path", required:"MethodDescriptor", received:arguments[2])
   }
   return "/" + protoFile.package + "." + service.name + "/" + method.name
 }
 
-func packageServiceMethodName(_ arguments: [Any?]) throws -> String {
+func packageServiceMethodName(filter:String, arguments: [Any?]) throws -> String {
   if arguments.count != 3 {
-    throw TemplateSyntaxError("tag expects 3 arguments")
+    throw invalidArgumentCount(filter:"packageServiceMethodName", expected:3)
   }
   guard let protoFile = arguments[0] as? SwiftProtobufPluginLibrary.FileDescriptor
     else {
-      throw TemplateSyntaxError("tag must be called with a " +
-        "FileDescriptor" +
-        " argument, received \(String(describing:arguments[0]))")
+      throw invalidArgumentType(filter:filter, required:"FileDescriptor", received:arguments[0])
   }
   guard let service = arguments[1] as? SwiftProtobufPluginLibrary.ServiceDescriptor
     else {
-      throw TemplateSyntaxError("tag must be called with a " +
-        "ServiceDescriptor" +
-        " argument, received \(String(describing:arguments[1]))")
+      throw invalidArgumentType(filter:filter, required:"ServiceDescriptor", received:arguments[0])
   }
   guard let method = arguments[2] as? SwiftProtobufPluginLibrary.MethodDescriptor
     else {
-      throw TemplateSyntaxError("tag must be called with a " +
-        "MethodDescriptor" +
-        " argument, received \(String(describing:arguments[2]))")
+      throw invalidArgumentType(filter:filter, required:"MethodDescriptor", received:arguments[0])
   }
   return protoFile.package.capitalized.undotted + "_" + service.name + method.name
 }
 
-func packageServiceName(_ arguments: [Any?]) throws -> String {
+func packageServiceName(filter:String, arguments: [Any?]) throws -> String {
   if arguments.count != 2 {
-    throw TemplateSyntaxError("tag expects 2 arguments")
+    throw invalidArgumentCount(filter:"packageServiceName", expected:2)
   }
   guard let protoFile = arguments[0] as? SwiftProtobufPluginLibrary.FileDescriptor
     else {
-      throw TemplateSyntaxError("tag must be called with a " +
-        "FileDescriptor" +
-        " argument, received \(String(describing:arguments[0]))")
+      throw invalidArgumentType(filter:filter, required:"FileDescriptor", received:arguments[0])
   }
   guard let service = arguments[1] as? SwiftProtobufPluginLibrary.ServiceDescriptor
     else {
-      throw TemplateSyntaxError("tag must be called with a " +
-        "ServiceDescriptor" +
-        " argument, received \(String(describing:arguments[1]))")
+      throw invalidArgumentType(filter:filter, required:"ServiceDescriptor", received:arguments[0])
   }
   return protoFile.package.capitalized.undotted + "_" + service.name
 }
@@ -133,79 +117,79 @@ class GRPCFilterExtension : Extension {
     // initialize template engine and add custom filters
     let ext = self
     ext.registerFilter("call") { (value: Any?, arguments: [Any?]) in
-      return try packageServiceMethodName(arguments) + "Call"
+      return try packageServiceMethodName(filter:"call", arguments:arguments) + "Call"
     }
     ext.registerFilter("session") { (value: Any?, arguments: [Any?]) in
-      return try packageServiceMethodName(arguments) + "Session"
+      return try packageServiceMethodName(filter:"session", arguments:arguments) + "Session"
     }
     ext.registerFilter("path") { (value: Any?, arguments: [Any?]) in
-      return try pathName(arguments)
+      return try pathName(filter:"path", arguments:arguments)
     }
     ext.registerFilter("provider") { (value: Any?, arguments: [Any?]) in
-      return try packageServiceName(arguments) + "Provider"
+      return try packageServiceName(filter:"provider", arguments:arguments) + "Provider"
     }
     ext.registerFilter("clienterror") { (value: Any?, arguments: [Any?]) in
-      return try packageServiceName(arguments) + "ClientError"
+      return try packageServiceName(filter:"clienterror", arguments:arguments) + "ClientError"
     }
     ext.registerFilter("serviceclass") { (value: Any?, arguments: [Any?]) in
-      return try packageServiceName(arguments) + "Service"
+      return try packageServiceName(filter:"serviceclass", arguments:arguments) + "Service"
     }
     ext.registerFilter("servererror") { (value: Any?, arguments: [Any?]) in
-      return try packageServiceName(arguments) + "ServerError"
+      return try packageServiceName(filter:"servererror", arguments:arguments) + "ServerError"
     }
     ext.registerFilter("server") { (value: Any?, arguments: [Any?]) in
-      return try packageServiceName(arguments) + "Server"
+      return try packageServiceName(filter:"server", arguments:arguments) + "Server"
     }
     ext.registerFilter("service") { (value: Any?, arguments: [Any?]) in
-      return try packageServiceName(arguments)
+      return try packageServiceName(filter:"server", arguments:arguments)
     }
     ext.registerFilter("input") { (value: Any?) in
       if let value = value as? SwiftProtobufPluginLibrary.MethodDescriptor {
         return protoMessageName(value.inputType)
       }
-      throw TemplateSyntaxError("message: invalid argument \(String(describing:value))")
+      throw invalidArgumentType(filter:"input", required:"MethodDescriptor", received:value)
     }
     ext.registerFilter("output") { (value: Any?) in
       if let value = value as? SwiftProtobufPluginLibrary.MethodDescriptor {
         return protoMessageName(value.outputType)
       }
-      throw TemplateSyntaxError("message: invalid argument \(String(describing:value))")
+      throw invalidArgumentType(filter:"output", required:"MethodDescriptor", received:value)
     }
     ext.registerFilter("fileDescriptorName") { (value: Any?) in
       if let value = value as? SwiftProtobufPluginLibrary.FileDescriptor {
         return value.name
       }
-      throw TemplateSyntaxError("message: invalid argument \(String(describing:value))")
+      throw invalidArgumentType(filter:"fileDescriptorName", required:"FileDescriptor", received:value)
     }
     ext.registerFilter("methodDescriptorName") { (value: Any?) in
       if let value = value as? SwiftProtobufPluginLibrary.MethodDescriptor {
         return value.name
       }
-      throw TemplateSyntaxError("message: invalid argument \(String(describing:value))")
+      throw invalidArgumentType(filter:"methodDescriptorName", required:"MethodDescriptor", received:value)
     }
     ext.registerFilter("methodIsUnary") { (value: Any?) in
       if let value = value as? SwiftProtobufPluginLibrary.MethodDescriptor {
         return !value.proto.clientStreaming && !value.proto.serverStreaming
       }
-      throw TemplateSyntaxError("message: invalid argument \(String(describing:value))")
+      throw invalidArgumentType(filter:"methodIsUnary", required:"MethodDescriptor", received:value)
     }
     ext.registerFilter("methodIsServerStreaming") { (value: Any?) in
       if let value = value as? SwiftProtobufPluginLibrary.MethodDescriptor {
         return !value.proto.clientStreaming && value.proto.serverStreaming
       }
-      throw TemplateSyntaxError("message: invalid argument \(String(describing:value))")
+      throw invalidArgumentType(filter:"methodIsServerStreaming", required:"MethodDescriptor", received:value)
     }
     ext.registerFilter("methodIsClientStreaming") { (value: Any?) in
       if let value = value as? SwiftProtobufPluginLibrary.MethodDescriptor {
         return value.proto.clientStreaming && !value.proto.serverStreaming
       }
-      throw TemplateSyntaxError("message: invalid argument \(String(describing:value))")
+      throw invalidArgumentType(filter:"methodIsClientStreaming", required:"MethodDescriptor", received:value)
     }
     ext.registerFilter("methodIsBidiStreaming") { (value: Any?) in
       if let value = value as? SwiftProtobufPluginLibrary.MethodDescriptor {
         return value.proto.clientStreaming && value.proto.serverStreaming
       }
-      throw TemplateSyntaxError("message: invalid argument \(String(describing:value))")
+      throw invalidArgumentType(filter:"methodIsBidiStreaming", required:"MethodDescriptor", received:value)
     }
   }
 }