Move source files to root dir
This commit is contained in:
72
resource/fs.go
Normal file
72
resource/fs.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type FsResource struct {
|
||||
MenuResource
|
||||
Path string
|
||||
fns map[string]EntryFunc
|
||||
}
|
||||
|
||||
func NewFsResource(path string) (FsResource) {
|
||||
absPath, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return FsResource{
|
||||
Path: absPath,
|
||||
}
|
||||
}
|
||||
|
||||
func(fs FsResource) GetTemplate(sym string) (string, error) {
|
||||
fp := path.Join(fs.Path, sym)
|
||||
r, err := ioutil.ReadFile(fp)
|
||||
s := string(r)
|
||||
return strings.TrimSpace(s), err
|
||||
}
|
||||
|
||||
func(fs FsResource) GetCode(sym string) ([]byte, error) {
|
||||
fb := sym + ".bin"
|
||||
fp := path.Join(fs.Path, fb)
|
||||
return ioutil.ReadFile(fp)
|
||||
}
|
||||
|
||||
func(fs FsResource) FuncFor(sym string) (EntryFunc, error) {
|
||||
fn, ok := fs.fns[sym]
|
||||
if ok {
|
||||
return fn, nil
|
||||
}
|
||||
_, err := fs.getFuncNoCtx(sym)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unknown sym: %s", sym)
|
||||
}
|
||||
return fs.getFunc, nil
|
||||
}
|
||||
|
||||
func(fs FsResource) String() string {
|
||||
return fmt.Sprintf("fs resource at path: %s", fs.Path)
|
||||
}
|
||||
|
||||
func(fs FsResource) getFunc(sym string, ctx context.Context) (string, error) {
|
||||
return fs.getFuncNoCtx(sym)
|
||||
}
|
||||
|
||||
func(fs FsResource) getFuncNoCtx(sym string) (string, error) {
|
||||
fb := sym + ".txt"
|
||||
fp := path.Join(fs.Path, fb)
|
||||
log.Printf("getfunc search dir %s %s for %s", fs.Path, fp, sym)
|
||||
r, err := ioutil.ReadFile(fp)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed getting data for sym '%s': %v", sym, err)
|
||||
}
|
||||
s := string(r)
|
||||
return strings.TrimSpace(s), nil
|
||||
}
|
||||
10
resource/fs_test.go
Normal file
10
resource/fs_test.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package resource
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewFs(t *testing.T) {
|
||||
n := NewFsResource("./testdata")
|
||||
_ = n
|
||||
}
|
||||
61
resource/resource.go
Normal file
61
resource/resource.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
// EntryFunc is a function signature for retrieving value for a key
|
||||
type EntryFunc func(sym string, ctx context.Context) (string, error)
|
||||
type CodeFunc func(sym string) ([]byte, error)
|
||||
type TemplateFunc func(sym string) (string, error)
|
||||
type FuncForFunc func(sym string) (EntryFunc, error)
|
||||
|
||||
// Resource implementation are responsible for retrieving values and templates for symbols, and can render templates from value dictionaries.
|
||||
type Resource interface {
|
||||
GetTemplate(sym string) (string, error) // Get the template for a given symbol.
|
||||
GetCode(sym string) ([]byte, error) // Get the bytecode for the given symbol.
|
||||
FuncFor(sym string) (EntryFunc, error) // Resolve symbol content point for.
|
||||
}
|
||||
|
||||
type MenuResource struct {
|
||||
sinkValues []string
|
||||
codeFunc CodeFunc
|
||||
templateFunc TemplateFunc
|
||||
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
|
||||
}
|
||||
|
||||
func(m *MenuResource) FuncFor(sym string) (EntryFunc, error) {
|
||||
return m.funcFunc(sym)
|
||||
}
|
||||
|
||||
func(m *MenuResource) GetCode(sym string) ([]byte, error) {
|
||||
return m.codeFunc(sym)
|
||||
}
|
||||
|
||||
func(m *MenuResource) GetTemplate(sym string) (string, error) {
|
||||
return m.templateFunc(sym)
|
||||
}
|
||||
|
||||
54
resource/resource_test.go
Normal file
54
resource/resource_test.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
|
||||
type TestSizeResource struct {
|
||||
*MenuResource
|
||||
}
|
||||
|
||||
func getTemplate(sym string) (string, error) {
|
||||
var tpl string
|
||||
switch sym {
|
||||
case "small":
|
||||
tpl = "one {{.foo}} two {{.bar}} three {{.baz}}"
|
||||
case "toobig":
|
||||
tpl = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in mattis lorem. Aliquam erat volutpat. Ut vitae metus."
|
||||
case "pages":
|
||||
tpl = "one {{.foo}} two {{.bar}} three {{.baz}}\n{{.xyzzy}}"
|
||||
}
|
||||
return tpl, nil
|
||||
}
|
||||
|
||||
func funcFor(sym string) (EntryFunc, error) {
|
||||
switch sym {
|
||||
case "foo":
|
||||
return get, nil
|
||||
case "bar":
|
||||
return get, nil
|
||||
case "baz":
|
||||
return get, nil
|
||||
case "xyzzy":
|
||||
return getXyzzy, nil
|
||||
}
|
||||
return nil, fmt.Errorf("unknown func: %s", sym)
|
||||
}
|
||||
|
||||
func get(sym string, ctx context.Context) (string, error) {
|
||||
switch sym {
|
||||
case "foo":
|
||||
return "inky", nil
|
||||
case "bar":
|
||||
return "pinky", nil
|
||||
case "baz":
|
||||
return "blinky", nil
|
||||
}
|
||||
return "", fmt.Errorf("unknown sym: %s", sym)
|
||||
}
|
||||
|
||||
func getXyzzy(sym string, ctx context.Context) (string, error) {
|
||||
return "inky pinky\nblinky clyde sue\ntinkywinky dipsy\nlala poo\none two three four five six seven\neight nine ten\neleven twelve", nil
|
||||
}
|
||||
29
resource/state.go
Normal file
29
resource/state.go
Normal 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(sr *StateResource) WithState(st *state.State) *StateResource {
|
||||
if sr.st != nil {
|
||||
panic("state already set")
|
||||
}
|
||||
sr.st = st
|
||||
return sr
|
||||
}
|
||||
14
resource/state_test.go
Normal file
14
resource/state_test.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user