pgsgo

package
v0.4.7 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2019 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package pgsgo contains Go-specific helpers for use with PG* based protoc-plugins

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddImportMapping

func AddImportMapping(p pgs.Parameters, proto, pkg string)

AddImportMapping adds a proto file to Go package import mapping to the parameters.

func AddPlugin

func AddPlugin(p pgs.Parameters, name ...string)

AddPlugin adds name to the list of plugins in the parameters. If all plugins are enabled, this method is a noop.

func EnableAllPlugins

func EnableAllPlugins(p pgs.Parameters)

EnableAllPlugins changes the parameters to enable all registered sub-plugins.

func GoFmt

func GoFmt() pgs.PostProcessor

GoFmt returns a PostProcessor that runs gofmt on any files ending in ".go"

func HasPlugin

func HasPlugin(p pgs.Parameters, name string) bool

HasPlugin returns true if the plugin name is enabled in the parameters. This method will always return true if all plugins are enabled.

func ImportPath

func ImportPath(p pgs.Parameters) string

ImportPath returns the protoc-gen-go parameter. This value is used as the package if the input proto files do not declare a go_package option. If it contains slashes, everything up to the rightmost slash is ignored.

See: https://github.com/golang/protobuf#parameters

func ImportPrefix

func ImportPrefix(p pgs.Parameters) string

ImportPrefix returns the protoc-gen-go parameter. This prefix is added onto the beginning of all Go import paths. This is useful for things like generating protos in a subdirectory, or regenerating vendored protobufs in-place. By default, this method returns an empty string.

See: https://github.com/golang/protobuf#parameters

func MappedImport

func MappedImport(p pgs.Parameters, proto string) (string, bool)

MappedImport returns the protoc-gen-go import overrides for the specified proto file. Each entry in the map keys off a proto file (as loaded by protoc) with values of the Go package to use. These values will be prefixed with the value of ImportPrefix when generating the Go code.

func PGGUpperCamelCase

func PGGUpperCamelCase(n pgs.Name) pgs.Name

PGGUpperCamelCase converts Name n to the protoc-gen-go defined upper camelcase. The rules are slightly different from pgs.UpperCamelCase in that leading underscores are converted to 'X', mid-string underscores followed by lowercase letters are removed and the letter is capitalized, all other punctuation is preserved. This method should be used when deriving names of protoc-gen-go generated code (ie, message/service struct names and field names).

See: https://godoc.org/github.com/golang/protobuf/protoc-gen-go/generator#CamelCase

func Plugins

func Plugins(p pgs.Parameters) (plugins []string, all bool)

Plugins returns the sub-plugins enabled for this protoc plugin. If the all value is true, all registered plugins are considered enabled (ie, protoc was called with an empty "plugins" parameter). Otherwise, plugins contains the list of plugins enabled by name.

func SetImportPath

func SetImportPath(p pgs.Parameters, path string)

SetImportPath sets the protoc-gen-go ImportPath parameter. This is useful for overriding the behavior of the ImportPath at runtime.

func SetImportPrefix

func SetImportPrefix(p pgs.Parameters, prefix string)

SetImportPrefix sets the protoc-gen-go ImportPrefix parameter. This is useful for overriding the behavior of the ImportPrefix at runtime.

func SetPaths

func SetPaths(p pgs.Parameters, pt PathType)

SetPaths sets the protoc-gen-go Paths parameter. This is useful for overriding the behavior of Paths at runtime.

Types

type Context

type Context interface {
	// Params returns the Parameters associated with this context.
	Params() pgs.Parameters

	// Name returns the name of a Node as it would appear in the generation output
	// of protoc-gen-go. For each type, the following is returned:
	//
	//     - Package: the Go package name
	//     - File: the Go package name
	//     - Message: the struct name
	//     - Field: the field name on the Message struct
	//     - OneOf: the field name on the Message struct
	//     - Enum: the type name
	//     - EnumValue: the constant name
	//     - Service: the server interface name
	//     - Method: the method name on the server and client interface
	//
	Name(node pgs.Node) pgs.Name

	// ServerName returns the name of the server interface for the Service.
	ServerName(service pgs.Service) pgs.Name

	// ClientName returns the name of the client interface for the Service.
	ClientName(service pgs.Service) pgs.Name

	// ServerStream returns the name of the grpc.ServerStream wrapper for this
	// method. This name is only used if client or server streaming is
	// implemented for this method.
	ServerStream(method pgs.Method) pgs.Name

	// OneofOption returns the struct name that wraps a OneOf option's value. These
	// messages contain one field, matching the value returned by Name for this
	// Field.
	OneofOption(field pgs.Field) pgs.Name

	// TypeName returns the type name of a Field as it would appear in the
	// generated message struct from protoc-gen-go. Fields from imported
	// packages will be prefixed with the package name.
	Type(field pgs.Field) TypeName

	// PackageName returns the name of the Node's package as it would appear in
	// Go source generated by the official protoc-gen-go plugin.
	PackageName(node pgs.Node) pgs.Name

	// ImportPath returns the Go import path for an entity as it would be
	// included in an import block in a Go file. This value is only appropriate
	// for Entities imported into a target file/package.
	ImportPath(entity pgs.Entity) pgs.FilePath

	// OutputPath returns the output path relative to the plugin's output destination
	OutputPath(entity pgs.Entity) pgs.FilePath
}

Context resolves Go-specific language for Packages & Entities generated by protoc-gen-go. The rules that drive the naming behavior are complicated, and result from an interplay of the go_package file option, the proto package, and the proto filename itself. Therefore, it is recommended that all proto files that are targeting Go should include a fully qualified go_package option. These must be consistent for all proto files that are intended to be in the same Go package.

func InitContext

func InitContext(params pgs.Parameters) Context

InitContext configures a Context that should be used for deriving Go names for all Packages and Entities.

type PathType

type PathType string

PathType describes how the generated output file paths should be constructed.

const (
	// ImportPathRelative is the default and outputs the file based off the go
	// import path defined in the go_package option.
	ImportPathRelative PathType = ""

	// SourceRelative indicates files should be output relative to the path of
	// the source file.
	SourceRelative PathType = "source_relative"
)

func Paths

func Paths(p pgs.Parameters) PathType

Paths returns the protoc-gen-go parameter. This value is used to switch the mode used to determine the output paths of the generated code. By default, paths are derived from the import path specified by go_package. It can be overridden to be "source_relative", ignoring the import path using the source path exclusively.

type TypeName

type TypeName string

A TypeName describes the name of a type (type on a field, or method signature)

func (TypeName) Element

func (n TypeName) Element() TypeName

Element returns the TypeName of the element of n. For types other than slices and maps, this just returns n.

Example
types := []string{
	"int",
	"*my.Type",
	"[]string",
	"map[string]*io.Reader",
}

for _, t := range types {
	fmt.Println(TypeName(t).Element())
}
Output:

int
*my.Type
string
*io.Reader

func (TypeName) Key

func (n TypeName) Key() TypeName

Key returns the TypeName of the key of n. For slices, the return TypeName is always "int", and for non slice/map types an empty TypeName is returned.

Example
types := []string{
	"int",
	"*my.Type",
	"[]string",
	"map[string]*io.Reader",
}

for _, t := range types {
	fmt.Println(TypeName(t).Key())
}
Output:


int
string

func (TypeName) Pointer

func (n TypeName) Pointer() TypeName

Pointer converts TypeName n to it's pointer type. If n is already a pointer, slice, or map, it is returned unmodified.

Example
types := []string{
	"int",
	"*my.Type",
	"[]string",
	"map[string]*io.Reader",
}

for _, t := range types {
	fmt.Println(TypeName(t).Pointer())
}
Output:

*int
*my.Type
[]string
map[string]*io.Reader

func (TypeName) String

func (n TypeName) String() string

String satisfies the strings.Stringer interface.

func (TypeName) Value

func (n TypeName) Value() TypeName

Value converts TypeName n to it's value type. If n is already a value type, slice, or map it is returned unmodified.

Example
types := []string{
	"int",
	"*my.Type",
	"[]string",
	"map[string]*io.Reader",
}

for _, t := range types {
	fmt.Println(TypeName(t).Value())
}
Output:

int
my.Type
[]string
map[string]*io.Reader

Jump to

Keyboard shortcuts

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