generator

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2019 License: BSD-3-Clause Imports: 17 Imported by: 0

Documentation

Overview

Package generator contains functions for generating transform functions.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNilOptions appears when object has no options.
	//
	// Options defined as
	//
	// messages MessageName {
	//	option (package.message_option_name) = value;
	//	int64 first_field = 1 [(package.field_option_name = value)];
	// }
	ErrNilOptions = errors.New("options are nil")

	// ErrFileSkipped is returned when .proto file has not go_models_file_path
	// option.
	ErrFileSkipped = errors.New("files was skipped")
)

Functions

func OptHelpers

func OptHelpers(packageName string) string

OptHelpers returns file content with optional functions for using options with transformations.

func ProcessFile

func ProcessFile(f *descriptor.FileDescriptorProto, packageName, helperPackageName *string, messages MessageOptionList, debug, usePackageInPath bool) (string, string, error)

ProcessFile processes .proto file and returns content as a string.

func SetParameters

func SetParameters(setter Setter, param *string) error

SetParameters accepts flag.CommandLine as a setter, string with params from protobuf compile input. Functions decodes params and adds it as command line flags.

func Version

func Version() string

Version returns current generator version.

Types

type Data

type Data struct {
	// Prefix for source structure.
	SrcPref string
	// Source structure name.
	Src string
	// Left (source) part of transform function name.
	SrcFn string
	// Contains "*" if source structure is a pointer.
	SrcPointer string
	// Prefix for destination structure.
	DstPref string
	// Destination structure name.
	Dst string
	// Right (destination) part of transform function name.
	DstFn string
	// Contains "*" if destination structure is a pointer.
	DstPointer string
	// Field list of structure.
	Fields []Field
	// If true Fields.GoToProtoType will be used instead of Fileds.ProtoToGoType.
	Swapped bool
	// Is not empty, package name will be used as prefix for helper functions,
	// such as TimeToNullTime, StringToStirnPtr etc.
	HelperPackage string
	// Ptr is used in template for indication of pointer usage.
	Ptr bool
}

Data contains data for fill out template.

func (Data) P

func (d Data) P(t bool) Data

P sets Ptr flag of Data structure. Used inside template. Should be exported in template case.

type Field

type Field struct {
	// Field name in Go structure.
	Name string
	// Field name in .proto file.
	ProtoName string
	// Field type in .proto file.
	ProtoType string
	// Name of function which is used for converting proto field into Go one.
	ProtoToGoType string
	// Name of function which is used for converting Go field into proto one.
	GoToProtoType string
	// True if field in model is a pointer.
	GoIsPointer bool
	// True if field in .proto file has an option gogoproto.nullable = false
	ProtoIsPointer bool
	// It true, field GoToProtoType and ProtoToGoType functions will be used
	// with prefix.
	UsePackage bool
	OneofDecl  string
	Opts       string
}

Field represents one structure field.

func (Field) IsOneof

func (f Field) IsOneof() bool

IsOneof returns true if Field has non-empty OneOf declaration.

type MessageOption

type MessageOption interface {
	// Returns model name from go_struct option.
	Target() string
	// Full returns FQTN of proto type, such as "google.protobuf.Timestamp" and
	// so on.
	Full() string
	// If true, proto message has not target struct, i.e. go_struct option is
	// empty or not found.
	Omitted() bool
	// Returns Oneof message name.
	OneofDecl() string
}

MessageOption represents protobuf message options.

type MessageOptionList

type MessageOptionList map[string]MessageOption

MessageOptionList is a list of proto message option. Map key is a message name with Full Qualified Type Name (FQTN), format like ".google.protobuf.Timestamp".

func CollectAllMessages

func CollectAllMessages(req plugin.CodeGeneratorRequest) (MessageOptionList, error)

CollectAllMessages processes all files passed within plugin request to collect info about all incoming messages. Generator should have information about all messages regardless have those messages transformer options or haven't.

func (MessageOptionList) String

func (sol MessageOptionList) String() string

type OneofData

type OneofData struct {
	// Package name form proto file.
	ProtoPackage string
	// Custom data type from proto file which is used for define oneof field.
	ProtoType string
	// Go destination type.
	GoType    string
	Decl      string
	OneofDecl string
}

OneofData contains info about OneOf fields.

message TheOne{  <= OneofType
  oneof strint { <= OneofDecl
    string string_value = 1;
    int64 int64_value = 2;
  }
}

type Setter

type Setter interface {
	Set(string, string) error
}

Setter is a interface which allows to set map key with value. Both are of type string.

type WriteStringer

type WriteStringer interface {
	io.Writer
	fmt.Stringer
}

WriteStringer exposes two methods: Write(p []byte) (n int, err error) String() string.

Jump to

Keyboard shortcuts

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