Documentation ¶
Overview ¶
Package golang is the generator backend for golang.
Index ¶
- Constants
- func DoRef(path string) (bool, string)
- func GetTypeID(t *parser.Type) string
- func GetTypeIDConstant(t *parser.Type) string
- func IsBaseType(t *parser.Type) bool
- func IsConstantInGo(v *parser.Constant) bool
- func IsFixedLengthType(t *parser.Type) bool
- func NeedRedirect(f *parser.Field) bool
- func SupportIsSet(f *parser.Field) bool
- func TemplatesRef() []string
- type Code
- type CodeUtils
- func (cu *CodeUtils) BuildFuncMap() template.FuncMap
- func (cu *CodeUtils) CombineOutputPath(outputPath string, t *parser.Thrift) string
- func (cu *CodeUtils) Debug(vs ...interface{}) string
- func (cu *CodeUtils) Features() Features
- func (cu *CodeUtils) GenFieldTags(f *Field, insertPoint string) (string, error)
- func (cu *CodeUtils) GenTags(f *parser.Field, insertPoint string) (string, error)
- func (cu *CodeUtils) GetFilePath(t *parser.Thrift) string
- func (cu *CodeUtils) GetFilename(t *parser.Thrift) string
- func (cu *CodeUtils) GetKeyType(s *Scope, t *parser.Type) (*Scope, *parser.Type, error)
- func (cu *CodeUtils) GetPackageName(ast *parser.Thrift) string
- func (cu *CodeUtils) GetPackagePrefix() (pp string)
- func (cu *CodeUtils) GetValType(s *Scope, t *parser.Type) (*Scope, *parser.Type, error)
- func (cu *CodeUtils) HandleOptions(args []string) error
- func (cu *CodeUtils) Identify(name string) (s string, err error)
- func (cu *CodeUtils) Import(t *parser.Thrift) (pkg, pth string)
- func (cu *CodeUtils) MkRWCtx(root *Scope, f *Field) (*ReadWriteContext, error)
- func (cu *CodeUtils) NamespaceToFullImportPath(ns string) string
- func (cu *CodeUtils) NamespaceToImportPath(ns string) string
- func (cu *CodeUtils) NamespaceToPackage(ns string) string
- func (cu *CodeUtils) NamingStyle() styles.Naming
- func (cu *CodeUtils) ParseNamespace(ast *parser.Thrift) (ref, pkg, pth string)
- func (cu *CodeUtils) RootScope() *Scope
- func (cu *CodeUtils) SetFeatures(fs Features)
- func (cu *CodeUtils) SetNamingStyle(style styles.Naming)
- func (cu *CodeUtils) SetPackagePrefix(pp string)
- func (cu *CodeUtils) SetRootScope(s *Scope)
- func (cu *CodeUtils) Template() string
- func (cu *CodeUtils) UseInitialisms(enable bool)
- func (cu *CodeUtils) UsePackage(path, repl string)
- func (cu *CodeUtils) UseTemplate(value string) error
- type Constant
- type Constants
- type Enum
- type EnumValue
- type Features
- type Field
- func (f *Field) DeepEqual() Name
- func (f *Field) DefaultTypeName() TypeName
- func (f *Field) DefaultValue() Code
- func (f *Field) Getter() Name
- func (f *Field) GoName() Name
- func (f *Field) GoTypeName() TypeName
- func (f *Field) IsResponseFieldOfResult() bool
- func (f *Field) IsSetter() Name
- func (f *Field) Reader() Name
- func (f *Field) Setter() Name
- func (f *Field) Writer() Name
- type FrugalResolver
- type Function
- type GoBackend
- func (g *GoBackend) BuiltinPlugins() []*plugin.Desc
- func (g *GoBackend) Generate(req *plugin.Request, log backend.LogFunc) *plugin.Response
- func (g *GoBackend) GetPlugin(desc *plugin.Desc) plugin.Plugin
- func (g *GoBackend) Lang() string
- func (g *GoBackend) Name() string
- func (g *GoBackend) Options() (opts []plugin.Option)
- func (g *GoBackend) PostProcess(path string, content []byte) ([]byte, error)
- type Include
- type Includes
- type Name
- type ReadWriteContext
- type Resolver
- func (r *Resolver) GetConstInit(name string, t *parser.Type, v *parser.ConstValue) (Code, error)
- func (r *Resolver) GetDefaultValueTypeName(f *parser.Field) (TypeName, error)
- func (r *Resolver) GetFieldInit(f *parser.Field) (Code, error)
- func (r *Resolver) GetTypeName(g *Scope, t *parser.Type) (name TypeName, err error)
- func (r *Resolver) ResolveConst(g *Scope, name string, t *parser.Type, v *parser.ConstValue) (Code, error)
- func (r *Resolver) ResolveFieldTypeName(f *parser.Field) (TypeName, error)
- func (r *Resolver) ResolveTypeName(t *parser.Type) (TypeName, error)
- type Scope
- func (s *Scope) AST() *parser.Thrift
- func (s *Scope) Constant(name string) *Constant
- func (s *Scope) Constants() Constants
- func (s *Scope) Enum(name string) *Enum
- func (s *Scope) Enums() []*Enum
- func (s *Scope) Exception(name string) *StructLike
- func (s *Scope) Exceptions() []*StructLike
- func (s *Scope) FilePackage() string
- func (s *Scope) Includes() Includes
- func (s *Scope) Namespace() namespace.Namespace
- func (s *Scope) RefPackage() string
- func (s *Scope) RefPath() string
- func (s *Scope) ResolveImports() (map[string]string, error)
- func (s *Scope) Service(name string) *Service
- func (s *Scope) Services() []*Service
- func (s *Scope) Struct(name string) *StructLike
- func (s *Scope) StructLike(name string) *StructLike
- func (s *Scope) StructLikes() (ss []*StructLike)
- func (s *Scope) Structs() []*StructLike
- func (s *Scope) Typedef(alias string) *Typedef
- func (s *Scope) Typedefs() []*Typedef
- func (s *Scope) Union(name string) *StructLike
- func (s *Scope) Unions() []*StructLike
- type Service
- type StructLike
- type TypeName
- type Typedef
Constants ¶
const ( DefaultThriftLib = "github.com/apache/thrift/lib/go/thrift" DefaultUnknownLib = "github.com/cloudwego/thriftgo/generator/golang/extension/unknown" DefaultMetaLib = "github.com/cloudwego/thriftgo/generator/golang/extension/meta" )
Default libraries.
Variables ¶
This section is empty.
Functions ¶
func GetTypeID ¶ added in v0.1.2
GetTypeID returns the thrift type ID literal for the given type which is suitable to concate with "Read" or "Write" to produce a valid method name in the TProtocol interface. Note that enum types results in I32.
func GetTypeIDConstant ¶ added in v0.1.2
GetTypeIDConstant returns the thrift type ID literal for the given type which is suitable to concate with "thrift." to produce a valid type ID constant.
func IsBaseType ¶ added in v0.1.2
IsBaseType determines whether the given type is a base type.
func IsConstantInGo ¶ added in v0.1.2
IsConstantInGo tells whether a constant in thrift IDL results in a constant in go.
func IsFixedLengthType ¶ added in v0.1.2
IsFixedLengthType determines whether the given type is a fixed length type.
func NeedRedirect ¶ added in v0.1.2
NeedRedirect deterimines whether the given field should result in a pointer type. Condition: struct-like || (optional non-binary base type without default vlaue).
func SupportIsSet ¶ added in v0.1.2
SupportIsSet determines whether a field supports IsSet query.
func TemplatesRef ¶ added in v0.2.4
func TemplatesRef() []string
Types ¶
type CodeUtils ¶
CodeUtils contains a set of utility functions.
func NewCodeUtils ¶
NewCodeUtils creates a new CodeUtils.
func (*CodeUtils) BuildFuncMap ¶
BuildFuncMap builds a function map for templates.
func (*CodeUtils) CombineOutputPath ¶ added in v0.2.5
CombineOutputPath read the output and path variables and render them into the final path
func (*CodeUtils) GenFieldTags ¶ added in v0.2.0
GenFieldTags generates go tags for the given parser.Field.
func (*CodeUtils) GetFilePath ¶
GetFilePath returns a path to the generated file for the given IDL. Note that the result is a path relative to the root output path.
func (*CodeUtils) GetFilename ¶ added in v0.2.5
func (*CodeUtils) GetKeyType ¶
GetKeyType returns the key type of the given type. T must be a map type.
func (*CodeUtils) GetPackageName ¶ added in v0.1.2
GetPackageName returns a go package name for the given thrift AST.
func (*CodeUtils) GetPackagePrefix ¶ added in v0.1.2
GetPackagePrefix sets the package prefix in generated codes.
func (*CodeUtils) GetValType ¶
GetValType returns the value type of the given type. T must be a container type.
func (*CodeUtils) HandleOptions ¶
HandleOptions updates the CodeUtils with options.
func (*CodeUtils) Identify ¶
Identify converts an raw name from IDL into an exported identifier in go.
func (*CodeUtils) Import ¶ added in v0.1.2
Import returns the package name and the full import path for the given AST.
func (*CodeUtils) MkRWCtx ¶
func (cu *CodeUtils) MkRWCtx(root *Scope, f *Field) (*ReadWriteContext, error)
MkRWCtx = MakeReadWriteContext. Check the documents of ReadWriteContext for more informations.
func (*CodeUtils) NamespaceToFullImportPath ¶ added in v0.1.2
NamespaceToFullImportPath returns an import path for the given namespace. The result path will contain the package prefix if it is set.
func (*CodeUtils) NamespaceToImportPath ¶
NamespaceToImportPath returns an import path for the given namespace. Note that the result will not have the package prefix set with SetPackagePrefix.
func (*CodeUtils) NamespaceToPackage ¶
NamespaceToPackage converts a namespace to a package.
func (*CodeUtils) NamingStyle ¶
NamingStyle returns the current naming style.
func (*CodeUtils) ParseNamespace ¶
ParseNamespace retrieves informations from the given AST and returns a reference name in the IDL, a package name for generated codes and an import path.
func (*CodeUtils) RootScope ¶ added in v0.1.2
RootScope returns the root scope previously set by SetRootScope.
func (*CodeUtils) SetFeatures ¶
SetFeatures sets the feature set.
func (*CodeUtils) SetNamingStyle ¶
SetNamingStyle sets the naming style.
func (*CodeUtils) SetPackagePrefix ¶
SetPackagePrefix sets the package prefix in generated codes.
func (*CodeUtils) SetRootScope ¶
SetRootScope sets the root scope for rendering templates.
func (*CodeUtils) Template ¶ added in v0.2.0
Template returns the current template name. Empty for the default.
func (*CodeUtils) UseInitialisms ¶
UseInitialisms sets the naming style's initialisms option.
func (*CodeUtils) UsePackage ¶
UsePackage forces the generated codes to use the specific package.
func (*CodeUtils) UseTemplate ¶ added in v0.2.0
UseTemplate specifies a different template to generate codes.
type Constant ¶ added in v0.1.2
Constant is a wrapper for the parser.Constant.
func (*Constant) GoTypeName ¶ added in v0.1.2
GoTypeName returns the type name in go code of the constant.
func (*Constant) Initialization ¶ added in v0.1.2
Initialization returns the initialization code of the constant.
type Constants ¶ added in v0.1.2
type Constants []*Constant
Constants is a list of constants.
func (Constants) GoConstants ¶ added in v0.1.2
GoConstants returns the subset of the current constants that each one of them results a constant in Go.
func (Constants) GoVariables ¶ added in v0.1.2
GoVariables returns the subset of the current constants that each one of them results a variable in Go.
type Enum ¶ added in v0.1.2
Enum is a wrapper for the parser.Enum.
type EnumValue ¶ added in v0.1.2
EnumValue is a wrapper for the parser.EnumValue.
type Features ¶
type Features struct { MarshalEnumToText bool `json_enum_as_text:"Generate MarshalText for enum values"` GenerateSetter bool `gen_setter:"Generate Set* methods for fields"` GenDatabaseTag bool `gen_db_tag:"Generate 'db:$field' tag"` GenOmitEmptyTag bool `omitempty_for_optional:"Generate 'omitempty' tags for optional fields."` TypedefAsTypeAlias bool `use_type_alias:"Generate type alias for typedef instead of type define."` ValidateSet bool `validate_set:"Generate codes to validate the uniqueness of set elements."` ValueTypeForSIC bool `value_type_in_container:"Genenerate value type for struct-like in container instead of pointer type."` ScanValueForEnum bool `scan_value_for_enum:"Generate Scan and Value methods for enums to implement interfaces in std sql library."` ReorderFields bool `reorder_fields:"Reorder fields of structs to improve memory usage."` TypedEnumString bool `typed_enum_string:"Add type prefix to the string representation of enum values."` KeepUnknownFields bool `keep_unknown_fields:"Genenerate codes to store unrecognized fields in structs."` GenDeepEqual bool `gen_deep_equal:"Generate DeepEqual function for struct/union/exception."` CompatibleNames bool `compatible_names:"Add a '_' suffix if an name has a prefix 'New' or suffix 'Args' or 'Result'."` ReserveComments bool `reserve_comments:"Reserve comments of definitions in thrift file"` NilSafe bool `nil_safe:"Generate nil-safe getters."` FrugalTag bool `frugal_tag:"Generate 'frugal' tags."` EscapeDoubleInTag bool `unescape_double_quote:"Unescape the double quotes in literals when generating go tags."` GenerateTypeMeta bool `gen_type_meta:"Generate and register type meta for structures."` GenerateJSONTag bool `gen_json_tag:"Generate struct with 'json' tag"` SnakeTyleJSONTag bool `snake_style_json_tag:"Generate snake style json tag"` }
Features controls the behavior of CodeUtils.
type Field ¶ added in v0.1.2
Field is a wrapper for the parser.Field.
func (*Field) DeepEqual ¶ added in v0.1.2
DeepEqual returns the deep compare method's name for the field.
func (*Field) DefaultTypeName ¶ added in v0.1.2
DefaultTypeName returns the type name in go code of the default value of the field. Note that it might be different with the result of GoTypeName.
func (*Field) DefaultValue ¶ added in v0.1.2
DefaultValue returns the go code of the field for its Initialization. Note that the code exists only when the field has default value.
func (*Field) GoTypeName ¶ added in v0.1.2
GoTypeName returns the type name in go code of the field.
func (*Field) IsResponseFieldOfResult ¶ added in v0.1.2
IsResponseFieldOfResult tells if the field is the response of a result type of a method.
type FrugalResolver ¶ added in v0.2.0
type FrugalResolver struct {
// contains filtered or unexported fields
}
FrugalResolver resolves type names for frugal.
func NewFrugalResolver ¶ added in v0.2.0
func NewFrugalResolver(root *Scope, cu *CodeUtils) *FrugalResolver
NewFrugalResolver creates a new FrugalResolver with the given scope.
func (*FrugalResolver) ResolveFrugalTypeName ¶ added in v0.2.0
func (r *FrugalResolver) ResolveFrugalTypeName(t *parser.Type) (TypeName, error)
ResolveFrugalTypeName returns a legal type name in frugal for the given AST type.
type Function ¶ added in v0.1.2
Function is a wrapper for the parser.Function.
func (*Function) ArgType ¶ added in v0.1.2
func (f *Function) ArgType() *StructLike
ArgType returns the synthesized structure that wraps of the function.
func (*Function) ResType ¶ added in v0.1.2
func (f *Function) ResType() *StructLike
ResType returns the synthesized structure of arguments of the function.
func (*Function) ResponseGoTypeName ¶ added in v0.1.2
ResponseGoTypeName returns the go type of the response type of the function.
type GoBackend ¶
type GoBackend struct {
// contains filtered or unexported fields
}
GoBackend generates go codes. The zero value of GoBackend is ready for use.
func (*GoBackend) BuiltinPlugins ¶
BuiltinPlugins implements the Backend interface.
type Include ¶ added in v0.1.2
Include associates an import in golang with the corresponding scope that built from an thrift AST in the include list.
type Includes ¶ added in v0.1.2
type Includes []*Include
Includes is a list of Include objects.
func (Includes) ByAST ¶ added in v0.1.2
ByAST returns an Include whose scope matches the given AST. It returns nil if such an Include is not found.
type Name ¶ added in v0.1.2
type Name string
Name is the type of identifiers converted from a thrift AST to Go code.
type ReadWriteContext ¶
type ReadWriteContext struct { Type *parser.Type TypeName TypeName // The type name in Go code TypeID string // For `thrift.TProtocol.(Read|Write)${TypeID}` methods IsPointer bool // Whether the target type is a pointer type in Go KeyCtx *ReadWriteContext // sub-context if the type is map ValCtx *ReadWriteContext // sub-context if the type is container Target string // The target for assignment Source string // The variable for right hand operand in deep-equal NeedDecl bool // Whether a declaration of target is needed // contains filtered or unexported fields }
ReadWriteContext contains information for generating codes in ReadField* and WriteField* functions. Each context stands for a struct field, a map key, a map value, a list elemement, or a set elemement.
func (*ReadWriteContext) GenID ¶ added in v0.1.2
func (c *ReadWriteContext) GenID(prefix string) (name string)
GenID returns a local variable with the given name as prefix.
func (*ReadWriteContext) WithDecl ¶ added in v0.1.2
func (c *ReadWriteContext) WithDecl() *ReadWriteContext
WithDecl claims that the context needs a variable declaration.
func (*ReadWriteContext) WithSource ¶ added in v0.1.2
func (c *ReadWriteContext) WithSource(s string) *ReadWriteContext
WithSource sets the source name.
func (*ReadWriteContext) WithTarget ¶ added in v0.1.2
func (c *ReadWriteContext) WithTarget(t string) *ReadWriteContext
WithTarget sets the target name.
type Resolver ¶ added in v0.1.2
type Resolver struct {
// contains filtered or unexported fields
}
Resolver resolves names for types, names and initialization value for thrift AST nodes in a scope (the root scope).
func NewResolver ¶ added in v0.1.2
NewResolver creates a new Resolver with the given scope.
func (*Resolver) GetConstInit ¶ added in v0.1.2
GetConstInit returns the initialization code for a constant.
func (*Resolver) GetDefaultValueTypeName ¶ added in v0.1.2
GetDefaultValueTypeName returns a type name suitable for the default value of the given field.
func (*Resolver) GetFieldInit ¶ added in v0.1.2
GetFieldInit returns the initialization code for a field. The given field must have a default value.
func (*Resolver) GetTypeName ¶ added in v0.1.2
GetTypeName returns a an type name (with selector if necessary) of the given type to be used in the root file. The type t must be a parser.Type associated with g.
func (*Resolver) ResolveConst ¶ added in v0.1.2
func (r *Resolver) ResolveConst(g *Scope, name string, t *parser.Type, v *parser.ConstValue) (Code, error)
ResolveConst returns the initialization code for a constant or a default value. The type t must be a parser.Type associated with g.
func (*Resolver) ResolveFieldTypeName ¶ added in v0.1.2
ResolveFieldTypeName returns a legal type name in go for the given field.
type Scope ¶
type Scope struct {
// contains filtered or unexported fields
}
Scope contains the type symbols defined in a thrift IDL and wraps them to provide access to resolved names in go code.
func BuildScope ¶ added in v0.1.2
BuildScope creates a scope of the AST with its includes processed recursively.
func (*Scope) Constant ¶ added in v0.1.2
Constant returns a constant defined in the current scope with the given name. It returns nil if the constant is not found.
func (*Scope) Constants ¶ added in v0.1.2
Constants returns all thrift constants defined in the current scope.
func (*Scope) Enum ¶ added in v0.1.2
Enum returns an enum defined in the current scope with the given name. It returns nil if the enum is not found.
func (*Scope) Exception ¶ added in v0.1.2
func (s *Scope) Exception(name string) *StructLike
Exception returns an exception defined in the current scope with the given name. It returns nil if the exception is not found.
func (*Scope) Exceptions ¶ added in v0.1.2
func (s *Scope) Exceptions() []*StructLike
Exceptions returns all exception defined in the current scope.
func (*Scope) FilePackage ¶ added in v0.2.5
func (*Scope) Namespace ¶ added in v0.1.2
Namespace returns the global namespace of the current scope.
func (*Scope) RefPackage ¶ added in v0.2.4
func (*Scope) ResolveImports ¶ added in v0.1.2
ResolveImports returns a map of import path to alias built from the include list of the IDL. An alias may be an empty string to indicate no alias is need for the import path.
func (*Scope) Service ¶ added in v0.1.2
Service returns a service defined in the current scope with the given name. It returns nil if the service is not found.
func (*Scope) Struct ¶ added in v0.1.2
func (s *Scope) Struct(name string) *StructLike
Struct returns a struct defined in the current scope with the given name. It returns nil if the struct is not found.
func (*Scope) StructLike ¶ added in v0.1.2
func (s *Scope) StructLike(name string) *StructLike
StructLike returns a struct-like defined in the current scope with the given name. It returns nil if the struct-like is not found.
func (*Scope) StructLikes ¶ added in v0.1.2
func (s *Scope) StructLikes() (ss []*StructLike)
StructLikes returns all struct-like defined in the current scope.
func (*Scope) Structs ¶ added in v0.1.2
func (s *Scope) Structs() []*StructLike
Structs returns all struct defined in the current scope.
func (*Scope) Typedef ¶ added in v0.1.2
Typedef returns a typedef defined in the current scope with the given name. It returns nil if the typedef is not found.
func (*Scope) Typedefs ¶ added in v0.1.2
Typedefs returns all typedefs defined in the current scope.
func (*Scope) Union ¶ added in v0.1.2
func (s *Scope) Union(name string) *StructLike
Union returns a union defined in the current scope with the given name. It returns nil if the union is not found.
func (*Scope) Unions ¶ added in v0.1.2
func (s *Scope) Unions() []*StructLike
Unions returns all union defined in the current scope.
type Service ¶ added in v0.1.2
Service is a wrapper for the parser.Service.
func (*Service) Base ¶ added in v0.1.2
Base returns the base service that the current service extends. It returns nil if the service has no base service.
func (*Service) Function ¶ added in v0.1.2
Function returns a function defined in the service with the given name. It returns nil if the function is not found.
type StructLike ¶ added in v0.1.2
type StructLike struct { *parser.StructLike // contains filtered or unexported fields }
StructLike is a wrapper for the parser.StructLike.
func (*StructLike) Field ¶ added in v0.1.2
func (s *StructLike) Field(name string) *Field
Field returns a field of the struct-like that has the given name. It returns nil if such a field is not found.
func (*StructLike) Fields ¶ added in v0.1.2
func (s *StructLike) Fields() []*Field
Fields returns all fields defined in the struct-like.
func (*StructLike) GoName ¶ added in v0.1.2
func (s *StructLike) GoName() Name
GoName returns the name in go code of the struct-like.
func (*StructLike) Namespace ¶ added in v0.1.2
func (s *StructLike) Namespace() namespace.Namespace
Namespace returns the namescope of the struct-like.
type TypeName ¶ added in v0.1.2
type TypeName string
TypeName is the type for Go symbols converted from a thrift AST. It provides serveral methods to manipulate a type name of a symbol.
func (TypeName) Deref ¶ added in v0.1.2
Deref removes the "&" and "*" prefix of the given type name.
func (TypeName) IsForeign ¶ added in v0.1.2
IsForeign reports whether the type name is from another module.
func (TypeName) IsPointer ¶ added in v0.1.2
IsPointer reports whether the type name is a pointer type by detecting the prefix "*".
func (TypeName) NewFunc ¶ added in v0.1.2
NewFunc returns the construction function of the given type.
func (TypeName) Pointerize ¶ added in v0.1.2
Pointerize returns the pointer type of the current type name.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
extension
|
|
unknown
Package unknown provides definitions that work with the thriftgo `keep_unknown_fields` option.
|
Package unknown provides definitions that work with the thriftgo `keep_unknown_fields` option. |
Package templates defines templates for generating golang codes.
|
Package templates defines templates for generating golang codes. |