Documentation
¶
Index ¶
- Constants
- Variables
- func CondExec(cond bool, trueFunc, falseFunc func() error) error
- func Get(obj any, out any, path ...string) error
- func IsBasicType(rt reflect.Kind) bool
- func IsTime(val *reflect.Value) bool
- func IsValid[T Validatable](m map[string]T) bool
- func Max[T constraints.Ordered](in ...T) T
- func Min[T constraints.Ordered](in ...T) T
- func NonEmpty[T comparable](vals ...T) T
- func OneOf[T comparable](tgt T, opts ...T) bool
- func Qop[T any](cond bool, onTrue T, onFalse T) T
- func Set(obj any, newVal any, path ...string) error
- func ToFlatMap(obj interface{}, tagName string) (out map[string]interface{})
- func Walk(obj interface{}, config *WalkConfig)
- func WalkPath(obj any, path ...string) (reflect.Value, error)
- type Arr
- type CommonParams
- type DateRange
- type DateRangeMatcher
- type DbJson
- type Deleter
- type FieldNameRetriever
- type Filter
- func (f *Filter) Bool(key string, value any) *Filter
- func (f *Filter) ConstIn(key string, values ...any) *Filter
- func (f *Filter) ConstNotIn(key string, values ...any) *Filter
- func (f *Filter) DateRangeIn(key string, from, to time.Time) *Filter
- func (f *Filter) DateRangeNotIn(key string, from, to time.Time) *Filter
- func (f *Filter) IsValid(name string, val any) bool
- func (f *Filter) ListIn(key string, values ...any) *Filter
- func (f *Filter) ListNotIn(key string, values ...any) *Filter
- func (f *Filter) PropIn(key string, values ...any) *Filter
- func (f *Filter) PropNotIn(key string, values ...any) *Filter
- func (f *Filter) RangeIn(key string, from, to float64) *Filter
- func (f *Filter) RangeNotIn(key string, from, to float64) *Filter
- func (f *Filter) SearchNotIn(key string, values ...any) *Filter
- func (f *Filter) SerachIn(key string, values ...any) *Filter
- type FilterSpec
- type FilterType
- type FilterValues
- type Getter
- type GetterDeleter
- type M
- type Matcher
- type NumberRange
- type PropMatcher
- type RangeMatcher
- type Validatable
- type Vec
- type VisitorFunc
- type WalkConfig
- type WalkerState
Constants ¶
const InfiniteDepth int = -1
InfiniteDepth - used to indicate that Walk should continue till all the nodes in the heirarchy are visited
Variables ¶
var ( ErrInvalidWalkPath = errors.New("invalid walk path") ErrInvalidData = errors.New("invalid data") )
Functions ¶
func IsBasicType ¶
IsBasicType - tells if the kind of data type is basic or composite
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 ToFlatMap ¶
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
Types ¶
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 ¶
type DateRangeMatcher ¶
func (*DateRangeMatcher) IsValid ¶
func (dr *DateRangeMatcher) IsValid() bool
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 (*Filter) DateRangeNotIn ¶
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 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"`
}
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 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 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