Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrStaticProviderNoRuntimeUpdates = errors.New(
"StaticProvider doesn't support adding data at runtime",
)
ErrStaticProviderNoRuntimeUpdates is returned when trying to add runtime data to a StaticProvider. This is a sentinel error that can be checked by CompositeProvider to handle gracefully.
Functions ¶
func AddDataToContextHelper ¶
func AddDataToContextHelper( ctx context.Context, logger *slog.Logger, provider Provider, d ...map[string]any, ) (context.Context, error)
AddDataToContextHelper is a utility function that implements the common logic for adding data to a context for evaluation. This function is used by various engine implementations to maintain consistent data handling behavior.
Parameters:
- ctx: The base context to enrich
- logger: A logger instance for recording operations
- provider: The data provider to use for storing data
- d: Variable list of data items to add to the context
Returns:
- enrichedCtx: The context with added data
- err: Any error encountered during the operation
Types ¶
type CompositeProvider ¶
type CompositeProvider struct {
// contains filtered or unexported fields
}
CompositeProvider combines multiple providers, with later providers overriding values from earlier ones in the chain.
func NewCompositeProvider ¶
func NewCompositeProvider(providers ...Provider) *CompositeProvider
NewCompositeProvider creates a provider that queries given providers in order.
func (*CompositeProvider) AddDataToContext ¶
func (p *CompositeProvider) AddDataToContext( ctx context.Context, data ...map[string]any, ) (context.Context, error)
AddDataToContext distributes data to all providers in the chain. Continues through all providers even if some fail. StaticProvider errors are handled specially based on context.
Example:
ctx := context.Background() staticProvider := NewStaticProvider(map[string]any{"config": configData}) contextProvider := NewContextProvider(constants.EvalData) composite := NewCompositeProvider(staticProvider, contextProvider) ctx, err := composite.AddDataToContext(ctx, req, userData)
func (*CompositeProvider) GetData ¶
GetData retrieves data from all providers and merges them into a single map. Queries providers in sequence, with later providers overriding values from earlier ones. Performs deep merging of nested maps for proper data composition. Returns error on first provider failure.
type ContextProvider ¶
type ContextProvider struct {
// contains filtered or unexported fields
}
ContextProvider retrieves and stores data in the context using a specified key.
func NewContextProvider ¶
func NewContextProvider(contextKey constants.ContextKey) *ContextProvider
NewContextProvider creates a new ContextProvider with the given context key. The context key determines where data is stored in the context object.
See README.md for usage examples.
func (*ContextProvider) AddDataToContext ¶
func (p *ContextProvider) AddDataToContext( ctx context.Context, data ...map[string]any, ) (context.Context, error)
AddDataToContext merges the provided maps into the context. Maps are recursively merged, HTTP Request objects are converted to maps, and later values override earlier ones for duplicate keys.
See README.md for detailed usage examples.
type Provider ¶
type Provider interface { // Getter retrieves associated data from a context during script eval. Getter // Setter enriches a context with a link to data, allowing the script // to access it using the ExecutableUnit's DataProvider. Setter }
Provider defines the interface for accessing runtime data for script execution.
type Setter ¶
type Setter interface { // AddDataToContext enriches a context with data for script evaluation. // It processes input data according to the engine implementation and stores it // in the context using the ExecutableUnit's DataProvider. // // The variadic data parameter accepts maps with string keys and arbitrary values. // HTTP requests, structs, and other types should be wrapped in maps with descriptive keys. // // Example: // scriptData := map[string]any{"greeting": "Hello, World!"} // enrichedCtx, err := evaluator.AddDataToContext(ctx, map[string]any{"request": request}, scriptData) // if err != nil { // return err // } // result, err := evaluator.Eval(enrichedCtx) AddDataToContext(ctx context.Context, data ...map[string]any) (context.Context, error) }
Setter prepares data for script evaluation by enriching a context. This interface supports separating data preparation from evaluation, enabling distributed architectures where these steps can occur on different systems.
type StaticProvider ¶
type StaticProvider struct {
// contains filtered or unexported fields
}
StaticProvider supplies a predefined map of data. Useful for configuration values and testing.
func NewStaticProvider ¶
func NewStaticProvider(data map[string]any) *StaticProvider
NewStaticProvider creates a provider with fixed data. Initializes with an empty map if nil is provided.
func (*StaticProvider) AddDataToContext ¶
func (p *StaticProvider) AddDataToContext( ctx context.Context, _ ...map[string]any, ) (context.Context, error)
AddDataToContext returns a sentinel error as StaticProvider doesn't support dynamic data. Use a ContextProvider or CompositeProvider when runtime data updates are needed. The CompositeProvider should check for this specific error using errors.Is.