semantics

package
v6.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2025 License: Apache-2.0 Imports: 5 Imported by: 2

Documentation

Overview

Package semantics implements TSL tree semantics.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Walk

func Walk(n *tsl.TSLNode, eval EvalFunc) (interface{}, error)

Walk traverses the TSL tree and implements search semantics.

Users can call the Walk method to check if a document compiles to `true` or `false` when applied to a tsl tree.

Example:

record := map[string]interface{} {
	"title":       "A good book",
	"author":      "Joe",
	"spec.pages":  14,
	"spec.rating": 5,
	"created_at":  time.Now(),
	"is_active":   true,
}

// evalFactory creates an evaluation function for a data record.
func evalFactory(r map[string]interface{}) semantics.EvalFunc {
	// Returns:
	// A function (semantics.EvalFunc) that gets a `key` for a record and returns
	// the value of the document for that key.
	// If no value can be found for this `key` in our record, it will return
	// ok = false, if value is found it will return ok = true.
	return func(k string) (interface{}, bool) {
		if v, ok := book[k]; ok {
			return v, true
		}
		return nil, false
	}
}

// Check if our record complies with our tsl tree.
//
// For example:
//   if our tsl tree represents the phrase "author = 'Joe' and created_at > '2023-01-01'"
//   we will get the boolean value `true` for our record.
//
//   if our tsl tree represents the phrase "spec.pages > 50"
//   we will get the boolean value `false` for our record.
eval := evalFactory(record)
compliance, err = semantics.Walk(tree, eval)

Types

type EvalFunc

type EvalFunc = func(string) (interface{}, bool)

EvalFunc is a key evaluation function type

Jump to

Keyboard shortcuts

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