本文整理汇总了Golang中github.com/golang/protobuf/protoc-gen-go/descriptor.FileDescriptorProto.GetName方法的典型用法代码示例。如果您正苦于以下问题:Golang FileDescriptorProto.GetName方法的具体用法?Golang FileDescriptorProto.GetName怎么用?Golang FileDescriptorProto.GetName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/golang/protobuf/protoc-gen-go/descriptor.FileDescriptorProto
的用法示例。
在下文中一共展示了FileDescriptorProto.GetName方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: goPackagePath
// goPackagePath returns the go package path which go files generated from "f" should have.
// It respects the mapping registered by AddPkgMap if exists. Or it generates a path from
// the file name of "f" if otherwise.
func (r *Registry) goPackagePath(f *descriptor.FileDescriptorProto) string {
name := f.GetName()
if pkg, ok := r.pkgMap[name]; ok {
return path.Join(r.prefix, pkg)
}
return path.Join(r.prefix, path.Dir(name))
}
示例2: uniquePackageOf
func uniquePackageOf(fd *descriptor.FileDescriptorProto) string {
s, ok := uniquePackageName[fd]
if !ok {
log.Fatal("internal error: no package name defined for " + fd.GetName())
}
return s
}
示例3: FileOf
// FileOf return the FileDescriptor for this FileDescriptorProto.
func (g *Generator) FileOf(fd *descriptor.FileDescriptorProto) *FileDescriptor {
for _, file := range g.allFiles {
if file.FileDescriptorProto == fd {
return file
}
}
g.Fail("could not find file in table:", fd.GetName())
return nil
}
示例4: getGoPackage
func getGoPackage(protoFile *descriptor.FileDescriptorProto) string {
if protoFile.Options != nil && protoFile.Options.GoPackage != nil {
return protoFile.Options.GetGoPackage()
}
if protoFile.Package == nil {
base := filepath.Base(protoFile.GetName())
ext := filepath.Ext(base)
return strings.TrimSuffix(base, ext)
}
return strings.Replace(protoFile.GetPackage(), ".", "_", -1)
}
示例5: getTmplData
func getTmplData(protoFile *descriptor.FileDescriptorProto) (*tmplData, error) {
var messageDatas []*tmplMessageData
for _, messageType := range protoFile.MessageType {
var parents []string
messageDatas = getTmplMessageDatas(protoFile.GetPackage(), parents, messageType, messageDatas)
}
return &tmplData{
Name: protoFile.GetName(),
GoPackage: getGoPackage(protoFile),
MessageDatas: messageDatas,
}, nil
}
示例6: packageIdentityName
// packageIdentityName returns the identity of packages.
// protoc-gen-grpc-gateway rejects CodeGenerationRequests which contains more than one packages
// as protoc-gen-go does.
func packageIdentityName(f *descriptor.FileDescriptorProto) string {
if f.Options != nil && f.Options.GoPackage != nil {
return f.Options.GetGoPackage()
}
if f.Package == nil {
base := filepath.Base(f.GetName())
ext := filepath.Ext(base)
return strings.TrimSuffix(base, ext)
}
return f.GetPackage()
}
示例7: defaultGoPackageName
// defaultGoPackageName returns the default go package name to be used for go files generated from "f".
// You might need to use an unique alias for the package when you import it. Use ReserveGoPackageAlias to get a unique alias.
func defaultGoPackageName(f *descriptor.FileDescriptorProto) string {
if f.Options != nil && f.Options.GoPackage != nil {
return f.Options.GetGoPackage()
}
if f.Package == nil {
base := filepath.Base(f.GetName())
ext := filepath.Ext(base)
return strings.TrimSuffix(base, ext)
}
return strings.Replace(f.GetPackage(), ".", "_", -1)
}
示例8: goPackagePath
// goPackagePath returns the go package path which go files generated from "f" should have.
// It respects the mapping registered by AddPkgMap if exists. Or it generates a path from
// the file name of "f" if otherwise.
func (r *Registry) goPackagePath(f *descriptor.FileDescriptorProto) string {
name := f.GetName()
if pkg, ok := r.pkgMap[name]; ok {
return path.Join(r.prefix, pkg)
}
ext := filepath.Ext(name)
if ext == ".protodevel" || ext == ".proto" {
name = strings.TrimSuffix(name, ext)
}
return path.Join(r.prefix, fmt.Sprintf("%s.pb", name))
}
示例9: PackageName
// PackageName returns the package name of the given file, which is either the
// result of f.GetPackage (a package set explicitly by the user) or the name of
// the file.
func PackageName(f *descriptor.FileDescriptorProto) string {
// Check for an explicit package name given by the user in a protobuf file as
//
// package foo;
//
if pkg := f.GetPackage(); len(pkg) > 0 {
return pkg
}
// Otherwise use the name of the file (note: not filepath.Base because
// protobuf only speaks in unix path terms).
pkg := path.Base(f.GetName())
return strings.TrimSuffix(pkg, path.Ext(pkg))
}
示例10: goPackagePath
// goPackagePath returns the go package path which go files generated from "f" should have.
// It respects the mapping registered by AddPkgMap if exists. Or use go_package as import path
// if it includes a slash, Otherwide, it generates a path from the file name of "f".
func (r *Registry) goPackagePath(f *descriptor.FileDescriptorProto) string {
name := f.GetName()
if pkg, ok := r.pkgMap[name]; ok {
return path.Join(r.prefix, pkg)
}
gopkg := f.Options.GetGoPackage()
idx := strings.LastIndex(gopkg, "/")
if idx >= 0 {
return gopkg
}
return path.Join(r.prefix, path.Dir(name))
}
示例11: convertFile
func convertFile(file *descriptor.FileDescriptorProto) ([]*plugin.CodeGeneratorResponse_File, error) {
name := path.Base(file.GetName())
pkg, ok := globalPkg.relativelyLookupPackage(file.GetPackage())
if !ok {
return nil, fmt.Errorf("no such package found: %s", file.GetPackage())
}
response := []*plugin.CodeGeneratorResponse_File{}
for _, msg := range file.GetMessageType() {
options := msg.GetOptions()
if options == nil {
continue
}
if !proto.HasExtension(options, E_TableName) {
continue
}
optionValue, err := proto.GetExtension(options, E_TableName)
if err != nil {
return nil, err
}
tableName := *optionValue.(*string)
if len(tableName) == 0 {
return nil, fmt.Errorf("table name of %s cannot be empty", msg.GetName())
}
glog.V(2).Info("Generating schema for a message type ", msg.GetName())
schema, err := convertMessageType(pkg, msg)
if err != nil {
glog.Errorf("Failed to convert %s: %v", name, err)
return nil, err
}
jsonSchema, err := json.Marshal(schema)
if err != nil {
glog.Error("Failed to encode schema", err)
return nil, err
}
resFile := &plugin.CodeGeneratorResponse_File{
Name: proto.String(fmt.Sprintf("%s/%s.schema", strings.Replace(file.GetPackage(), ".", "/", -1), tableName)),
Content: proto.String(string(jsonSchema)),
}
response = append(response, resFile)
}
return response, nil
}
示例12: packageIdentityName
// packageIdentityName returns the identity of packages.
// protoc-gen-grpc-gateway rejects CodeGenerationRequests which contains more than one packages
// as protoc-gen-go does.
func packageIdentityName(f *descriptor.FileDescriptorProto) string {
if f.Options != nil && f.Options.GoPackage != nil {
gopkg := f.Options.GetGoPackage()
idx := strings.LastIndex(gopkg, "/")
if idx < 0 {
return gopkg
}
return gopkg[idx+1:]
}
if f.Package == nil {
base := filepath.Base(f.GetName())
ext := filepath.Ext(base)
return strings.TrimSuffix(base, ext)
}
return f.GetPackage()
}
示例13: loadFile
// loadFile loads messages and fiels from "file".
// It does not loads services and methods in "file". You need to call
// loadServices after loadFiles is called for all files to load services and methods.
func (r *Registry) loadFile(file *descriptor.FileDescriptorProto) {
pkg := GoPackage{
Path: r.goPackagePath(file),
Name: defaultGoPackageName(file),
}
if err := r.ReserveGoPackageAlias(pkg.Name, pkg.Path); err != nil {
for i := 0; ; i++ {
alias := fmt.Sprintf("%s_%d", pkg.Name, i)
if err := r.ReserveGoPackageAlias(alias, pkg.Path); err == nil {
pkg.Alias = alias
break
}
}
}
f := &File{
FileDescriptorProto: file,
GoPkg: pkg,
}
r.files[file.GetName()] = f
r.registerMsg(f, nil, file.GetMessageType())
}
示例14: loadFile
// loadFile loads messages and fiels from "file".
// It does not loads services and methods in "file". You need to call
// loadServices after loadFiles is called for all files to load services and methods.
func (r *Registry) loadFile(file *descriptor.FileDescriptorProto) {
pkg := GoPackage{
Path: r.goPackagePath(file),
Name: defaultGoPackageName(file),
}
if err := r.ReserveGoPackageAlias(pkg.Name, pkg.Path); err != nil {
for i := 0; ; i++ {
alias := fmt.Sprintf("%s_%d", pkg.Name, i)
if err := r.ReserveGoPackageAlias(alias, pkg.Path); err == nil {
pkg.Alias = alias
break
}
}
}
comments := make(map[string]*descriptor.SourceCodeInfo_Location)
for _, loc := range file.GetSourceCodeInfo().GetLocation() {
if loc.LeadingComments == nil && loc.TrailingComments == nil {
continue
}
var p []string
for _, n := range loc.Path {
p = append(p, strconv.Itoa(int(n)))
}
comments[strings.Join(p, ",")] = loc
}
f := &File{
FileDescriptorProto: file,
GoPkg: pkg,
Comments: comments,
}
r.files[file.GetName()] = f
r.registerMsg(f, nil, nil, file.GetMessageType())
}
示例15: TestResolveFieldPath
//.........这里部分代码省略.........
type_name: 'ExampleMessage'
label: LABEL_OPTIONAL
number: 1
>
>
`,
path: "nested.nested2.nested.nested2.nested.nested2.terminal",
wantErr: false,
},
// non aggregate field on the path
{
src: `
name: 'example.proto'
package: 'example'
message_type <
name: 'ExampleMessage'
field <
name: 'nested'
type: TYPE_MESSAGE
type_name: 'AnotherMessage'
label: LABEL_OPTIONAL
number: 1
>
field <
name: 'terminal'
type: TYPE_BOOL
label: LABEL_OPTIONAL
number: 2
>
>
message_type <
name: 'AnotherMessage'
field <
name: 'nested2'
type: TYPE_MESSAGE
type_name: 'ExampleMessage'
label: LABEL_OPTIONAL
number: 1
>
>
`,
path: "nested.terminal.nested2",
wantErr: true,
},
// repeated field
{
src: `
name: 'example.proto'
package: 'example'
message_type <
name: 'ExampleMessage'
field <
name: 'nested'
type: TYPE_MESSAGE
type_name: 'AnotherMessage'
label: LABEL_OPTIONAL
number: 1
>
field <
name: 'terminal'
type: TYPE_BOOL
label: LABEL_OPTIONAL
number: 2
>
>
message_type <
name: 'AnotherMessage'
field <
name: 'nested2'
type: TYPE_MESSAGE
type_name: 'ExampleMessage'
label: LABEL_REPEATED
number: 1
>
>
`,
path: "nested.nested2.terminal",
wantErr: true,
},
} {
var file descriptor.FileDescriptorProto
if err := proto.UnmarshalText(spec.src, &file); err != nil {
t.Fatalf("proto.Unmarshal(%s) failed with %v; want success", spec.src, err)
}
reg := NewRegistry()
reg.loadFile(&file)
f, err := reg.LookupFile(file.GetName())
if err != nil {
t.Fatalf("reg.LookupFile(%q) failed with %v; want success; on file=%s", file.GetName(), err, spec.src)
}
_, err = reg.resolveFiledPath(f.Messages[0], spec.path)
if got, want := err != nil, spec.wantErr; got != want {
if want {
t.Errorf("reg.resolveFiledPath(%q, %q) succeeded; want an error", f.Messages[0].GetName(), spec.path)
continue
}
t.Errorf("reg.resolveFiledPath(%q, %q) failed with %v; want success", f.Messages[0].GetName(), spec.path, err)
}
}
}