go_naming.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2018 Twitch Interactive, Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"). You may not
  4. // use this file except in compliance with the License. A copy of the License is
  5. // located at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // or in the "license" file accompanying this file. This file is distributed on
  10. // an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  11. // express or implied. See the License for the specific language governing
  12. // permissions and limitations under the License.
  13. package main
  14. import (
  15. "path"
  16. "strings"
  17. "go-common/app/tool/liverpc/protoc-gen-liverpc/gen/stringutils"
  18. "github.com/golang/protobuf/protoc-gen-go/descriptor"
  19. )
  20. // goPackageOption interprets the file's go_package option.
  21. // If there is no go_package, it returns ("", "", false).
  22. // If there's a simple name, it returns ("", pkg, true).
  23. // If the option implies an import path, it returns (impPath, pkg, true).
  24. func goPackageOption(f *descriptor.FileDescriptorProto) (impPath, pkg string, ok bool) {
  25. pkg = f.GetOptions().GetGoPackage()
  26. if pkg == "" {
  27. return
  28. }
  29. ok = true
  30. // The presence of a slash implies there's an import path.
  31. slash := strings.LastIndex(pkg, "/")
  32. if slash < 0 {
  33. return
  34. }
  35. impPath, pkg = pkg, pkg[slash+1:]
  36. // A semicolon-delimited suffix overrides the package name.
  37. sc := strings.IndexByte(impPath, ';')
  38. if sc < 0 {
  39. return
  40. }
  41. impPath, pkg = impPath[:sc], impPath[sc+1:]
  42. return
  43. }
  44. // goPackageName returns the Go package name to use in the generated Go file.
  45. // The result explicitly reports whether the name came from an option go_package
  46. // statement. If explicit is false, the name was derived from the protocol
  47. // buffer's package statement or the input file name.
  48. func goPackageName(f *descriptor.FileDescriptorProto) (name string, explicit bool) {
  49. // Does the file have a "go_package" option?
  50. if _, pkg, ok := goPackageOption(f); ok {
  51. return pkg, true
  52. }
  53. // Does the file have a package clause?
  54. if pkg := f.GetPackage(); pkg != "" {
  55. return pkg, false
  56. }
  57. // Use the file base name.
  58. return stringutils.BaseName(f.GetName()), false
  59. }
  60. // goFileName returns the output name for the generated Go file.
  61. func goFileName(f *descriptor.FileDescriptorProto) string {
  62. name := *f.Name
  63. if ext := path.Ext(name); ext == ".proto" || ext == ".protodevel" {
  64. name = name[:len(name)-len(ext)]
  65. }
  66. name += ".liverpc.go"
  67. // Does the file have a "go_package" option? If it does, it may override the
  68. // filename.
  69. if impPath, _, ok := goPackageOption(f); ok && impPath != "" {
  70. // Replace the existing dirname with the declared import path.
  71. _, name = path.Split(name)
  72. name = path.Join(impPath, name)
  73. return name
  74. }
  75. return name
  76. }