refactor(search): simplify osu builder interface and make use of a base for the requests

This commit is contained in:
fschade
2025-08-08 21:55:14 +02:00
parent 7fe5383d61
commit 8c509263b7
9 changed files with 22 additions and 66 deletions

View File

@@ -12,7 +12,6 @@ import (
type Builder interface {
json.Marshaler
fmt.Stringer
Map() (map[string]any, error)
}
@@ -64,11 +63,10 @@ func applyBuilder(target map[string]any, key string, builder Builder) error {
return fmt.Errorf("failed to map builder %s: %w", key, err)
}
if isEmpty(data) {
return nil
if !isEmpty(data) {
target[key] = data
}
target[key] = data
return nil
}

View File

@@ -85,8 +85,3 @@ func (q *BoolQuery) MarshalJSON() ([]byte, error) {
}
return json.Marshal(data)
}
func (q *BoolQuery) String() string {
b, _ := q.MarshalJSON()
return string(b)
}

View File

@@ -56,8 +56,3 @@ func (q *MatchPhraseQuery) MarshalJSON() ([]byte, error) {
}
return json.Marshal(data)
}
func (q *MatchPhraseQuery) String() string {
b, _ := q.MarshalJSON()
return string(b)
}

View File

@@ -47,8 +47,3 @@ func (q *IDsQuery) MarshalJSON() ([]byte, error) {
}
return json.Marshal(data)
}
func (q *IDsQuery) String() string {
b, _ := q.MarshalJSON()
return string(b)
}

View File

@@ -16,6 +16,15 @@ func TestIDsQuery(t *testing.T) {
Got: osu.NewIDsQuery(),
Want: nil,
},
{
Name: "no options",
Got: osu.NewIDsQuery("1", "2", "3", "3"),
Want: map[string]any{
"ids": map[string]any{
"values": []string{"1", "2", "3"},
},
},
},
{
Name: "ids",
Got: osu.NewIDsQuery("1", "2", "3", "3").Options(&osu.IDsQueryOptions{Boost: 1.0}),

View File

@@ -90,8 +90,3 @@ func (q *RangeQuery[T]) MarshalJSON() ([]byte, error) {
}
return json.Marshal(data)
}
func (q *RangeQuery[T]) String() string {
b, _ := q.MarshalJSON()
return string(b)
}

View File

@@ -56,8 +56,3 @@ func (q *TermQuery[T]) MarshalJSON() ([]byte, error) {
}
return json.Marshal(data)
}
func (q *TermQuery[T]) String() string {
b, _ := q.MarshalJSON()
return string(b)
}

View File

@@ -56,8 +56,3 @@ func (q *WildcardQuery) MarshalJSON() ([]byte, error) {
}
return json.Marshal(data)
}
func (q *WildcardQuery) String() string {
b, _ := q.MarshalJSON()
return string(b)
}

View File

@@ -3,12 +3,8 @@ package osu
import (
"bytes"
"encoding/json"
"io"
"strings"
opensearchgoAPI "github.com/opensearch-project/opensearch-go/v4/opensearchapi"
"github.com/opencloud-eu/opencloud/pkg/conversions"
)
type RequestBody[O any] struct {
@@ -21,20 +17,16 @@ func NewRequestBody[O any](q Builder, o ...O) *RequestBody[O] {
}
func (q RequestBody[O]) Map() (map[string]any, error) {
data, err := conversions.To[map[string]any](q.options)
base, err := newBase(q.options)
if err != nil {
return nil, err
}
if err := applyBuilder(data, "query", q.query); err != nil {
if err := applyBuilder(base, "query", q.query); err != nil {
return nil, err
}
if isEmpty(data) {
return nil, nil
}
return data, nil
return base, nil
}
func (q RequestBody[O]) MarshalJSON() ([]byte, error) {
@@ -46,15 +38,6 @@ func (q RequestBody[O]) MarshalJSON() ([]byte, error) {
return json.Marshal(data)
}
func (q RequestBody[O]) String() string {
b, _ := q.MarshalJSON()
return string(b)
}
func (q RequestBody[O]) Reader() io.Reader {
return strings.NewReader(q.String())
}
//----------------------------------------------------------------------------//
type HighlightOption struct {
@@ -72,12 +55,11 @@ type ScriptOption struct {
//----------------------------------------------------------------------------//
func BuildSearchReq(req *opensearchgoAPI.SearchReq, q Builder, o ...SearchReqOptions) (*opensearchgoAPI.SearchReq, error) {
body := NewRequestBody(q, o...)
data, err := body.MarshalJSON()
body, err := json.Marshal(NewRequestBody(q, o...))
if err != nil {
return nil, err
}
req.Body = bytes.NewReader(data)
req.Body = bytes.NewReader(body)
return req, nil
}
@@ -88,24 +70,22 @@ type SearchReqOptions struct {
//----------------------------------------------------------------------------//
func BuildDocumentDeleteByQueryReq(req opensearchgoAPI.DocumentDeleteByQueryReq, q Builder) (opensearchgoAPI.DocumentDeleteByQueryReq, error) {
body := NewRequestBody[any](q)
data, err := body.MarshalJSON()
body, err := json.Marshal(NewRequestBody[any](q))
if err != nil {
return req, err
}
req.Body = bytes.NewReader(data)
req.Body = bytes.NewReader(body)
return req, nil
}
//----------------------------------------------------------------------------//
func BuildUpdateByQueryReq(req opensearchgoAPI.UpdateByQueryReq, q Builder, o ...UpdateByQueryReqOptions) (opensearchgoAPI.UpdateByQueryReq, error) {
body := NewRequestBody(q, o...)
data, err := body.MarshalJSON()
body, err := json.Marshal(NewRequestBody(q, o...))
if err != nil {
return req, err
}
req.Body = bytes.NewReader(data)
req.Body = bytes.NewReader(body)
return req, nil
}
@@ -116,11 +96,10 @@ type UpdateByQueryReqOptions struct {
//----------------------------------------------------------------------------//
func BuildIndicesCountReq(req *opensearchgoAPI.IndicesCountReq, q Builder) (*opensearchgoAPI.IndicesCountReq, error) {
body := NewRequestBody[any](q)
data, err := body.MarshalJSON()
body, err := json.Marshal(NewRequestBody[any](q))
if err != nil {
return nil, err
}
req.Body = bytes.NewReader(data)
req.Body = bytes.NewReader(body)
return req, nil
}