distil

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2021 License: MIT Imports: 6 Imported by: 0

README

distil 💧

GoDoc Build Status Go Report Card license

In memory dataset filtering.

Installation

go get -u github.com/mattevans/distil

Example

Given a dataset...

data := []map[string]interface{}{
	{"location": "New York", "department": "Engineering", "salary": 120000, "start_date": "2016-01-23T12:00:00Z"},
	{"location": "New York", "department": "Engineering", "salary": 80000, "start_date": "2016-03-23T12:00:00Z"},
	{"location": "New York", "department": "Marketing", "salary": 90000, "start_date": "2016-01-23T12:00:00Z"},
	{"location": "New York", "department": "Marketing", "salary": 150000, "start_date": "2016-01-23T12:00:00Z"},
	{"location": "Chicago", "department": "Engineering", "salary": 120000, "start_date": "2016-01-23T12:00:00Z"},
	{"location": "Chicago", "department": "Engineering", "salary": 160000, "start_date": "2016-03-23T12:00:00Z"},
}

Initialize distil, build filters and run...

// Init distil dataset.
dataset := NewDataset(data...)

// Build a distil query and apply filters.
query := &Query{}
query.Filters = append(query.Filters, Filter{
	Field: "location",
	Value: "Chicago",
	Operator: Operator{
		Code: "eq",
		Type: "string",
	},
})

// Run it.
results, err := dataset.Run(query)
if err != nil {
  errors.New("Unexpected error running query: %s", err.Error())
}

Returns...

[]map[string]interface{}{
	{"location": "Chicago", "department": "Engineering", "salary": 120000, "start_date": "2016-01-23T12:00:00Z"},
	{"location": "Chicago", "department": "Engineering", "salary": 160000, "start_date": "2016-03-23T12:00:00Z"},
}

Find a list of available operators here.

Thanks & Acknowledgements 👌

The packages's architecture is adapted from aggro, created by Mal Curtis. 🍻

Contributing

If you've found a bug or would like to contribute, please create an issue here on GitHub, or better yet fork the project and submit a pull request!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dataset

type Dataset struct {
	Rows []map[string]interface{}
}

Dataset holds our raw data that filters will be applied against.

func NewDataset

func NewDataset(data ...map[string]interface{}) *Dataset

NewDataset creates a new Dataset adding the appropriate rows.

func (*Dataset) Run

func (set *Dataset) Run(query *Query) ([]map[string]interface{}, error)

Run executes the query against the dataset.

type Filter

type Filter struct {
	Field    string      `json:"field"`
	Operator Operator    `json:"operator"`
	Value    interface{} `json:"value"`
}

Filter represents fields needed to distil a dataset.

type Operator

type Operator struct {
	Code string `json:"code"`
	Type string `json:"type"`
}

Operator represents a filter operator that will be used to distil a dataset.

type Query

type Query struct {
	Filters []Filter
}

Query ...

Jump to

Keyboard shortcuts

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