You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use filter JSON string to filter your object list.
// should matchfilterBuilder:=builder.NewPlayerFilterBuilder()
filter:=filterBuilder.IsMaleObject(filterbuilder.Eq(true)).Build()
filterBuilder.Reset()
//filter := "{\"isMale\": {\"$eq\": true}}"filteredPlayers:=filterService.FilterOut(filter, players)
for_, player:=rangefilteredPlayers {
slog.Info(fmt.Sprintf("after FilterOut, filter %s match %+#v ok", filter, player))
}
// INFO after FilterOut, filter {"isMale":{"$eq":true}} match &models.Player{User:models.User{Name:"Scott", IsMale:true}, Level:10} ok// INFO after FilterOut, filter {"isMale":{"$eq":true}} not match &models.Player{User:models.User{Name:"Alice", IsMale:false}, Level:25}
Data types for StructuredFilter
Object
Object is a JSON object but allows exactly one key-value pair.
Range is a StructuredFilter Array containing two elements, the first element of the Array is less than or equal to the second element, and the elements need to be one of the following types:
string
double
Version
The value of Range uses a closed interval, that is, the match is successful when it is greater than or equal to the first element and less than or equal to the second element.
Match successfully when the value of the matching object and filter value are equal
{"isMale": {"$eq": true}}
$ne
bool
Match successfully when the value of the matching object and filter value are not equal
{"isMale": {"$ne": true}}
Number filters
Key
Value Type
Description
Filter Examples
$eq
double
Match successfully when the value of the matching object and filter value are equal
{"age": {"$eq": 20}}
$ne
double
Match successfully when the value of the matching object and filter value are not equal
{"age": {"$ne": 20}}
$in
double Array
Match successfully when the value of the matching object is equal to any of the filter values
{"age": {"$in": [20, 21, 22]}}
$lt
double
Match successfully when the value of the matching object is less than the filter value
{"age": {"$lt": 20}}
$gt
double
Match successfully when the value of the matching object is greater than the filter value
{"age": {"$gt": 20}}
$le
double
Match successfully when the value of the matching object is less than or equal to the filter value
{"age": {"$le": 20}}
$ge
double
Match successfully when the value of the matching object is greater than or equal to the filter value
{"age": {"$ge": 20}}
$range
double Range
Match successfully when the value of the matching object is greater than or equal to the first element in the filter values and less than or equal to the second element in the filter values
{"age": {"$range": [20, 30]}}
Number Array filters
Key
Value Type
Description
Filter Examples
$eq
double Array
Match successfully when each element of the matching array is equal to the element at each corresponding position of the filter value
{"itemIds": {"$eq": [1000, 1001, 1002]}}
$all
double Array
Match successfully when the matching array contains every element of the filter value
{"itemIds": {"$all": [1000, 1005]}}
String filters
Key
Value Type
Description
Filter Examples
$eq
string
Match successfully when the value of the matching object and filter value are equal
{"userName": {"$eq": "Scott"}}
$ne
string
Match successfully when the value of the matching object and filter value are not equal
{"userName": {"$ne": "Scott"}}
$in
string Array
Match successfully when the value of the matching object is equal to any of the filter values
{"userName": {"$in": ["Scott", "Tom", "Bob"]}}
$range
string Range
Match successfully when the value of the matching object is greater than or equal to the first element in the filter values and less than or equal to the second element in the filter values
Match successfully when the value of the matching object match filter value as regular expression
{"userName": {"$regex": "^S"}}
String Array filters
Key
Value Type
Description
Filter Examples
$eq
string Array
Match successfully when each element of the matching array is equal to the element at each corresponding position of the filter value
{"tags": {"$eq": ["season 1", "region c"]}}
$all
string Array
Match successfully when the matching array contains every element of the filter value
{"tags": {"$all": ["season 3"]}}
Scene filters
Scene filters are defined by the user and can be nested as subordinates of Logic filters or superior of Basic filters.
The following classes of scene filters are provided to simplify the writing of your scene filters:
BoolSceneFilter
NumberSceneFilter
StringSceneFilter
Filter Builders
Filter Builders are used to construct the filter string by chaining method calls.
typeIFilterBuilderinterface {
// Or And must be placed before any other method call, and only one of them can be called, at most once.Or() IFilterBuilderAnd() IFilterBuilder// KStringV KStringArrayV KBoolV KNumberV KNumberArrayV are used to build `$eq` filters of corresponding data types.KStringV(keystring, valuestring) IFilterBuilderKStringArrayV(keystring, value []string) IFilterBuilderKBoolV(keystring, valuebool) IFilterBuilderKNumberV(keystring, valuefloat64) IFilterBuilderKNumberArrayV(keystring, value []float64) IFilterBuilder// KObjectV is used to build filters other than `$eq`.KObjectV(keystring, valueFilterBuilderObject) IFilterBuilder// Build is usually placed at the end of the chaining method calls and returns the filter string. It is reentrant.Build() string// Reset is used to clean all the filters in the `IFilterBuilder` if you need to build another filter string.Reset()
}
Or/And must be placed before any other method call, and only one of them can be called, at most once.
KStringV/KStringArrayV/KBoolV/KNumberV/KNumberArrayV are used to build $eq filters of corresponding data types when KObjectV is used to build other filters like $lt, $range, $in and all other filters mentioned above.
Build is usually placed at the end of the chaining method calls and returns the filter string. Build is reentrant and you can call Reset to clean all the filters in the IFilterBuilder if you need to build another filter string.
About
A general-purpose, business-agnostic structured filter library.