Skip to content

Commit 3dc3051

Browse files
committed
Use builders and Actions in more clean way
1 parent be6dfd5 commit 3dc3051

File tree

2 files changed

+156
-111
lines changed

2 files changed

+156
-111
lines changed

search_builders.go

Lines changed: 87 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ type SearchBuilder struct {
1818
options *FTSearchOptions
1919
}
2020

21-
// Search starts building an FT.SEARCH command.
22-
func (c *Client) Search(ctx context.Context, index, query string) *SearchBuilder {
21+
// NewSearchBuilder creates a new SearchBuilder for FT.SEARCH commands.
22+
func (c *Client) NewSearchBuilder(ctx context.Context, index, query string) *SearchBuilder {
2323
b := &SearchBuilder{c: c, ctx: ctx, index: index, query: query, options: &FTSearchOptions{LimitOffset: -1}}
2424
return b
2525
}
@@ -215,8 +215,8 @@ type AggregateBuilder struct {
215215
options *FTAggregateOptions
216216
}
217217

218-
// Aggregate starts building an FT.AGGREGATE command.
219-
func (c *Client) Aggregate(ctx context.Context, index, query string) *AggregateBuilder {
218+
// NewAggregateBuilder creates a new AggregateBuilder for FT.AGGREGATE commands.
219+
func (c *Client) NewAggregateBuilder(ctx context.Context, index, query string) *AggregateBuilder {
220220
return &AggregateBuilder{c: c, ctx: ctx, index: index, query: query, options: &FTAggregateOptions{LimitOffset: -1}}
221221
}
222222

@@ -367,8 +367,8 @@ type CreateIndexBuilder struct {
367367
schema []*FieldSchema
368368
}
369369

370-
// CreateIndex starts building an FT.CREATE command.
371-
func (c *Client) CreateIndex(ctx context.Context, index string) *CreateIndexBuilder {
370+
// NewCreateIndexBuilder creates a new CreateIndexBuilder for FT.CREATE commands.
371+
func (c *Client) NewCreateIndexBuilder(ctx context.Context, index string) *CreateIndexBuilder {
372372
return &CreateIndexBuilder{c: c, ctx: ctx, index: index, options: &FTCreateOptions{}}
373373
}
374374

@@ -473,8 +473,8 @@ type DropIndexBuilder struct {
473473
options *FTDropIndexOptions
474474
}
475475

476-
// DropIndex starts FT.DROPINDEX builder.
477-
func (c *Client) DropIndex(ctx context.Context, index string) *DropIndexBuilder {
476+
// NewDropIndexBuilder creates a new DropIndexBuilder for FT.DROPINDEX commands.
477+
func (c *Client) NewDropIndexBuilder(ctx context.Context, index string) *DropIndexBuilder {
478478
return &DropIndexBuilder{c: c, ctx: ctx, index: index}
479479
}
480480

@@ -499,19 +499,35 @@ type AliasBuilder struct {
499499
action string // add|del|update
500500
}
501501

502-
// AliasAdd starts FT.ALIASADD builder.
503-
func (c *Client) AliasAdd(ctx context.Context, alias, index string) *AliasBuilder {
504-
return &AliasBuilder{c: c, ctx: ctx, alias: alias, index: index, action: "add"}
502+
// NewAliasBuilder creates a new AliasBuilder for FT.ALIAS* commands.
503+
func (c *Client) NewAliasBuilder(ctx context.Context, alias string) *AliasBuilder {
504+
return &AliasBuilder{c: c, ctx: ctx, alias: alias}
505505
}
506506

507-
// AliasDel starts FT.ALIASDEL builder.
508-
func (c *Client) AliasDel(ctx context.Context, alias string) *AliasBuilder {
509-
return &AliasBuilder{c: c, ctx: ctx, alias: alias, action: "del"}
507+
// Action sets the action for the alias builder.
508+
func (b *AliasBuilder) Action(action string) *AliasBuilder {
509+
b.action = action
510+
return b
511+
}
512+
513+
// Add sets the action to "add" and requires an index.
514+
func (b *AliasBuilder) Add(index string) *AliasBuilder {
515+
b.action = "add"
516+
b.index = index
517+
return b
518+
}
519+
520+
// Del sets the action to "del".
521+
func (b *AliasBuilder) Del() *AliasBuilder {
522+
b.action = "del"
523+
return b
510524
}
511525

512-
// AliasUpdate starts FT.ALIASUPDATE builder.
513-
func (c *Client) AliasUpdate(ctx context.Context, alias, index string) *AliasBuilder {
514-
return &AliasBuilder{c: c, ctx: ctx, alias: alias, index: index, action: "update"}
526+
// Update sets the action to "update" and requires an index.
527+
func (b *AliasBuilder) Update(index string) *AliasBuilder {
528+
b.action = "update"
529+
b.index = index
530+
return b
515531
}
516532

517533
// Run executes the configured alias command.
@@ -542,8 +558,8 @@ type ExplainBuilder struct {
542558
options *FTExplainOptions
543559
}
544560

545-
// Explain starts FT.EXPLAIN builder.
546-
func (c *Client) Explain(ctx context.Context, index, query string) *ExplainBuilder {
561+
// NewExplainBuilder creates a new ExplainBuilder for FT.EXPLAIN commands.
562+
func (c *Client) NewExplainBuilder(ctx context.Context, index, query string) *ExplainBuilder {
547563
return &ExplainBuilder{c: c, ctx: ctx, index: index, query: query, options: &FTExplainOptions{}}
548564
}
549565

@@ -566,8 +582,8 @@ type FTInfoBuilder struct {
566582
index string
567583
}
568584

569-
// SearchInfo starts building an FT.INFO command for RediSearch.
570-
func (c *Client) SearchInfo(ctx context.Context, index string) *FTInfoBuilder {
585+
// NewSearchInfoBuilder creates a new FTInfoBuilder for FT.INFO commands.
586+
func (c *Client) NewSearchInfoBuilder(ctx context.Context, index string) *FTInfoBuilder {
571587
return &FTInfoBuilder{c: c, ctx: ctx, index: index}
572588
}
573589

@@ -589,8 +605,8 @@ type SpellCheckBuilder struct {
589605
options *FTSpellCheckOptions
590606
}
591607

592-
// SpellCheck starts FT.SPELLCHECK builder.
593-
func (c *Client) SpellCheck(ctx context.Context, index, query string) *SpellCheckBuilder {
608+
// NewSpellCheckBuilder creates a new SpellCheckBuilder for FT.SPELLCHECK commands.
609+
func (c *Client) NewSpellCheckBuilder(ctx context.Context, index, query string) *SpellCheckBuilder {
594610
return &SpellCheckBuilder{c: c, ctx: ctx, index: index, query: query, options: &FTSpellCheckOptions{}}
595611
}
596612

@@ -633,19 +649,35 @@ type DictBuilder struct {
633649
action string // add|del|dump
634650
}
635651

636-
// DictAdd starts FT.DICTADD builder.
637-
func (c *Client) DictAdd(ctx context.Context, dict string, terms ...interface{}) *DictBuilder {
638-
return &DictBuilder{c: c, ctx: ctx, dict: dict, terms: terms, action: "add"}
652+
// NewDictBuilder creates a new DictBuilder for FT.DICT* commands.
653+
func (c *Client) NewDictBuilder(ctx context.Context, dict string) *DictBuilder {
654+
return &DictBuilder{c: c, ctx: ctx, dict: dict}
639655
}
640656

641-
// DictDel starts FT.DICTDEL builder.
642-
func (c *Client) DictDel(ctx context.Context, dict string, terms ...interface{}) *DictBuilder {
643-
return &DictBuilder{c: c, ctx: ctx, dict: dict, terms: terms, action: "del"}
657+
// Action sets the action for the dictionary builder.
658+
func (b *DictBuilder) Action(action string) *DictBuilder {
659+
b.action = action
660+
return b
644661
}
645662

646-
// DictDump starts FT.DICTDUMP builder.
647-
func (c *Client) DictDump(ctx context.Context, dict string) *DictBuilder {
648-
return &DictBuilder{c: c, ctx: ctx, dict: dict, action: "dump"}
663+
// Add sets the action to "add" and requires terms.
664+
func (b *DictBuilder) Add(terms ...interface{}) *DictBuilder {
665+
b.action = "add"
666+
b.terms = terms
667+
return b
668+
}
669+
670+
// Del sets the action to "del" and requires terms.
671+
func (b *DictBuilder) Del(terms ...interface{}) *DictBuilder {
672+
b.action = "del"
673+
b.terms = terms
674+
return b
675+
}
676+
677+
// Dump sets the action to "dump".
678+
func (b *DictBuilder) Dump() *DictBuilder {
679+
b.action = "dump"
680+
return b
649681
}
650682

651683
// Run executes the configured dictionary command.
@@ -675,8 +707,8 @@ type TagValsBuilder struct {
675707
field string
676708
}
677709

678-
// TagVals starts FT.TAGVALS builder.
679-
func (c *Client) TagVals(ctx context.Context, index, field string) *TagValsBuilder {
710+
// NewTagValsBuilder creates a new TagValsBuilder for FT.TAGVALS commands.
711+
func (c *Client) NewTagValsBuilder(ctx context.Context, index, field string) *TagValsBuilder {
680712
return &TagValsBuilder{c: c, ctx: ctx, index: index, field: field}
681713
}
682714

@@ -699,14 +731,27 @@ type CursorBuilder struct {
699731
action string // read|del
700732
}
701733

702-
// CursorRead starts FT.CURSOR READ builder.
703-
func (c *Client) CursorRead(ctx context.Context, index string, cursorId int64) *CursorBuilder {
704-
return &CursorBuilder{c: c, ctx: ctx, index: index, cursorId: cursorId, action: "read"}
734+
// NewCursorBuilder creates a new CursorBuilder for FT.CURSOR* commands.
735+
func (c *Client) NewCursorBuilder(ctx context.Context, index string, cursorId int64) *CursorBuilder {
736+
return &CursorBuilder{c: c, ctx: ctx, index: index, cursorId: cursorId}
705737
}
706738

707-
// CursorDel starts FT.CURSOR DEL builder.
708-
func (c *Client) CursorDel(ctx context.Context, index string, cursorId int64) *CursorBuilder {
709-
return &CursorBuilder{c: c, ctx: ctx, index: index, cursorId: cursorId, action: "del"}
739+
// Action sets the action for the cursor builder.
740+
func (b *CursorBuilder) Action(action string) *CursorBuilder {
741+
b.action = action
742+
return b
743+
}
744+
745+
// Read sets the action to "read".
746+
func (b *CursorBuilder) Read() *CursorBuilder {
747+
b.action = "read"
748+
return b
749+
}
750+
751+
// Del sets the action to "del".
752+
func (b *CursorBuilder) Del() *CursorBuilder {
753+
b.action = "del"
754+
return b
710755
}
711756

712757
// Count for READ.
@@ -738,8 +783,8 @@ type SynUpdateBuilder struct {
738783
terms []interface{}
739784
}
740785

741-
// SynUpdate starts FT.SYNUPDATE builder.
742-
func (c *Client) SynUpdate(ctx context.Context, index string, groupId interface{}) *SynUpdateBuilder {
786+
// NewSynUpdateBuilder creates a new SynUpdateBuilder for FT.SYNUPDATE commands.
787+
func (c *Client) NewSynUpdateBuilder(ctx context.Context, index string, groupId interface{}) *SynUpdateBuilder {
743788
return &SynUpdateBuilder{c: c, ctx: ctx, index: index, groupId: groupId, options: &FTSynUpdateOptions{}}
744789
}
745790

0 commit comments

Comments
 (0)