golang

package
v0.3.18 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2024 License: Apache-2.0 Imports: 34 Imported by: 12

Documentation

Overview

Package golang is the generator backend for golang.

Index

Constants

View Source
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"
	ThriftReflectionLib = "github.com/cloudwego/thriftgo/thrift_reflection"
	ThriftFieldMaskLib  = "github.com/cloudwego/thriftgo/fieldmask"
	ThriftOptionLib     = "github.com/cloudwego/thriftgo/extension/thrift_option"

	ThriftJSONUtilLib = "github.com/cloudwego/thriftgo/utils/json_utils"
	KitexStreamingLib = "github.com/cloudwego/kitex/pkg/streaming"
	ApacheWarningLib  = "github.com/cloudwego/thriftgo/utils"
)

Default libraries.

Variables

This section is empty.

Functions

func BackQuoted added in v0.3.3

func BackQuoted(s string) string

func BuildRefScope added in v0.3.0

func BuildRefScope(cu *CodeUtils, ast *parser.Thrift) (*Scope, *Scope, error)

func GetImportPackage added in v0.3.0

func GetImportPackage(path string) string

func GetImportPath added in v0.3.0

func GetImportPath(cu *CodeUtils, ast *parser.Thrift) string

func GetTypeID added in v0.1.2

func GetTypeID(t *parser.Type) string

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

func GetTypeIDConstant(t *parser.Type) string

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

func IsBaseType(t *parser.Type) bool

IsBaseType determines whether the given type is a base type.

func IsConstantInGo added in v0.1.2

func IsConstantInGo(v *parser.Constant) bool

IsConstantInGo tells whether a constant in thrift IDL results in a constant in go.

func IsFixedLengthType added in v0.1.2

func IsFixedLengthType(t *parser.Type) bool

IsFixedLengthType determines whether the given type is a fixed length type.

func IsIntType added in v0.3.3

func IsIntType(t *parser.Type) bool

IsIntType determines whether the given type is a Int type.

func IsStrType added in v0.3.3

func IsStrType(t *parser.Type) bool

IsStrType determines whether the given type is a Str type.

func JoinPath added in v0.2.9

func JoinPath(elem ...string) string

func NeedRedirect added in v0.1.2

func NeedRedirect(f *parser.Field) bool

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

func SupportIsSet(f *parser.Field) bool

SupportIsSet determines whether a field supports IsSet query.

func ToRefFilename added in v0.3.0

func ToRefFilename(keepName bool, filename string) string

func ToReflectionFilename added in v0.3.0

func ToReflectionFilename(filename string) string

func ToReflectionRefFilename added in v0.3.0

func ToReflectionRefFilename(keepName bool, filename string) string

func ZeroWriter added in v0.3.4

func ZeroWriter(t *parser.Type, oprot string, err string) string

IsBaseType determines whether the given type is a base type.

Types

type Code added in v0.1.2

type Code string

Code is the type of go code segments.

func (Code) String added in v0.1.2

func (c Code) String() string

type CodeUtils

type CodeUtils struct {
	backend.LogFunc
	// contains filtered or unexported fields
}

CodeUtils contains a set of utility functions.

func NewCodeUtils

func NewCodeUtils(log backend.LogFunc) *CodeUtils

NewCodeUtils creates a new CodeUtils.

func (*CodeUtils) BuildFuncMap

func (cu *CodeUtils) BuildFuncMap() template.FuncMap

BuildFuncMap builds a function map for templates.

func (*CodeUtils) CombineOutputPath added in v0.2.5

func (cu *CodeUtils) CombineOutputPath(outputPath string, t *parser.Thrift) string

CombineOutputPath read the output and path variables and render them into the final path

func (*CodeUtils) Debug

func (cu *CodeUtils) Debug(vs ...interface{}) string

Debug prints the given values with println.

func (*CodeUtils) Features

func (cu *CodeUtils) Features() Features

Features returns the current settings of generator features.

func (*CodeUtils) GenFieldTags added in v0.2.0

func (cu *CodeUtils) GenFieldTags(f *Field, insertPoint string) (string, error)

GenFieldTags generates go tags for the given parser.Field.

func (*CodeUtils) GenTags

func (cu *CodeUtils) GenTags(f *parser.Field, insertPoint string) (string, error)

GenTags generates go tags for the given parser.Field.

func (*CodeUtils) GetFilePath

func (cu *CodeUtils) GetFilePath(t *parser.Thrift) string

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 (cu *CodeUtils) GetFilename(t *parser.Thrift) string

func (*CodeUtils) GetKeyType

func (cu *CodeUtils) GetKeyType(s *Scope, t *parser.Type) (*Scope, *parser.Type, error)

GetKeyType returns the key type of the given type. T must be a map type.

func (*CodeUtils) GetPackageName added in v0.1.2

func (cu *CodeUtils) GetPackageName(ast *parser.Thrift) string

GetPackageName returns a go package name for the given thrift AST.

func (*CodeUtils) GetPackagePrefix added in v0.1.2

func (cu *CodeUtils) GetPackagePrefix() (pp string)

GetPackagePrefix sets the package prefix in generated codes.

func (*CodeUtils) GetValType

func (cu *CodeUtils) GetValType(s *Scope, t *parser.Type) (*Scope, *parser.Type, error)

GetValType returns the value type of the given type. T must be a container type.

func (*CodeUtils) HandleOptions

func (cu *CodeUtils) HandleOptions(args []string) error

HandleOptions updates the CodeUtils with options.

func (*CodeUtils) Identify

func (cu *CodeUtils) Identify(name string) (s string, err error)

Identify converts an raw name from IDL into an exported identifier in go.

func (*CodeUtils) Import added in v0.1.2

func (cu *CodeUtils) Import(t *parser.Thrift) (pkg, pth string)

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

func (cu *CodeUtils) NamespaceToFullImportPath(ns string) string

NamespaceToFullImportPath returns an import path for the given namespace. The result path will contain the package prefix if it is set.

func (*CodeUtils) NamespaceToImportPath

func (cu *CodeUtils) NamespaceToImportPath(ns string) string

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

func (cu *CodeUtils) NamespaceToPackage(ns string) string

NamespaceToPackage converts a namespace to a package.

func (*CodeUtils) NamingStyle

func (cu *CodeUtils) NamingStyle() styles.Naming

NamingStyle returns the current naming style.

func (*CodeUtils) ParseNamespace

func (cu *CodeUtils) ParseNamespace(ast *parser.Thrift) (ref, pkg, pth string)

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

func (cu *CodeUtils) RootScope() *Scope

RootScope returns the root scope previously set by SetRootScope.

func (*CodeUtils) SetFeatures

func (cu *CodeUtils) SetFeatures(fs Features)

SetFeatures sets the feature set.

func (*CodeUtils) SetNamingStyle

func (cu *CodeUtils) SetNamingStyle(style styles.Naming)

SetNamingStyle sets the naming style.

func (*CodeUtils) SetPackagePrefix

func (cu *CodeUtils) SetPackagePrefix(pp string)

SetPackagePrefix sets the package prefix in generated codes.

func (*CodeUtils) SetRootScope

func (cu *CodeUtils) SetRootScope(s *Scope)

SetRootScope sets the root scope for rendering templates.

func (*CodeUtils) SetWithFieldMask added in v0.3.4

func (cu *CodeUtils) SetWithFieldMask(enable bool) bool

func (*CodeUtils) Template added in v0.2.0

func (cu *CodeUtils) Template() string

Template returns the current template name. Empty for the default.

func (*CodeUtils) UseInitialisms

func (cu *CodeUtils) UseInitialisms(enable bool)

UseInitialisms sets the naming style's initialisms option.

func (*CodeUtils) UsePackage

func (cu *CodeUtils) UsePackage(path, repl string)

UsePackage forces the generated codes to use the specific package.

func (*CodeUtils) UseTemplate added in v0.2.0

func (cu *CodeUtils) UseTemplate(value string) error

UseTemplate specifies a different template to generate codes.

type Constant added in v0.1.2

type Constant struct {
	*parser.Constant
	// contains filtered or unexported fields
}

Constant is a wrapper for the parser.Constant.

func (*Constant) GoName added in v0.1.2

func (c *Constant) GoName() Name

GoName returns the name in go code of the constant.

func (*Constant) GoTypeName added in v0.1.2

func (c *Constant) GoTypeName() TypeName

GoTypeName returns the type name in go code of the constant.

func (*Constant) Initialization added in v0.1.2

func (c *Constant) Initialization() Code

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

func (cs Constants) GoConstants() (gcs Constants)

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

func (cs Constants) GoVariables() (gvs Constants)

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

type Enum struct {
	*parser.Enum
	// contains filtered or unexported fields
}

Enum is a wrapper for the parser.Enum.

func (*Enum) GoName added in v0.1.2

func (e *Enum) GoName() Name

GoName returns the name in go code of the enum.

func (*Enum) Value added in v0.1.2

func (e *Enum) Value(name string) *EnumValue

Value returns a value defined in the enum with the given name. It returns nil if the value is not found.

func (*Enum) Values added in v0.1.2

func (e *Enum) Values() []*EnumValue

Values returns all values defined in the enum.

type EnumValue added in v0.1.2

type EnumValue struct {
	*parser.EnumValue
	// contains filtered or unexported fields
}

EnumValue is a wrapper for the parser.EnumValue.

func (*EnumValue) GoLiteral added in v0.1.2

func (ev *EnumValue) GoLiteral() Code

GoLiteral returns the literal in go code of the enum value.

func (*EnumValue) GoName added in v0.1.2

func (ev *EnumValue) GoName() Name

GoName returns the name in go code of the enum value.

type Features

type Features struct {
	MarshalEnumToText           bool `json_enum_as_text:"Generate MarshalText and UnmarshalText for enum values"`
	MarshalEnum                 bool `enum_marshal:"Generate MarshalText for enum values"`
	UnmarshalEnum               bool `enum_unmarshal:"Generate UnmarshalText 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:"Generate 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:"Generate 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"`
	AlwaysGenerateJSONTag       bool `always_gen_json_tag:"Always generate 'json' tag even if go.tag is provided (Disabled by default)"`
	SnakeTyleJSONTag            bool `snake_style_json_tag:"Generate snake style json tag"`
	LowerCamelCaseJSONTag       bool `lower_camel_style_json_tag:"Generate lower camel case style json tag"`
	WithReflection              bool `with_reflection:"Generate reflection info"`
	EnumAsINT32                 bool `enum_as_int_32:"Generate enum type as int32"`
	CodeRefSlim                 bool `code_ref_slim:"Generate code ref by given idl-ref.yaml with less refs to avoid conflict"`
	CodeRef                     bool `code_ref:"Generate code ref by given idl-ref.yaml"`
	ExpCodeRef                  bool `` /* 146-byte string literal not displayed */
	KeepCodeRefName             bool `keep_code_ref_name:"Generate code ref but still keep file name."`
	TrimIDL                     bool `trim_idl:"Simplify IDL to the most concise form before generating code."`
	EnableNestedStruct          bool `` /* 147-byte string literal not displayed */
	JSONStringer                bool `json_stringer:"Generate the JSON marshal method in String() method."`
	WithFieldMask               bool `with_field_mask:"Support field-mask for generated code."`
	FieldMaskHalfway            bool `field_mask_halfway:"Support set field-mask on not-root struct."`
	FieldMaskZeroRequired       bool `field_mask_zero_required:"Write zero value instead of current value for required fields filtered by fieldmask."`
	ThriftStreaming             bool `thrift_streaming:"Recognize thrift streaming annotation and generate streaming code."`
	NoDefaultSerdes             bool `no_default_serdes:"Do not generate default thrift serdes code."`
	NoAliasTypeReflectionMethod bool `` /* 153-byte string literal not displayed */
	EnableRefInterface          bool `` /* 151-byte string literal not displayed */
	UseOption                   bool `` /* 127-byte string literal not displayed */
	// ForceUseOption         bool `use_option:"Forcefully parse all Thrift annotations into struct-style option fields. If parsing is not possible, an error will be thrown."`
	NoFmt             bool `` /* 129-byte string literal not displayed */
	SkipEmpty         bool `skip_empty:"If there's not content in file, just skip it. Later this feature will be a default feature."`
	NoProcessor       bool `no_processor:" Do not generate default thrift processor and client. Later this feature will be a default feature."`
	GetEnumAnnotation bool `get_enum_annotation:"Generate GetAnnotation method for enum types."`
	ApacheWarning     bool `apache_warning:"Generate Apache Codec with warning on the first line."`
}

Features controls the behavior of CodeUtils.

type Field added in v0.1.2

type Field struct {
	*parser.Field
	// contains filtered or unexported fields
}

Field is a wrapper for the parser.Field.

func (*Field) DeepEqual added in v0.1.2

func (f *Field) DeepEqual() Name

DeepEqual returns the deep compare method's name for the field.

func (*Field) DefaultTypeName added in v0.1.2

func (f *Field) DefaultTypeName() TypeName

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

func (f *Field) DefaultValue() Code

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) Getter added in v0.1.2

func (f *Field) Getter() Name

Getter returns the getter method's name for the field.

func (*Field) GoName added in v0.1.2

func (f *Field) GoName() Name

GoName returns the name in go code of the field.

func (*Field) GoTypeName added in v0.1.2

func (f *Field) GoTypeName() TypeName

GoTypeName returns the type name in go code of the field.

func (*Field) IsNested added in v0.3.5

func (f *Field) IsNested() bool

IsNested returns whether the field is a nested type.

func (*Field) IsResponseFieldOfResult added in v0.1.2

func (f *Field) IsResponseFieldOfResult() bool

IsResponseFieldOfResult tells if the field is the response of a result type of a method.

func (*Field) IsSetter added in v0.1.2

func (f *Field) IsSetter() Name

IsSetter returns the isset method's name for the field.

func (*Field) Reader added in v0.1.2

func (f *Field) Reader() Name

Reader returns the name of the method that reads the field.

func (*Field) Setter added in v0.1.2

func (f *Field) Setter() Name

Setter returns the setter method's name for the field.

func (*Field) Writer added in v0.1.2

func (f *Field) Writer() Name

Writer returns the name of the method that writes the field.

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

type Function struct {
	*parser.Function
	// contains filtered or unexported fields
}

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) Arguments added in v0.1.2

func (f *Function) Arguments() []*Field

Arguments returns the argument list of the function.

func (*Function) GoName added in v0.1.2

func (f *Function) GoName() Name

GoName returns the go name 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

func (f *Function) ResponseGoTypeName() TypeName

ResponseGoTypeName returns the go type of the response type of the function.

func (*Function) Service added in v0.3.5

func (f *Function) Service() *Service

Service returns the service that the function is defined in.

func (*Function) Streaming added in v0.3.5

func (f *Function) Streaming() *streaming.Streaming

func (*Function) Throws added in v0.1.2

func (f *Function) Throws() []*Field

Throws returns the throw list 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

func (g *GoBackend) BuiltinPlugins() []*plugin.Desc

BuiltinPlugins implements the Backend interface.

func (*GoBackend) Generate

func (g *GoBackend) Generate(req *plugin.Request, log backend.LogFunc) *plugin.Response

Generate implements the Backend interface.

func (*GoBackend) GetCoreUtils added in v0.3.18

func (g *GoBackend) GetCoreUtils() *CodeUtils

func (*GoBackend) GetPlugin

func (g *GoBackend) GetPlugin(desc *plugin.Desc) plugin.Plugin

GetPlugin implements the Backend interface.

func (*GoBackend) Lang

func (g *GoBackend) Lang() string

Lang implements the Backend interface.

func (*GoBackend) Name

func (g *GoBackend) Name() string

Name implements the Backend interface.

func (*GoBackend) Options

func (g *GoBackend) Options() (opts []plugin.Option)

Options implements the Backend interface.

func (*GoBackend) PostProcess added in v0.1.2

func (g *GoBackend) PostProcess(path string, content []byte) ([]byte, error)

PostProcess implements the backend.PostProcessor interface to do source formatting before writing files out.

type Include added in v0.1.2

type Include struct {
	PackageName string
	ImportPath  string
	*Scope
}

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

func (is Includes) ByAST(ast *parser.Thrift) *Include

ByAST returns an Include whose scope matches the given AST. It returns nil if such an Include is not found.

func (Includes) ByIndex added in v0.1.2

func (is Includes) ByIndex(i int) *Include

ByIndex returns an Include at the given index. It returns nil if the index is invalid.

func (Includes) ByPackage added in v0.1.2

func (is Includes) ByPackage(pkg string) *Include

ByPackage returns an Include whose package name matches the given one. 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.

func (Name) String added in v0.1.2

func (n Name) String() string

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

	FieldMask string
	// 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) NeedFieldMask added in v0.3.4

func (c *ReadWriteContext) NeedFieldMask() bool

func (*ReadWriteContext) WithDecl added in v0.1.2

func (c *ReadWriteContext) WithDecl() *ReadWriteContext

WithDecl claims that the context needs a variable declaration.

func (*ReadWriteContext) WithFieldMask added in v0.3.3

func (c *ReadWriteContext) WithFieldMask(fm string) *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

func NewResolver(root *Scope, cu *CodeUtils) *Resolver

NewResolver creates a new Resolver with the given scope.

func (*Resolver) GetConstInit added in v0.1.2

func (r *Resolver) GetConstInit(name string, t *parser.Type, v *parser.ConstValue) (Code, error)

GetConstInit returns the initialization code for a constant.

func (*Resolver) GetDefaultValueTypeName added in v0.1.2

func (r *Resolver) GetDefaultValueTypeName(f *parser.Field) (TypeName, error)

GetDefaultValueTypeName returns a type name suitable for the default value of the given field.

func (*Resolver) GetFieldInit added in v0.1.2

func (r *Resolver) GetFieldInit(f *parser.Field) (Code, error)

GetFieldInit returns the initialization code for a field. The given field must have a default value.

func (*Resolver) GetTypeName added in v0.1.2

func (r *Resolver) GetTypeName(g *Scope, t *parser.Type) (name TypeName, err error)

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

func (r *Resolver) ResolveFieldTypeName(f *parser.Field) (TypeName, error)

ResolveFieldTypeName returns a legal type name in go for the given field.

func (*Resolver) ResolveTypeName added in v0.1.2

func (r *Resolver) ResolveTypeName(t *parser.Type) (TypeName, error)

ResolveTypeName returns a legal type name in go for the given AST type.

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

func BuildScope(cu *CodeUtils, ast *parser.Thrift) (*Scope, error)

BuildScope creates a scope of the AST with its includes processed recursively. todo check cache

func (*Scope) AST added in v0.1.2

func (s *Scope) AST() *parser.Thrift

AST returns the thrift AST associated with the scope.

func (*Scope) Constant added in v0.1.2

func (s *Scope) Constant(name string) *Constant

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

func (s *Scope) Constants() Constants

Constants returns all thrift constants defined in the current scope.

func (*Scope) Enum added in v0.1.2

func (s *Scope) Enum(name string) *Enum

Enum returns an enum defined in the current scope with the given name. It returns nil if the enum is not found.

func (*Scope) Enums added in v0.1.2

func (s *Scope) Enums() []*Enum

Enums returns all enums defined in the current scope.

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 (s *Scope) FilePackage() string

func (*Scope) GetFirstDescriptor added in v0.3.0

func (s *Scope) GetFirstDescriptor() string

func (*Scope) GetOption added in v0.3.8

func (s *Scope) GetOption(filepath string) []string

func (*Scope) IDLName added in v0.2.8

func (s *Scope) IDLName() string

func (*Scope) Includes added in v0.1.2

func (s *Scope) Includes() Includes

Includes returns the include list of the current scope.

func (*Scope) IsEmpty added in v0.3.11

func (s *Scope) IsEmpty() bool

func (*Scope) MarshalDescriptor added in v0.3.0

func (s *Scope) MarshalDescriptor() string

func (*Scope) Namespace added in v0.1.2

func (s *Scope) Namespace() namespace.Namespace

Namespace returns the global namespace of the current scope.

func (*Scope) RefPackage added in v0.2.4

func (s *Scope) RefPackage() string

func (*Scope) RefPath added in v0.2.4

func (s *Scope) RefPath() string

func (*Scope) ResolveImports added in v0.1.2

func (s *Scope) ResolveImports() (map[string]string, error)

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

func (s *Scope) Service(name string) *Service

Service returns a service defined in the current scope with the given name. It returns nil if the service is not found.

func (*Scope) Services added in v0.1.2

func (s *Scope) Services() []*Service

Services returns all service defined in the current scope.

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

func (s *Scope) Typedef(alias string) *Typedef

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

func (s *Scope) Typedefs() []*Typedef

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

type Service struct {
	*parser.Service
	// contains filtered or unexported fields
}

Service is a wrapper for the parser.Service.

func (*Service) Base added in v0.1.2

func (s *Service) Base() *Service

Base returns the base service that the current service extends. It returns nil if the service has no base service.

func (*Service) From added in v0.1.2

func (s *Service) From() *Scope

From returns the scope that the service is defined in.

func (*Service) Function added in v0.1.2

func (s *Service) Function(name string) *Function

Function returns a function defined in the service with the given name. It returns nil if the function is not found.

func (*Service) Functions added in v0.1.2

func (s *Service) Functions() []*Function

Functions returns all functions defined in the service.

func (*Service) GoName added in v0.1.2

func (s *Service) GoName() Name

GoName returns the name in go code of the service.

func (*Service) Namespace added in v0.1.2

func (s *Service) Namespace() namespace.Namespace

Namespace returns the namespace of the service.

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) IsAlias added in v0.3.8

func (s *StructLike) IsAlias() bool

IsAlias returns whether this type is alias of existing type

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

func (tn TypeName) Deref() TypeName

Deref removes the "&" and "*" prefix of the given type name.

func (TypeName) IsForeign added in v0.1.2

func (tn TypeName) IsForeign() bool

IsForeign reports whether the type name is from another module.

func (TypeName) IsPointer added in v0.1.2

func (tn TypeName) IsPointer() bool

IsPointer reports whether the type name is a pointer type by detecting the prefix "*".

func (TypeName) NewFunc added in v0.1.2

func (tn TypeName) NewFunc() Name

NewFunc returns the construction function of the given type.

func (TypeName) Pointerize added in v0.1.2

func (tn TypeName) Pointerize() TypeName

Pointerize returns the pointer type of the current type name.

func (TypeName) String added in v0.1.2

func (tn TypeName) String() string

type Typedef added in v0.1.2

type Typedef struct {
	*parser.Typedef
	// contains filtered or unexported fields
}

Typedef is a wrapper for the parser.Typedef.

func (*Typedef) GoName added in v0.1.2

func (t *Typedef) GoName() Name

GoName returns the name in go code of the typedef.

func (*Typedef) GoTypeName added in v0.1.2

func (t *Typedef) GoTypeName() TypeName

GoTypeName returns the type name in go code of the typedef.

Directories

Path Synopsis
extension
unknown
Package unknown .
Package unknown .
Package templates defines templates for generating golang codes.
Package templates defines templates for generating golang codes.
ref

Jump to

Keyboard shortcuts

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