File size: 1,526 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package uik

import (
	"fmt"

	"github.com/expr-lang/expr"
	"github.com/expr-lang/expr/vm"
	"github.com/target/goalert/gadb"
)

// CompiledAction is a compiled version of a UIKActionV1.
type CompiledAction struct {
	gadb.UIKActionV1
	Params map[string]*vm.Program
}

// ParamError is an error that occurred while processing a param.
type ParamError struct {
	ParamID string
	Err     error
}

func (p *ParamError) Error() string {
	return fmt.Sprintf("param %s: %s", p.ParamID, p.Err)
}

// NewCompiledAction will compile a UIKActionV1 into a CompiledAction.
func NewCompiledAction(a gadb.UIKActionV1) (*CompiledAction, error) {
	res := &CompiledAction{
		UIKActionV1: a,
		Params:      make(map[string]*vm.Program, len(a.Params)),
	}
	for k, v := range a.Params {
		p, err := expr.Compile(v, expr.AllowUndefinedVariables(), expr.Optimize(true))
		if err != nil {
			return nil, &ParamError{
				ParamID: k,
				Err:     fmt.Errorf("compile: %w", err),
			}
		}
		res.Params[k] = p
	}
	return res, nil
}

// Run will execute the compiled action against the provided VM and environment.
func (a *CompiledAction) Run(vm *vm.VM, env any) (result gadb.UIKActionV1, err error) {
	result.ChannelID = a.ChannelID
	result.Dest = a.Dest
	result.Params = make(map[string]string, len(a.Params))
	for k, v := range a.Params {
		res, err := vm.Run(v, env)
		if err != nil {
			return result, &ParamError{
				ParamID: k,
				Err:     fmt.Errorf("run: %w", err),
			}
		}
		result.Params[k] = fmt.Sprintf("%v", res)
	}
	return result, nil
}