• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang descriptor.FileDescriptorProto类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/golang/protobuf/protoc-gen-go/descriptor.FileDescriptorProto的典型用法代码示例。如果您正苦于以下问题:Golang FileDescriptorProto类的具体用法?Golang FileDescriptorProto怎么用?Golang FileDescriptorProto使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了FileDescriptorProto类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: generateFile

func (g *Generator) generateFile(
	file *descriptor.FileDescriptorProto,
) (content string, err error) {
	descriptor := &fileDescriptor{
		Package:  file.GetPackage(),
		Services: make([]*serviceDescriptor, len(file.Service)),
	}
	for i, service := range file.Service {
		serviceDescriptor := &serviceDescriptor{
			ServerInterface: fmt.Sprintf("%sServer", service.GetName()),
			Methods:         make([]*methodDescriptor, len(service.Method)),
		}
		for n, method := range service.Method {
			serviceDescriptor.Methods[n] = &methodDescriptor{
				Service:         descriptor.Package,
				ServerInterface: serviceDescriptor.ServerInterface,
				Name:            method.GetName(),
				InputType:       strings.Split(method.GetInputType(), ".")[2],
				OutputType:      strings.Split(method.GetOutputType(), ".")[2],
			}
		}
		descriptor.Services[i] = serviceDescriptor
	}
	var buffer bytes.Buffer
	if err := loggerTemplate.Execute(&buffer, descriptor); err != nil {
		return "", err
	}
	return buffer.String(), nil
}
开发者ID:kazegusuri,项目名称:grpcinstrument,代码行数:29,代码来源:generator.go


示例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
}
开发者ID:samegoal,项目名称:protoclosure,代码行数:7,代码来源:helper.go


示例3: fillTreeWithFile

func fillTreeWithFile(tree *tree, file *descriptor.FileDescriptorProto) {
	key := fmt.Sprintf(".%s", file.GetPackage())
	locs := make(map[string]*descriptor.SourceCodeInfo_Location)
	for _, loc := range file.GetSourceCodeInfo().GetLocation() {
		if loc.LeadingComments == nil {
			continue
		}
		var p []string
		for _, n := range loc.Path {
			p = append(p, strconv.Itoa(int(n)))
		}
		locs[strings.Join(p, ",")] = loc
	}

	// Messages
	for idx, proto := range file.GetMessageType() {
		fillTreeWithMessage(tree, key, proto, fmt.Sprintf("4,%d", idx), locs)
	}

	// Enums
	for idx, proto := range file.GetEnumType() {
		fillTreeWithEnum(tree, key, proto, fmt.Sprintf("5,%d", idx), locs)
	}

	// Services
	for idx, proto := range file.GetService() {
		fillTreeWithService(tree, key, proto, fmt.Sprintf("6,%d", idx), locs)
	}
}
开发者ID:TheThingsNetwork,项目名称:ttn,代码行数:29,代码来源:build_tree.go


示例4: 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))
}
开发者ID:floridoo,项目名称:grpc-gateway,代码行数:10,代码来源:registry.go


示例5: 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
}
开发者ID:samegoal,项目名称:protoclosure,代码行数:10,代码来源:generator.go


示例6: 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)
}
开发者ID:kunthar,项目名称:pachyderm,代码行数:11,代码来源:main.go


示例7: 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()
}
开发者ID:floridoo,项目名称:grpc-gateway,代码行数:15,代码来源:registry.go


示例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))
}
开发者ID:vvakame,项目名称:grpc-gateway,代码行数:15,代码来源:registry.go


示例9: 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
}
开发者ID:kunthar,项目名称:pachyderm,代码行数:12,代码来源:main.go


示例10: 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)
}
开发者ID:vvakame,项目名称:grpc-gateway,代码行数:14,代码来源:registry.go


示例11: 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))
}
开发者ID:ethernetdan,项目名称:prototools,代码行数:16,代码来源:util.go


示例12: 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))
}
开发者ID:peter-edge,项目名称:protoeasy-go,代码行数:17,代码来源:registry.go


示例13: 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
}
开发者ID:GoogleCloudPlatform,项目名称:protoc-gen-bq-schema,代码行数:47,代码来源:main.go


示例14: 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()
}
开发者ID:peter-edge,项目名称:protoeasy-go,代码行数:21,代码来源:registry.go


示例15: 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())
}
开发者ID:vvakame,项目名称:grpc-gateway,代码行数:25,代码来源:registry.go


示例16: LintProtoFile

// LintProtoFile takes a file name, proto file description, and a file.
// It checks the file for errors and writes them to the output file
func LintProtoFile(
	protoFile *descriptor.FileDescriptorProto,
	outFile io.WriteCloser,
) (int, error) {
	var (
		errors      = protoBufErrors{}
		protoSource = protoFile.GetSourceCodeInfo()
	)

	for i, v := range protoFile.GetMessageType() {
		errors.lintProtoMessage(int32(i), pathMessage, []int32{}, v)
	}

	for i, v := range protoFile.GetEnumType() {
		errors.lintProtoEnumType(int32(i), pathEnumType, []int32{}, v)
	}

	for i, v := range protoFile.GetService() {
		errors.lintProtoService(int32(i), v)
	}
	for _, v := range errors {
		line, col := v.getSourceLineNumber(protoSource)
		fmt.Fprintf(
			outFile,
			"%s:%d:%d: '%s' - %s\n",
			*protoFile.Name,
			line,
			col,
			v.errorString,
			linterErrors[v.errorCode],
		)
	}

	return len(errors), nil

}
开发者ID:ckaznocha,项目名称:protoc-gen-lint,代码行数:38,代码来源:linter.go


示例17: 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())
}
开发者ID:ceram1,项目名称:grpc-gateway,代码行数:41,代码来源:registry.go


示例18: processFile

func processFile(inFile *descriptor.FileDescriptorProto) (*plugin.CodeGeneratorResponse_File, error) {
	if inFile.GetSyntax() != "proto3" {
		return nil, fmt.Errorf("Only proto3 syntax is supported")
	}

	outFile := &plugin.CodeGeneratorResponse_File{}

	inFilePath := inFile.GetName()
	inFileDir, inFileName := filepath.Split(inFilePath)

	shortModuleName := firstUpper(strings.TrimSuffix(inFileName, ".proto"))

	fullModuleName := ""
	outFileName := ""
	for _, segment := range strings.Split(inFileDir, "/") {
		if segment == "" {
			continue
		}
		fullModuleName += firstUpper(segment) + "."
		outFileName += firstUpper(segment) + "/"
	}
	fullModuleName += shortModuleName
	outFileName += shortModuleName + ".elm"

	outFile.Name = proto.String(outFileName)

	b := &bytes.Buffer{}
	fg := NewFileGenerator(b)

	fg.GenerateModule(fullModuleName)
	fg.GenerateImports()
	fg.GenerateRuntime()

	var err error

	// Top-level enums.
	for _, inEnum := range inFile.GetEnumType() {
		err = fg.GenerateEnumDefinition("", inEnum)
		if err != nil {
			return nil, err
		}

		err = fg.GenerateEnumDecoder("", inEnum)
		if err != nil {
			return nil, err
		}

		err = fg.GenerateEnumEncoder("", inEnum)
		if err != nil {
			return nil, err
		}
	}

	// Top-level messages.
	for _, inMessage := range inFile.GetMessageType() {
		err = fg.GenerateEverything("", inMessage)
		if err != nil {
			return nil, err
		}
	}

	outFile.Content = proto.String(b.String())

	return outFile, nil
}
开发者ID:tiziano88,项目名称:elm-protobuf,代码行数:65,代码来源:main.go


示例19: getFileName

func getFileName(protoFile *descriptor.FileDescriptorProto) string {
	name := protoFile.GetName()
	return fmt.Sprintf("%s.pb.log.go", strings.TrimSuffix(name, filepath.Ext(name)))
}
开发者ID:kunthar,项目名称:pachyderm,代码行数:4,代码来源:main.go


示例20: 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)
		}
	}
}
开发者ID:tmc,项目名称:grpc-gateway,代码行数:101,代码来源:services_test.go



注:本文中的github.com/golang/protobuf/protoc-gen-go/descriptor.FileDescriptorProto类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang descriptor.MethodDescriptorProto类代码示例发布时间:2022-05-23
下一篇:
Golang descriptor.FieldDescriptorProto类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap