예제 #1
0
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
}
예제 #2
0
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)
	}
}
예제 #3
0
파일: main.go 프로젝트: kunthar/pachyderm
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)
}
예제 #4
0
// 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)
}
예제 #5
0
// 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()
}
예제 #6
0
파일: main.go 프로젝트: kunthar/pachyderm
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
}
예제 #7
0
// 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))
}
예제 #8
0
// 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()
}
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
}