新增: @use 同文件片段复用

支持 @use("name") 引用同一文件内 @tpl 定义的块,
消除 _list/_count 模板中 WHERE 条件重复问题。
This commit is contained in:
2026-04-01 01:59:51 +08:00
parent a6847c7d18
commit 1b5b6aff8f
7 changed files with 190 additions and 8 deletions

View File

@@ -15,6 +15,7 @@ type Executor struct {
style PlaceholderStyle
rawPolicy rawValidator
strict bool
blocks map[string][]Node
}
type Result struct {
@@ -22,11 +23,12 @@ type Result struct {
Args []any
}
func NewExecutor(style PlaceholderStyle, rawPolicy rawValidator, strict bool) *Executor {
func NewExecutor(style PlaceholderStyle, rawPolicy rawValidator, strict bool, blocks map[string][]Node) *Executor {
return &Executor{
style: style,
rawPolicy: rawPolicy,
strict: strict,
blocks: blocks,
}
}
@@ -99,6 +101,17 @@ func (e *Executor) walk(ctx *Context, ph *Placeholder, sql *strings.Builder, arg
case *BlockNode, *NamespaceNode, *IncludeNode, *CommentNode:
// skip
case *UseNode:
if e.blocks == nil {
return fmt.Errorf("line %d, col %d: @use(\"%s\") no blocks available", n.Pos.Line, n.Pos.Col, n.Name)
}
blockNodes, ok := e.blocks[n.Name]
if !ok {
return fmt.Errorf("line %d, col %d: @use(\"%s\") block not found", n.Pos.Line, n.Pos.Col, n.Name)
}
if err := e.walk(ctx, ph, sql, args, blockNodes); err != nil {
return err
}
}
}
return nil