Skip to content

Commit

Permalink
Change server to accept both spec compatible and old paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
ofpiyush committed Aug 7, 2020
1 parent d0e66ee commit 160a546
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion protoc-gen-twirp/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,13 @@ func pathPrefix(file *descriptor.FileDescriptorProto, service *descriptor.Servic
// pathFor returns the complete path for requests to a particular method on a
// particular service.
func pathFor(file *descriptor.FileDescriptorProto, service *descriptor.ServiceDescriptorProto, method *descriptor.MethodDescriptorProto) string {
return pathPrefix(file, service) + method.GetName()
}

// pathForCamelCompat returns the old CamelCase complete path for requests to a
// particular method on a particular service.
// This method is here to ensure compatibility with older clients.
func pathForCamelCompat(file *descriptor.FileDescriptorProto, service *descriptor.ServiceDescriptorProto, method *descriptor.MethodDescriptorProto) string {
return pathPrefix(file, service) + stringutils.CamelCase(method.GetName())
}

Expand Down Expand Up @@ -1104,8 +1111,13 @@ func (t *twirp) generateServerRouting(servStruct string, file *descriptor.FileDe
t.P(` switch req.URL.Path {`)
for _, method := range service.Method {
path := pathFor(file, service, method)
pathCamel := pathForCamelCompat(file, service, method)
methName := "serve" + stringutils.CamelCase(method.GetName())
t.P(` case `, strconv.Quote(path), `:`)
caseCondition := strconv.Quote(path)
if path != pathCamel {
caseCondition += `, ` + strconv.Quote(pathCamel)
}
t.P(` case `, caseCondition, `:`)
t.P(` s.`, methName, `(ctx, resp, req)`)
t.P(` return`)
}
Expand Down

0 comments on commit 160a546

Please sign in to comment.