Documentation
¶
Index ¶
- Variables
- func ApplyServiceFile(file *ast.File, action *dsl.Action) bool
- func BuildMainFile(projectName string) (string, error)
- func BuildModelFile(pkgName string, modelImports []string, stmts ...ast.Stmt) (string, error)
- func BuildRouterFile(pkgName string, modelImports []string, stmts ...ast.Stmt) (string, error)
- func BuildServiceFile(pkgName string, modelImports []string, stmts ...ast.Stmt) (string, error)
- func EmptyLine() *ast.EmptyStmt
- func FormatNode(node ast.Node, processImport ...bool) (string, error)
- func FormatNodeExtra(node ast.Node, processImport ...bool) (string, error)
- func FormatNodeExtraWithFileSet(node ast.Node, fset *token.FileSet, processImport ...bool) (string, error)
- func GenerateService(info *ModelInfo, action *dsl.Action, phase consts.Phase) *ast.File
- func GetModulePath() (string, error)
- func MethodAddComments(code string, modelName string) string
- func ResolveImportConflicts(imports []string) map[string]string
- func Returns(exprs ...ast.Expr) *ast.ReturnStmt
- func StmtLogInfo(str string) *ast.ExprStmt
- func StmtLogWithServiceContext(modelVarName string) *ast.AssignStmt
- func StmtModelRegister(modelName string) *ast.ExprStmt
- func StmtRouterRegister(modelPkgName, modelName, reqName, rspName string, router string, ...) *ast.ExprStmt
- func StmtServiceRegister(serviceImport string, phase consts.Phase) *ast.ExprStmt
- type ModelInfo
Constants ¶
This section is empty.
Variables ¶
var Methods = []string{ strcase.UpperCamelCase(string(consts.PHASE_CREATE_BEFORE)), strcase.UpperCamelCase(string(consts.PHASE_CREATE_AFTER)), strcase.UpperCamelCase(string(consts.PHASE_DELETE_BEFORE)), strcase.UpperCamelCase(string(consts.PHASE_DELETE_AFTER)), strcase.UpperCamelCase(string(consts.PHASE_UPDATE_BEFORE)), strcase.UpperCamelCase(string(consts.PHASE_UPDATE_AFTER)), strcase.UpperCamelCase(string(consts.PHASE_PATCH_BEFORE)), strcase.UpperCamelCase(string(consts.PHASE_PATCH_AFTER)), strcase.UpperCamelCase(string(consts.PHASE_LIST_BEFORE)), strcase.UpperCamelCase(string(consts.PHASE_LIST_AFTER)), strcase.UpperCamelCase(string(consts.PHASE_GET_BEFORE)), strcase.UpperCamelCase(string(consts.PHASE_GET_AFTER)), strcase.UpperCamelCase(string(consts.PHASE_CREATE_MANY_BEFORE)), strcase.UpperCamelCase(string(consts.PHASE_CREATE_MANY_AFTER)), strcase.UpperCamelCase(string(consts.PHASE_DELETE_MANY_BEFORE)), strcase.UpperCamelCase(string(consts.PHASE_DELETE_MANY_AFTER)), strcase.UpperCamelCase(string(consts.PHASE_UPDATE_MANY_BEFORE)), strcase.UpperCamelCase(string(consts.PHASE_UPDATE_MANY_AFTER)), strcase.UpperCamelCase(string(consts.PHASE_PATCH_MANY_BEFORE)), strcase.UpperCamelCase(string(consts.PHASE_PATCH_MANY_AFTER)), }
Functions ¶
func ApplyServiceFile ¶
ApplyServiceFile will apply the dsl.Action to the ast.File. It will modify the struct type and struct methods if Payload or Result is changed, and returns true. Otherwise returns false.
func BuildMainFile ¶
BuildMainFile generates a main.go file, the content like below:
package main
import (
"helloworld/configx" "helloworld/cronjob" _ "helloworld/model" "helloworld/router" "helloworld/service" "github.com/forbearing/gst/bootstrap" . "github.com/forbearing/gst/util"
)
func main() { RunOrDie(bootstrap.Bootstrap) RunOrDie(configx.Init) RunOrDie(cronjob.Init) RunOrDie(service.Init) RunOrDie(router.Init) RunOrDie(bootstrap.Run) }
func BuildModelFile ¶
BuildModelFile generates a model.go file, the content like below:
package model
import "github.com/forbearing/gst/model"
func init() { model.Register[*Group]() model.Register[*User]() }
func BuildRouterFile ¶
BuildRouterFile generates a router.go file, the content like below:
package router
import (
"helloworld/model" "github.com/forbearing/gst/router"
)
func Init() error { router.Register[*model.Group, *model.Group, *model.Group](router.Auth(), "group") router.Register[*model.User, *model.User, *model.User](router.Pub(), "user") return nil }
FIXME: process imports automatically problem.
func BuildServiceFile ¶
BuildServiceFile generates a service.go file, the content like below:
package service
import "github.com/forbearing/gst/service"
func Init() error { service.Register[*group]() service.Register[*user]() return nil }
FIXME: process imports automatically problem.
func FormatNode ¶
FormatNode use go standard lib "go/format" to format ast.Node into code.
func FormatNodeExtra ¶
FormatNodeExtra use "https://github.com/mvdan/gofumpt" to format ast.Node into code.
func FormatNodeExtraWithFileSet ¶
func FormatNodeExtraWithFileSet(node ast.Node, fset *token.FileSet, processImport ...bool) (string, error)
FormatNodeExtraWithFileSet 使用指定的 FileSet 格式化节点,保持注释位置
func GenerateService ¶
func GetModulePath ¶
GetModulePath parses go.mod to get module path
func MethodAddComments ¶
func ResolveImportConflicts ¶
ResolveImportConflicts detects import conflicts and generates unique aliases Returns a map where key is the import path and value is the alias (empty string means no alias needed)
func StmtLogInfo ¶
StmtLogInfo create *ast.ExprStmt represents `log.Info(str)`
func StmtLogWithServiceContext ¶
func StmtLogWithServiceContext(modelVarName string) *ast.AssignStmt
StmtLogWithServiceContext create *ast.AssignStmt represents `log := u.WithServiceContext(ctx, ctx.GetPhase())` modelVarName is model variable name.
func StmtModelRegister ¶
StmtModelRegister creates a *ast.ExprStmt represents golang code like below:
model.Register[*User]()
func StmtRouterRegister ¶
func StmtRouterRegister(modelPkgName, modelName, reqName, rspName string, router string, endpoint string, paramName string, verb string) *ast.ExprStmt
StmtRouterRegister creates a *ast.ExprStmt represents golang code like below:
router.Register[*model.Group, *model.Group, *model.Group](router.Auth(), "group", &types.ControllerConfig[*model.Group]{}, consts.Create) router.Register[*model.Group, *model.Group, *model.Group](router.Pub(), "login", &types.ControllerConfig[*auth.LoginReq]{}, consts.Create)
Types ¶
type ModelInfo ¶
type ModelInfo struct { // module related fields ModulePath string // module path parsed from go.mod // model related fields ModelPkgName string // model package name, e.g.: model, model_authz, model_log ModelName string // model name, e.g.: User, Group ModelVarName string // lowercase model variable name, e.g.: u, g ModelFileDir string // relative path of model file directory, e.g.: github.com/forbearing/gst/model ModelFilePath string // relative path of model file, e.g.: github.com/forbearing/gst/model/user.go // custom request and response related fields Design *dsl.Design }
ModelInfo stores model information
Examples: {ModulePath:"github.com/forbearing/gst", ModelPkgName:"model", ModelName:"User", ModelVarName:"u", ModelFileDir:"/tmp/model"}, {ModulePath:"github.com/forbearing/gst", ModelPkgName:"model", ModelName:"Group", ModelVarName:"g", ModelFileDir:"/tmp/model"}, {ModulePath:"github.com/forbearing/gst", ModelPkgName:"model_auth", ModelName:"User", ModelVarName:"u", ModelFileDir:"/tmp/model"}, {ModulePath:"github.com/forbearing/gst", ModelPkgName:"model_auth", ModelName:"Group", ModelVarName:"g", ModelFileDir:"/tmp/model"},
func FindModels ¶
FindModels finds all structs in model files