core

package
v1.3.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 24, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetStringRes added in v1.3.5

func GetStringRes(g *Generator, sep string) (string, error)

func ScopeGet added in v1.3.2

func ScopeGet[T any](st ScopeType, what string) (T, error)

Types

type CommandMeta

type CommandMeta[E, N any] struct {
	Handler CommandType[E, N]
	Doc     string
}

type CommandType

type CommandType[E, N any] func(*E, N) error

type EventInput added in v1.3.5

type EventInput struct {
	Option *Option
	Input  interface{}
}

type EventType

type EventType func(*Events, *EventInput) error

type Events

type Events struct {
	Context context.Context
	// contains filtered or unexported fields
}

Events manages an ordered collection of event handlers. Each event has a name (string) and a list of EventType functions. The CallEvents method invokes all handlers of an event in registration order. Events can also automatically wrap calls with start/end events for logging.

func NewEvents

func NewEvents(context context.Context) *Events

NewEvents creates an empty Events instance with an ordered map. The Scope map is initially empty but can be used to pass data between event handlers.

func (*Events) CallEvents

func (e *Events) CallEvents(input *EventInput, name string,
	canWorkWithoutHandler bool) error

func (*Events) GetEvents

func (e *Events) GetEvents(name string) ([]EventType, error)

func (*Events) NewEvent

func (e *Events) NewEvent(name string, event EventType)

func (*Events) NewEventBefore added in v1.2.0

func (e *Events) NewEventBefore(name string, event EventType) error

func (*Events) Scope

func (e *Events) Scope() ScopeType

type EventsInterface added in v1.3.5

type EventsInterface interface {
	GetEvents(name string) ([]EventType, error)
	NewEvent(name string, event EventType)
	NewEventBefore(name string, event EventType) error
	CallEvents(input *EventInput, name string, canWorkWithoutHandler bool) error
	Scope() ScopeType
}

type Generator

type Generator struct {
	Pipeline []string
	// contains filtered or unexported fields
}

Generator accumulates code fragments (strings or bytes) into named points (e.g., "pre", "main"). The Pipeline defines the order in which points are emitted. It supports both text and binary generation modes via res_type. Thread‑safe due to internal mutex.

func NewGenerator

func NewGenerator(res_type public.ResType, pipeline []string) *Generator

NewGenerator initializes a Generator with a given resource type (StringResType or ByteResType) and a pipeline slice. Empty code slices are pre‑created for each pipeline point.

func (*Generator) AddBytes

func (g *Generator) AddBytes(code []byte, point string) error

func (*Generator) AddString

func (g *Generator) AddString(code string, point string) error

func (*Generator) AddStrings

func (g *Generator) AddStrings(code []string, point string) error

func (*Generator) GetBytesRes

func (g *Generator) GetBytesRes() ([]byte, error)

func (*Generator) GetStringArrRes

func (g *Generator) GetStringArrRes() ([]string, error)

type Logger

type Logger struct {
	Logging           map[string]bool
	Log               []string
	Statuses          map[string]string
	DefaultStatusForm string
	// contains filtered or unexported fields
}

Logger is a thread-safe structured logger for engine diagnostics. It stores a list of log lines, supports custom status formats, and allows different formatting per status (e.g., "error", "info", "debug"). Typical usage: attach to UniversalEngineParams.Logger.

func NewLogger

func NewLogger(defaultStatusForm string) *Logger

NewLogger creates a new Logger with an optional defaultStatusForm. The format uses three placeholders: %s for status, %v for timestamp, and %s for the message. Example default: "%s [%v] [%s]\n" If empty string is passed, the default format is used.

func (*Logger) GetLog

func (l *Logger) GetLog() string

GetLog returns the entire log as a single string with newline separators. It is useful for saving logs to a file or showing them after execution.

func (*Logger) GetStatusForm

func (l *Logger) GetStatusForm(status string) string

GetStatusForm returns the format string associated with the given status, falling back to DefaultStatusForm if no custom format is set. Custom formats can be registered by directly assigning to l.Statuses map.

func (*Logger) PrintLog

func (l *Logger) PrintLog(status string, message string)

PrintLog writes a log entry to stdout and appends it to the internal slice. The status string determines the format via GetStatusForm (if a custom format for that status exists). The message is inserted into the format. Example: logger.PrintLog("error", "failed to parse token")

type Option added in v1.3.2

type Option struct {
	Flags []string
	Scope ScopeType
}

func (*Option) HasFlag added in v1.3.2

func (o *Option) HasFlag(name string) bool

type ScopeType

type ScopeType map[string]interface{}

type UniversalEngineParams

type UniversalEngineParams struct {
	// Generator *Generator - controls code/output generation across pipeline points.
	Generator *Generator
	// Event *Events - allows hooking into parsing and command dispatch.
	Event EventsInterface
	// Scope ScopeType - a map[string]interface{} that can be used to share
	//   variables between events, parsers, and command handlers.
	Scope ScopeType
	// Logger *Logger - if set, logs internal steps (event calls, errors).
	Logger *Logger

	Context context.Context
}

UniversalEngineParams is a container shared by both StringEngine and ByteEngine. It holds the Generator (for output accumulation), Events (for hooks), Scope (for passing arbitrary data between stages), and Logger (for diagnostics). This struct is embedded (not composed by pointer) in the engine types, promoting its fields and methods to the engine itself.

func NewUniversalEngineParams

func NewUniversalEngineParams(
	generator *Generator,
	events *Events,
	scope ScopeType,
	logger *Logger,
	context context.Context,
) (*UniversalEngineParams, error)

NewUniversalEngineParams constructs an initialized UniversalEngineParams. It automatically injects two event handlers into the Events system:

  • CallEventsStartEvent - logs the start of any event call (debug level)
  • CallEventsEndEvent - logs the end, including any error

Parameters: generator, events, scope, logger. All must be non‑nil. Returns a filled struct or an error if event registration fails.

func (*UniversalEngineParams) GetContext

func (p *UniversalEngineParams) GetContext() context.Context

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL