Documentation
¶
Index ¶
- Variables
- func ApplyServiceFile(file *ast.File, action *dsl.Action, servicePkgName string) bool
- func ApplyServiceFileWithModelSync(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. The servicePkgName parameter specifies the expected package name for the service file. This should match the package name used in service registration to maintain consistency.
func ApplyServiceFileWithModelSync ¶ added in v0.10.0
func ApplyServiceFileWithModelSync(file *ast.File, action *dsl.Action, servicePkgName, correctModelImportPath, correctModelPkgName string) bool
ApplyServiceFileWithModelSync extends ApplyServiceFile to handle import path and package name updates. It will update import statements and package references when model packages are renamed.
Design Philosophy: This function uses AST manipulation instead of regenerating files to preserve user's code formatting and custom modifications. Different developers have different code formatting preferences, and we should not force our formatting on their existing code. We only update the necessary parts (imports and type references) while keeping everything else intact.
Example transformation when "model/oldpkg" is renamed to "model/newpkg": - Import statement: "myproject/model/oldpkg" -> "myproject/model/newpkg" - Type references: oldpkg.User -> newpkg.User, oldpkg.UserReq -> newpkg.UserReq, oldpkg.UserRsp -> newpkg.UserRsp
Parameters: - file: The AST file to process - action: The DSL action configuration - servicePkgName: The expected service package name - correctModelImportPath: The correct model import path (e.g., "myproject/model/auth") - correctModelPkgName: The correct model package name (e.g., "auth")
Returns true if any changes were made to the file.
func BuildMainFile ¶
BuildMainFile generates a main.go file, the content like below:
package main
import (
"helloworld/configx" _ "helloworld/cronjob" _ "helloworld/middleware" _ "helloworld/model" _ "helloworld/module" "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