struql

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2020 License: Apache-2.0 Imports: 4 Imported by: 0

README

struql

GoDoc Build Status Coverage Status Go Report Card

Allows to filter data in the structure, presenting it as a table. Note, that all filters values should be converted to appropriate field types.

Install

go get github.com/ssetin/struql

Using

Lets consider you have such struct:

type detail struct {
	Id    int32
	Value string
}

type device struct {
	Number  int
	Model   string
	Details []detail
}

var dev = device{
	Number: 1,
	Model:  "OEM",
	Details: []detail{
		{Id: 1, Value: "First"},
		{Id: 2, Value: "Soul"},
		{Id: 3, Value: "Seven"},
	},
}

struql represents it as table:

Number Model Details.Id Details.Value
1 OEM 1 Fisrt
1 OEM 2 Soul
1 OEM 3 Seven

So now you can filter that data like this:

	sq.Init(dev)
	filter := []struql.Filter{
		{FieldName: "Details.Id", Value: int32(1), Operation: struql.ComparisonGreater},
		{FieldName: "Details.Value", Value: "S", Operation: struql.ComparisonBeginWith},
	}
	dataSet := make(struql.RowCollection, 1)
	dataSet, _ = sq.Where(dataSet, filter...)

	values, _ := dataSet.CollectValues("Details.Id")
	fmt.Printf("Details.Id: %v\n", vals)

This way of searching data no so fast as just iterating the struct, but could be useful in some cases.

Documentation

Overview

Package struql helps to filter data in the structure, presenting it as a table.

Typical usage

type detail struct {
	Id    int32
	Value string
}

type device struct {
	Number  int
	Model   string
	Details []detail
}

var dev = device{
	Number: 1,
	Model:  "OEM",
	Details: []detail{
		{Id: 1, Value: "First"},
		{Id: 2, Value: "Soul"},
		{Id: 3, Value: "Seven"},
	},
}

var sq struql.StruQL
sq.Init(dev)
filter := []struql.Filter{
	{
		FieldName: "Details.Id",
		Value: int32(1),
		Operation: struql.ComparisonGreater
	},
	{
		FieldName: "Details.Value",
		Value: "S",
		Operation: struql.ComparisonBeginWith
	},
}
dataSet := make(struql.RowCollection, 1)
dataSet, _ = sq.Where(dataSet, filter...)

values, _ := dataSet.CollectValues("Details.Id")
fmt.Printf("Details.Id: %v\n", vals)

Index

Constants

View Source
const (
	// ComparisonEqual - =
	ComparisonEqual = iota
	// ComparisonNotEqual - !=
	ComparisonNotEqual
	// ComparisonIn - field in [values]
	ComparisonIn
	// ComparisonGreater - >
	ComparisonGreater
	// ComparisonLesser - <
	ComparisonLesser
	// ComparisonBeginWith - field like 'value%'
	ComparisonBeginWith
	// ComparisonEndWith - field like '%value'
	ComparisonEndWith
	// ComparisonExists - value in[field values]
	ComparisonExists
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Field

type Field struct {
	Name  string
	Value interface{}
	// contains filtered or unexported fields
}

Field represents struct field in the table

func (Field) Index

func (f Field) Index() int

Index returns field index in the row

type Filter

type Filter struct {
	FieldName string
	Value     interface{}
	Operation int
	Modifier  ValueModifier
	// contains filtered or unexported fields
}

Filter sets the search condition

type Row

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

Row - collection of fields / table row

func (*Row) AddField

func (r *Row) AddField(name string, value interface{})

AddField add new field to row

func (*Row) FieldByIndex

func (r *Row) FieldByIndex(index int) *Field

FieldByIndex returns field by it's index or nil

func (*Row) FieldByName

func (r *Row) FieldByName(name string) *Field

FieldByName returns field by it's name or nil

func (Row) String

func (r Row) String() string

String represents row as string

type RowCollection

type RowCollection []*Row

RowCollection - collection of rows / table

func (RowCollection) AddField

func (r RowCollection) AddField(name string, value interface{})

AddField add field to all rows

func (RowCollection) CollectValues

func (r RowCollection) CollectValues(fieldName string) ([]interface{}, error)

CollectValues returns array of values of according field

func (RowCollection) Count

func (r RowCollection) Count() int

Count of rows

func (RowCollection) String

func (r RowCollection) String() string

String represents rows as string

func (RowCollection) Where

func (r RowCollection) Where(result RowCollection, filters ...Filter) (RowCollection, error)

Where collects data in the rows according to filters

type StruQL

type StruQL struct {
	Rows RowCollection
	// contains filtered or unexported fields
}

StruQL implements struct to table transforming algorithm

func (*StruQL) Init

func (s *StruQL) Init(object interface{}) error

Init - initialize struql from presented object (struct)

func (StruQL) String

func (s StruQL) String() string

String represents struql rows as string

func (*StruQL) Where

func (s *StruQL) Where(result RowCollection, filters ...Filter) (RowCollection, error)

Where collects data in the rows according to filters

type ValueModifier

type ValueModifier func(Value interface{}) interface{}

ValueModifier - callback for transforming field value before compare

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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