protogen
packageThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
Documentation¶
Overview¶
Package protogen provides support for writing protoc plugins.
Plugins for protoc, the Protocol Buffer compiler,are programs which read apluginpb.CodeGeneratorRequest message from standard inputand write apluginpb.CodeGeneratorResponse message to standard output.This package provides support for writing plugins which generate Go code.
Index¶
- type Annotation
- type CommentSet
- type Comments
- type Enum
- type EnumValue
- type Extension
- type Field
- type File
- type GeneratedFile
- func (g *GeneratedFile) Annotate(symbol string, loc Location)deprecated
- func (g *GeneratedFile) AnnotateSymbol(symbol string, info Annotation)
- func (g *GeneratedFile) Content() ([]byte, error)
- func (g *GeneratedFile) Import(importPath GoImportPath)
- func (g *GeneratedFile) InternalStripForEditionsDiff() bool
- func (g *GeneratedFile) P(v ...any)
- func (g *GeneratedFile) QualifiedGoIdent(ident GoIdent) string
- func (g *GeneratedFile) Skip()
- func (g *GeneratedFile) Unskip()
- func (g *GeneratedFile) Write(p []byte) (n int, err error)
- type GoIdent
- type GoImportPath
- type GoPackageName
- type Location
- type Message
- type Method
- type Oneof
- type Options
- type Plugin
- type Service
Constants¶
This section is empty.
Variables¶
This section is empty.
Functions¶
This section is empty.
Types¶
typeAnnotation¶added inv1.31.0
type Annotation struct {// Location is the source .proto file for the element.LocationLocation// Semantic is the symbol's effect on the element in the original .proto file.Semantic *descriptorpb.GeneratedCodeInfo_Annotation_Semantic}An Annotation provides semantic detail for a generated proto element.
See the google.protobuf.GeneratedCodeInfo.Annotation documentation indescriptor.proto for details.
typeCommentSet¶
CommentSet is a set of leading and trailing comments associatedwith a .proto descriptor declaration.
typeEnum¶
type Enum struct {Descprotoreflect.EnumDescriptorGoIdentGoIdent// name of the generated Go typeValues []*EnumValue// enum value declarationsLocationLocation// location of this enumCommentsCommentSet// comments associated with this enum}An Enum describes an enum.
typeEnumValue¶
type EnumValue struct {Descprotoreflect.EnumValueDescriptorGoIdentGoIdent// name of the generated Go declaration// PrefixedAlias is usually empty, except when the strip_enum_prefix feature// for this enum was set to GENERATE_BOTH, in which case PrefixedAlias holds// the old name which should be generated as an alias for the new name for// compatibility.PrefixedAliasGoIdentParent *Enum// enum in which this value is declaredLocationLocation// location of this enum valueCommentsCommentSet// comments associated with this enum value}An EnumValue describes an enum value.
typeField¶
type Field struct {Descprotoreflect.FieldDescriptor// GoName is the base name of this field's Go field and methods.// For code generated by protoc-gen-go, this means a field named// '{{GoName}}' and a getter method named 'Get{{GoName}}'.GoNamestring// e.g., "FieldName"// GoIdent is the base name of a top-level declaration for this field.// For code generated by protoc-gen-go, this means a wrapper type named// '{{GoIdent}}' for members fields of a oneof, and a variable named// 'E_{{GoIdent}}' for extension fields.GoIdentGoIdent// e.g., "MessageName_FieldName"Parent *Message// message in which this field is declared; nil if top-level extensionOneof *Oneof// containing oneof; nil if not part of a oneofExtendee *Message// extended message for extension fields; nil otherwiseEnum *Enum// type for enum fields; nil otherwiseMessage *Message// type for message or group fields; nil otherwiseLocationLocation// location of this fieldCommentsCommentSet// comments associated with this field// contains filtered or unexported fields}A Field describes a message field.
func (*Field)BuilderFieldName¶added inv1.36.0
BuilderFieldName returns the name of this field in the corresponding _builderstruct.
func (*Field)MethodName¶added inv1.36.0
MethodName returns the (possibly mangled) name of the generated accessormethod, along with the backwards-compatible name (if needed).
method must be one of Get, Set, Has, Clear. MethodName panics otherwise.
typeFile¶
type File struct {Descprotoreflect.FileDescriptorProto *descriptorpb.FileDescriptorProtoGoDescriptorIdentGoIdent// name of Go variable for the file descriptorGoPackageNameGoPackageName// name of this file's Go packageGoImportPathGoImportPath// import path of this file's Go packageEnums []*Enum// top-level enum declarationsMessages []*Message// top-level message declarationsExtensions []*Extension// top-level extension declarationsServices []*Service// top-level service declarationsGeneratebool// true if we should generate code for this file// GeneratedFilenamePrefix is used to construct filenames for generated// files associated with this source file.//// For example, the source file "dir/foo.proto" might have a filename prefix// of "dir/foo". Appending ".pb.go" produces an output file of "dir/foo.pb.go".GeneratedFilenamePrefixstring// APILevel specifies which API to generate. One of OPEN, HYBRID or OPAQUE.APILevelgofeaturespb.GoFeatures_APILevel// contains filtered or unexported fields}A File describes a .proto source file.
typeGeneratedFile¶
type GeneratedFile struct {// contains filtered or unexported fields}A GeneratedFile is a generated file.
func (*GeneratedFile)Annotatedeprecated
func (g *GeneratedFile) Annotate(symbolstring, locLocation)
Annotate associates a symbol in a generated Go file with a location in asource .proto file.
The symbol may refer to a type, constant, variable, function, method, orstruct field. The "T.sel" syntax is used to identify the method or field'sel' on type 'T'.
Deprecated: Use theGeneratedFile.AnnotateSymbol method instead.
func (*GeneratedFile)AnnotateSymbol¶added inv1.31.0
func (g *GeneratedFile) AnnotateSymbol(symbolstring, infoAnnotation)
AnnotateSymbol associates a symbol in a generated Go file with a locationin a source .proto file and a semantic type.
The symbol may refer to a type, constant, variable, function, method, orstruct field. The "T.sel" syntax is used to identify the method or field'sel' on type 'T'.
func (*GeneratedFile)Content¶
func (g *GeneratedFile) Content() ([]byte,error)
Content returns the contents of the generated file.
func (*GeneratedFile)Import¶
func (g *GeneratedFile) Import(importPathGoImportPath)
Import ensures a package is imported by the generated file.
Packages referenced byGeneratedFile.QualifiedGoIdent are automatically imported.Explicitly importing a package with Import is generally only necessarywhen the import will be blank (import _ "package").
func (*GeneratedFile)InternalStripForEditionsDiff¶added inv1.35.0
func (g *GeneratedFile) InternalStripForEditionsDiff()bool
InternalStripForEditionsDiff returns true if the plugin should not emit certainparts of the generated code in order to make it possible to compare aproto2/proto3 file with its equivalent (according to proto spec) editionsfile. Primarily, this is the encoded descriptor.
This function is for internal use by Go Protobuf only. Do not use it, wemight remove it at any time.
func (*GeneratedFile)P¶
func (g *GeneratedFile) P(v ...any)
P prints a line to the generated output. It converts each parameter to astring following the same rules asfmt.Print. It never inserts spacesbetween parameters.
func (*GeneratedFile)QualifiedGoIdent¶
func (g *GeneratedFile) QualifiedGoIdent(identGoIdent)string
QualifiedGoIdent returns the string to use for a Go identifier.
If the identifier is from a different Go package than the generated file,the returned name will be qualified (package.name) and an import statementfor the identifier's package will be included in the file.
func (*GeneratedFile)Skip¶
func (g *GeneratedFile) Skip()
Skip removes the generated file from the plugin output.
func (*GeneratedFile)Unskip¶added inv1.23.0
func (g *GeneratedFile) Unskip()
Unskip reverts a previous call toGeneratedFile.Skip,re-including the generated file in the plugin output.
typeGoIdent¶
type GoIdent struct {GoNamestringGoImportPathGoImportPath}A GoIdent is a Go identifier, consisting of a name and import path.The name is a single identifier and may not be a dot-qualified selector.
typeGoImportPath¶
type GoImportPathstring
A GoImportPath is the import path of a Go package.For example: "google.golang.org/protobuf/compiler/protogen"
func (GoImportPath)Ident¶
func (pGoImportPath) Ident(sstring)GoIdent
Ident returns a GoIdent with s as the GoName and p as the GoImportPath.
func (GoImportPath)String¶
func (pGoImportPath) String()string
typeGoPackageName¶
type GoPackageNamestring
A GoPackageName is the name of a Go package. e.g., "protobuf".
typeLocation¶
type Location struct {SourceFilestringPathprotoreflect.SourcePath}A Location is a location in a .proto source file.
See the google.protobuf.SourceCodeInfo documentation in descriptor.protofor details.
typeMessage¶
type Message struct {Descprotoreflect.MessageDescriptorGoIdentGoIdent// name of the generated Go typeFields []*Field// message field declarationsOneofs []*Oneof// message oneof declarationsEnums []*Enum// nested enum declarationsMessages []*Message// nested message declarationsExtensions []*Extension// nested extension declarationsLocationLocation// location of this messageCommentsCommentSet// comments associated with this message// APILevel specifies which API to generate. One of OPEN, HYBRID or OPAQUE.APILevelgofeaturespb.GoFeatures_APILevel}A Message describes a message.
typeMethod¶
type Method struct {Descprotoreflect.MethodDescriptorGoNamestringParent *Service// service in which this method is declaredInput *MessageOutput *MessageLocationLocation// location of this methodCommentsCommentSet// comments associated with this method}A Method describes a method in a service.
typeOneof¶
type Oneof struct {Descprotoreflect.OneofDescriptor// GoName is the base name of this oneof's Go field and methods.// For code generated by protoc-gen-go, this means a field named// '{{GoName}}' and a getter method named 'Get{{GoName}}'.GoNamestring// e.g., "OneofName"// GoIdent is the base name of a top-level declaration for this oneof.GoIdentGoIdent// e.g., "MessageName_OneofName"Parent *Message// message in which this oneof is declaredFields []*Field// fields that are part of this oneofLocationLocation// location of this oneofCommentsCommentSet// comments associated with this oneof// contains filtered or unexported fields}A Oneof describes a message oneof.
func (*Oneof)MethodName¶added inv1.36.0
MethodName returns the (possibly mangled) name of the generated accessormethod.
method must be one of Has, Clear, Which. MethodName panics otherwise.
typeOptions¶
type Options struct {// If ParamFunc is non-nil, it will be called with each unknown// generator parameter.//// Plugins for protoc can accept parameters from the command line,// passed in the --<lang>_out protoc, separated from the output// directory with a colon; e.g.,//// --go_out=<param1>=<value1>,<param2>=<value2>:<output_directory>//// Parameters passed in this fashion as a comma-separated list of// key=value pairs will be passed to the ParamFunc.//// The (flag.FlagSet).Set method matches this function signature,// so parameters can be converted into flags as in the following://// var flags flag.FlagSet// value := flags.Bool("param", false, "")// opts := &protogen.Options{// ParamFunc: flags.Set,// }// opts.Run(func(p *protogen.Plugin) error {// if *value { ... }// })ParamFunc func(name, valuestring)error// ImportRewriteFunc is called with the import path of each package// imported by a generated file. It returns the import path to use// for this package.ImportRewriteFunc func(GoImportPath)GoImportPath// StripForEditionsDiff true means that the plugin will not emit certain// parts of the generated code in order to make it possible to compare a// proto2/proto3 file with its equivalent (according to proto spec)// editions file. Primarily, this is the encoded descriptor.//// This must be a registered flag that is initialized by ParamFunc. It will// be used by Options.New after it has parsed the flags.//// This struct field is for internal use by Go Protobuf only. Do not use it,// we might remove it at any time.InternalStripForEditionsDiff *bool// DefaultAPILevel overrides which API to generate by default (despite what// the editions feature default specifies). One of OPEN, HYBRID or OPAQUE.DefaultAPILevelgofeaturespb.GoFeatures_APILevel}func (Options)New¶
func (optsOptions) New(req *pluginpb.CodeGeneratorRequest) (*Plugin,error)
New returns a new Plugin.
func (Options)Run¶
Run executes a function as a protoc plugin.
It reads apluginpb.CodeGeneratorRequest message fromos.Stdin, invokes the pluginfunction, and writes apluginpb.CodeGeneratorResponse message toos.Stdout.
If a failure occurs while reading or writing, Run prints an error toos.Stderr and callsos.Exit(1).
typePlugin¶
type Plugin struct {// Request is the CodeGeneratorRequest provided by protoc.Request *pluginpb.CodeGeneratorRequest// Files is the set of files to generate and everything they import.// Files appear in topological order, so each file appears before any// file that imports it.Files []*FileFilesByPath map[string]*File// SupportedFeatures is the set of protobuf language features supported by// this generator plugin. See the documentation for// google.protobuf.CodeGeneratorResponse.supported_features for details.SupportedFeaturesuint64SupportedEditionsMinimumdescriptorpb.EditionSupportedEditionsMaximumdescriptorpb.Edition// contains filtered or unexported fields}A Plugin is a protoc plugin invocation.
func (*Plugin)Error¶
Error records an error in code generation. The generator will report theerror back to protoc and will not produce output.
func (*Plugin)InternalStripForEditionsDiff¶added inv1.35.0
InternalStripForEditionsDiff returns whether or not to strip non-functionalcodegen for editions diff testing.
This function is for internal use by Go Protobuf only. Do not use it, wemight remove it at any time.
func (*Plugin)NewGeneratedFile¶
func (gen *Plugin) NewGeneratedFile(filenamestring, goImportPathGoImportPath) *GeneratedFile
NewGeneratedFile creates a new generated file with the given filenameand import path.
func (*Plugin)Response¶
func (gen *Plugin) Response() *pluginpb.CodeGeneratorResponse
Response returns the generator output.
typeService¶
type Service struct {Descprotoreflect.ServiceDescriptorGoNamestringMethods []*Method// service method declarationsLocationLocation// location of this serviceCommentsCommentSet// comments associated with this service}A Service describes a service.