Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CustomField ¶
CustomField is a message field with an unchecked, custom type. This can be used to define fields that use imported types.
func (CustomField) Write ¶
func (c CustomField) Write() (string, error)
Write a CustomField as a string
type CustomMapField ¶
type CustomMapField struct { Name NameType Tag TagType Rule FieldRule Comment string KeyTyping FieldType ValueTyping string }
CustomMapField is a message field that maps between a built-in protobuf type as the key and a custom type as the value. https://developers.google.com/protocol-buffers/docs/proto3#maps
func (CustomMapField) Write ¶
func (c CustomMapField) Write() (string, error)
Write a CustomMapField as a string
type Enum ¶
Enum defines an enumeration type of a set of values. https://developers.google.com/protocol-buffers/docs/proto3#enum
type EnumValue ¶
EnumValue describes a single enumerated value within an enumeration. https://developers.google.com/protocol-buffers/docs/proto3#enum
type FieldRule ¶
type FieldRule uint8
FieldRule specifies additional rules (e.g. repeated) that can be set on a field
Rules that can be applied to message fields https://developers.google.com/protocol-buffers/docs/proto3#specifying-field-rules
type FieldType ¶
type FieldType uint8
FieldType applies to the data-type of a field value
const ( DoubleType FieldType = iota FloatType Int32Type Int64Type UInt32Type UInt64Type SInt32Type SInt64Type Fixed32Type Fixed64Type SFixed32Type SFixed64Type BoolType StringType BytesType )
Built-in field types https://developers.google.com/protocol-buffers/docs/proto3#scalar
type MapField ¶
type MapField struct { Name NameType Tag TagType Rule FieldRule Comment string KeyTyping FieldType ValueTyping FieldType }
MapField is a message field that maps built-in protobuf type as key-value pairs https://developers.google.com/protocol-buffers/docs/proto3#maps
type Message ¶
type Message struct { Name string Comment string Messages []Message ReservedValues []Reserved Fields []Field OneOfs []OneOf Enums []Enum }
Message is a single Protobuf message definition.
type OneOf ¶
OneOf defines a set of fields for which only the most-recently-set field will be used. https://developers.google.com/protocol-buffers/docs/proto3#oneof
type ReservedName ¶
type ReservedName struct {
Name NameType
}
ReservedName is a field name that is reserved within a message type and cannot be reused. https://developers.google.com/protocol-buffers/docs/proto3#reserved
func (ReservedName) Write ¶
func (r ReservedName) Write() (string, error)
Write a ReservedName as a string
type ReservedTagRange ¶
ReservedTagRange is a range of numeric tag values that are reserved within a message type and cannot be reused. https://developers.google.com/protocol-buffers/docs/proto3#reserved
func (ReservedTagRange) Validate ¶
func (r ReservedTagRange) Validate() error
Validate field attributes
func (ReservedTagRange) Write ¶
func (r ReservedTagRange) Write() (string, error)
Write a ReservedTagRange as a string
type ReservedTagValue ¶
type ReservedTagValue struct {
Tag TagType
}
ReservedTagValue is a single field tag value that is reserved within a message type and cannot be reused. https://developers.google.com/protocol-buffers/docs/proto3#reserved
func (ReservedTagValue) Validate ¶
func (r ReservedTagValue) Validate() error
Validate field attributes
func (ReservedTagValue) Write ¶
func (r ReservedTagValue) Write() (string, error)
Write a ReservedTagValue as a string
type ScalarField ¶
type ScalarField struct { Name NameType Tag TagType Rule FieldRule Comment string Typing FieldType }
ScalarField is a message field that uses a built-in protobuf type.
func (ScalarField) Write ¶
func (s ScalarField) Write() (string, error)
Write a ScalarField as a string
type Spec ¶
type Spec struct { FileComment string Package string // https://developers.google.com/protocol-buffers/docs/proto3#packages GoPackage string // https://developers.google.com/protocol-buffers/docs/reference/go-generated#package JavaPackage string // https://developers.google.com/protocol-buffers/docs/reference/java-generated#package Imports []ImportType // https://developers.google.com/protocol-buffers/docs/proto3#importing-definitions Messages []Message Enums []Enum }
Spec represents a top-level Protobuf specification.
func (*Spec) Write ¶
Write turns the specification into a string.
Example ¶
spec := &Spec{ FileComment: "DO NOT EDIT - File generated using protogen", Package: "foo", GoPackage: "example.com/foo", JavaPackage: "com.foo", Messages: []Message{ { Name: "Beacon", Comment: "Beacon Message containing event information", Messages: []Message{ { Name: "Event", ReservedValues: []Reserved{ ReservedTagValue{Tag: 1}, ReservedTagValue{Tag: 2}, ReservedTagValue{Tag: 3}, ReservedTagRange{LowerTag: 6, UpperTag: 9}, }, Fields: []Field{ CustomField{Name: "Habitat", Typing: "string", Tag: 10, Rule: Repeated, Comment: "What am I?"}, ScalarField{Name: "Continent", Typing: StringType, Tag: 11, Comment: "Where am I?"}, MapField{Name: "LanguageMap", KeyTyping: StringType, ValueTyping: StringType, Tag: 12, Comment: "Super essential"}, }, }, }, ReservedValues: []Reserved{ ReservedTagValue{Tag: 1}, ReservedTagValue{Tag: 2}, ReservedTagValue{Tag: 3}, ReservedTagRange{LowerTag: 6, UpperTag: 9}, }, Fields: []Field{ CustomField{Name: "Habitat", Typing: "string", Tag: 20, Comment: "What am I?"}, ScalarField{Name: "Continent", Typing: StringType, Tag: 21, Rule: Repeated, Comment: "Where am I?"}, MapField{Name: "LanguageMap", KeyTyping: StringType, ValueTyping: StringType, Tag: 22, Comment: "Super essential"}, CustomMapField{Name: "CustomMap", KeyTyping: StringType, ValueTyping: "Event", Tag: 23}, }, OneOfs: []OneOf{ { Name: "test_oneof", Comment: "Can have a name or sub-message, but not both", Fields: []Field{ ScalarField{Name: "name", Typing: StringType, Tag: 24, Comment: "Name"}, CustomField{Name: "sub_message", Typing: "Event", Tag: 25, Comment: "Sub-Message"}, }, }, }, Enums: []Enum{ { Name: "Country", Comment: "Country code", Values: []EnumValue{ {Name: "CA", Tag: 1, Comment: "Canada"}, {Name: "US", Tag: 0}, {Name: "MX", Tag: 3, Comment: "Mexico"}, {Name: "GB", Tag: 2, Comment: "Great Britain"}, }, }, { Name: "PlaybackState", AllowAlias: true, Values: []EnumValue{ {Name: "Playing", Tag: 1}, {Name: "Waiting", Tag: 0}, {Name: "Stopped", Tag: 2}, {Name: "Started", Tag: 1}, }, }, }, }, }, } s, err := spec.Write() if err != nil { fmt.Println(err) } else { fmt.Println(s) }
Output: // DO NOT EDIT - File generated using protogen syntax = "proto3"; package foo; option go_package = "example.com/foo"; option java_package = "com.foo"; // Beacon Message containing event information message Beacon { message Event { reserved 1; reserved 2; reserved 3; reserved 6 to 9; repeated string Habitat = 10; // What am I? string Continent = 11; // Where am I? map<string, string> LanguageMap = 12; // Super essential } // Country code enum Country { US = 0; CA = 1; // Canada GB = 2; // Great Britain MX = 3; // Mexico } enum PlaybackState { option allow_alias = true; Waiting = 0; Playing = 1; Started = 1; Stopped = 2; } reserved 1; reserved 2; reserved 3; reserved 6 to 9; string Habitat = 20; // What am I? repeated string Continent = 21; // Where am I? map<string, string> LanguageMap = 22; // Super essential map<string, Event> CustomMap = 23; // Can have a name or sub-message, but not both oneof test_oneof { string name = 24; // Name Event sub_message = 25; // Sub-Message } }