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