parse

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package parse provides JSON parsing for cortex rule definitions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseAndBuild

func ParseAndBuild(name string, data []byte, config *cortex.Config) (*cortex.Engine, error)

ParseAndBuild is a convenience function that parses JSON and builds an Engine.

Types

type AllocationDef

type AllocationDef struct {
	Source    string             `json:"source"`
	Strategy  string             `json:"strategy"`
	Targets   []AllocationTarget `json:"targets"`
	Remainder string             `json:"remainder,omitempty"`
	Precision int                `json:"precision,omitempty"`
}

AllocationDef is the config structure for allocation rules.

type AllocationTarget

type AllocationTarget struct {
	Key    string  `json:"key"`
	Amount float64 `json:"amount"`
}

AllocationTarget defines an allocation destination.

type AssignmentDef

type AssignmentDef struct {
	Target string `json:"target"`
	Value  any    `json:"value"`
}

AssignmentDef is the config structure for assignment rules.

type BuildupDef

type BuildupDef struct {
	Buildup   string  `json:"buildup"`
	Operation string  `json:"operation"`
	Source    string  `json:"source,omitempty"`
	Initial   float64 `json:"initial,omitempty"`
	Target    string  `json:"target,omitempty"`
}

BuildupDef is the config structure for buildup rules.

type FormulaDef

type FormulaDef struct {
	Target     string   `json:"target"`
	Expression string   `json:"expression,omitempty"`
	Function   string   `json:"function,omitempty"` // named registered function
	Inputs     []string `json:"inputs,omitempty"`
}

FormulaDef is the config structure for formula rules.

type LookupDef

type LookupDef struct {
	Name    string         `json:"name"`
	Type    string         `json:"type"` // "map" or "range"
	Entries []LookupEntry  `json:"entries,omitempty"`
	Items   map[string]any `json:"items,omitempty"` // for map type
}

LookupDef defines a lookup table in config.

type LookupEntry

type LookupEntry struct {
	Min   float64  `json:"min"`
	Max   *float64 `json:"max"` // nil means +infinity
	Value any      `json:"value"`
}

LookupEntry defines a single entry in a range lookup.

type LookupRuleDef

type LookupRuleDef struct {
	Table    string `json:"table"`
	Key      string `json:"key"`
	Target   string `json:"target"`
	Default  any    `json:"default,omitempty"`
	Required bool   `json:"required,omitempty"`
}

LookupRuleDef is the config structure for lookup rules.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser parses config into rules.

func NewParser

func NewParser() *Parser

NewParser creates a new parser.

func (*Parser) ParseAndBuildEngine

func (p *Parser) ParseAndBuildEngine(name string, data []byte, config *cortex.Config) (*cortex.Engine, error)

ParseAndBuildEngine parses JSON and builds an Engine.

func (*Parser) ParseJSON

func (p *Parser) ParseJSON(data []byte) (*RuleSet, error)

ParseJSON parses a rule set from JSON.

func (*Parser) RegisterFormula

func (p *Parser) RegisterFormula(name string, fn cortex.FormulaFunc)

RegisterFormula registers a named formula function for use in config.

func (*Parser) ToLookups

func (p *Parser) ToLookups(rs *RuleSet) ([]cortex.Lookup, error)

ToLookups converts lookup definitions to Lookup instances.

func (*Parser) ToRules

func (p *Parser) ToRules(rs *RuleSet) ([]cortex.Rule, error)

ToRules converts rule definitions to Rule instances.

type RuleDefinition

type RuleDefinition struct {
	ID          string         `json:"id"`
	Type        string         `json:"type"` // assignment, formula, allocation, lookup, buildup
	Name        string         `json:"name,omitempty"`
	Description string         `json:"description,omitempty"`
	Deps        []string       `json:"deps,omitempty"`
	Disabled    bool           `json:"disabled,omitempty"`
	Config      map[string]any `json:"config"`
}

RuleDefinition is a config-driven rule.

type RuleSet

type RuleSet struct {
	Version string           `json:"version"`
	Name    string           `json:"name"`
	Lookups []LookupDef      `json:"lookups,omitempty"`
	Rules   []RuleDefinition `json:"rules"`
}

RuleSet represents a collection of rules from config.

Jump to

Keyboard shortcuts

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