Skip to content

Commit 464e267

Browse files
committed
Use SwiftProtobufNamer from SwiftProtobufPluginLibrary to name messages.
1 parent 19b1023 commit 464e267

File tree

1 file changed

+46
-62
lines changed

1 file changed

+46
-62
lines changed

Plugin/Sources/protoc-gen-swiftgrpc/filters.swift

Lines changed: 46 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -37,92 +37,76 @@ extension String {
3737
}
3838
}
3939

40+
// error-generating helpers
41+
42+
func invalidArgumentCount(filter:String, expected:Int) -> TemplateSyntaxError {
43+
return TemplateSyntaxError("\(filter): expects \(expected) arguments")
44+
}
45+
46+
func invalidArgument(filter:String, value:Any?) -> TemplateSyntaxError {
47+
return TemplateSyntaxError("\(filter): invalid argument \(String(describing:value))")
48+
}
49+
50+
func invalidArgumentType(filter: String, required: String, received: Any?) -> TemplateSyntaxError {
51+
return TemplateSyntaxError("\(filter): invalid argument type: required \(required) received \(String(describing:received))")
52+
}
53+
4054
// functions for use in templates
4155

4256
// Transform .some.package_name.FooBarRequest -> Some_PackageName_FooBarRequest
4357
func protoMessageName(_ descriptor :SwiftProtobufPluginLibrary.Descriptor) -> String {
44-
let name = descriptor.fullName
45-
46-
var parts : [String] = []
47-
for dotComponent in name.components(separatedBy:".") {
48-
var part = ""
49-
if dotComponent == "" {
50-
continue
51-
}
52-
for underscoreComponent in dotComponent.components(separatedBy:"_") {
53-
part.append(underscoreComponent.uppercasedFirst)
54-
}
55-
parts.append(part)
56-
}
57-
58-
return parts.joined(separator:"_")
58+
return namer.fullName(message:descriptor)
5959
}
6060

61-
func pathName(_ arguments: [Any?]) throws -> String {
61+
func pathName(filter:String, arguments: [Any?]) throws -> String {
6262
if arguments.count != 3 {
63-
throw TemplateSyntaxError("path expects 3 arguments")
63+
throw invalidArgumentCount(filter:"path", expected:3)
6464
}
6565
guard let protoFile = arguments[0] as? SwiftProtobufPluginLibrary.FileDescriptor
6666
else {
67-
throw TemplateSyntaxError("tag must be called with a " +
68-
"FileDescriptor" +
69-
" argument, received \(String(describing:arguments[0]))")
67+
throw invalidArgumentType(filter:"path", required:"FileDescriptor", received:arguments[0])
7068
}
7169
guard let service = arguments[1] as? SwiftProtobufPluginLibrary.ServiceDescriptor
7270
else {
73-
throw TemplateSyntaxError("tag must be called with a " +
74-
"ServiceDescriptor" +
75-
" argument, received \(String(describing:arguments[1]))")
71+
throw invalidArgumentType(filter:"path", required:"ServiceDescriptor", received:arguments[1])
7672
}
7773
guard let method = arguments[2] as? SwiftProtobufPluginLibrary.MethodDescriptor
7874
else {
79-
throw TemplateSyntaxError("tag must be called with a " +
80-
"MethodDescriptor" +
81-
" argument, received \(String(describing:arguments[2]))")
75+
throw invalidArgumentType(filter:"path", required:"MethodDescriptor", received:arguments[2])
8276
}
8377
return "/" + protoFile.package + "." + service.name + "/" + method.name
8478
}
8579

86-
func packageServiceMethodName(_ arguments: [Any?]) throws -> String {
80+
func packageServiceMethodName(filter:String, arguments: [Any?]) throws -> String {
8781
if arguments.count != 3 {
88-
throw TemplateSyntaxError("tag expects 3 arguments")
82+
throw invalidArgumentCount(filter:"packageServiceMethodName", expected:3)
8983
}
9084
guard let protoFile = arguments[0] as? SwiftProtobufPluginLibrary.FileDescriptor
9185
else {
92-
throw TemplateSyntaxError("tag must be called with a " +
93-
"FileDescriptor" +
94-
" argument, received \(String(describing:arguments[0]))")
86+
throw invalidArgumentType(filter:filter, required:"FileDescriptor", received:arguments[0])
9587
}
9688
guard let service = arguments[1] as? SwiftProtobufPluginLibrary.ServiceDescriptor
9789
else {
98-
throw TemplateSyntaxError("tag must be called with a " +
99-
"ServiceDescriptor" +
100-
" argument, received \(String(describing:arguments[1]))")
90+
throw invalidArgumentType(filter:filter, required:"ServiceDescriptor", received:arguments[0])
10191
}
10292
guard let method = arguments[2] as? SwiftProtobufPluginLibrary.MethodDescriptor
10393
else {
104-
throw TemplateSyntaxError("tag must be called with a " +
105-
"MethodDescriptor" +
106-
" argument, received \(String(describing:arguments[2]))")
94+
throw invalidArgumentType(filter:filter, required:"MethodDescriptor", received:arguments[0])
10795
}
10896
return protoFile.package.capitalized.undotted + "_" + service.name + method.name
10997
}
11098

111-
func packageServiceName(_ arguments: [Any?]) throws -> String {
99+
func packageServiceName(filter:String, arguments: [Any?]) throws -> String {
112100
if arguments.count != 2 {
113-
throw TemplateSyntaxError("tag expects 2 arguments")
101+
throw invalidArgumentCount(filter:"packageServiceName", expected:2)
114102
}
115103
guard let protoFile = arguments[0] as? SwiftProtobufPluginLibrary.FileDescriptor
116104
else {
117-
throw TemplateSyntaxError("tag must be called with a " +
118-
"FileDescriptor" +
119-
" argument, received \(String(describing:arguments[0]))")
105+
throw invalidArgumentType(filter:filter, required:"FileDescriptor", received:arguments[0])
120106
}
121107
guard let service = arguments[1] as? SwiftProtobufPluginLibrary.ServiceDescriptor
122108
else {
123-
throw TemplateSyntaxError("tag must be called with a " +
124-
"ServiceDescriptor" +
125-
" argument, received \(String(describing:arguments[1]))")
109+
throw invalidArgumentType(filter:filter, required:"ServiceDescriptor", received:arguments[0])
126110
}
127111
return protoFile.package.capitalized.undotted + "_" + service.name
128112
}
@@ -133,79 +117,79 @@ class GRPCFilterExtension : Extension {
133117
// initialize template engine and add custom filters
134118
let ext = self
135119
ext.registerFilter("call") { (value: Any?, arguments: [Any?]) in
136-
return try packageServiceMethodName(arguments) + "Call"
120+
return try packageServiceMethodName(filter:"call", arguments:arguments) + "Call"
137121
}
138122
ext.registerFilter("session") { (value: Any?, arguments: [Any?]) in
139-
return try packageServiceMethodName(arguments) + "Session"
123+
return try packageServiceMethodName(filter:"session", arguments:arguments) + "Session"
140124
}
141125
ext.registerFilter("path") { (value: Any?, arguments: [Any?]) in
142-
return try pathName(arguments)
126+
return try pathName(filter:"path", arguments:arguments)
143127
}
144128
ext.registerFilter("provider") { (value: Any?, arguments: [Any?]) in
145-
return try packageServiceName(arguments) + "Provider"
129+
return try packageServiceName(filter:"provider", arguments:arguments) + "Provider"
146130
}
147131
ext.registerFilter("clienterror") { (value: Any?, arguments: [Any?]) in
148-
return try packageServiceName(arguments) + "ClientError"
132+
return try packageServiceName(filter:"clienterror", arguments:arguments) + "ClientError"
149133
}
150134
ext.registerFilter("serviceclass") { (value: Any?, arguments: [Any?]) in
151-
return try packageServiceName(arguments) + "Service"
135+
return try packageServiceName(filter:"serviceclass", arguments:arguments) + "Service"
152136
}
153137
ext.registerFilter("servererror") { (value: Any?, arguments: [Any?]) in
154-
return try packageServiceName(arguments) + "ServerError"
138+
return try packageServiceName(filter:"servererror", arguments:arguments) + "ServerError"
155139
}
156140
ext.registerFilter("server") { (value: Any?, arguments: [Any?]) in
157-
return try packageServiceName(arguments) + "Server"
141+
return try packageServiceName(filter:"server", arguments:arguments) + "Server"
158142
}
159143
ext.registerFilter("service") { (value: Any?, arguments: [Any?]) in
160-
return try packageServiceName(arguments)
144+
return try packageServiceName(filter:"server", arguments:arguments)
161145
}
162146
ext.registerFilter("input") { (value: Any?) in
163147
if let value = value as? SwiftProtobufPluginLibrary.MethodDescriptor {
164148
return protoMessageName(value.inputType)
165149
}
166-
throw TemplateSyntaxError("message: invalid argument \(String(describing:value))")
150+
throw invalidArgumentType(filter:"input", required:"MethodDescriptor", received:value)
167151
}
168152
ext.registerFilter("output") { (value: Any?) in
169153
if let value = value as? SwiftProtobufPluginLibrary.MethodDescriptor {
170154
return protoMessageName(value.outputType)
171155
}
172-
throw TemplateSyntaxError("message: invalid argument \(String(describing:value))")
156+
throw invalidArgumentType(filter:"output", required:"MethodDescriptor", received:value)
173157
}
174158
ext.registerFilter("fileDescriptorName") { (value: Any?) in
175159
if let value = value as? SwiftProtobufPluginLibrary.FileDescriptor {
176160
return value.name
177161
}
178-
throw TemplateSyntaxError("message: invalid argument \(String(describing:value))")
162+
throw invalidArgumentType(filter:"fileDescriptorName", required:"FileDescriptor", received:value)
179163
}
180164
ext.registerFilter("methodDescriptorName") { (value: Any?) in
181165
if let value = value as? SwiftProtobufPluginLibrary.MethodDescriptor {
182166
return value.name
183167
}
184-
throw TemplateSyntaxError("message: invalid argument \(String(describing:value))")
168+
throw invalidArgumentType(filter:"methodDescriptorName", required:"MethodDescriptor", received:value)
185169
}
186170
ext.registerFilter("methodIsUnary") { (value: Any?) in
187171
if let value = value as? SwiftProtobufPluginLibrary.MethodDescriptor {
188172
return !value.proto.clientStreaming && !value.proto.serverStreaming
189173
}
190-
throw TemplateSyntaxError("message: invalid argument \(String(describing:value))")
174+
throw invalidArgumentType(filter:"methodIsUnary", required:"MethodDescriptor", received:value)
191175
}
192176
ext.registerFilter("methodIsServerStreaming") { (value: Any?) in
193177
if let value = value as? SwiftProtobufPluginLibrary.MethodDescriptor {
194178
return !value.proto.clientStreaming && value.proto.serverStreaming
195179
}
196-
throw TemplateSyntaxError("message: invalid argument \(String(describing:value))")
180+
throw invalidArgumentType(filter:"methodIsServerStreaming", required:"MethodDescriptor", received:value)
197181
}
198182
ext.registerFilter("methodIsClientStreaming") { (value: Any?) in
199183
if let value = value as? SwiftProtobufPluginLibrary.MethodDescriptor {
200184
return value.proto.clientStreaming && !value.proto.serverStreaming
201185
}
202-
throw TemplateSyntaxError("message: invalid argument \(String(describing:value))")
186+
throw invalidArgumentType(filter:"methodIsClientStreaming", required:"MethodDescriptor", received:value)
203187
}
204188
ext.registerFilter("methodIsBidiStreaming") { (value: Any?) in
205189
if let value = value as? SwiftProtobufPluginLibrary.MethodDescriptor {
206190
return value.proto.clientStreaming && value.proto.serverStreaming
207191
}
208-
throw TemplateSyntaxError("message: invalid argument \(String(describing:value))")
192+
throw invalidArgumentType(filter:"methodIsBidiStreaming", required:"MethodDescriptor", received:value)
209193
}
210194
}
211195
}

0 commit comments

Comments
 (0)