From 97dc090619eddcccb8162f84d074c8656c1b051c Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 8 Apr 2023 10:20:34 +0100 Subject: [PATCH] Add stateful resource navigation --- go/resource/resource.go | 4 ++++ go/resource/state.go | 29 +++++++++++++++++++++++++++++ go/resource/state_test.go | 14 ++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 go/resource/state.go create mode 100644 go/resource/state_test.go diff --git a/go/resource/resource.go b/go/resource/resource.go index c2a8a58..d10ef95 100644 --- a/go/resource/resource.go +++ b/go/resource/resource.go @@ -36,20 +36,24 @@ type MenuResource struct { funcFunc FuncForFunc } +// NewMenuResource creates a new MenuResource instance. func NewMenuResource() *MenuResource { return &MenuResource{} } +// WithCodeGetter sets the code symbol resolver method. func(m *MenuResource) WithCodeGetter(codeGetter CodeFunc) *MenuResource { m.codeFunc = codeGetter return m } +// WithEntryGetter sets the content symbol resolver getter method. func(m *MenuResource) WithEntryFuncGetter(entryFuncGetter FuncForFunc) *MenuResource { m.funcFunc = entryFuncGetter return m } +// WithTemplateGetter sets the template symbol resolver method. func(m *MenuResource) WithTemplateGetter(templateGetter TemplateFunc) *MenuResource { m.templateFunc = templateGetter return m diff --git a/go/resource/state.go b/go/resource/state.go new file mode 100644 index 0000000..5b0b8de --- /dev/null +++ b/go/resource/state.go @@ -0,0 +1,29 @@ +package resource + +import ( + "git.defalsify.org/festive/state" +) + +type StateResource struct { + Resource + st *state.State +} + +func ToStateResource(rs Resource) *StateResource { + return &StateResource{rs, nil} +} + +func NewStateResource(st *state.State) *StateResource { + return &StateResource { + NewMenuResource(), + st, + } +} + +func(s *StateResource) WithState(st *state.State) *StateResource { + if s.st != nil { + panic("state already set") + } + s.st = st + return s +} diff --git a/go/resource/state_test.go b/go/resource/state_test.go new file mode 100644 index 0000000..b743958 --- /dev/null +++ b/go/resource/state_test.go @@ -0,0 +1,14 @@ +package resource + +import ( + "testing" + + "git.defalsify.org/festive/state" +) + +func TestStateResourceInit(t *testing.T) { + st := state.NewState(0) + rs := NewMenuResource() + _ = ToStateResource(rs).WithState(&st) + _ = NewStateResource(&st) +}