...

Package generator

import "github.com/gogo/protobuf/protoc-gen-gogo/generator"
Overview
Index
Subdirectories

Overview ▾

The code generator for the plugin for the Google protocol buffer compiler. It generates Go code from the protocol buffer description files read by the main routine.

Index ▾

func CamelCase(s string) string
func CamelCaseSlice(elem []string) string
func EmbedFieldName(goTyp string) string
func FileName(file *FileDescriptor) string
func GetCustomType(field *descriptor.FieldDescriptorProto) (packageName string, typ string, err error)
func GoMapValueTypes(mapField, valueField *descriptor.FieldDescriptorProto, goValueType, goValueAliasType string) (nullable bool, outGoType string, outGoAliasType string)
func GoTypeToName(goTyp string) string
func IsScalar(field *descriptor.FieldDescriptorProto) bool
func NewPluginImports(generator *Generator) *pluginImports
func RegisterPlugin(p Plugin)
func RegisterUniquePackageName(pkg string, f *FileDescriptor) string
type AnnotatedAtoms
    func Annotate(file *FileDescriptor, path string, atoms ...interface{}) *AnnotatedAtoms
type Descriptor
    func (c *Descriptor) File() *FileDescriptor
    func (c *Descriptor) GoImportPath() GoImportPath
    func (d *Descriptor) IsGroup() bool
    func (d *Descriptor) Path() string
    func (d *Descriptor) TypeName() []string
type EnumDescriptor
    func (c *EnumDescriptor) File() *FileDescriptor
    func (c *EnumDescriptor) GoImportPath() GoImportPath
    func (e *EnumDescriptor) TypeName() (s []string)
type ExtensionDescriptor
    func (e *ExtensionDescriptor) DescName() string
    func (c *ExtensionDescriptor) File() *FileDescriptor
    func (c *ExtensionDescriptor) GoImportPath() GoImportPath
    func (e *ExtensionDescriptor) TypeName() (s []string)
type FileDescriptor
    func (d *FileDescriptor) Enums() []*EnumDescriptor
    func (d *FileDescriptor) GoPackageName() string
    func (d *FileDescriptor) Messages() []*Descriptor
    func (d *FileDescriptor) VarName() string
type Generator
    func New() *Generator
    func (g *Generator) AddImport(importPath GoImportPath) GoPackageName
    func (g *Generator) AllFiles() *descriptor.FileDescriptorSet
    func (g *Generator) BuildTypeNameMap()
    func (g *Generator) CommandLineParameters(parameter string)
    func (g *Generator) Comments(path string) string
    func (g *Generator) DefaultPackageName(obj Object) string
    func (g *Generator) Error(err error, msgs ...string)
    func (g *Generator) Fail(msgs ...string)
    func (g *Generator) GenerateAllFiles()
    func (g *Generator) GeneratePlugin(p Plugin)
    func (g *Generator) GetFieldName(message *Descriptor, field *descriptor.FieldDescriptorProto) string
    func (g *Generator) GetMapKeyField(field, keyField *descriptor.FieldDescriptorProto) *descriptor.FieldDescriptorProto
    func (g *Generator) GetMapValueField(field, valField *descriptor.FieldDescriptorProto) *descriptor.FieldDescriptorProto
    func (g *Generator) GetOneOfFieldName(message *Descriptor, field *descriptor.FieldDescriptorProto) string
    func (g *Generator) GoMapType(d *Descriptor, field *descriptor.FieldDescriptorProto) *GoMapDescriptor
    func (g *Generator) GoPackageName(importPath GoImportPath) GoPackageName
    func (g *Generator) GoType(message *Descriptor, field *descriptor.FieldDescriptorProto) (typ string, wire string)
    func (g *Generator) In()
    func (g *Generator) IsGroup(field *descriptor.FieldDescriptorProto) bool
    func (g *Generator) IsMap(field *descriptor.FieldDescriptorProto) bool
    func (g *Generator) ObjectNamed(typeName string) Object
    func (g *Generator) OneOfTypeName(message *Descriptor, field *descriptor.FieldDescriptorProto) string
    func (g *Generator) Out()
    func (g *Generator) P(str ...interface{})
    func (g *Generator) PrintComments(path string) bool
    func (g *Generator) PrintImport(alias GoPackageName, pkg GoImportPath)
    func (g *Generator) RecordTypeUse(t string)
    func (g *Generator) SetFile(filename string)
    func (g *Generator) SetPackageNames()
    func (g *Generator) TypeName(obj Object) string
    func (g *Generator) TypeNameByObject(typeName string) Object
    func (g *Generator) WrapTypes()
type GoImportPath
    func (p GoImportPath) String() string
type GoMapDescriptor
type GoPackageName
type ImportedDescriptor
    func (c *ImportedDescriptor) File() *FileDescriptor
    func (c *ImportedDescriptor) GoImportPath() GoImportPath
    func (id *ImportedDescriptor) TypeName() []string
type Object
type Plugin
type PluginImports
type Single

Package files

generator.go helper.go

func CamelCase

func CamelCase(s string) string

CamelCase returns the CamelCased name. If there is an interior underscore followed by a lower case letter, drop the underscore and convert the letter to upper case. There is a remote possibility of this rewrite causing a name collision, but it's so remote we're prepared to pretend it's nonexistent - since the C++ generator lowercases names, it's extremely unlikely to have two fields with different capitalizations. In short, _my_field_name_2 becomes XMyFieldName_2.

func CamelCaseSlice

func CamelCaseSlice(elem []string) string

CamelCaseSlice is like CamelCase, but the argument is a slice of strings to be joined with "_".

func EmbedFieldName

func EmbedFieldName(goTyp string) string

func FileName

func FileName(file *FileDescriptor) string

func GetCustomType

func GetCustomType(field *descriptor.FieldDescriptorProto) (packageName string, typ string, err error)

func GoMapValueTypes

func GoMapValueTypes(mapField, valueField *descriptor.FieldDescriptorProto, goValueType, goValueAliasType string) (nullable bool, outGoType string, outGoAliasType string)

GoMapValueTypes returns the map value Go type and the alias map value Go type (for casting), taking into account whether the map is nullable or the value is a message.

func GoTypeToName

func GoTypeToName(goTyp string) string

func IsScalar

func IsScalar(field *descriptor.FieldDescriptorProto) bool

Is this field a scalar numeric type?

func NewPluginImports

func NewPluginImports(generator *Generator) *pluginImports

func RegisterPlugin

func RegisterPlugin(p Plugin)

RegisterPlugin installs a (second-order) plugin to be run when the Go output is generated. It is typically called during initialization.

func RegisterUniquePackageName

func RegisterUniquePackageName(pkg string, f *FileDescriptor) string

Create and remember a guaranteed unique package name. Pkg is the candidate name. The FileDescriptor parameter is unused.

type AnnotatedAtoms

AnnotatedAtoms is a list of atoms (as consumed by P) that records the file name and proto AST path from which they originated.

type AnnotatedAtoms struct {
    // contains filtered or unexported fields
}

func Annotate

func Annotate(file *FileDescriptor, path string, atoms ...interface{}) *AnnotatedAtoms

Annotate records the file name and proto AST path of a list of atoms so that a later call to P can emit a link from each atom to its origin.

type Descriptor

Descriptor represents a protocol buffer message.

type Descriptor struct {
    *descriptor.DescriptorProto
    // contains filtered or unexported fields
}

func (*Descriptor) File

func (c *Descriptor) File() *FileDescriptor

func (*Descriptor) GoImportPath

func (c *Descriptor) GoImportPath() GoImportPath

GoImportPath is the import path of the Go package containing the type.

func (*Descriptor) IsGroup

func (d *Descriptor) IsGroup() bool

func (*Descriptor) Path

func (d *Descriptor) Path() string

func (*Descriptor) TypeName

func (d *Descriptor) TypeName() []string

TypeName returns the elements of the dotted type name. The package name is not part of this name.

type EnumDescriptor

EnumDescriptor describes an enum. If it's at top level, its parent will be nil. Otherwise it will be the descriptor of the message in which it is defined.

type EnumDescriptor struct {
    *descriptor.EnumDescriptorProto
    // contains filtered or unexported fields
}

func (*EnumDescriptor) File

func (c *EnumDescriptor) File() *FileDescriptor

func (*EnumDescriptor) GoImportPath

func (c *EnumDescriptor) GoImportPath() GoImportPath

GoImportPath is the import path of the Go package containing the type.

func (*EnumDescriptor) TypeName

func (e *EnumDescriptor) TypeName() (s []string)

TypeName returns the elements of the dotted type name. The package name is not part of this name.

type ExtensionDescriptor

ExtensionDescriptor describes an extension. If it's at top level, its parent will be nil. Otherwise it will be the descriptor of the message in which it is defined.

type ExtensionDescriptor struct {
    *descriptor.FieldDescriptorProto
    // contains filtered or unexported fields
}

func (*ExtensionDescriptor) DescName

func (e *ExtensionDescriptor) DescName() string

DescName returns the variable name used for the generated descriptor.

func (*ExtensionDescriptor) File

func (c *ExtensionDescriptor) File() *FileDescriptor

func (*ExtensionDescriptor) GoImportPath

func (c *ExtensionDescriptor) GoImportPath() GoImportPath

GoImportPath is the import path of the Go package containing the type.

func (*ExtensionDescriptor) TypeName

func (e *ExtensionDescriptor) TypeName() (s []string)

TypeName returns the elements of the dotted type name. The package name is not part of this name.

type FileDescriptor

FileDescriptor describes an protocol buffer descriptor file (.proto). It includes slices of all the messages and enums defined within it. Those slices are constructed by WrapTypes.

type FileDescriptor struct {
    *descriptor.FileDescriptorProto
    // contains filtered or unexported fields
}

func (*FileDescriptor) Enums

func (d *FileDescriptor) Enums() []*EnumDescriptor

func (*FileDescriptor) GoPackageName

func (d *FileDescriptor) GoPackageName() string

func (*FileDescriptor) Messages

func (d *FileDescriptor) Messages() []*Descriptor

func (*FileDescriptor) VarName

func (d *FileDescriptor) VarName() string

VarName is the variable name we'll use in the generated code to refer to the compressed bytes of this descriptor. It is not exported, so it is only valid inside the generated package.

type Generator

Generator is the type whose methods generate the output, stored in the associated response structure.

type Generator struct {
    *bytes.Buffer

    Request  *plugin.CodeGeneratorRequest  // The input.
    Response *plugin.CodeGeneratorResponse // The output.

    Param             map[string]string // Command-line parameters.
    PackageImportPath string            // Go import path of the package we're generating code for
    ImportPrefix      string            // String to prefix to imported package file names.
    ImportMap         map[string]string // Mapping from .proto file name to import path

    Pkg map[string]string // The names under which we import support packages
    // contains filtered or unexported fields
}

func New

func New() *Generator

New creates a new generator and allocates the request and response protobufs.

func (*Generator) AddImport

func (g *Generator) AddImport(importPath GoImportPath) GoPackageName

AddImport adds a package to the generated file's import section. It returns the name used for the package.

func (*Generator) AllFiles

func (g *Generator) AllFiles() *descriptor.FileDescriptorSet

func (*Generator) BuildTypeNameMap

func (g *Generator) BuildTypeNameMap()

BuildTypeNameMap builds the map from fully qualified type names to objects. The key names for the map come from the input data, which puts a period at the beginning. It should be called after SetPackageNames and before GenerateAllFiles.

func (*Generator) CommandLineParameters

func (g *Generator) CommandLineParameters(parameter string)

CommandLineParameters breaks the comma-separated list of key=value pairs in the parameter (a member of the request protobuf) into a key/value map. It then sets file name mappings defined by those entries.

func (*Generator) Comments

func (g *Generator) Comments(path string) string

Comments returns any comments from the source .proto file and empty string if comments not found. The path is a comma-separated list of intergers. See descriptor.proto for its format.

func (*Generator) DefaultPackageName

func (g *Generator) DefaultPackageName(obj Object) string

DefaultPackageName returns the package name printed for the object. If its file is in a different package, it returns the package name we're using for this file, plus ".". Otherwise it returns the empty string.

func (*Generator) Error

func (g *Generator) Error(err error, msgs ...string)

Error reports a problem, including an error, and exits the program.

func (*Generator) Fail

func (g *Generator) Fail(msgs ...string)

Fail reports a problem and exits the program.

func (*Generator) GenerateAllFiles

func (g *Generator) GenerateAllFiles()

GenerateAllFiles generates the output for all the files we're outputting.

func (*Generator) GeneratePlugin

func (g *Generator) GeneratePlugin(p Plugin)

func (*Generator) GetFieldName

func (g *Generator) GetFieldName(message *Descriptor, field *descriptor.FieldDescriptorProto) string

func (*Generator) GetMapKeyField

func (g *Generator) GetMapKeyField(field, keyField *descriptor.FieldDescriptorProto) *descriptor.FieldDescriptorProto

func (*Generator) GetMapValueField

func (g *Generator) GetMapValueField(field, valField *descriptor.FieldDescriptorProto) *descriptor.FieldDescriptorProto

func (*Generator) GetOneOfFieldName

func (g *Generator) GetOneOfFieldName(message *Descriptor, field *descriptor.FieldDescriptorProto) string

func (*Generator) GoMapType

func (g *Generator) GoMapType(d *Descriptor, field *descriptor.FieldDescriptorProto) *GoMapDescriptor

func (*Generator) GoPackageName

func (g *Generator) GoPackageName(importPath GoImportPath) GoPackageName

GoPackageName returns the name used for a package.

func (*Generator) GoType

func (g *Generator) GoType(message *Descriptor, field *descriptor.FieldDescriptorProto) (typ string, wire string)

GoType returns a string representing the type name, and the wire type

func (*Generator) In

func (g *Generator) In()

In Indents the output one tab stop.

func (*Generator) IsGroup

func (g *Generator) IsGroup(field *descriptor.FieldDescriptorProto) bool

func (*Generator) IsMap

func (g *Generator) IsMap(field *descriptor.FieldDescriptorProto) bool

func (*Generator) ObjectNamed

func (g *Generator) ObjectNamed(typeName string) Object

ObjectNamed, given a fully-qualified input type name as it appears in the input data, returns the descriptor for the message or enum with that name.

func (*Generator) OneOfTypeName

func (g *Generator) OneOfTypeName(message *Descriptor, field *descriptor.FieldDescriptorProto) string

func (*Generator) Out

func (g *Generator) Out()

Out unindents the output one tab stop.

func (*Generator) P

func (g *Generator) P(str ...interface{})

P prints the arguments to the generated output. It handles strings and int32s, plus handling indirections because they may be *string, etc. Any inputs of type AnnotatedAtoms may emit annotations in a .meta file in addition to outputting the atoms themselves (if g.annotateCode is true).

func (*Generator) PrintComments

func (g *Generator) PrintComments(path string) bool

PrintComments prints any comments from the source .proto file. The path is a comma-separated list of integers. It returns an indication of whether any comments were printed. See descriptor.proto for its format.

func (*Generator) PrintImport

func (g *Generator) PrintImport(alias GoPackageName, pkg GoImportPath)

func (*Generator) RecordTypeUse

func (g *Generator) RecordTypeUse(t string)

func (*Generator) SetFile

func (g *Generator) SetFile(filename string)

func (*Generator) SetPackageNames

func (g *Generator) SetPackageNames()

SetPackageNames sets the package name for this run. The package name must agree across all files being generated. It also defines unique package names for all imported files.

func (*Generator) TypeName

func (g *Generator) TypeName(obj Object) string

TypeName is the printed name appropriate for an item. If the object is in the current file, TypeName drops the package name and underscores the rest. Otherwise the object is from another package; and the result is the underscored package name followed by the item name. The result always has an initial capital.

func (*Generator) TypeNameByObject

func (g *Generator) TypeNameByObject(typeName string) Object

func (*Generator) WrapTypes

func (g *Generator) WrapTypes()

WrapTypes walks the incoming data, wrapping DescriptorProtos, EnumDescriptorProtos and FileDescriptorProtos into file-referenced objects within the Generator. It also creates the list of files to generate and so should be called before GenerateAllFiles.

type GoImportPath

A GoImportPath is the import path of a Go package. e.g., "google.golang.org/genproto/protobuf".

type GoImportPath string

func (GoImportPath) String

func (p GoImportPath) String() string

type GoMapDescriptor

GoMapDescriptor is a full description of the map output struct.

type GoMapDescriptor struct {
    GoType string

    KeyField      *descriptor.FieldDescriptorProto
    KeyAliasField *descriptor.FieldDescriptorProto
    KeyTag        string

    ValueField      *descriptor.FieldDescriptorProto
    ValueAliasField *descriptor.FieldDescriptorProto
    ValueTag        string
}

type GoPackageName

A GoPackageName is the name of a Go package. e.g., "protobuf".

type GoPackageName string

type ImportedDescriptor

ImportedDescriptor describes a type that has been publicly imported from another file.

type ImportedDescriptor struct {
    // contains filtered or unexported fields
}

func (*ImportedDescriptor) File

func (c *ImportedDescriptor) File() *FileDescriptor

func (*ImportedDescriptor) GoImportPath

func (c *ImportedDescriptor) GoImportPath() GoImportPath

GoImportPath is the import path of the Go package containing the type.

func (*ImportedDescriptor) TypeName

func (id *ImportedDescriptor) TypeName() []string

type Object

Object is an interface abstracting the abilities shared by enums, messages, extensions and imported objects.

type Object interface {
    GoImportPath() GoImportPath
    TypeName() []string
    File() *FileDescriptor
}

type Plugin

A Plugin provides functionality to add to the output during Go code generation, such as to produce RPC stubs.

type Plugin interface {
    // Name identifies the plugin.
    Name() string
    // Init is called once after data structures are built but before
    // code generation begins.
    Init(g *Generator)
    // Generate produces the code generated by the plugin for this file,
    // except for the imports, by calling the generator's methods P, In, and Out.
    Generate(file *FileDescriptor)
    // GenerateImports produces the import declarations for this file.
    // It is called after Generate.
    GenerateImports(file *FileDescriptor)
}

type PluginImports

type PluginImports interface {
    NewImport(pkg string) Single
    GenerateImports(file *FileDescriptor)
}

type Single

type Single interface {
    Use() string
    IsUsed() bool
    Name() string
    Location() string
}

Subdirectories

Name Synopsis
..