2023-03-31 11:52:04 +02:00
|
|
|
package resource
|
|
|
|
|
2023-03-31 11:59:55 +02:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
2023-04-13 10:38:35 +02:00
|
|
|
// Result contains the results of an external code operation.
|
2023-04-13 10:16:17 +02:00
|
|
|
type Result struct {
|
2023-04-13 10:38:35 +02:00
|
|
|
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.
|
2023-04-13 10:16:17 +02:00
|
|
|
}
|
|
|
|
|
2023-03-31 23:35:13 +02:00
|
|
|
// EntryFunc is a function signature for retrieving value for a key
|
2023-04-13 10:16:17 +02:00
|
|
|
type EntryFunc func(sym string, input []byte, ctx context.Context) (Result, error)
|
2023-04-06 16:21:26 +02:00
|
|
|
type CodeFunc func(sym string) ([]byte, error)
|
2023-04-09 00:35:13 +02:00
|
|
|
type TemplateFunc func(sym string) (string, error)
|
2023-04-06 16:21:26 +02:00
|
|
|
type FuncForFunc func(sym string) (EntryFunc, error)
|
2023-03-31 11:52:04 +02:00
|
|
|
|
2023-03-31 23:35:13 +02:00
|
|
|
// Resource implementation are responsible for retrieving values and templates for symbols, and can render templates from value dictionaries.
|
|
|
|
type Resource interface {
|
2023-04-09 00:35:13 +02:00
|
|
|
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
|
|
|
}
|
2023-04-03 00:53:21 +02:00
|
|
|
|
2023-04-13 10:38:35 +02:00
|
|
|
// MenuResource contains the base definition for building Resource implementations.
|
|
|
|
//
|
|
|
|
// TODO: Rename to BaseResource
|
2023-04-03 00:53:21 +02:00
|
|
|
type MenuResource struct {
|
2023-04-07 12:31:30 +02:00
|
|
|
sinkValues []string
|
2023-04-06 16:21:26 +02:00
|
|
|
codeFunc CodeFunc
|
|
|
|
templateFunc TemplateFunc
|
|
|
|
funcFunc FuncForFunc
|
|
|
|
}
|
|
|
|
|
2023-04-08 11:20:34 +02:00
|
|
|
// NewMenuResource creates a new MenuResource instance.
|
2023-04-06 16:21:26 +02:00
|
|
|
func NewMenuResource() *MenuResource {
|
|
|
|
return &MenuResource{}
|
|
|
|
}
|
|
|
|
|
2023-04-08 11:20:34 +02:00
|
|
|
// WithCodeGetter sets the code symbol resolver method.
|
2023-04-06 16:21:26 +02:00
|
|
|
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.
|
2023-04-06 16:21:26 +02:00
|
|
|
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.
|
2023-04-06 16:21:26 +02:00
|
|
|
func(m *MenuResource) WithTemplateGetter(templateGetter TemplateFunc) *MenuResource {
|
|
|
|
m.templateFunc = templateGetter
|
|
|
|
return m
|
2023-04-03 10:11:44 +02:00
|
|
|
}
|
|
|
|
|
2023-04-13 10:38:35 +02:00
|
|
|
// FuncFor implements Resource interface
|
2023-04-06 16:21:26 +02:00
|
|
|
func(m *MenuResource) FuncFor(sym string) (EntryFunc, error) {
|
|
|
|
return m.funcFunc(sym)
|
|
|
|
}
|
|
|
|
|
2023-04-13 10:38:35 +02:00
|
|
|
// GetCode implements Resource interface
|
2023-04-06 16:21:26 +02:00
|
|
|
func(m *MenuResource) GetCode(sym string) ([]byte, error) {
|
|
|
|
return m.codeFunc(sym)
|
|
|
|
}
|
|
|
|
|
2023-04-13 10:38:35 +02:00
|
|
|
// GetTemplate implements Resource interface
|
2023-04-09 00:35:13 +02:00
|
|
|
func(m *MenuResource) GetTemplate(sym string) (string, error) {
|
|
|
|
return m.templateFunc(sym)
|
2023-04-06 16:21:26 +02:00
|
|
|
}
|
|
|
|
|