generator

package
v0.8.1 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	Skip   = "skip"
	Cover  = "cover"
	Append = "append"
)
View Source
const (
	RegisterFile = "router_gen.go"
)

Variables

View Source
var DefaultDelimiters = [2]string{"{{", "}}"}

Functions

func IsDefaultPackageTpl added in v0.5.1

func IsDefaultPackageTpl(name string) bool

func MapSerializer

func MapSerializer(serializer string) string

func SetDefaultTemplateConfig

func SetDefaultTemplateConfig()

Types

type Backend

type Backend interface {
	Template() (*template.Template, error)
	List() map[string]string
	SetOption(opts string) error
	GetOptions() []string
	Funcs(name string, fn interface{}) error
}

type Client

type Client struct {
	Handler
	ServiceName string
}

type ClientFile added in v0.5.0

type ClientFile struct {
	FilePath      string
	PackageName   string
	ServiceName   string
	BaseDomain    string
	Imports       map[string]*model.Model
	ClientMethods []*ClientMethod
}

type ClientMethod added in v0.5.0

type ClientMethod struct {
	*HttpMethod
	BodyParamsCode   string
	QueryParamsCode  string
	PathParamsCode   string
	HeaderParamsCode string
	FormValueCode    string
	FormFileCode     string
}

type CustomizedFileForIDL added in v0.5.1

type CustomizedFileForIDL struct {
	*IDLPackageRenderInfo
	FilePath    string
	FilePackage string
}

type CustomizedFileForMethod added in v0.5.1

type CustomizedFileForMethod struct {
	*HttpMethod
	FilePath       string
	FilePackage    string
	ServiceInfo    *Service              // service info for this method
	IDLPackageInfo *IDLPackageRenderInfo // IDL info for this service
}

type CustomizedFileForService added in v0.5.1

type CustomizedFileForService struct {
	*Service
	FilePath       string
	FilePackage    string
	IDLPackageInfo *IDLPackageRenderInfo // IDL info for this service
}

type File

type File struct {
	Path        string
	Content     string
	NoRepeat    bool
	FileTplName string
}

func (*File) Lint

func (file *File) Lint() error

Lint is used to statically analyze and format go code

type FilePathRenderInfo added in v0.5.1

type FilePathRenderInfo struct {
	MasterIDLName  string // master IDL name
	GenPackage     string // master IDL generate code package
	HandlerDir     string // handler generate dir
	ModelDir       string // model generate dir
	RouterDir      string // router generate dir
	ProjectDir     string // projectDir
	GoModule       string // go module
	ServiceName    string // service name, changed as services are traversed
	MethodName     string // method name, changed as methods are traversed
	HandlerGenPath string // "api.gen_path" value
}

type GolangBackend

type GolangBackend struct{}

func (*GolangBackend) Funcs

func (gb *GolangBackend) Funcs(name string, fn interface{}) error

func (*GolangBackend) GetOptions

func (gb *GolangBackend) GetOptions() []string

func (*GolangBackend) List

func (gb *GolangBackend) List() map[string]string

func (*GolangBackend) SetOption

func (gb *GolangBackend) SetOption(opts string) error

func (*GolangBackend) Template

func (gb *GolangBackend) Template() (*template.Template, error)

type Handler

type Handler struct {
	FilePath    string
	PackageName string
	ProjPackage string
	Imports     map[string]*model.Model
	Methods     []*HttpMethod
}

func (*Handler) Format

func (h *Handler) Format()

type HttpMethod

type HttpMethod struct {
	Name               string
	HTTPMethod         string
	Comment            string
	RequestTypeName    string
	RequestTypePackage string
	RequestTypeRawName string
	ReturnTypeName     string
	ReturnTypePackage  string
	ReturnTypeRawName  string
	Path               string
	Serializer         string
	OutputDir          string
	RefPackage         string // handler import dir
	RefPackageAlias    string // handler import alias
	ModelPackage       map[string]string
	GenHandler         bool // Whether to generate one handler, when an idl interface corresponds to multiple http method
	// Annotations     map[string]string
	Models map[string]*model.Model
}

func (*HttpMethod) InitComment

func (m *HttpMethod) InitComment()

type HttpPackage

type HttpPackage struct {
	IdlName    string
	Package    string
	Services   []*Service
	Models     []*model.Model
	RouterInfo *Router
}

type HttpPackageGenerator

type HttpPackageGenerator struct {
	ConfigPath     string       // package template path
	Backend        meta.Backend // model template
	Options        []Option
	CmdType        string
	ProjPackage    string // go module for project
	HandlerDir     string
	RouterDir      string
	ModelDir       string
	UseDir         string // model dir for third repo
	ClientDir      string // client dir for "new"/"update" command
	IdlClientDir   string // client dir for "client" command
	ForceClientDir string // client dir without namespace for "client" command
	BaseDomain     string // request domain for "client" command
	ServiceGenDir  string

	NeedModel            bool
	HandlerByMethod      bool // generate handler files with method dimension
	SnakeStyleMiddleware bool // use snake name style for middleware

	TemplateGenerator
	// contains filtered or unexported fields
}

HttpPackageGenerator is used to record the configuration related to generating hertz http code.

func (*HttpPackageGenerator) GenModel

func (pkgGen *HttpPackageGenerator) GenModel(data *model.Model, gen bool) error

func (*HttpPackageGenerator) Generate

func (pkgGen *HttpPackageGenerator) Generate(pkg *HttpPackage) error

func (*HttpPackageGenerator) Init

func (pkgGen *HttpPackageGenerator) Init() error

func (*HttpPackageGenerator) LoadBackend

func (pkgGen *HttpPackageGenerator) LoadBackend(backend meta.Backend) error

type IDLPackageRenderInfo added in v0.5.1

type IDLPackageRenderInfo struct {
	FilePathRenderInfo
	ServiceInfos *HttpPackage
}

type Layout

type Layout struct {
	OutDir          string
	GoModule        string
	ServiceName     string
	UseApacheThrift bool
	HasIdl          bool
	NeedGoMod       bool
	ModelDir        string
	HandlerDir      string
	RouterDir       string
}

Layout contains the basic information of idl

type LayoutGenerator

type LayoutGenerator struct {
	ConfigPath string
	TemplateGenerator
}

LayoutGenerator contains the information generated by generating the layout template

func (*LayoutGenerator) Degenerate

func (lg *LayoutGenerator) Degenerate() error

func (*LayoutGenerator) Generate

func (lg *LayoutGenerator) Generate(data map[string]interface{}) error

func (*LayoutGenerator) GenerateByConfig

func (lg *LayoutGenerator) GenerateByConfig(configPath string) error

func (*LayoutGenerator) GenerateByService

func (lg *LayoutGenerator) GenerateByService(service Layout) error

func (*LayoutGenerator) Init

func (lg *LayoutGenerator) Init() error

type Option

type Option string
const (
	OptionMarshalEnumToText  Option = "MarshalEnumToText"
	OptionTypedefAsTypeAlias Option = "TypedefAsTypeAlias"
)

type RegisterInfo added in v0.6.0

type RegisterInfo struct {
	PackageName string
	DepPkgAlias string
	DepPkg      string
}

type Router

type Router struct {
	FilePath        string
	PackageName     string
	HandlerPackages map[string]string // {{basename}}:{{import_path}}
	Router          *RouterNode
}

type RouterNode

type RouterNode struct {
	GroupName         string // current group name(the parent middleware name), used to register route. example: {{.GroupName}}.{{HttpMethod}}
	MiddleWare        string // current node middleware, used to be group name for children.
	HandlerMiddleware string
	GroupMiddleware   string
	PathPrefix        string

	Path     string
	Parent   *RouterNode
	Children childrenRouterInfo

	Handler             string // {{HandlerPackage}}.{{HandlerName}}
	HandlerPackage      string
	HandlerPackageAlias string
	HttpMethod          string
}

func NewRouterTree

func NewRouterTree() *RouterNode

NewRouterTree contains "/" as root node

func (*RouterNode) DFS

func (routerNode *RouterNode) DFS(i int, hook func(layer int, node *RouterNode) error) error

func (*RouterNode) DyeGroupName

func (routerNode *RouterNode) DyeGroupName(snakeStyleMiddleware bool) error

DyeGroupName traverses the routing tree in depth and names the handler/group middleware for each node. If snakeStyleMiddleware is set to true, the name style of the middleware will use snake name style.

func (*RouterNode) FindNearest

func (routerNode *RouterNode) FindNearest(paths []string) (*RouterNode, int)

func (*RouterNode) Insert

func (routerNode *RouterNode) Insert(name string, method *HttpMethod, handlerType string, paths []string, handlerPkg string)

func (*RouterNode) RawHandlerName added in v0.6.2

func (routerNode *RouterNode) RawHandlerName() string

func (*RouterNode) Sort

func (routerNode *RouterNode) Sort()

func (*RouterNode) Update

func (routerNode *RouterNode) Update(method *HttpMethod, handlerType, handlerPkg string) error

type Service

type Service struct {
	Name          string
	Methods       []*HttpMethod
	ClientMethods []*ClientMethod
	Models        []*model.Model // all dependency models
	BaseDomain    string         // base domain for client code
	ServiceGroup  string         // service level router group
	ServiceGenDir string         // handler_dir for handler_by_service
}

type SingleHandler added in v0.8.0

type SingleHandler struct {
	*HttpMethod
	FilePath    string
	PackageName string
	ProjPackage string
}

type Template

type Template struct {
	Default        bool           // Is it the default template
	Path           string         `yaml:"path"`            // The generated path and its filename, such as biz/handler/ping.go
	Delims         [2]string      `yaml:"delims"`          // Template Action Instruction Identifier, default: "{{}}"
	Body           string         `yaml:"body"`            // Render template, currently only supports go template syntax
	Disable        bool           `yaml:"disable"`         // Disable generating file, used to disable default package template
	LoopMethod     bool           `yaml:"loop_method"`     // Loop generate files based on "method"
	LoopService    bool           `yaml:"loop_service"`    // Loop generate files based on "service"
	UpdateBehavior UpdateBehavior `yaml:"update_behavior"` // Update command behavior; 0:unchanged, 1:regenerate, 2:append
}

type TemplateConfig

type TemplateConfig struct {
	Layouts []Template `yaml:"layouts"`
}

type TemplateGenerator

type TemplateGenerator struct {
	OutputDir string
	Config    *TemplateConfig
	Excludes  []string
	// contains filtered or unexported fields
}

TemplateGenerator contains information about the output template

func (*TemplateGenerator) Degenerate

func (tg *TemplateGenerator) Degenerate() error

func (*TemplateGenerator) Files

func (tg *TemplateGenerator) Files() []File

func (*TemplateGenerator) Generate

func (tg *TemplateGenerator) Generate(input interface{}, tplName, filepath string, noRepeat bool) error

func (*TemplateGenerator) GetFormatAndExcludedFiles

func (tg *TemplateGenerator) GetFormatAndExcludedFiles() ([]File, error)

func (*TemplateGenerator) Init

func (tg *TemplateGenerator) Init() error

func (*TemplateGenerator) Persist

func (tg *TemplateGenerator) Persist() error

type UpdateBehavior added in v0.5.1

type UpdateBehavior struct {
	Type string `yaml:"type"` // Update behavior type: skip/cover/append
	// the following variables are used for append update
	AppendKey      string   `yaml:"append_key"`         // Append content based in key; for example: 'method'/'service'
	InsertKey      string   `yaml:"insert_key"`         // Insert content by "insert_key"
	AppendTpl      string   `yaml:"append_content_tpl"` // Append content if UpdateBehavior is "append"
	ImportTpl      []string `yaml:"import_tpl"`         // Import insert template
	AppendLocation string   `yaml:"append_location"`    // AppendLocation specifies the location of append,  the default is the end of the file
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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