From 9f9ef86b9eb5d6ef62f6d4e224a9e960f80a5cd1 Mon Sep 17 00:00:00 2001 From: lash Date: Fri, 31 Mar 2023 18:17:43 +0100 Subject: [PATCH] Add error to arg getset --- go/state/state.go | 10 +++++++--- go/vm/vm_test.go | 14 +++++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/go/state/state.go b/go/state/state.go index e6a0617..bf84f23 100644 --- a/go/state/state.go +++ b/go/state/state.go @@ -39,12 +39,16 @@ func(st State) WithCacheSize(cacheSize uint32) State { return st } -func(st *State) PutArg(input string) { +func(st *State) PutArg(input string) error { st.Arg = &input + return nil } -func(st *State) PopArg() string { - return *st.Arg +func(st *State) PopArg() (string, error) { + if st.Arg == nil { + return "", fmt.Errorf("arg is not set") + } + return *st.Arg, nil } func(st *State) Down(input string) { diff --git a/go/vm/vm_test.go b/go/vm/vm_test.go index fda715d..0c1c855 100644 --- a/go/vm/vm_test.go +++ b/go/vm/vm_test.go @@ -16,6 +16,7 @@ import ( var dynVal = "three" type TestResource struct { + state *state.State } func getOne(input []byte, ctx context.Context) (string, error) { @@ -30,6 +31,15 @@ func getDyn(input []byte, ctx context.Context) (string, error) { return dynVal, nil } +type TestStatefulResolver struct { + state *state.State +} + + +func (r *TestResource) getEachArg(input []byte, ctx context.Context) (string, error) { + return r.state.PopArg() +} + func (r *TestResource) Get(sym string) (string, error) { switch sym { case "foo": @@ -70,6 +80,8 @@ func (r *TestResource) FuncFor(sym string) (resource.EntryFunc, error) { return getTwo, nil case "dyn": return getDyn, nil + case "arg": + return r.getEachArg, nil } return nil, fmt.Errorf("invalid function: '%s'", sym) } @@ -92,7 +104,7 @@ func TestRun(t *testing.T) { _ = r } -func TestRunLoad(t *testing.T) { +func TestRunLoadRender(t *testing.T) { st := state.NewState(5) st.Down("barbarbar") rs := TestResource{}