vise/resource/resource.go

75 lines
2.3 KiB
Go
Raw Permalink Normal View History

2023-03-31 11:52:04 +02:00
package resource
2023-03-31 11:59:55 +02:00
import (
"context"
)
// Result contains the results of an external code operation.
type Result struct {
Content string // content value for symbol after execution.
FlagSet []uint32 // request caller to set error flags at given indices.
FlagReset []uint32 // request caller to reset error flags at given indices.
}
// EntryFunc is a function signature for retrieving value for a key
type EntryFunc func(sym string, input []byte, ctx context.Context) (Result, error)
type CodeFunc func(sym string) ([]byte, error)
type TemplateFunc func(sym string) (string, error)
type FuncForFunc func(sym string) (EntryFunc, error)
2023-03-31 11:52:04 +02:00
// 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.
2023-04-01 11:58:02 +02:00
GetCode(sym string) ([]byte, error) // Get the bytecode for the given symbol.
2023-04-06 13:08:30 +02:00
FuncFor(sym string) (EntryFunc, error) // Resolve symbol content point for.
2023-03-31 11:52:04 +02:00
}
// MenuResource contains the base definition for building Resource implementations.
//
// TODO: Rename to BaseResource
type MenuResource struct {
sinkValues []string
codeFunc CodeFunc
templateFunc TemplateFunc
funcFunc FuncForFunc
}
2023-04-08 11:20:34 +02:00
// NewMenuResource creates a new MenuResource instance.
func NewMenuResource() *MenuResource {
return &MenuResource{}
}
2023-04-08 11:20:34 +02:00
// WithCodeGetter sets the code symbol resolver method.
func(m *MenuResource) WithCodeGetter(codeGetter CodeFunc) *MenuResource {
m.codeFunc = codeGetter
return m
}
2023-04-08 11:20:34 +02:00
// WithEntryGetter sets the content symbol resolver getter method.
func(m *MenuResource) WithEntryFuncGetter(entryFuncGetter FuncForFunc) *MenuResource {
m.funcFunc = entryFuncGetter
return m
}
2023-04-08 11:20:34 +02:00
// WithTemplateGetter sets the template symbol resolver method.
func(m *MenuResource) WithTemplateGetter(templateGetter TemplateFunc) *MenuResource {
m.templateFunc = templateGetter
return m
2023-04-03 10:11:44 +02:00
}
// FuncFor implements Resource interface
func(m *MenuResource) FuncFor(sym string) (EntryFunc, error) {
return m.funcFunc(sym)
}
// GetCode implements Resource interface
func(m *MenuResource) GetCode(sym string) ([]byte, error) {
return m.codeFunc(sym)
}
// GetTemplate implements Resource interface
func(m *MenuResource) GetTemplate(sym string) (string, error) {
return m.templateFunc(sym)
}