protobuf

package
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2018 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultMappings = TypeMappings{
	"float64": &ProtoType{Name: "double", Basic: true},
	"float32": &ProtoType{Name: "float", Basic: true},
	"int32":   &ProtoType{Name: "int32", Basic: true},
	"int64":   &ProtoType{Name: "int64", Basic: true},
	"uint32":  &ProtoType{Name: "uint32", Basic: true},
	"uint64":  &ProtoType{Name: "uint64", Basic: true},
	"bool":    &ProtoType{Name: "bool", Basic: true},
	"string":  &ProtoType{Name: "string", Basic: true},
	"uint8": &ProtoType{
		Name:       "uint32",
		Basic:      true,
		Warn:       "type %s was upgraded to uint32",
		Decorators: CastToBasicType("uint8"),
	},
	"int8": &ProtoType{
		Name:       "int32",
		Basic:      true,
		Warn:       "type %s was upgraded to int32",
		Decorators: CastToBasicType("int8"),
	},
	"byte": &ProtoType{
		Name:       "uint32",
		Basic:      true,
		Warn:       "type %s was upgraded to uint32",
		Decorators: CastToBasicType("byte"),
	},
	"uint16": &ProtoType{
		Name:       "uint32",
		Basic:      true,
		Warn:       "type %s was upgraded to uint32",
		Decorators: CastToBasicType("uint16"),
	},
	"int16": &ProtoType{
		Name:       "int32",
		Basic:      true,
		Warn:       "type %s was upgraded to int32",
		Decorators: CastToBasicType("int16"),
	},
	"int": &ProtoType{
		Name:       "int64",
		Basic:      true,
		Warn:       "type %s was upgraded to int64",
		Decorators: CastToBasicType("int"),
	},
	"uint": &ProtoType{
		Name:       "uint64",
		Basic:      true,
		Warn:       "type %s was upgraded to uint64",
		Decorators: CastToBasicType("uint"),
	},
	"uintptr": &ProtoType{
		Name:       "uint64",
		Basic:      true,
		Decorators: CastToBasicType("uintptr"),
	},
	"rune": &ProtoType{
		Name:       "int32",
		Basic:      true,
		Decorators: CastToBasicType("rune"),
	},
	"time.Time": &ProtoType{
		Name:     "Timestamp",
		Package:  "google.protobuf",
		Import:   "google/protobuf/timestamp.proto",
		GoImport: "github.com/gogo/protobuf/types",
		Decorators: NewDecorators(
			func(p *Package, m *Message, f *Field) {
				if f.Options == nil {
					f.Options = make(Options)
				}
				f.Options["(gogoproto.stdtime)"] = NewLiteralValue("true")
				f.Options["(gogoproto.nullable)"] = NewLiteralValue("false")
			},
		),
	},
	"time.Duration": &ProtoType{
		Name:     "Duration",
		Package:  "google.protobuf",
		Import:   "google/protobuf/duration.proto",
		GoImport: "github.com/gogo/protobuf/types",
		Decorators: NewDecorators(
			func(p *Package, m *Message, f *Field) {
				if f.Options == nil {
					f.Options = make(Options)
				}
				f.Options["(gogoproto.stdduration)"] = NewLiteralValue("true")
				f.Options["(gogoproto.nullable)"] = NewLiteralValue("false")
			},
		),
	},
}

Functions

This section is empty.

Types

type Alias

type Alias struct {
	Type       Type
	Underlying Type
	Src        scanner.Type
}

Alias represent a type declaration from one type to another.

func NewAlias

func NewAlias(typ, underlying Type) *Alias

NewAlias returns a new Alias

func (Alias) IsNullable

func (a Alias) IsNullable() bool

IsNullable returns whether an alias can be nulled or not

func (*Alias) SetSource

func (a *Alias) SetSource(t scanner.Type)

SetSource sets the scanner type source for a given protobuf named type.

func (*Alias) Source

func (a *Alias) Source() scanner.Type

Source returns the scanner source type for a given protobuf type

func (Alias) String

func (a Alias) String() string

type Basic

type Basic struct {
	Name string
	Src  scanner.Type
}

Basic is one of the basic types of protobuf.

func NewBasic

func NewBasic(name string) *Basic

NewBasic creates a new basic type given its name.

func (*Basic) IsNullable

func (b *Basic) IsNullable() bool

func (*Basic) SetSource

func (b *Basic) SetSource(t scanner.Type)

SetSource sets the scanner type source for a given protobuf basic type.

func (*Basic) Source

func (b *Basic) Source() scanner.Type

Source returns the scanner source type for a given protobuf basic type

func (Basic) String

func (b Basic) String() string

type Decorator

type Decorator func(*Package, *Message, *Field)

Decorator is run when a type is resolved. The decorator gets the package, message and field this type was resolved for. In some corner cases, a type might need to be resolved not for used in a field. In such cases, an empty structure is passed for each argument that does not exists.

func (Decorator) Run

func (d Decorator) Run(p *Package, m *Message, f *Field)

type Decorators

type Decorators []Decorator

Decorators is a collection of Decorator that simplifies running them all with a given set of options.

func CastToBasicType added in v1.3.1

func CastToBasicType(basic string) Decorators

func NewDecorators

func NewDecorators(fns ...Decorator) Decorators

func (Decorators) Run

func (ds Decorators) Run(p *Package, m *Message, f *Field)

type Enum

type Enum struct {
	Docs    []string
	Name    string
	Options Options
	Values  []*EnumValue
}

Enum is the representation of a protobuf enumeration.

type EnumValue

type EnumValue struct {
	Docs    []string
	Name    string
	Value   uint
	Options Options
}

EnumValue is a single value in an enumeration.

type Field

type Field struct {
	Docs     []string
	Name     string
	Pos      int
	Repeated bool
	Type     Type
	Options  Options
}

Field is the representation of a protobuf message field.

type Generator

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

Generator is in charge of generating the .proto files and write them to disk in a file at the given path.

func NewGenerator

func NewGenerator(basePath string) *Generator

NewGenerator creates a new Generator with the given base path.

func (*Generator) Generate

func (g *Generator) Generate(pkg *Package) error

Generate generates the proto3 .proto file of the given package and writes it to disk.

type LiteralValue

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

LiteralValue is a literal option value like true, false or a number.

func NewLiteralValue

func NewLiteralValue(val string) LiteralValue

NewLiteralValue creates a new literal option value.

func (LiteralValue) String

func (v LiteralValue) String() string

type Map

type Map struct {
	Key   Type
	Value Type
	Src   scanner.Type
}

Map is a key-value map type.

func NewMap

func NewMap(k, v Type) *Map

NewMap creates a new Map type with the key and value types given.

func (*Map) IsNullable

func (m *Map) IsNullable() bool

func (*Map) SetSource

func (m *Map) SetSource(t scanner.Type)

SetSource sets the scanner type source for a given protobuf map type.

func (*Map) Source

func (m *Map) Source() scanner.Type

Source returns the scanner source type for a given protobuf map type

func (Map) String

func (m Map) String() string

type Message

type Message struct {
	Docs     []string
	Name     string
	Reserved []uint
	Options  Options
	Fields   []*Field
}

Message is the representation of a Protobuf message.

func (*Message) Reserve

func (m *Message) Reserve(pos uint)

Reserve reserves a position in the message.

type Named

type Named struct {
	Package string
	Name    string
	// Generated reports whether the named type was generated by proteus or is
	// an user defined type.
	Generated bool
	Src       scanner.Type
}

Named is a type which has a name and is defined somewhere else, maybe even in another package.

func NewGeneratedNamed

func NewGeneratedNamed(pkg, name string) *Named

NewGeneratedNamed creates a new Named type generated by proteus given its package and name.

func NewNamed

func NewNamed(pkg, name string) *Named

NewNamed creates a new Named type given its package and name.

func (*Named) IsNullable

func (n *Named) IsNullable() bool

IsNullable returns whether the type can be nulled or not.

func (*Named) SetSource

func (n *Named) SetSource(t scanner.Type)

SetSource sets the scanner type source for a given protobuf named type.

func (*Named) Source

func (n *Named) Source() scanner.Type

Source returns the scanner source type for a given protobuf type

func (Named) String

func (n Named) String() string

type Option

type Option struct {
	Name  string
	Value OptionValue
}

Option name and value pair.

type OptionValue

type OptionValue interface {
	fmt.Stringer
	// contains filtered or unexported methods
}

OptionValue is the common interface for the value of an option, which can be a literal value (a number, true, etc) or a string value ("foo").

type Options

type Options map[string]OptionValue

Options are the set of options given to a field, message or enum value.

func (Options) Sorted

func (o Options) Sorted() []*Option

Sorted returns a sorted set of options.

type Package

type Package struct {
	Name     string
	Path     string
	Imports  []string
	Options  Options
	Messages []*Message
	Enums    []*Enum
	RPCs     []*RPC
}

Package represents an unique .proto file with its own package definition.

func (*Package) Import

func (p *Package) Import(typ *ProtoType)

Import tries to import the given protobuf type to the current package. If the type requires no import at all, nothing will be done.

func (*Package) ImportFromPath

func (p *Package) ImportFromPath(path string)

ImportFromPath adds a new import from a Go path.

func (*Package) ServiceName

func (p *Package) ServiceName() string

ServiceName returns the service name of the package.

type ProtoType

type ProtoType struct {
	Package string
	Basic   bool
	Name    string
	Import  string
	// GoImport represents the go package to import to use this type.
	GoImport string
	// Decorators define a set of function to apply to each field, message and
	// package that contain a field with this type.
	Decorators Decorators
	// Warn contains the warning message to show if this mapping happens. This string
	// is passed to fmt.Sprintf with the original type parameter.
	// For example, if a mapping is defined for Go type "A" to become "B" in
	// protobuf and the warning message "%s becomes B", then the reported message
	// will be "A becomes B"
	Warn string
}

ProtoType represents a protobuf type. It can optionally have a package and it may require an import to work.

func (*ProtoType) Decorate

func (pt *ProtoType) Decorate(p *Package, m *Message, f *Field)

func (*ProtoType) Type

func (t *ProtoType) Type() Type

Type returns the type representation of the protobuf type.

type RPC

type RPC struct {
	Docs []string
	Name string
	// Recv is the name of the receiver Go type. Empty if it's not a method.
	Recv string
	// Method is the name of the Go method or function.
	Method string
	// HasCtx reports whether the Go function accepts context.
	HasCtx bool
	// HasError reports whether the Go function returns an error.
	HasError bool
	// IsVariadic reports whether the Go function is variadic or not.
	IsVariadic bool
	Input      Type
	Output     Type
	Options    Options
}

RPC is a single exposed RPC method in the RPC service.

type StringValue

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

StringValue is a string option value.

func NewStringValue

func NewStringValue(val string) StringValue

NewStringValue creates a new string option value.

func (StringValue) String

func (v StringValue) String() string

type Transformer

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

Transformer is in charge of converting scanned Go entities to protobuf entities as well as mapping between Go and Protobuf types. Take into account that custom mappings are used first to check for the corresponding type mapping, and then the default mappings to give the user ability to override any kind of type.

func NewTransformer

func NewTransformer() *Transformer

NewTransformer creates a new transformer instance.

func (*Transformer) IsEnum

func (t *Transformer) IsEnum(pkg, name string) bool

IsEnum checks if the given pkg path and name is a known enum.

func (*Transformer) IsStruct

func (t *Transformer) IsStruct(pkg, name string) bool

IsStruct checks if the given pkg path and name is a known struct.

func (*Transformer) SetEnumSet

func (t *Transformer) SetEnumSet(ts TypeSet)

SetEnumSet sets the passed TypeSet as a known list of enums.

func (*Transformer) SetMappings

func (t *Transformer) SetMappings(m TypeMappings)

SetMappings will set the custom mappings of the transformer. If nil is provided, the change will be ignored.

func (*Transformer) SetStructSet

func (t *Transformer) SetStructSet(ts TypeSet)

SetStructSet sets the passed TypeSet as a known list of structs.

func (*Transformer) Transform

func (t *Transformer) Transform(p *scanner.Package) *Package

Transform converts a scanned package to a protobuf package.

type Type

type Type interface {
	fmt.Stringer

	SetSource(scanner.Type)
	Source() scanner.Type
	IsNullable() bool
	// contains filtered or unexported methods
}

Type is the common interface of all possible types, which are named types, maps and basic types.

type TypeMappings

type TypeMappings map[string]*ProtoType

TypeMappings is a mapping between Go types and protobuf types. The names of the Go types can have packages. For example: "time.Time" is a valid name. "foo.bar/baz.Qux" is a valid type name as well.

func (TypeMappings) ToGoOutPath

func (t TypeMappings) ToGoOutPath() string

ToGoOutPath returns the set of import mappings for the --go_out family of options. For more info see src-d/proteus#41

type TypeSet

type TypeSet map[string]map[string]struct{}

TypeSet represents a set of packages and their types.

Example
ts := NewTypeSet()

// Returns whether the item was added or not. If false, it means the item was
// already there.
res := ts.Add("gopkg.in/src-d/proteus.v1/protobuf", "TypeSet")
fmt.Println(res)

res = ts.Add("gopkg.in/src-d/proteus.v1/protobuf", "TypeSet")
fmt.Println(res)
fmt.Println(ts.Len())

fmt.Println(ts.Contains("gopkg.in/src-d/proteus.v1/protobuf", "TypeSet"))
fmt.Println(ts.Len())
Output:

true
false
1
true
1

func NewTypeSet

func NewTypeSet() TypeSet

NewTypeSet returns a new, empty TypeSet.

func (TypeSet) Add

func (ts TypeSet) Add(pkg, name string) bool

Add adds an element with the given package path and name to the set. Returns whether the element was or not added.

func (TypeSet) Contains

func (ts TypeSet) Contains(pkg, name string) bool

Contains checks if the given pkg and name is in the type set.

func (TypeSet) Len

func (ts TypeSet) Len() (c int)

Len returns the total number of elements in the set.

Jump to

Keyboard shortcuts

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