commit f1cf589ad6e8b081b1a92f1350bc47f60e13f123
parent d3a358d6eaa600d4ccb8c37b2a2b26490c9b06b8
Author: lash <dev@holbrook.no>
Date: Sun, 15 Sep 2024 02:16:08 +0100
Add input validator access in engine, complete example
Diffstat:
1 file changed, 17 insertions(+), 2 deletions(-)
diff --git a/engine/db.go b/engine/db.go
@@ -26,6 +26,7 @@ type DefaultEngine struct {
first resource.EntryFunc
initd bool
exit string
+ regexCount int
}
// NewEngine instantiates the default Engine implementation.
@@ -123,6 +124,21 @@ func(en *DefaultEngine) WithFirst(fn resource.EntryFunc) *DefaultEngine {
return en
}
+// AddValidInput defines a regular expressing string to match input against.
+//
+// The added regular expression will be evaluated after the builtin match (see
+// vm/input.go for the actual string details).
+//
+// The function may be called more than once. Input will be validated against each
+// in the sequence they were added.
+//
+// When a match is found, remaining regular expressions will be skipped.
+func(en *DefaultEngine) AddValidInput(re string) error {
+ err := vm.RegisterInputValidator(en.regexCount, re)
+ en.regexCount += 1
+ return err
+}
+
// ensure state is present in engine.
func(en *DefaultEngine) ensureState() {
if en.st == nil {
@@ -366,7 +382,6 @@ func(en *DefaultEngine) init(ctx context.Context, input []byte) (bool, error) {
logg.DebugCtxf(ctx, "already initialized")
return true, nil
}
-
sym := en.cfg.Root
if sym == "" {
@@ -374,7 +389,7 @@ func(en *DefaultEngine) init(ctx context.Context, input []byte) (bool, error) {
}
inSave, _ := en.st.GetInput()
- err = en.st.SetInput([]byte{})
+ err = en.st.SetInput(input)
if err != nil {
return false, err
}