Documentation ¶
Index ¶
- Variables
- type Alias
- type Basic
- type Decorator
- type Decorators
- type Enum
- type EnumValue
- type Field
- type Generator
- type LiteralValue
- type Map
- type Message
- type Named
- type Option
- type OptionValue
- type Options
- type Package
- type ProtoType
- type RPC
- type StringValue
- type Transformer
- func (t *Transformer) IsEnum(pkg, name string) bool
- func (t *Transformer) IsStruct(pkg, name string) bool
- func (t *Transformer) SetEnumSet(ts TypeSet)
- func (t *Transformer) SetMappings(m TypeMappings)
- func (t *Transformer) SetStructSet(ts TypeSet)
- func (t *Transformer) Transform(p *scanner.Package) *Package
- type Type
- type TypeMappings
- type TypeSet
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
Alias represent a type declaration from one type to another.
func (Alias) IsNullable ¶
IsNullable returns whether an alias can be nulled or not
type Basic ¶
Basic is one of the basic types of protobuf.
func (*Basic) IsNullable ¶
type Decorator ¶
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.
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
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 ¶
NewGenerator creates a new Generator with the given base path.
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 ¶
Map is a key-value map type.
func (*Map) IsNullable ¶
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 ¶
NewGeneratedNamed creates a new Named type generated by proteus given its package and name.
func (*Named) IsNullable ¶
IsNullable returns whether the type can be nulled or not.
type OptionValue ¶
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.
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 ¶
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 ¶
ImportFromPath adds a new import from a Go path.
func (*Package) ServiceName ¶
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.
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.
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 ¶
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 ¶
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 (TypeSet) Add ¶
Add adds an element with the given package path and name to the set. Returns whether the element was or not added.