proto3

package
v0.0.0-...-e70ed56 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2019 License: Apache-2.0 Imports: 4 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CustomField

type CustomField struct {
	Name    NameType
	Tag     TagType
	Rule    FieldRule
	Comment string
	Typing  string
}

CustomField is a message field with an unchecked, custom type. This can be used to define fields that use imported types.

func (CustomField) Validate

func (c CustomField) Validate() error

Validate field attributes

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) Validate

func (c CustomMapField) Validate() error

Validate field attributes

func (CustomMapField) Write

func (c CustomMapField) Write() (string, error)

Write a CustomMapField as a string

type Enum

type Enum struct {
	Name       NameType
	Values     []EnumValue
	AllowAlias bool
	Comment    string
}

Enum defines an enumeration type of a set of values. https://developers.google.com/protocol-buffers/docs/proto3#enum

func (Enum) Len

func (e Enum) Len() int

Len reports the number of enum values.

func (Enum) Less

func (e Enum) Less(i, j int) bool

Less returns true iff the tag at the first position is less than the tag at the second position.

func (Enum) Swap

func (e Enum) Swap(i, j int)

Swap entries in the enum at the given positions.

func (*Enum) Validate

func (e *Enum) Validate() error

Validate enum attributes

func (Enum) Write

func (e Enum) Write(level int) (string, error)

Write a CustomMapField as a string

type EnumValue

type EnumValue struct {
	Name    NameType
	Tag     TagType
	Comment string
}

EnumValue describes a single enumerated value within an enumeration. https://developers.google.com/protocol-buffers/docs/proto3#enum

type Field

type Field interface {
	Validate() error
	Write() (string, error)
}

Field describes a Protobuf message field.

type FieldRule

type FieldRule uint8

FieldRule specifies additional rules (e.g. repeated) that can be set on a field

const (
	None FieldRule = iota
	Repeated
)

Rules that can be applied to message fields https://developers.google.com/protocol-buffers/docs/proto3#specifying-field-rules

func (*FieldRule) Write

func (f *FieldRule) Write() string

Write a FieldRule as a string

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

func (*FieldType) Write

func (f *FieldType) Write() string

Write a FieldType as a string

type ImportType

type ImportType string

ImportType applies to a package import statement

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

func (MapField) Validate

func (m MapField) Validate() error

Validate map attributes

func (MapField) Write

func (m MapField) Write() (string, error)

Write a MapField as a string

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.

func (Message) Validate

func (m Message) Validate() error

Validate the attributes of a message, including all children that can be validated individually.

func (*Message) Write

func (m *Message) Write(level int) (string, error)

Write the message specification as a string at a given indentation level.

type NameType

type NameType string

NameType applies to a field name

type OneOf

type OneOf struct {
	Name    NameType
	Fields  []Field
	Comment string
}

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

func (OneOf) Validate

func (o OneOf) Validate() error

Validate oneof attributes

func (OneOf) Write

func (o OneOf) Write(level int) (string, error)

type Reserved

type Reserved interface {
	Validate() error
	Write() (string, error)
}

Reserved describes a tag that can be written to a Protobuf.

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) Validate

func (r ReservedName) Validate() error

Validate field attributes

func (ReservedName) Write

func (r ReservedName) Write() (string, error)

Write a ReservedName as a string

type ReservedTagRange

type ReservedTagRange struct {
	LowerTag TagType
	UpperTag TagType
}

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) Validate

func (s ScalarField) Validate() error

Validate field attributes

func (ScalarField) Write

func (s ScalarField) Write() (string, error)

Write a ScalarField as a string

type Spec

Spec represents a top-level Protobuf specification.

func (*Spec) Validate

func (s *Spec) Validate() error

Validate spec

func (*Spec) Write

func (s *Spec) Write() (string, error)

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
  }
}

type TagType

type TagType uint8

TagType applies to a field tag value

Jump to

Keyboard shortcuts

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