compare

package module
v0.0.0-...-7e9b657 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2017 License: MIT Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

compare

compare interface{} values to see if it matches the criteria or criteria groups

Idea

The basic idea is I find myself having user/admin driven rules that I need to compare against individuals to see if what groups they belong to.

This works well with JSON

Use case Example

Lets say you are creating an emailing application.

Your users upload their contacts and demographics (name, age, email, ...etc...)

In this type of application, the user will want to segment their contacts into different campaigns.

This tool can be used to compare users demographic information and return what segments they belong to based on the user defined rules.

Example

todo include example that uses this json as input

Segment Rules Input
{
  {
    "name": "Jon-child",
    "criteria": [{
      "key": "/profile/first",
      "operator": "=",
      "compare": "Jon"
    }, {
      "key": "/age",
      "operator": "<",
      "compare": "18"
    }]
  }, {
    "name": "Jon-adult",
    "criteria": [{
      "key": "/profile/first",
      "operator": "=",
      "compare": "Jon"
    }, {
      "key": "/age",
      "operator": ">",
      "compare": "18"
    }]
  }, {
    "name": "first-Jon",
    "criteria": [{
      "key": "/profile/first",
      "operator": "=",
      "compare": "Jon"
    }]
  }
}
User Demographics Input
{
  "profile": {
    "first": "Jon",
    "last": "Doe",
  },
  "age": 20,
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Eval

func Eval(a interface{}, operator Operator, b interface{}) (bool, error)

Logical evaulator. Takes two items and compares them based on the Operator passed in

func Group

func Group(group GroupCompare, input interface{}) (bool, error)

checks a GroupCompare to see if the input matches the criteria

func MultipleGroups

func MultipleGroups(groups []GroupCompare, input interface{}) ([]string, error)

Checks if the input matches any groupings of criteria returns the matched group names

if Criteria is the same for most of the Groups, it could be optimized by comaparing the unique criteria then finding which ones implement all the criteria

Types

type Criteria

type Criteria struct {
	Key      string
	Operator Operator
	Compare  interface{}
}

type GroupCompare

type GroupCompare struct {
	Name     string
	Criteria []Criteria
}

type Operator

type Operator string
const (
	EQUAL              Operator = "="
	LESS_THAN          Operator = "<"
	LESS_THAN_EQUAL    Operator = "<="
	GREATER_THAN       Operator = ">"
	GREATER_THAN_EQUAL Operator = ">="
	NOT_EQUAL          Operator = "!="
	IN                 Operator = "in"
	NOT_IN             Operator = "not-in"
)

Jump to

Keyboard shortcuts

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