gologic

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2025 License: MIT Imports: 1 Imported by: 0

README

gologic

gologic is a small Go library to create and combine logical rules. It helps you check conditions on data in a simple and flexible way.


Features

  • Create rules that return true or false.
  • Combine rules with AND, OR, and NOT.
  • Check if values are equal, greater than, or less than.
  • Works with any kind of data stored in a context map.

Installation

go get github.com/yourusername/gologic

How it Works

Context

A Context is just a map of keys and values:

ctx := gologic.Context{
    "age": 25,
    "role": "admin",
}
Rule

A Rule is anything that can check a condition on a context:

type Rule interface {
    Eval(c Context) bool
}
RuleFunction

You can also use a function as a rule:

myRule := gologic.RuleFunction(func(c gologic.Context) bool {
    return c["age"].(int) > 18
})

Logical Operators

  • All – returns true if all rules are true.
  • Any – returns true if at least one rule is true.
  • Not – negates a rule.
rule := gologic.All(
    gologic.Equals("role", "admin"),
    gologic.Greater("age", 18),
)

Comparison Rules

  • Equals(key, value) – checks if the value in context equals the given value.
  • Greater(key, value) – checks if the value in context is greater.
  • Less(key, value) – checks if the value in context is smaller.
gologic.Equals("role", "admin")      // role == "admin"
gologic.Greater("score", 100)        // score > 100
gologic.Less("attempts", 5)          // attempts < 5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleValidity

func HandleValidity[T any](cField string, c Context) (T, bool)

HandleValidity extracts a value of type T from the given Context map. It returns the value and true if the key exists and the value has the correct type. If the key is missing or the type assertion fails, it returns the zero value of T and false.

Types

type Context

type Context map[string]any

Context is a type used to evaluate a certain rule

type Rule

type Rule interface {
	Eval(c Context) bool
}

Rule is an interface representing a boolean condition evaluator. Any type that implements Rule must provide the Eval method, which takes a Context and returns true or false depending on whether the context satisfies the rule.

func All

func All(rules ...Rule) Rule

All returns a Rule that evaluates to true only if all the provided rules evaluate to true. If any rule returns false, the combined rule returns false.

func Any

func Any(rules ...Rule) Rule

Any returns a Rule that evaluates to true if at least one of the provided rules evaluates to true. If all rules return false, the combined rule returns false.

func Equals

func Equals[T comparable](cField string, value T) Rule

Equals creates a Rule that checks if the value in Context at the given key is equal to the specified value of type T. The rule returns false if the key is missing or the type does not match.

func Greater

func Greater[T cmp.Ordered](cField string, value T) Rule

Greater creates a Rule that checks if the value in Context at the given key is greater than the specified value of type T. T must satisfy cmp.Ordered (i.e., support >, <, >=, <=). The rule returns false if the key is missing or the type does not match.

func Less

func Less[T cmp.Ordered](cField string, value T) Rule

Less creates a Rule that checks if the value in Context at the given key is less than the specified value of type T. T must satisfy cmp.Ordered (i.e., support >, <, >=, <=). The rule returns false if the key is missing or the type does not match.

func Not

func Not(r Rule) Rule

Not returns a Rule that negates the result of the provided rule. If the original rule returns true, Not returns false, and vice versa.

type RuleFunction

type RuleFunction func(c Context) bool

RuleFunction is a helper type that allows ordinary functions with the signature func(Context) bool to implement the Rule interface. By defining an Eval method, any RuleFunction can be used wherever a Rule is expected.

func (RuleFunction) Eval

func (rf RuleFunction) Eval(c Context) bool

Eval calls the underlying function with the provided Context and returns its boolean result. This allows RuleFunction to satisfy the Rule interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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