generator

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2025 License: Apache-2.0 Imports: 23 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

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

Variables

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

Functions

func IsDefaultPackageTpl

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 ClientConfig

type ClientConfig struct {
	QueryEnumAsInt bool
}

type ClientFile

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

type ClientMethod

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

type CustomizedFileForIDL

type CustomizedFileForIDL struct {
	*IDLPackageRenderInfo
	FilePath    string
	FilePackage string
}

type CustomizedFileForMethod

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

type CustomizedFileForService

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

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 {
	ServiceGroup   string
	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
	QueryEnumAsInt bool   // client code use number for query parameter
	ServiceGenDir  string

	NeedModel            bool
	HandlerByMethod      bool // generate handler files with method dimension
	SnakeStyleMiddleware bool // use snake name style for middleware
	SortRouter           bool
	ForceUpdateClient    bool // force update 'crafter_client.go'

	TemplateGenerator
	// contains filtered or unexported fields
}

HttpPackageGenerator is used to record the configuration related to generating crafter 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

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

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, method string, sortRouter bool) (*RouterNode, int)

func (*RouterNode) Insert

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

func (*RouterNode) RawHandlerName

func (routerNode *RouterNode) RawHandlerName() string

func (*RouterNode) Sort

func (routerNode *RouterNode) Sort()

func (*RouterNode) Update

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

type Service

type Service struct {
	Name          string
	Version       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

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

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