Skip to content

Commit

Permalink
Merge pull request #9 from Trendyol/feature/range-v2
Browse files Browse the repository at this point in the history
feature: update range v2
  • Loading branch information
GokselKUCUKSAHIN authored Oct 29, 2024
2 parents 9b69b17 + d6d9ca1 commit 95e7587
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 45 deletions.
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
}

0 comments on commit 95e7587

Please sign in to comment.