Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: update range v2 #9

Merged
merged 5 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 31 additions & 41 deletions es/range.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,34 @@ type rangeType Object
// A rangeType object with the specified field ready for defining range conditions.
func Range(key string) rangeType {
return rangeType{
key: Object{},
"range": Object{
key: Object{},
},
}
}

func (r rangeType) putInTheField(key string, value any) rangeType {
if rang, ok := r["range"].(Object); ok {
for field := range rang {
if fieldObject, foOk := rang[field].(Object); foOk {
fieldObject[key] = value
}
}
}
return r
}

func (r rangeType) delete(key string) rangeType {
if rang, ok := r["range"].(Object); ok {
for field := range rang {
if fieldObject, foOk := rang[field].(Object); foOk {
delete(fieldObject, key)
}
}
}
return r
}

// LesserThan sets the "lt" (less than) field for the range query.
//
// This method specifies that the range query should match values that are less than
Expand All @@ -42,13 +66,7 @@ func Range(key string) rangeType {
//
// The updated rangeType object with the "lt" field set to the specified value.
func (r rangeType) LesserThan(lt any) rangeType {
for key := range r {
if rangeObject, ok := r[key].(Object); ok {
rangeObject["lt"] = lt
delete(rangeObject, "lte")
}
}
return r
return r.putInTheField("lt", lt).delete("lte")
}

// LesserThanOrEqual sets the "lte" (less than or equal to) field for the range query.
Expand All @@ -69,13 +87,7 @@ func (r rangeType) LesserThan(lt any) rangeType {
//
// The updated rangeType object with the "lte" field set to the specified value.
func (r rangeType) LesserThanOrEqual(lte any) rangeType {
for key := range r {
if rangeObject, ok := r[key].(Object); ok {
rangeObject["lte"] = lte
delete(rangeObject, "lt")
}
}
return r
return r.putInTheField("lte", lte).delete("lt")
}

// GreaterThan sets the "gt" (greater than) field for the range query.
Expand All @@ -96,13 +108,7 @@ func (r rangeType) LesserThanOrEqual(lte any) rangeType {
//
// The updated rangeType object with the "gt" field set to the specified value.
func (r rangeType) GreaterThan(gt any) rangeType {
for key := range r {
if rangeObject, ok := r[key].(Object); ok {
rangeObject["gt"] = gt
delete(rangeObject, "gte")
}
}
return r
return r.putInTheField("gt", gt).delete("gte")
}

// GreaterThanOrEqual sets the "gte" (greater than or equal to) field for the range query.
Expand All @@ -123,13 +129,7 @@ func (r rangeType) GreaterThan(gt any) rangeType {
//
// The updated rangeType object with the "gte" field set to the specified value.
func (r rangeType) GreaterThanOrEqual(gte any) rangeType {
for key := range r {
if rangeObject, ok := r[key].(Object); ok {
rangeObject["gte"] = gte
delete(rangeObject, "gt")
}
}
return r
return r.putInTheField("gte", gte).delete("gt")
}

// Format sets the "format" field for the range query.
Expand All @@ -150,12 +150,7 @@ func (r rangeType) GreaterThanOrEqual(gte any) rangeType {
//
// The updated rangeType object with the "format" field set to the specified value.
func (r rangeType) Format(format string) rangeType {
for key := range r {
if rangeObject, ok := r[key].(Object); ok {
rangeObject["format"] = format
}
}
return r
return r.putInTheField("format", format)
}

// Boost sets the "boost" field for the range query.
Expand All @@ -175,10 +170,5 @@ func (r rangeType) Format(format string) rangeType {
//
// The updated rangeType object with the "boost" field set to the specified value.
func (r rangeType) Boost(boost float64) rangeType {
for key := range r {
if rangeObject, ok := r[key].(Object); ok {
rangeObject["boost"] = boost
}
}
return r
return r.putInTheField("boost", boost)
}
5 changes: 1 addition & 4 deletions es/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ func correctType(b any) (any, bool) {
if b == nil || unsafeIsNil(b) {
return Object{}, false
}
switch b.(type) {
case BoolType:
if _, ok := b.(BoolType); ok {
return Object{"bool": b}, true
case rangeType:
return Object{"range": b}, true
}
return b, true
}
Loading