salix/for_tag.go

109 lines
2.4 KiB
Go
Raw Normal View History

2023-10-29 01:10:17 +00:00
package salix
import (
2024-02-20 20:07:39 +00:00
"errors"
2023-10-29 01:10:17 +00:00
"reflect"
2023-10-31 01:38:53 +00:00
"go.elara.ws/salix/ast"
2023-10-29 01:10:17 +00:00
)
// forTag represents a #for tag within a Salix template
2023-10-29 01:10:17 +00:00
type forTag struct{}
func (ft forTag) Run(tc *TagContext, block, args []ast.Node) error {
2024-02-20 20:07:39 +00:00
if len(args) == 0 || len(args) > 3 {
2023-12-18 00:55:13 +00:00
return tc.PosError(tc.Tag, "invalid argument amount")
2023-10-29 01:10:17 +00:00
}
2024-02-20 20:07:39 +00:00
expr, ok := args[len(args)-1].(ast.Expr)
if !ok {
return tc.PosError(args[0], "invalid argument type: %T (expected ast.Expr)", args[0])
2023-10-29 01:10:17 +00:00
}
var vars []string
var in reflect.Value
2024-02-20 20:07:39 +00:00
if len(args) > 1 {
for _, arg := range args[:len(args)-1] {
varName, ok := unwrap(arg).(ast.Ident)
if !ok {
return tc.PosError(arg, "invalid argument type: %T (expected ast.Ident)", expr.First)
}
vars = append(vars, varName.Value)
2023-10-29 01:10:17 +00:00
}
}
varName, ok := unwrap(expr.First).(ast.Ident)
2023-10-29 01:10:17 +00:00
if !ok {
2023-12-18 00:55:13 +00:00
return tc.PosError(expr.First, "invalid argument type: %T (expected ast.Ident)", args[0])
2023-10-29 01:10:17 +00:00
}
vars = append(vars, varName.Value)
if len(expr.Rest) != 1 {
2023-12-18 00:55:13 +00:00
return tc.PosError(expr.First, "invalid expression (expected 1 element, got %d)", len(expr.Rest))
2023-10-29 01:10:17 +00:00
}
rest := expr.Rest[0]
if rest.Operator.Value != "in" {
2023-12-18 00:55:13 +00:00
return tc.PosError(expr.First, `invalid operator in expression (expected "in", got %q)`, rest.Operator.Value)
2023-10-29 01:10:17 +00:00
}
val, err := tc.GetValue(rest, nil)
if err != nil {
return err
}
in = reflect.ValueOf(val)
switch in.Kind() {
case reflect.Slice, reflect.Array:
local := map[string]any{}
for i := 0; i < in.Len(); i++ {
if len(vars) == 1 {
local[vars[0]] = in.Index(i).Interface()
} else if len(vars) == 2 {
local[vars[0]] = i
local[vars[1]] = in.Index(i).Interface()
2024-02-20 20:07:39 +00:00
} else {
return errors.New("slices and arrays can only use two for loop variables")
2023-10-29 01:10:17 +00:00
}
err = tc.Execute(block, local)
if err != nil {
return err
}
}
case reflect.Map:
local := map[string]any{}
iter := in.MapRange()
2024-02-20 20:07:39 +00:00
i := 0
2023-10-29 01:10:17 +00:00
for iter.Next() {
if len(vars) == 1 {
local[vars[0]] = iter.Value().Interface()
} else if len(vars) == 2 {
local[vars[0]] = iter.Key().Interface()
local[vars[1]] = iter.Value().Interface()
2024-02-20 20:07:39 +00:00
} else if len(vars) == 3 {
local[vars[0]] = i
local[vars[1]] = iter.Key().Interface()
local[vars[2]] = iter.Value().Interface()
2023-10-29 01:10:17 +00:00
}
err = tc.Execute(block, local)
if err != nil {
return err
}
2024-02-20 20:07:39 +00:00
i++
2023-10-29 01:10:17 +00:00
}
}
return nil
}
func unwrap(n ast.Node) ast.Node {
if v, ok := n.(ast.Value); ok {
return v.Node
}
return n
}