Add stateful resource navigation

This commit is contained in:
lash 2023-04-08 10:20:34 +01:00
parent adbd790c6f
commit 97dc090619
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 47 additions and 0 deletions

View File

@ -36,20 +36,24 @@ type MenuResource struct {
funcFunc FuncForFunc funcFunc FuncForFunc
} }
// NewMenuResource creates a new MenuResource instance.
func NewMenuResource() *MenuResource { func NewMenuResource() *MenuResource {
return &MenuResource{} return &MenuResource{}
} }
// WithCodeGetter sets the code symbol resolver method.
func(m *MenuResource) WithCodeGetter(codeGetter CodeFunc) *MenuResource { func(m *MenuResource) WithCodeGetter(codeGetter CodeFunc) *MenuResource {
m.codeFunc = codeGetter m.codeFunc = codeGetter
return m return m
} }
// WithEntryGetter sets the content symbol resolver getter method.
func(m *MenuResource) WithEntryFuncGetter(entryFuncGetter FuncForFunc) *MenuResource { func(m *MenuResource) WithEntryFuncGetter(entryFuncGetter FuncForFunc) *MenuResource {
m.funcFunc = entryFuncGetter m.funcFunc = entryFuncGetter
return m return m
} }
// WithTemplateGetter sets the template symbol resolver method.
func(m *MenuResource) WithTemplateGetter(templateGetter TemplateFunc) *MenuResource { func(m *MenuResource) WithTemplateGetter(templateGetter TemplateFunc) *MenuResource {
m.templateFunc = templateGetter m.templateFunc = templateGetter
return m return m

29
go/resource/state.go Normal file
View File

@ -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
}

14
go/resource/state_test.go Normal file
View File

@ -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)
}