go-ruler

basic rules engine for go

Github stars Tracking Chart

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.

Main metrics

Overview
Name With Ownerhopkinsth/go-ruler
Primary LanguageGo
Program languageGo (Language Count: 1)
Platform
License:Apache License 2.0
所有者活动
Created At2015-06-11 02:09:08
Pushed At2019-01-03 20:30:06
Last Commit At2015-06-18 21:27:15
Release Count0
用户参与
Stargazers Count118
Watchers Count7
Fork Count30
Commits Count27
Has Issues Enabled
Issues Count4
Issue Open Count3
Pull Requests Count0
Pull Requests Open Count3
Pull Requests Close Count1
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private