build

package
v0.1.12 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2022 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// positions
	FileHead       = "file_head"
	BeforeImport   = "before_import"
	InImport       = "in_import"
	AfterImport    = "after_import"
	BeforeConst    = "before_const"
	AfterConst     = "after_const"
	ConstFront     = "const_front"
	ConstBack      = "const_back"
	BeforeEnum     = "before_enum"
	EnumFront      = "enum_front"
	EnumBack       = "enum_back"
	AfterEnum      = "after_enum"
	BeforeStruct   = "before_struct"
	StructFront    = "struct_front"
	StructBack     = "struct_back"
	AfterStruct    = "after_struct"
	BeforeProtocol = "before_protocol"
	ProtocolFront  = "protocol_front"
	ProtocolBack   = "protocol_back"
	AfterProtocol  = "after_protocol"
	BeforeService  = "before_service"
	ServiceFront   = "service_front"
	ServiceBack    = "service_back"
	AfterService   = "after_service"

	// Extension config filename
	ExtConfigFilename = "ext.json"
)

Variables

View Source
var (
	ErrAmbiguousNames = errors.New("ambiguous names")
)
View Source
var (
	ErrExtensionName = errors.New("invalid extension name")
)

Functions

func BuildComment

func BuildComment(comment *ast.CommentGroup) string

BuildDoc builds Comment node to string

func BuildDoc

func BuildDoc(doc *ast.CommentGroup) string

BuildDoc builds Doc node to string

func BuildIdent

func BuildIdent(ident *ast.Ident) string

BuildIdent builds Ident node to string

func BuildIdentList

func BuildIdentList(idents []*ast.Ident) []string

BuildIdentList builds Ident nodes to string slice

func IntFromExpr added in v0.1.3

func IntFromExpr(expr Expr) (string, bool)

IntFromExpr converts Expr node to string which represents an integer

func IsValidKind added in v0.1.2

func IsValidKind(kind string) bool

func JoinBeanKey added in v0.1.3

func JoinBeanKey(pkgName, beanName string) string

func ParseFlags

func ParseFlags() (plugin Plugin, config PluginRuntimeConfig, builder *Builder, err error)

func ParseIntFromExpr added in v0.1.4

func ParseIntFromExpr(expr Expr) (int, bool)

func ReadBeanIds added in v0.1.3

func ReadBeanIds(reader io.Reader, sep string) (map[string]int, error)

ReadBeanIds read key-id pairs from reader. Each line contains one key-id pair seperated by `sep`.

func SplitBeanKey added in v0.1.3

func SplitBeanKey(key string) (pkgName, beanName string)

Types

type ArrayType

type ArrayType struct {
	TypeBase
	T    Type
	Size Expr
}

func BuildArray

func BuildArray(t *ast.ArrayType) *ArrayType

func (ArrayType) IsArray added in v0.1.2

func (ArrayType) IsArray() bool

type BasicLit

type BasicLit struct {
	Kind  lexer.Token
	Value string
}

func BuildBasicLit

func BuildBasicLit(lit *ast.BasicLit) *BasicLit

func (BasicLit) ExprNode

func (BasicLit) ExprNode()

type BasicType

type BasicType struct {
	TypeBase
	Name string
}

func (BasicType) IsBool added in v0.1.2

func (t BasicType) IsBool() bool

func (BasicType) IsFloat added in v0.1.3

func (t BasicType) IsFloat() bool

func (BasicType) IsInt added in v0.1.2

func (t BasicType) IsInt() bool

func (BasicType) IsString added in v0.1.2

func (t BasicType) IsString() bool

func (BasicType) IsVector added in v0.1.2

func (t BasicType) IsVector() bool

type Bean

type Bean struct {
	Id      int
	Kind    string
	Doc     string
	Name    string
	Extends []Type
	Tag     Tag
	Fields  []*Field
	Comment string
	Group   string
}

func BuildBean

func BuildBean(bean *ast.BeanDecl) *Bean

func (Bean) Field

func (bean Bean) Field(i int) *Field

func (Bean) FindFieldByName added in v0.1.4

func (bean Bean) FindFieldByName(name string) *Field

func (Bean) GetTag added in v0.1.3

func (bean Bean) GetTag(key string) string

func (Bean) HasTag added in v0.1.3

func (bean Bean) HasTag(key string) bool

func (*Bean) IsNil added in v0.1.2

func (bean *Bean) IsNil() bool

func (Bean) NumField

func (bean Bean) NumField() int

type BeanIdAllocator added in v0.1.3

type BeanIdAllocator interface {
	// Allocate returns a integer as id of key
	Allocate(key string) int
	// Output outputs all key-id pairs to writer w or outputs to default writer of allocator if w is nil
	Output(w io.Writer) error
}

func NewBeanIdAllocator added in v0.1.3

func NewBeanIdAllocator(name, opts string) (BeanIdAllocator, error)

func NewFileBeanIdAllocator added in v0.1.3

func NewFileBeanIdAllocator(filename string) (BeanIdAllocator, error)

type Builder

type Builder struct {
	Packages       map[string]*Package
	SortedPackages []*Package
	// contains filtered or unexported fields
}

Builder

func Build

func Build(pkgs map[string]*ast.Package) (*Builder, error)

func NewBuilder

func NewBuilder() *Builder

func (*Builder) Decode

func (builder *Builder) Decode(s string) error

func (*Builder) Encode

func (builder *Builder) Encode() string

type ConstSpec

type ConstSpec struct {
	Doc     string
	Name    string
	Value   Expr
	Comment string
}

func BuildConstSpec

func BuildConstSpec(spec *ast.ConstSpec) *ConstSpec

func (ConstSpec) ValueString

func (c ConstSpec) ValueString() string

type EmbeddedPosition

type EmbeddedPosition string

func (EmbeddedPosition) IsValid

func (pos EmbeddedPosition) IsValid() bool

func (EmbeddedPosition) Match

func (pos EmbeddedPosition) Match(at string) bool

type EmbeddedValue

type EmbeddedValue struct {
	Text     string   `join:"text"`
	Template string   `json:"template"`
	Suffix   string   `json:"suffix"`
	Kinds    []string `json:"kinds"`
}

func (EmbeddedValue) IsValid

func (v EmbeddedValue) IsValid() bool

func (EmbeddedValue) MatchKind added in v0.1.2

func (v EmbeddedValue) MatchKind(kind string) bool

type Expr

type Expr interface {
	ExprNode()
}

Expr is an interface which represents expression node

func BuildExpr

func BuildExpr(expr ast.Expr) Expr

type ExprBase

type ExprBase struct{}

func (ExprBase) ExprNode

func (ExprBase) ExprNode()

type Extension added in v0.1.3

type Extension struct {
	Name    string   `json:"name"`
	Author  string   `json:"author"`
	URL     string   `json:"url"`
	Version string   `json:"version"`
	Deps    []string `json:"deps"`
	// language -> position -> embedded_values
	EmbeddedAt map[string]map[EmbeddedPosition][]EmbeddedValue `json:"at"`

	Path string `json:"path"`
}

func LoadExtensions

func LoadExtensions(rootdir string, names []string) ([]Extension, error)

func (Extension) Find added in v0.1.3

func (e Extension) Find(lang, kind, at string) []EmbeddedValue

type ExtensionKey added in v0.1.3

type ExtensionKey struct {
	Author string
	Name   string
}

func GetExtensionKey added in v0.1.3

func GetExtensionKey(name string) (ExtensionKey, error)

func (ExtensionKey) Path added in v0.1.3

func (key ExtensionKey) Path(rootdir string) string

func (ExtensionKey) Subdir added in v0.1.3

func (key ExtensionKey) Subdir() string

type Field

type Field struct {
	Doc     string
	Options []string
	Type    Type
	Names   []string
	Default Expr
	Tag     Tag
	Comment string
}

Field represents a field of struct or protocol

func BuildField

func BuildField(field *ast.Field) *Field

BuildField builds Field node to Field struct

func BuildFieldList

func BuildFieldList(fields *ast.FieldList) []*Field

BuildFieldList builds Field nodes to field struct slice

func (*Field) AddTag

func (field *Field) AddTag(key, value string)

AddTag adds key-value pair to tag

func (Field) GetTag

func (field Field) GetTag(key string) string

GetTag gets tag value for key

func (Field) HasTag

func (field Field) HasTag(key string) bool

HasTag checks if tag contains key

func (Field) Name

func (field Field) Name() (string, error)

Name returns name of field

func (Field) Value

func (field Field) Value() string

Value returns default value of field

type File

type File struct {
	Filename   string
	Doc        string
	Package    string
	Beans      []*Bean
	Decls      []*GenDecl
	Groups     []*Group
	Unresolved []string
}

func BuildFile

func BuildFile(file *ast.File) *File

type FuncType

type FuncType struct {
	TypeBase
	Params []*Field
	Result Type
}

func BuildFunc

func BuildFunc(t *ast.FuncType) *FuncType

type GenDecl

type GenDecl struct {
	Doc     string
	Imports []*ImportSpec
	Consts  []*ConstSpec
	Group   string
}

func BuildGenDecl

func BuildGenDecl(decl *ast.GenDecl) *GenDecl

type Group added in v0.1.3

type Group struct {
	Doc    string
	Name   string
	Tag    Tag
	Beans  []*Bean
	Decls  []*GenDecl
	Groups []*Group
	Parent string
}

func BuildGroup added in v0.1.3

func BuildGroup(group *ast.GroupDecl) *Group

func (*Group) IsNil added in v0.1.3

func (group *Group) IsNil() bool

type IdPair added in v0.1.3

type IdPair struct {
	Key string
	Id  int
}

type Ident

type Ident string

func (Ident) ExprNode

func (Ident) ExprNode()

type ImportSpec

type ImportSpec struct {
	Doc     string
	Name    string
	Package string
	Comment string
}

func BuildImportSpec

func BuildImportSpec(spec *ast.ImportSpec) *ImportSpec

type MapType

type MapType struct {
	TypeBase
	K Type
	V Type
}

func BuildMap

func BuildMap(t *ast.MapType) *MapType

func (MapType) IsMap added in v0.1.2

func (MapType) IsMap() bool

type ObjectId

type ObjectId string

func (ObjectId) IsValid

func (id ObjectId) IsValid() bool

func (ObjectId) Name

func (id ObjectId) Name() string

func (ObjectId) Package

func (id ObjectId) Package() string

func (ObjectId) String

func (id ObjectId) String() string

type Package

type Package struct {
	Name    string
	Imports map[string]string
	Files   []*File
}

func BuildPackage

func BuildPackage(pkg *ast.Package) *Package

func (*Package) FindBean added in v0.1.4

func (pkg *Package) FindBean(name string) *Bean

type Plugin

type Plugin struct {
	Lang         string `json:"lang"`
	Name         string `json:"name"`
	Bin          string `json:"bin"`
	TemplatesDir string `json:"templates,omitempty"`

	RuntimeConfig PluginRuntimeConfig `json:"-"`
}

func (Plugin) Generate

func (plugin Plugin) Generate(builder *Builder, stdout, stderr io.Writer) error

func (*Plugin) Init

func (plugin *Plugin) Init() error

type PluginRuntimeConfig

type PluginRuntimeConfig struct {
	Verbose       string
	Outdir        string
	ExtentionsDir string
	Extensions    []Extension
	Envvars       map[string]string
}

func (*PluginRuntimeConfig) BoolEnv

func (config *PluginRuntimeConfig) BoolEnv(name string) bool

func (*PluginRuntimeConfig) Decode

func (config *PluginRuntimeConfig) Decode(s string) error

func (PluginRuntimeConfig) Encode

func (config PluginRuntimeConfig) Encode() string

func (*PluginRuntimeConfig) FloatEnv

func (config *PluginRuntimeConfig) FloatEnv(name string) float64

func (*PluginRuntimeConfig) Getenv

func (config *PluginRuntimeConfig) Getenv(name string) string

func (*PluginRuntimeConfig) IntEnv

func (config *PluginRuntimeConfig) IntEnv(name string) int64

func (*PluginRuntimeConfig) UintEnv

func (config *PluginRuntimeConfig) UintEnv(name string) uint64

type PluginSet

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

func NewPluginSet

func NewPluginSet() *PluginSet

func (*PluginSet) Len

func (pset *PluginSet) Len() int

func (*PluginSet) Lookup

func (pset *PluginSet) Lookup(lang string) (*Plugin, bool)

func (*PluginSet) Register

func (pset *PluginSet) Register(plugin Plugin) error

type StructType

type StructType struct {
	TypeBase
	Package string
	Name    string
}

func BuildStruct

func BuildStruct(t *ast.StructType) *StructType

func (StructType) IsStruct added in v0.1.2

func (StructType) IsStruct() bool

func (StructType) String added in v0.1.3

func (t StructType) String(sep string) string

type Tag

type Tag string

Tag represents a tag

func BuildTag

func BuildTag(tag *ast.BasicLit) Tag

BuildTag builds BasicLit to Tag

func (Tag) Clone added in v0.1.3

func (tag Tag) Clone() *Tag

Clone clones a new tag

func (*Tag) Del added in v0.1.3

func (tag *Tag) Del(key string) string

Del deletes pair for key

func (Tag) Get

func (tag Tag) Get(key string) string

Get gets value for key

func (Tag) HasKey added in v0.1.3

func (tag Tag) HasKey(key string) bool

HasKey checks if key found in tag

func (Tag) Lookup

func (tag Tag) Lookup(key string) (value string, ok bool)

Lookup finds value for key

func (*Tag) Set

func (tag *Tag) Set(key, value string) string

Set sets value for key

func (Tag) String added in v0.1.3

func (tag Tag) String() string

String covnerts ta string

type Type

type Type interface {
	Expr
	TypeNode()
	IsArray() bool
	IsVector() bool
	IsMap() bool
	IsStruct() bool
	IsString() bool
	IsInt() bool
	IsFloat() bool
	IsBool() bool
}

func BuildType

func BuildType(typ ast.Type) Type

type TypeBase

type TypeBase struct {
	ExprBase
}

func (TypeBase) IsArray added in v0.1.2

func (TypeBase) IsArray() bool

func (TypeBase) IsBool added in v0.1.2

func (TypeBase) IsBool() bool

func (TypeBase) IsFloat added in v0.1.3

func (TypeBase) IsFloat() bool

func (TypeBase) IsInt added in v0.1.2

func (TypeBase) IsInt() bool

func (TypeBase) IsMap added in v0.1.2

func (TypeBase) IsMap() bool

func (TypeBase) IsString added in v0.1.2

func (TypeBase) IsString() bool

func (TypeBase) IsStruct added in v0.1.2

func (TypeBase) IsStruct() bool

func (TypeBase) IsVector added in v0.1.2

func (TypeBase) IsVector() bool

func (TypeBase) TypeNode

func (TypeBase) TypeNode()

type VectorType

type VectorType struct {
	TypeBase
	T Type
}

func BuildVector

func BuildVector(t *ast.VectorType) *VectorType

func (VectorType) IsVector added in v0.1.2

func (VectorType) IsVector() bool

Jump to

Keyboard shortcuts

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