protobuf

package
v0.0.0-...-9c8aaae Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2021 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Copyright © 2019 hori-ryota <hori.ryota@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Index

Constants

This section is empty.

Variables

View Source
var Template = template.Must(template.New("").Funcs(map[string]interface{}{
	"PrintMessageDefs": printMessageDefs,
	"ToImportList": func(rootParam TemplateParam) []string {
		importList := make([]string, 0, 10)
		for _, service := range rootParam.Services {
			for _, rpc := range service.RPCs {
				importList = append(importList, toImportList(rpc.InputType)...)
				if rpc.OutputType == nil {
					importList = append(importList, "google/protobuf/empty.proto")
				} else {
					importList = append(importList, toImportList(rpc.OutputType)...)
				}
			}
		}

		return removeDuplicateAndEmpty(importList)
	},
}).Parse(strings.TrimSpace(`
syntax = "proto3";

package {{.Package}};

{{- if .GoPackage}}
option go_package = "{{.GoPackage}}";
{{- end}}
{{- if .JavaPackage}}
option java_multiple_files = true;
option java_package = "{{.JavaPackage}}";
{{- end}}
{{- if .JavaOuterClassName}}
option java_outer_classname = "{{.JavaOuterClassName}}";
{{- end}}

{{- range ToImportList . }}
import "{{.}}";
{{- end}}
{{- range .Services}}

service {{.Name}} {
{{- range .RPCs}}
  rpc {{.MethodName}}({{.InputType.Obj.Name}}) returns ({{if .OutputType}}{{.OutputType.Obj.Name}}{{else}}google.protobuf.Empty{{end}});
{{- end}}
}
{{- end}}
{{PrintMessageDefs .Services}}
`)))

Functions

func AddIndent

func AddIndent(s string, indent int) string

func Generate

func Generate(
	pkgInfo *loader.PackageInfo,
	protoPackage string,
	goPackage string,
	javaPackage string,
	javaOuterClassName string,
) (string, error)

func KnownTypesToProtoType

func KnownTypesToProtoType(t types.Type) (string, bool)

func NewProtobufCmd

func NewProtobufCmd() *cobra.Command

func Run

func Run(
	usecaseDir string,
	outputFile string,
	protoPackage string,
	goPackage string,
	javaPackage string,
	javaOuterClassName string,
	useStdOut bool,
) error

Types

type EnumDef

type EnumDef struct {
	Type   *types.Named
	Values []*types.Const
}

type RPC

type RPC struct {
	MethodName string
	InputType  *types.Named
	OutputType *types.Named
}

func ParseRPC

func ParseRPC(method *types.Func) *RPC

type Service

type Service struct {
	Name string
	RPCs []RPC
}

type TemplateParam

type TemplateParam struct {
	Package            string
	GoPackage          string
	JavaPackage        string
	JavaOuterClassName string
	Services           []Service
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL