coll

package module
v0.0.0-...-803f9c7 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2023 License: MIT Imports: 5 Imported by: 23

README

Coll

Build Status Coverage Status Go Report Card codecov Sourcegraph GitHub

Coll is a collection library for Go.

cc := coll.MustNew(testMapData)
collectionWithoutEmpty := cc.Filter(func(value string) bool {
    return value != ""
}).Filter(func(value string, key string) bool {
    return key != ""
})
collectionWithoutEmpty.Each(func(value, key string) {
    if value == "" || key == "" {
        t.Errorf("test failed: %s=>%s", key, value)
    }
})

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTargetInvalid = errors.New("target must be a pointer to struct")
View Source
var ErrTargetIsNil = errors.New("target is nil")
View Source
var (
	// ErrorInvalidDataType invalid data type error
	ErrorInvalidDataType = errors.New("invalid data type")
)

Functions

func Copy

func Copy(source interface{}, targets ...interface{}) error

Copy alias of CopyProperties

func CopyProperties

func CopyProperties(source interface{}, targets ...interface{}) error

CopyProperties copy exported properties(with same name and type) from source to target target must be a pointer to struct

func Filter

func Filter(origin interface{}, dest interface{}, filters ...interface{}) error

func IsFunction

func IsFunction(in interface{}, argumentCheck ...[]int) bool

IsFunction returns if the argument is a function.

func Map

func Map(origin interface{}, dest interface{}, mappers ...interface{}) error

func Unique

func Unique(origin interface{}, dest interface{}, uniquers ...interface{}) error

Types

type Collection

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

Collection is a data collection

func MustNew

func MustNew(data interface{}) *Collection

MustNew create a new collection from data with error suppress

func New

func New(data interface{}) (*Collection, error)

New create a new collection from data

func (*Collection) All

func (collection *Collection) All(result interface{}) (err error)

All get all of the items in the collection. the argument result must be a pointer to map or slice

func (*Collection) AsArray

func (collection *Collection) AsArray() *Collection

AsArray convert the collection to an array collection Remember: the underlying data type is changed to []interface{}

func (*Collection) AsMap

func (collection *Collection) AsMap(keyFunc interface{}) *Collection

AsMap convert collection to a map collection

keyFunc(value interface{}) interface{}
keyFunc(value interface{}, key interface{}) interface{}

Remember: the underlying data type is changed to map[interface{}]interface{}

func (*Collection) DataType

func (collection *Collection) DataType() DataType

DataType return the data type

func (*Collection) Each

func (collection *Collection) Each(eachFunc interface{})

Each Execute a callback over each item.

func (*Collection) Filter

func (collection *Collection) Filter(filter interface{}) *Collection

Filter iterates over elements of collection, return all element meet the needs filter(interface{}) bool filter(interface{}, int) bool

func (*Collection) GroupBy

func (collection *Collection) GroupBy(groupFunc interface{}) *Collection

GroupBy iterates over elements of collection, grouping all elements by specified conditions groupFunc(interface{}) interface{} groupFunc(interface{}, int) interface{} Remember: the result of All must be type: map[interface{}][]interface{}

func (*Collection) HasIndex

func (collection *Collection) HasIndex(index int) bool

HasIndex return whether the collection has a index

func (*Collection) Index

func (collection *Collection) Index(index int) interface{}

Index Get an item from the collection by index.

func (*Collection) IsEmpty

func (collection *Collection) IsEmpty() bool

IsEmpty Determine if the collection is empty or not.

func (*Collection) Items

func (collection *Collection) Items() interface{}

Items return all items in the collection

func (*Collection) Map

func (collection *Collection) Map(mapFunc interface{}) *Collection

Map manipulates an iterate and transforms it to another type. mapFunc(value interface{}) interface{} mapFunc(value interface{}) (value interface{}, key interface{}) mapFunc(value interface{}, key interface{}) interface{} mapFunc(value interface{}, key interface{}) (value interface{}, key interface{})

func (*Collection) MapHasIndex

func (collection *Collection) MapHasIndex(key interface{}) bool

MapHasIndex return whether the collection has a key

func (*Collection) MapIndex

func (collection *Collection) MapIndex(key interface{}) interface{}

MapIndex get an item from the collection by key

func (*Collection) Reduce

func (collection *Collection) Reduce(reduceFunc interface{}, initial interface{}) interface{}

Reduce Iteratively reduce the array to a single value using a callback function reduceFunc(carry interface{}, item interface{}) interface{}

func (*Collection) Size

func (collection *Collection) Size() int

Size count the number of items in the collection.

func (*Collection) Sort

func (collection *Collection) Sort(compareFunc interface{}) *Collection

Sort sort collection with orderBy function, only support array collection

compareFunc(val1 interface{}, val2 interface{}) bool

func (*Collection) ToArray

func (collection *Collection) ToArray() ([]interface{}, error)

func (*Collection) ToString

func (collection *Collection) ToString() string

ToString print the data element

func (*Collection) Unique

func (collection *Collection) Unique(uniqFunc interface{}) *Collection

Unique remove duplicated elements from collection

uniqFunc(value interface{}) interface{}
uniqFunc(value interface{}, key interface{}) interface{}

Remember: the return collection type is map[interface{}]interface{} for map, []interface{} for array or slices

type DataType

type DataType int8

DataType is collection data type

const (
	// DataTypeMap represent the collection is a map collection
	DataTypeMap DataType = iota
	// DataTypeArrayOrSlice represent the collection is a array or slice collection
	DataTypeArrayOrSlice
)

type InvalidTypeError

type InvalidTypeError struct {
	Type reflect.Type
}

InvalidTypeError describes an invalid argument passed to To. (The argument to To must be a non-nil pointer.)

func (*InvalidTypeError) Error

func (e *InvalidTypeError) Error() string

Jump to

Keyboard shortcuts

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