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

improvement: Update interface{} casting to avoid runtime panic #5

Merged
merged 4 commits into from
Sep 5, 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
12 changes: 6 additions & 6 deletions es/aggregations.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ func (agg aggsType) putInTheField(key string, value any) aggsType {
//
// The updated aggsType object with the nested aggregation added.
func (agg aggsType) Aggs(name string, nestedAgg aggsType) aggsType {
aggs, exists := agg["aggs"]
if !exists {
aggs, ok := agg["aggs"].(Object)
if !ok {
aggs = Object{}
}
aggs.(Object)[name] = nestedAgg
aggs[name] = nestedAgg
agg["aggs"] = aggs
return agg
}
Expand Down Expand Up @@ -377,11 +377,11 @@ func (agg aggsType) Terms(terms ...aggTermType) aggsType {
//
// The updated Object with the "aggs" field containing the new named aggregation.
func (o Object) Aggs(name string, agg aggsType) Object {
aggs, exists := o["aggs"]
if !exists {
aggs, ok := o["aggs"].(Object)
if !ok {
aggs = Object{}
}
aggs.(Object)[name] = agg
aggs[name] = agg
o["aggs"] = aggs
return o
}
32 changes: 16 additions & 16 deletions es/bool_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ func (b boolType) Boost(boost float64) boolType {
//
// The updated boolType object with the new filter conditions added.
func (b boolType) Filter(items ...any) boolType {
filter, exists := b["filter"]
if !exists {
filter, ok := b["filter"].(filterType)
if !ok {
filter = filterType{}
}
for i := 0; i < len(items); i++ {
if field, ok := correctType(items[i]); ok {
filter = append(filter.(filterType), field)
if field, fOk := correctType(items[i]); fOk {
filter = append(filter, field)
}
}
b["filter"] = filter
Expand Down Expand Up @@ -133,13 +133,13 @@ func (b boolType) Filter(items ...any) boolType {
//
// The updated boolType object with the new conditions added to the "must" section.
func (b boolType) Must(items ...any) boolType {
must, exists := b["must"]
if !exists {
must, ok := b["must"].(mustType)
if !ok {
must = mustType{}
}
for i := 0; i < len(items); i++ {
if field, ok := correctType(items[i]); ok {
must = append(must.(mustType), field)
if field, fOk := correctType(items[i]); fOk {
must = append(must, field)
}
}
b["must"] = must
Expand Down Expand Up @@ -168,13 +168,13 @@ func (b boolType) Must(items ...any) boolType {
//
// The updated boolType object with the new conditions added to the "must_not" section.
func (b boolType) MustNot(items ...any) boolType {
mustNot, exists := b["must_not"]
if !exists {
mustNot, ok := b["must_not"].(mustNotType)
if !ok {
mustNot = mustNotType{}
}
for i := 0; i < len(items); i++ {
if field, ok := correctType(items[i]); ok {
mustNot = append(mustNot.(mustNotType), field)
if field, fOk := correctType(items[i]); fOk {
mustNot = append(mustNot, field)
}
}
b["must_not"] = mustNot
Expand Down Expand Up @@ -203,13 +203,13 @@ func (b boolType) MustNot(items ...any) boolType {
//
// The updated boolType object with the new conditions added to the "should" section.
func (b boolType) Should(items ...any) boolType {
should, exists := b["should"]
if !exists {
should, ok := b["should"].(shouldType)
if !ok {
should = shouldType{}
}
for i := 0; i < len(items); i++ {
if field, ok := correctType(items[i]); ok {
should = append(should.(shouldType), field)
if field, fOk := correctType(items[i]); fOk {
should = append(should, field)
}
}
b["should"] = should
Expand Down
4 changes: 2 additions & 2 deletions es/match_all_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func MatchAll() matchAllType {
}

func (m matchAllType) putInTheField(key string, value any) matchAllType {
if matchAll, exists := m["match_all"]; exists {
matchAll.(Object)[key] = value
if matchAll, ok := m["match_all"].(Object); ok {
matchAll[key] = value
}
return m
}
Expand Down
10 changes: 4 additions & 6 deletions es/match_none_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ func MatchNone[T any](key string, query T) matchNoneType {
}

func (m matchNoneType) putInTheField(key string, value any) matchNoneType {
if matchNone, exists := m["match_none"]; exists {
if matchNoneObject, mnoOk := matchNone.(Object); mnoOk {
for field := range matchNoneObject {
if fieldObject, foOk := matchNoneObject[field].(Object); foOk {
fieldObject[key] = value
}
if matchNone, ok := m["match_none"].(Object); ok {
for field := range matchNone {
if fieldObject, foOk := matchNone[field].(Object); foOk {
fieldObject[key] = value
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions es/match_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ func Match[T any](key string, query T) matchType {
}

func (m matchType) putInTheField(key string, value any) matchType {
if match, exists := m["match"]; exists {
if matchObject, moOk := match.(Object); moOk {
for field := range matchObject {
if fieldObject, foOk := matchObject[field].(Object); foOk {
fieldObject[key] = value
}
if match, ok := m["match"].(Object); ok {
for field := range match {
if fieldObject, foOk := match[field].(Object); foOk {
fieldObject[key] = value
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions es/nested_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func Nested[T any](path string, nestedQuery T) nestedType {
}

func (n nestedType) putInNested(key string, value any) nestedType {
if nested, exists := n["nested"]; exists {
nested.(Object)[key] = value
if nestedObject, ok := n["nested"].(Object); ok {
nestedObject[key] = value
}
return n
}
Expand Down
Loading