2023-03-31 11:52:04 +02:00
|
|
|
package resource
|
|
|
|
|
2023-03-31 11:59:55 +02:00
|
|
|
import (
|
|
|
|
"context"
|
2023-04-03 00:53:21 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2023-04-07 10:30:23 +02:00
|
|
|
"strings"
|
2023-03-31 11:59:55 +02:00
|
|
|
)
|
|
|
|
|
2023-03-31 23:35:13 +02:00
|
|
|
// EntryFunc is a function signature for retrieving value for a key
|
|
|
|
type EntryFunc func(ctx context.Context) (string, error)
|
2023-04-06 16:21:26 +02:00
|
|
|
type CodeFunc func(sym string) ([]byte, error)
|
|
|
|
type TemplateFunc func(sym string, sizer *Sizer) (string, error)
|
|
|
|
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-06 16:21:26 +02:00
|
|
|
GetTemplate(sym string, sizer *Sizer) (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-07 12:31:30 +02:00
|
|
|
RenderTemplate(sym string, values map[string]string, idx uint16, sizer *Sizer) (string, error) // Render the given data map using the template of the symbol.
|
|
|
|
Render(sym string, values map[string]string, idx uint16, sizer *Sizer) (string, error) // Render full output.
|
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
|
|
|
|
|
|
|
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-07 10:30:23 +02:00
|
|
|
// render menu and all syms except sink, split sink into display chunks
|
2023-04-07 12:31:30 +02:00
|
|
|
func(m *MenuResource) prepare(sym string, values map[string]string, idx uint16, sizer *Sizer) (map[string]string, error) {
|
|
|
|
var sink string
|
2023-04-07 10:30:23 +02:00
|
|
|
var sinkValues []string
|
|
|
|
noSinkValues := make(map[string]string)
|
|
|
|
for k, v := range values {
|
|
|
|
sz, err := sizer.Size(k)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if sz == 0 {
|
2023-04-07 12:31:30 +02:00
|
|
|
sink = k
|
2023-04-07 10:30:23 +02:00
|
|
|
sinkValues = strings.Split(v, "\n")
|
|
|
|
v = ""
|
|
|
|
log.Printf("found sink %s with field count %v", k, len(sinkValues))
|
|
|
|
}
|
|
|
|
noSinkValues[k] = v
|
|
|
|
}
|
|
|
|
|
2023-04-07 12:31:30 +02:00
|
|
|
if sink == "" {
|
2023-04-07 10:30:23 +02:00
|
|
|
log.Printf("no sink found for sym %s", sym)
|
|
|
|
return values, nil
|
|
|
|
}
|
|
|
|
|
2023-04-07 12:31:30 +02:00
|
|
|
s, err := m.render(sym, noSinkValues, 0, nil)
|
2023-04-07 10:30:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
remaining, ok := sizer.Check(s)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("capacity exceeded")
|
|
|
|
}
|
|
|
|
|
2023-04-07 12:31:30 +02:00
|
|
|
log.Printf("%v bytes available for sink split", remaining)
|
|
|
|
|
2023-04-07 10:30:23 +02:00
|
|
|
l := 0
|
2023-04-07 12:31:30 +02:00
|
|
|
tb := strings.Builder{}
|
|
|
|
rb := strings.Builder{}
|
2023-04-07 10:30:23 +02:00
|
|
|
|
2023-04-07 12:31:30 +02:00
|
|
|
sizer.AddCursor(0)
|
2023-04-07 10:30:23 +02:00
|
|
|
for i, v := range sinkValues {
|
2023-04-07 12:31:30 +02:00
|
|
|
log.Printf("processing sinkvalue %v: %s", i, v)
|
2023-04-07 10:30:23 +02:00
|
|
|
l += len(v)
|
|
|
|
if uint32(l) > remaining {
|
2023-04-07 12:31:30 +02:00
|
|
|
if tb.Len() == 0 {
|
2023-04-07 10:30:23 +02:00
|
|
|
return nil, fmt.Errorf("capacity insufficient for sink field %v", i)
|
|
|
|
}
|
2023-04-07 12:31:30 +02:00
|
|
|
rb.WriteString(tb.String())
|
|
|
|
rb.WriteRune('\n')
|
|
|
|
c := uint32(rb.Len())
|
|
|
|
sizer.AddCursor(c)
|
|
|
|
tb.Reset()
|
2023-04-07 10:30:23 +02:00
|
|
|
l = 0
|
|
|
|
}
|
2023-04-07 12:31:30 +02:00
|
|
|
if tb.Len() > 0 {
|
|
|
|
tb.WriteByte(byte(0x00))
|
|
|
|
}
|
|
|
|
tb.WriteString(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tb.Len() > 0 {
|
|
|
|
rb.WriteString(tb.String())
|
2023-04-07 10:30:23 +02:00
|
|
|
}
|
|
|
|
|
2023-04-07 12:31:30 +02:00
|
|
|
r := rb.String()
|
|
|
|
r = strings.TrimRight(r, "\n")
|
2023-04-07 10:30:23 +02:00
|
|
|
|
2023-04-07 12:31:30 +02:00
|
|
|
noSinkValues[sink] = r
|
|
|
|
|
|
|
|
for i, v := range strings.Split(r, "\n") {
|
|
|
|
log.Printf("nosinkvalue %v: %s", i, v)
|
2023-04-07 10:30:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return noSinkValues, nil
|
|
|
|
}
|
|
|
|
|
2023-04-07 12:31:30 +02:00
|
|
|
func(m *MenuResource) RenderTemplate(sym string, values map[string]string, idx uint16, sizer *Sizer) (string, error) {
|
|
|
|
return DefaultRenderTemplate(m, sym, values, idx, sizer)
|
2023-04-06 16:21:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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, sizer *Sizer) (string, error) {
|
|
|
|
return m.templateFunc(sym, sizer)
|
|
|
|
}
|
|
|
|
|
2023-04-07 12:31:30 +02:00
|
|
|
func(m *MenuResource) render(sym string, values map[string]string, idx uint16, sizer *Sizer) (string, error) {
|
2023-04-07 10:30:23 +02:00
|
|
|
var ok bool
|
2023-04-06 16:21:26 +02:00
|
|
|
r := ""
|
2023-04-07 12:31:30 +02:00
|
|
|
s, err := m.RenderTemplate(sym, values, idx, sizer)
|
2023-04-06 16:21:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
log.Printf("rendered %v bytes for template", len(s))
|
|
|
|
r += s
|
2023-04-07 12:31:30 +02:00
|
|
|
if sizer != nil {
|
|
|
|
_, ok = sizer.Check(r)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("limit exceeded: %v", sizer)
|
|
|
|
}
|
2023-04-06 16:21:26 +02:00
|
|
|
}
|
2023-04-08 12:46:30 +02:00
|
|
|
s, err = m.RenderMenu(idx)
|
2023-04-06 16:21:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
log.Printf("rendered %v bytes for menu", len(s))
|
|
|
|
r += s
|
2023-04-07 12:31:30 +02:00
|
|
|
if sizer != nil {
|
|
|
|
_, ok = sizer.Check(r)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("limit exceeded: %v", sizer)
|
|
|
|
}
|
2023-04-06 16:21:26 +02:00
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
2023-04-07 10:30:23 +02:00
|
|
|
|
2023-04-07 12:31:30 +02:00
|
|
|
func(m *MenuResource) Render(sym string, values map[string]string, idx uint16, sizer *Sizer) (string, error) {
|
2023-04-07 10:30:23 +02:00
|
|
|
var err error
|
|
|
|
|
2023-04-07 12:31:30 +02:00
|
|
|
values, err = m.prepare(sym, values, idx, sizer)
|
2023-04-07 10:30:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-04-07 12:31:30 +02:00
|
|
|
return m.render(sym, values, idx, sizer)
|
2023-04-07 10:30:23 +02:00
|
|
|
}
|