gensupport

package
v1.2.5 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2024 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PrefixCreate = "Create"
	PrefixDelete = "Delete"
	PrefixUpdate = "Update"
	PrefixAdd    = "Add"
	PrefixRemove = "Remove"
)
View Source
const AutoGenComment = "// Auto-generated code: DO NOT EDIT"
View Source
const DeletePrepareName = "delete_prepare"
View Source
const (
	UpdateListActionField = "UpdateListAction"
)

Variables

This section is empty.

Functions

func ClientStreaming

func ClientStreaming(method *descriptor.MethodDescriptorProto) bool

func CollectMethodGroups

func CollectMethodGroups(g *generator.Generator, service *descriptor.ServiceDescriptorProto, groups map[string]*MethodGroup)

group methods by input type

func ConvTypeName

func ConvTypeName(typeName string) (string, string)

ConvTypeNames takes a protoc format type name (as used in Fields and Methods) and returns the package plus a Go-ified type name. The protoc format is .package.Name or .package.Parent.Name for nested types.

func DottedName

func DottedName(packageName string, names ...string) string

DottedName gets the proto-style name. For normal objects, name should be singular. For nested objects, there will be multiple names to include parent objects.

func FieldTypeNameValid

func FieldTypeNameValid(field *descriptor.FieldDescriptorProto) bool

Field type name is only set if type is ENUM, MESSAGE, or GROUP. Otherwise it may be an invalid pointer (not even nil)

func FindField

func FindField(message *descriptor.DescriptorProto, camelCaseName string) *descriptor.FieldDescriptorProto

func FindStringExtension

func FindStringExtension(pb proto.Message, extension *proto.ExtensionDesc) (string, bool)

func GenerateHideTags

func GenerateHideTags(g *generator.Generator, support *PluginSupport, desc *generator.Descriptor)

GenerateHideTags requires "strings" and "cli" packages to be imported.

func GenerateMessageArgs

func GenerateMessageArgs(g *generator.Generator, support *PluginSupport, desc *generator.Descriptor, methodGroup *MethodGroup, prefixMessageToAlias bool, count int)

func GenerateMethodArgs

func GenerateMethodArgs(g *generator.Generator, support *PluginSupport, method *descriptor.MethodDescriptorProto, methodGroups map[string]*MethodGroup, prefixMessageToAlias bool, count int)

func GetAlias

func GetAlias(message *descriptor.DescriptorProto) string

func GetAllMethodGroups

func GetAllMethodGroups(g *generator.Generator, support *PluginSupport) map[string]*MethodGroup

func GetAlsoRequired

func GetAlsoRequired(message *descriptor.DescriptorProto, method *descriptor.MethodDescriptorProto) string

func GetCamelCasePrefix

func GetCamelCasePrefix(name string) string

func GetCustomKeyType

func GetCustomKeyType(message *descriptor.DescriptorProto) string

func GetCustomYamlJsonMarshalers

func GetCustomYamlJsonMarshalers(message *descriptor.DescriptorProto) bool

func GetDeletePrepareField

func GetDeletePrepareField(g *generator.Generator, desc *generator.Descriptor) string

GetDeletePrepareField gets the hierarchical camel case name of the delete prepare field in the descriptor, or blank if not found.

func GetDesc

func GetDesc(g *generator.Generator, typeName string) *generator.Descriptor

GetDesc returns the Descriptor based on the protoc type name referenced in Fields and Methods.

func GetDescKeyName

func GetDescKeyName(g *generator.Generator, msg *generator.Descriptor) string

func GetE2edata

func GetE2edata(message *descriptor.DescriptorProto) bool

func GetEnumBackend

func GetEnumBackend(enumVal *descriptor.EnumValueDescriptorProto) bool

func GetEnumCommonPrefix

func GetEnumCommonPrefix(en *descriptor.EnumDescriptorProto) string

func GetEnumDesc

func GetEnumDesc(g *generator.Generator, typeName string) *generator.EnumDescriptor

GetEnumDesc returns the EnumDescriptor based on the protoc type name referenced in Fields.

func GetEnumName

func GetEnumName(en *generator.EnumDescriptor) string

GetEnumName returns the hierarchical type name of the Enum without package

func GetFirstFile

func GetFirstFile(gen *generator.Generator) string

func GetGenerateCud

func GetGenerateCud(message *descriptor.DescriptorProto) bool

func GetGenerateCudStreamout

func GetGenerateCudStreamout(message *descriptor.DescriptorProto) bool

func GetHideTag

func GetHideTag(field *descriptor.FieldDescriptorProto) string

func GetIgnoreRefersTo

func GetIgnoreRefersTo(message *descriptor.DescriptorProto) bool

func GetInputMessages

func GetInputMessages(g *generator.Generator, support *PluginSupport) map[string]*MessageInfo

Get all message types that are used as input to a method in any of the files. Also track which service api group each message type is in to avoid naming conflicts.

func GetInputRequired

func GetInputRequired(method *descriptor.MethodDescriptorProto) bool

func GetInternalApi

func GetInternalApi(service *descriptor.ServiceDescriptorProto) bool

func GetLabelString

func GetLabelString(label *descriptor.FieldDescriptorProto_Label) string

func GetMsgName

func GetMsgName(msg *generator.Descriptor) string

GetMsgName returns the hierarchical type name of the Message without package

func GetNonStandardShow

func GetNonStandardShow(method *descriptor.MethodDescriptorProto) bool

func GetObjAndKey

func GetObjAndKey(message *descriptor.DescriptorProto) bool

func GetPackageDesc

func GetPackageDesc(g *generator.Generator, file *generator.FileDescriptor, name string) *generator.Descriptor

This assumes the Message is in the same package as the given file, but possibly in a different file.

func GetRedisApi

func GetRedisApi(service *descriptor.ServiceDescriptorProto) bool

func GetRefersTo

func GetRefersTo(field *descriptor.FieldDescriptorProto) string

func GetSingularData

func GetSingularData(message *descriptor.DescriptorProto) bool

func GetStreamOutIncremental

func GetStreamOutIncremental(method *descriptor.MethodDescriptorProto) bool

func GetStringExtension

func GetStringExtension(pb proto.Message, extension *proto.ExtensionDesc, def string) string

func GetStringKeyField

func GetStringKeyField(message *descriptor.DescriptorProto) string

func GetTracksRefersTo

func GetTracksRefersTo(message *descriptor.DescriptorProto) bool

func GetTracksRefsBy

func GetTracksRefsBy(field *descriptor.FieldDescriptorProto) string

func HasExtension

func HasExtension(pb proto.Message, extension *proto.ExtensionDesc) bool

func HasGrpcFields

func HasGrpcFields(message *descriptor.DescriptorProto) bool

func HasHideTags

func HasHideTags(g *generator.Generator, desc *generator.Descriptor, hideTag *proto.ExtensionDesc, visited []*generator.Descriptor) bool

func HasMethodArgs

func HasMethodArgs(method *descriptor.MethodDescriptorProto) bool

func IsRepeated

func IsRepeated(field *descriptor.FieldDescriptorProto) bool

func IsShow

func IsShow(method *descriptor.MethodDescriptorProto) bool

func Pnil

func Pnil(g *generator.Generator, str ...interface{})

Pnil is a copy of generator.P(), but allows nil objects to be passed in. The normal generator.P() will crash on nil objects.

func RemoveNewLines

func RemoveNewLines(r rune) rune

func RunMain

func RunMain(pkg, fileSuffix string, p generator.Plugin, support *PluginSupport)

RunMain should be called by the main function with the plugin that will be used to generate the code. The pkg string is the name of the package used in the generated files. The fileSuffix will replace .pb.go as the generated file suffix. The target directory of the generated files is controlled by the call to protoc, and cannot be manipulated here. If a PluginSupport is provided, it will be initialized so that support functions can be used by the plugin.

func RunParseCheck

func RunParseCheck(g *generator.Generator, file *generator.FileDescriptor)

RunParseCheck will run the parser to check for parse errors in the generated code. While the gogo generator does this as well, if there is a failure it does not generate line numbers, which makes it very difficult to locate the line with the error. This function can be called at the end of the Generate() function to check the generated code. At that point the generated code will be missing the imports and some of the other header code generated by the gogo generator code, but that is the last place it can be called before the gogo generator parser runs.

func ServerStreaming

func ServerStreaming(method *descriptor.MethodDescriptorProto) bool

func WasVisited

func WasVisited(desc *generator.Descriptor, visited []*generator.Descriptor) bool

Types

type Arg

type Arg struct {
	Name    string
	Comment string
}

func GetArgs

func GetArgs(g *generator.Generator, support *PluginSupport, parents []string, desc *generator.Descriptor, isUpdate bool) ([]Arg, map[string]string)

type MapType

type MapType struct {
	KeyField     *descriptor.FieldDescriptorProto
	ValField     *descriptor.FieldDescriptorProto
	KeyType      string
	ValType      string
	ValIsMessage bool
	FlagType     string
	DefValue     string
}

type MessageInfo

type MessageInfo struct {
	Desc     *generator.Descriptor
	Services map[string]*descriptor.ServiceDescriptorProto
}

type MethodGroup

type MethodGroup struct {
	ServiceName  string
	MethodInfos  []*MethodInfo
	InType       string
	In           *generator.Descriptor
	HasStream    bool
	HasUpdate    bool
	HasMc2Api    bool
	HasShow      bool
	SingularData bool
	Suffix       string
}

func (*MethodGroup) ApiName

func (m *MethodGroup) ApiName() string

type MethodInfo

type MethodInfo struct {
	Name     string
	Prefix   string
	Stream   bool
	Mc2Api   bool
	IsShow   bool
	IsUpdate bool
	Method   *descriptor.MethodDescriptorProto
	Out      *generator.Descriptor
	OutType  string
}

type Op

type Op func(opts *Options)

func WithNoImport

func WithNoImport() Op

type Options

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

type PluginSupport

type PluginSupport struct {
	// ProtoFiles are all of the proto files that support or possibly
	// are dependencies of the proto file being generated
	ProtoFiles []*descriptor.FileDescriptorProto
	// ProtoFilesGen are all of the proto files in the request to
	// generate.
	ProtoFilesGen map[string]struct{}
	// MessageTypesGen are all the message types that are defined
	// in this package.
	MessageTypesGen map[string]struct{}
	// Map of all packages used from calls to FQTypeName
	// Can be used to generate imports.
	UsedPkgs map[string]*UsedPkg
	// Current package, used for plugins adding code to .pb.go.
	// For plugins that are generating files to separate directory
	// and package, this is not needed.
	PbGoPackage string
	// Lookup by file and location of comments
	Comments map[string]map[string]*descriptor.SourceCodeInfo_Location
	// When generating files from protos that reference the
	// originally generated objects in the .pb.go files, this is
	// the import path.
	SrcImport string
}

PluginSupport provides support data and functions for the generator.Plugin struct that will generate the code. The generator.Plugin that will use it should include an instance of it and pass it to the RunMain function.

func (*PluginSupport) DumpProtoFiles

func (s *PluginSupport) DumpProtoFiles(g *generator.Generator)

DumpProtoFiles will dump as comments the proto data passed in by protoc. Note this does not dump wrapped data that the generator builds on top of the proto data.

This is useful to help understand what exactly is the data that is passed in and available to the generator. This function skips the descriptor.proto file because the format definitions are not useful and quite lengthly.

func (*PluginSupport) FQTypeName

func (s *PluginSupport) FQTypeName(g *generator.Generator, obj generator.Object, ops ...Op) string

FQTypeName returns the fully qualified type name (includes package and parents for nested definitions) for the given generator.Object. This also adds the package to a list of used packages for PrintUsedImports().

func (*PluginSupport) GatherRefData

func (s *PluginSupport) GatherRefData(g *generator.Generator) *RefData

func (*PluginSupport) GenFile

func (s *PluginSupport) GenFile(filename string) bool

func (*PluginSupport) GetComments

func (s *PluginSupport) GetComments(fileName string, path string) string

See generator.GetComments(). The path is a comma separated list of integers.

func (*PluginSupport) GetGeneratorFiles

func (s *PluginSupport) GetGeneratorFiles(g *generator.Generator) []*generator.FileDescriptor

GetGeneratorFiles gets the wrapped files of the generator. This requires that Generator.WrapTypes() and Generator.BuildTypeNameMap() have already been called. Generator doesn't provide a way to access the wrapped files directly, which are useful if generating something besides golang. This gets the files in a back-door kind of way.

func (*PluginSupport) GetMapType

func (s *PluginSupport) GetMapType(g *generator.Generator, field *descriptor.FieldDescriptorProto, ops ...Op) *MapType

func (*PluginSupport) GetMessageKeyType

func (s *PluginSupport) GetMessageKeyType(g *generator.Generator, desc *generator.Descriptor) (string, error)

func (*PluginSupport) GetPackage

func (s *PluginSupport) GetPackage(g *generator.Generator, obj generator.Object, ops ...Op) string

func (*PluginSupport) GetPackageName

func (s *PluginSupport) GetPackageName(g *generator.Generator, obj generator.Object) string

func (*PluginSupport) GoType

Similar to generator.GoType(), but does not prepend any array or pointer references (* or &).

func (*PluginSupport) Init

func (*PluginSupport) InitFile

func (s *PluginSupport) InitFile()

InitFile should be called by the plugin whenever a new file is being generated.

func (*PluginSupport) PrintUsedImports

func (s *PluginSupport) PrintUsedImports(g *generator.Generator)

PrintUsedImports will print imports based on calls to FQTypeName() and RegisterUsedPkg().

func (*PluginSupport) PrintUsedImportsPlugin

func (s *PluginSupport) PrintUsedImportsPlugin(g *generator.Generator, handledFiles map[string]struct{})

PrintUsedImportsPlugin is like PrintUsedImports, but it avoids a bug in the generator code that doesn't register printed imports for dependent files (i.e. doesn't use generator.PrintImport() when printing the import). This only happens where we're being used as a plugin alongside the go code generator (i.e. as part of .pb.go).

func (*PluginSupport) SetPbGoPackage

func (s *PluginSupport) SetPbGoPackage(pkgName string)

SetPbGoPackage should be called when using support to help generate .pb.go, with the current package, to prevent generating an import for the current package.

type RefByField

type RefByField struct {
	Desc     *descriptor.FieldDescriptorProto
	HierName string
	InKey    bool
}

Particular field in the RefByObj that is making reference.

type RefByFieldTo

type RefByFieldTo struct {
	Field RefByField
	To    RefToObj
}

ByField and ToObj

type RefByGroup

type RefByGroup struct {
	By  RefByObj
	Tos []RefByFieldTo
}

All references made by a particular refBy object

type RefByObj

type RefByObj struct {
	Type        string
	TypeDesc    *generator.Descriptor
	KeyType     string
	GenerateCud bool
}

RefBy is the object that is referring to another object. RefBy -> points to RefTo.

type RefByObjField

type RefByObjField struct {
	By    RefByObj
	Field RefByField
}

ByField and ByObj

type RefData

type RefData struct {
	RefBys   map[string]*RefByGroup // key is RefByObj type
	RefTos   map[string]*RefToGroup // key is RefToObj type
	Trackers map[string]*RefTracker // key is Tracker type
}

Handle references between protobuffer objects (Messages). For example, suppose object AppInst refers to object App. I.e., App must exist before AppInst (an instance of that App) can be created. So "AppInst" is the RefByObj, and "App" is the RefToObj.

func (*RefData) BuildRefData

func (s *RefData) BuildRefData(g *generator.Generator, file *generator.FileDescriptor, top *generator.Descriptor, parents []string, message *generator.Descriptor, visited []*generator.Descriptor, inKey bool)

func (*RefData) BuildTrackers

func (s *RefData) BuildTrackers(g *generator.Generator, file *generator.FileDescriptor, desc *generator.Descriptor)

type RefToGroup

type RefToGroup struct {
	To  RefToObj
	Bys []RefByObjField
}

All references to a particular refTo object

type RefToObj

type RefToObj struct {
	Type        string
	TypeDesc    *generator.Descriptor
	KeyType     string
	GenerateCud bool
}

RefTo is the object that another object is referring to. RefTo <- is pointed to by RefBy.

type RefTracker

type RefTracker struct {
	Type     string // object tracking refs
	TypeDesc *generator.Descriptor
	KeyType  string
	To       RefToObj        // object it's tracking refs for
	Bys      []RefByObjField // objects that have references
}

RefTracker is an object that tracks and records references in the database

type UsedPkg

type UsedPkg struct {
	Pkg        string
	ImportPath string
	File       *generator.FileDescriptor
}

Jump to

Keyboard shortcuts

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