feat: Pipes now have access to the full template context.

This commit is contained in:
Spencer Brower
2026-07-23 11:38:17 -04:00
parent b3608fe493
commit 4560014954
2 changed files with 20 additions and 14 deletions
+5 -4
View File
@@ -2,6 +2,7 @@ package mustache
import "core:fmt"
import "core:log"
import "core:reflect"
import "core:strings"
// ---------------------------------------------------------------------------
@@ -556,7 +557,7 @@ render_nodes :: proc(
warn_unknown_key(current, ctx[:], node)
}
if len(node.filters) > 0 {
transformed, perr := apply_pipeline(val, node.filters[:], node.pos)
transformed, perr := apply_pipeline(val, node.filters[:], node.pos, ctx[:])
if perr != nil {
return perr
}
@@ -598,7 +599,7 @@ render_nodes :: proc(
warn_unknown_key(current, ctx[:], node)
}
if len(node.filters) > 0 {
transformed, perr := apply_pipeline(val, node.filters[:], node.pos)
transformed, perr := apply_pipeline(val, node.filters[:], node.pos, ctx[:])
if perr != nil {
return perr
}
@@ -636,7 +637,7 @@ render_nodes :: proc(
warn_unknown_key(current, ctx[:], node)
}
if len(node.filters) > 0 {
transformed, perr := apply_pipeline(val, node.filters[:], node.pos)
transformed, perr := apply_pipeline(val, node.filters[:], node.pos, ctx[:])
if perr != nil {
return perr
}
@@ -692,7 +693,7 @@ render_nodes :: proc(
warn_unknown_key(current, ctx[:], node)
}
if len(node.filters) > 0 {
transformed, perr := apply_pipeline(val, node.filters[:], node.pos)
transformed, perr := apply_pipeline(val, node.filters[:], node.pos, ctx[:])
if perr != nil {
return perr
}
+14 -9
View File
@@ -99,19 +99,24 @@ parse_pipeline :: proc(
return key, nil
}
apply_pipeline :: proc(value: any, filters: []Pipe_Filter, pos: int) -> (any, Error) {
current := value
apply_pipeline :: proc(
value: any,
filters: []Pipe_Filter,
pos: int,
ctx: []any,
) -> (
current: any,
err: Error,
) {
current = value
for &filter in filters {
result, err := apply_filter(current, &filter, pos)
if err != nil {
return nil, err
current = apply_filter(current, &filter, pos, ctx) or_return
}
current = result
}
return current, nil
return
}
apply_filter :: proc(value: any, filter: ^Pipe_Filter, pos: int) -> (any, Error) {
// TODO: diagnostics don't show anything relevent
apply_filter :: proc(value: any, filter: ^Pipe_Filter, pos: int, ctx: []any) -> (any, Error) {
switch filter.op {
case "group_by":
return apply_group_by(value, filter.args[:], pos)