gen

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2025 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func ApplyServiceFile(file *ast.File, action *dsl.Action) bool

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

func BuildMainFile(projectName string) (string, error)

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

func BuildModelFile(pkgName string, modelImports []string, stmts ...ast.Stmt) (string, error)

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

func BuildRouterFile(pkgName string, modelImports []string, stmts ...ast.Stmt) (string, error)

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

func BuildServiceFile(pkgName string, modelImports []string, stmts ...ast.Stmt) (string, error)

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 EmptyLine

func EmptyLine() *ast.EmptyStmt

func FormatNode

func FormatNode(node ast.Node, processImport ...bool) (string, error)

FormatNode use go standard lib "go/format" to format ast.Node into code.

func FormatNodeExtra

func FormatNodeExtra(node ast.Node, processImport ...bool) (string, error)

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 GenerateService(info *ModelInfo, action *dsl.Action, phase consts.Phase) *ast.File

func GetModulePath

func GetModulePath() (string, error)

GetModulePath parses go.mod to get module path

func MethodAddComments

func MethodAddComments(code string, modelName string) string

func ResolveImportConflicts

func ResolveImportConflicts(imports []string) map[string]string

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 Returns

func Returns(exprs ...ast.Expr) *ast.ReturnStmt

func StmtLogInfo

func StmtLogInfo(str string) *ast.ExprStmt

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

func StmtModelRegister(modelName string) *ast.ExprStmt

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)

func StmtServiceRegister

func StmtServiceRegister(serviceImport string, phase consts.Phase) *ast.ExprStmt

StmtServiceRegister creates a *ast.ExprStmt represents golang code like below:

service.Register[*user.Creator](consts.PHASE_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

func FindModels(module string, modelDir string, filename string) ([]*ModelInfo, error)

FindModels finds all structs in model files

func (*ModelInfo) ModelImportPath

func (m *ModelInfo) ModelImportPath() (string, bool)

func (*ModelInfo) RouterImportPath

func (m *ModelInfo) RouterImportPath() string

func (*ModelInfo) ServiceImportPath

func (m *ModelInfo) ServiceImportPath(modelDir, serviceDir string) string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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