data

package
v0.0.0-...-82274f7 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2025 License: MIT Imports: 14 Imported by: 1

Documentation

Index

Constants

View Source
const InfiniteDepth int = -1

InfiniteDepth - used to indicate that Walk should continue till all the nodes in the heirarchy are visited

Variables

View Source
var (
	ErrInvalidWalkPath = errors.New("invalid walk path")
	ErrInvalidData     = errors.New("invalid data")
)

Functions

func CondExec

func CondExec(cond bool, trueFunc, falseFunc func() error) error

CondExec - if cond is true run the trueFunc otherwise run the flase func

func Get

func Get(obj any, out any, path ...string) error

func IsBasicType

func IsBasicType(rt reflect.Kind) bool

IsBasicType - tells if the kind of data type is basic or composite

func IsTime

func IsTime(val *reflect.Value) bool

IsTime - tells if a reflected value is time

func IsValid

func IsValid[T Validatable](m map[string]T) bool

func Max

func Max[T constraints.Ordered](in ...T) T

func Min

func Min[T constraints.Ordered](in ...T) T

func NonEmpty

func NonEmpty[T comparable](vals ...T) T

func OneOf

func OneOf[T comparable](tgt T, opts ...T) bool

func Qop

func Qop[T any](cond bool, onTrue T, onFalse T) T

Qop - equivalent to ternary operator in other languages

func Set

func Set(obj any, newVal any, path ...string) error

func ToFlatMap

func ToFlatMap(obj interface{}, tagName string) (out map[string]interface{})

ToFlatMap - converts given composite data structure into a map of string to interfaces. The heirarchy of types are flattened into single level. The keys of the map indicate the original heirarchy

func Walk

func Walk(
	obj interface{},
	config *WalkConfig)

Walk - walk a given instance of struct/slice/map/basic type

func WalkPath

func WalkPath(obj any, path ...string) (reflect.Value, error)

Types

type Arr

type Arr interface {
	int64 | float64 | bool | []byte | string | time.Time
}

type CommonParams

type CommonParams struct {
	Filter         *Filter `json:"filter" db:"filter" bson:"filter"`
	Page           int64   `json:"page" db:"page" bson:"page"`
	PageSize       int64   `json:"pageSize" db:"page_size" bson:"pageSize"`
	Sort           string  `json:"sort" db:"sort" bson:"sort"`
	SortDescending bool    `json:"sortDescending" db:"sort_desc" bson:"sortDescending"`
}

func (*CommonParams) Limit

func (qp *CommonParams) Limit() int64

func (*CommonParams) Offset

func (qp *CommonParams) Offset() int64

type DateRange

type DateRange struct {
	// Name string    `json:"name" bson:"name"`
	From time.Time `json:"from" db:"_from" bson:"from"`
	To   time.Time `json:"to" db:"_to" bson:"to"`
}

DateRange - represents date ranges

func (*DateRange) Difference

func (r *DateRange) Difference() time.Duration

func (*DateRange) IsValid

func (r *DateRange) IsValid() bool

IsValid - returns true if both From and To dates are non-zero

type DateRangeMatcher

type DateRangeMatcher struct {
	DateRange
	Invert bool `json:"invert" db:"invert" bson:"invert"`
}

func (*DateRangeMatcher) IsValid

func (dr *DateRangeMatcher) IsValid() bool

type DbJson

type DbJson[T any] struct {
	// contains filtered or unexported fields
}

func (*DbJson[T]) Scan

func (dbj *DbJson[T]) Scan(value any) error

func (*DbJson[T]) Value

func (dbj *DbJson[T]) Value() (driver.Value, error)

type Deleter

type Deleter interface {
	Delete(
		gtx context.Context,
		dataType string,
		keyField string,
		keys ...any) error
}

type FieldNameRetriever

type FieldNameRetriever func(field *reflect.StructField) (name string)

FieldNameRetriever - retrieves name for the field from given

type Filter

type Filter struct {
	Bools     map[string]any               `json:"bools" db:"bools" bson:"bools"`
	Props     map[string]*Matcher          `json:"props" db:"props" bson:"props"`
	Lists     map[string]*Matcher          `json:"lists" db:"lists" bson:"lists"`
	Searches  map[string]*Matcher          `json:"searches" db:"searches" bson:"searches"`
	Constants map[string]*Matcher          `json:"constants" db:"constants" bson:"constants"`
	Dates     map[string]*DateRangeMatcher `json:"dates" db:"dates" bson:"dates"`
	Ranges    map[string]*RangeMatcher     `json:"range" db:"range" bson:"range"`
}

Filter - generic filter used to filter data in any mongodb collection

func NewFilter

func NewFilter() *Filter

func (*Filter) Bool

func (f *Filter) Bool(key string, value any) *Filter

func (*Filter) ConstIn

func (f *Filter) ConstIn(key string, values ...any) *Filter

func (*Filter) ConstNotIn

func (f *Filter) ConstNotIn(key string, values ...any) *Filter

func (*Filter) DateRangeIn

func (f *Filter) DateRangeIn(key string, from, to time.Time) *Filter

func (*Filter) DateRangeNotIn

func (f *Filter) DateRangeNotIn(key string, from, to time.Time) *Filter

func (*Filter) IsValid

func (f *Filter) IsValid(name string, val any) bool

func (*Filter) ListIn

func (f *Filter) ListIn(key string, values ...any) *Filter

func (*Filter) ListNotIn

func (f *Filter) ListNotIn(key string, values ...any) *Filter

func (*Filter) PropIn

func (f *Filter) PropIn(key string, values ...any) *Filter

func (*Filter) PropNotIn

func (f *Filter) PropNotIn(key string, values ...any) *Filter

func (*Filter) RangeIn

func (f *Filter) RangeIn(key string, from, to float64) *Filter

func (*Filter) RangeNotIn

func (f *Filter) RangeNotIn(key string, from, to float64) *Filter

func (*Filter) SearchNotIn

func (f *Filter) SearchNotIn(key string, values ...any) *Filter

func (*Filter) SerachIn

func (f *Filter) SerachIn(key string, values ...any) *Filter

type FilterSpec

type FilterSpec struct {
	Field string     `json:"field" db:"field" bson:"field"`
	Name  string     `json:"name" db:"name" bson:"name"`
	Type  FilterType `json:"type" db:"type" bson:"type"`
}

FilterSpec - filter specification

type FilterType

type FilterType string

FilterType - Type of filter item

const FtArray FilterType = "Array"

FtArray - filter for an array

const FtBoolean FilterType = "Boolean"

FtBoolean - filter for boolean field

const FtConstant FilterType = "Constant"

FtConstant - constant filter value

const FtDateRange FilterType = "DateRange"

DateRange - filter for date range

const FtNumRange FilterType = "NumRange"

FtNumRange - filter for real number range

const FtProp FilterType = "Prop"

FtProp - filter for a value

const FtSearch FilterType = "Search"

FtSearch - filter for search text field

type FilterValues

type FilterValues struct {
	Values map[string][]any        `json:"values" db:"values" bson:"values"`
	Dates  map[string]*DateRange   `json:"dates" db:"dates" bson:"dates"`
	Ranges map[string]*NumberRange `json:"ranges" db:"ranges" bson:"ranges"`
}

func NewFilterValues

func NewFilterValues() *FilterValues

type Getter

type Getter interface {
	Exists(
		gtx context.Context,
		dtype, keyField string,
		id any) (bool, error)

	Count(
		gtx context.Context,
		dtype string,
		filter *Filter) (int64, error)

	GetOne(
		gtx context.Context,
		dataType string,
		keyField string,
		key any,
		dataOut any) error

	Get(
		gtx context.Context,
		dtype string,
		params *CommonParams,
		out any) error

	FilterValues(
		gtx context.Context,
		dtype string,
		specs []*FilterSpec,
		filter *Filter) (*FilterValues, error)
}

type GetterDeleter

type GetterDeleter interface {
	Getter
	Deleter
}

type M

type M map[string]any

func (*M) Scan

func (u *M) Scan(value any) error

func (M) Value

func (u M) Value() (driver.Value, error)

type Matcher

type Matcher struct {
	Invert bool  `json:"invert" db:"invert" bson:"invert"`
	Fields []any `json:"fields" db:"fields" bson:"fields"`
}

FilterVal - values for filter along with the count

type FilterVal struct {
	Name  string `json:"name" db:"name" bson:"name"`
	Count int    `json:"count" db:"count" bson:"count"`
}

func (*Matcher) IsValid

func (m *Matcher) IsValid() bool

type NumberRange

type NumberRange struct {
	From float64 `json:"from" db:"_from" bson:"from"`
	To   float64 `json:"to" db:"_to" bson:"to"`
}

NumRange - represents real number ranges

func (*NumberRange) IsValid

func (r *NumberRange) IsValid() bool

IsValid - returns true if both From and To dates are non-zero

type PropMatcher

type PropMatcher []any

PropMatcher - matches props

type RangeMatcher

type RangeMatcher struct {
	NumberRange
	Invert bool `json:"invert" db:"invert" bson:"invert"`
}

func (*RangeMatcher) IsValid

func (r *RangeMatcher) IsValid() bool

type Validatable

type Validatable interface {
	IsValid() bool
}

type Vec

type Vec[T Arr] []T

func (Vec[T]) AsSlice

func (v Vec[T]) AsSlice() []T

func (*Vec[T]) Scan

func (v *Vec[T]) Scan(value any) error

func (Vec[T]) Value

func (v Vec[T]) Value() (driver.Value, error)

type VisitorFunc

type VisitorFunc func(state *WalkerState) (cont bool)

VisitorFunc - function that will be called on each value of reflected type. The return value decides whether to continue with depth search in current branch

type WalkConfig

type WalkConfig struct {
	Visitor            VisitorFunc        //visitor function
	FieldNameRetriever FieldNameRetriever //func to get name from struct field
	MaxDepth           int                //Stop walk at this depth
	IgnoreContainers   bool               //Ignore slice and map parent objects
	VisitPrivate       bool               //Visit private fields
	VisitRootStruct    bool               //Visit the root struct thats passed
}

WalkConfig - determines how Walk is carried out

type WalkerState

type WalkerState struct {
	Depth   int
	Field   *reflect.StructField
	Path    string
	Parent  *reflect.Value
	Current *reflect.Value
}

WalkerState - current state of the walk

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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