ruler

package module
v0.0.0-...-71fb8a2 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2018 License: Apache-2.0 Imports: 5 Imported by: 0

README

go-ruler

json-y object testing in go

Installation

go get github.com/hopkinsth/go-ruler

Introduction

go-ruler is an implementaion of ruler in go, partly as an experiment, eventually as an optimization strategy. go-ruler supports programmatically constructing rules in a way similar to js-ruler and can also process rules stored in arrays of JSON objects with this structure:

{
  "comparator": "eq",
  "path": "library.name",
  "value": "go-ruler"
}

Each of those objects (or 'filters') describes a condition for a property on some JSON object or json-object-esque structure. (In this go implementation, we're using map[string]interface{}.)

Example

package main

import "github.com/hopkinsth/go-ruler"
import "fmt"

func main() {
  rules := []byte(`[
    {"comparator": "eq", "path": "library.name", "value": "go-ruler"},
    {"comparator": "gt", "path": "library.age", "value": 0.5}
  ]`)

  // supports loading rules from JSON data
  engine, _ := ruler.NewRulerWithJson(rules)

  // can also build rules programmatically
  engine = ruler.NewRuler()
  engine.Rule("library.name").
    Eq("go-ruler").
    End().
  Rule("library.age").
    Gt(0.5)

  result := engine.Test(map[string]interface{}{
    "library": map[string]interface{}{
      "name": "go-ruler",
      "age":  1.24,
    },
  })

  fmt.Println(result == true)
}

License

Copyright 2015 Thomas Hopkins

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with the License. You may obtain a copy of the License in the LICENSE file, or at:

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Rule

type Rule struct {
	Comparator string      `json:"comparator"`
	Path       string      `json:"path"`
	Value      interface{} `json:"value"`
}

This struct is the main format for rules or conditions in ruler-compatable libraries. Here's a sample in JSON format:

{
	"comparator": "eq",
	"path": "person.name",
	"value": "James"
}

Valid comparators are: eq, neq, lt, lte, gt, gte, contains (regex), ncontains (!regex)

This struct is exported here so that you can include it in your own JSON encoding/decoding, but go-ruler has a facility to help decode your rules from JSON into its own structs.

type Ruler

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

func NewRuler

func NewRuler(rules []*Rule) *Ruler

creates a new Ruler for you optionally accepts a pointer to a slice of filters if you have filters that you want to start with

func NewRulerWithJson

func NewRulerWithJson(jsonstr []byte) (*Ruler, error)

returns a new ruler with filters parsed from JSON data expects JSON as a slice of bytes and will parse your JSON for you!

func (*Ruler) Rule

func (r *Ruler) Rule(path string) *RulerRule

adds a new rule for the property at `path` returns a RulerFilter that you can use to add conditions and more filters

func (*Ruler) Test

func (r *Ruler) Test(o map[string]interface{}) bool

tests all the rules (i.e. filters) in your set of rules, given a map that looks like a JSON object (map[string]interface{})

type RulerRule

type RulerRule struct {
	*Ruler
	*Rule
}

A RulerRule combines a single rule and a whole set of rules and is used when building rules programmatically through Ruler's Rule() function. It's not meant to be created directly.

func (*RulerRule) End

func (rf *RulerRule) End() *Ruler

Stops chaining for the current rule, allowing you to add rules for other properties

func (*RulerRule) Eq

func (rf *RulerRule) Eq(value interface{}) *RulerRule

adds an equals condition

func (*RulerRule) Gt

func (rf *RulerRule) Gt(value interface{}) *RulerRule

adds a greater than condition

func (*RulerRule) Gte

func (rf *RulerRule) Gte(value interface{}) *RulerRule

adds a greater than or equal to condition

func (*RulerRule) Lt

func (rf *RulerRule) Lt(value interface{}) *RulerRule

adds a less than condition

func (*RulerRule) Lte

func (rf *RulerRule) Lte(value interface{}) *RulerRule

adds a less than or equal condition

func (*RulerRule) Matches

func (rf *RulerRule) Matches(value interface{}) *RulerRule

adds a matches (regex) condition

func (*RulerRule) Neq

func (rf *RulerRule) Neq(value interface{}) *RulerRule

adds a not equals condition

func (*RulerRule) NotMatches

func (rf *RulerRule) NotMatches(value interface{}) *RulerRule

adds a not matches condition (ncontains, in the way this thinks of it)

Jump to

Keyboard shortcuts

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