Skip to content

Commit

Permalink
Merge pull request #3 from awakari/exact-text-match-conditions-support
Browse files Browse the repository at this point in the history
exact text match conditions support
  • Loading branch information
akurilov authored Jul 2, 2023
2 parents e43b9b7 + ac11e76 commit 5194038
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 117 deletions.
5 changes: 3 additions & 2 deletions api/grpc/subscriptions/client_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ func (cm clientMock) Read(ctx context.Context, req *ReadRequest, opts ...grpc.Ca
{
Cond: &ConditionOutput_Tc{
Tc: &TextConditionOutput{
Key: "k1",
Term: "p1",
Key: "k1",
Term: "p1",
Exact: true,
},
},
},
Expand Down
7 changes: 4 additions & 3 deletions api/grpc/subscriptions/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ func encodeCondition(src condition.Condition) (dst *ConditionInput) {
case condition.TextCondition:
dst.Cond = &ConditionInput_Tc{
Tc: &TextConditionInput{
Key: c.GetKey(),
Term: c.GetTerm(),
Key: c.GetKey(),
Term: c.GetTerm(),
Exact: c.IsExact(),
},
}
}
Expand Down Expand Up @@ -173,7 +174,7 @@ func decodeCondition(src *ConditionOutput) (dst condition.Condition, err error)
case tc != nil:
dst = condition.NewTextCondition(
condition.NewKeyCondition(condition.NewCondition(src.Not), tc.GetKey()),
tc.GetTerm(),
tc.GetTerm(), tc.GetExact(),
)
default:
err = fmt.Errorf("%w: unsupported condition type", ErrInternal)
Expand Down
200 changes: 109 additions & 91 deletions api/grpc/subscriptions/service.pb.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions api/grpc/subscriptions/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ enum GroupLogic {
message TextConditionInput {
string key = 1;
string term = 2;
bool exact = 3;
}

message CreateResponse {
Expand Down Expand Up @@ -79,6 +80,7 @@ message TextConditionOutput {
string id = 1;
string key = 2;
string term = 3;
bool exact = 4;
}

// Update
Expand Down
13 changes: 7 additions & 6 deletions api/grpc/subscriptions/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestService_Create(t *testing.T) {
Description: "invalid",
Condition: condition.NewTextCondition(
condition.NewKeyCondition(condition.NewCondition(false), "key0"),
"ok",
"ok", false,
),
},
err: ErrInvalid,
Expand All @@ -34,7 +34,7 @@ func TestService_Create(t *testing.T) {
Description: "busy",
Condition: condition.NewTextCondition(
condition.NewKeyCondition(condition.NewCondition(false), ""),
"locked",
"locked", false,
),
},
err: ErrBusy,
Expand All @@ -47,7 +47,7 @@ func TestService_Create(t *testing.T) {
condition.NewCondition(false),
"fail",
),
"fail",
"fail", false,
),
},
err: ErrInternal,
Expand All @@ -57,7 +57,7 @@ func TestService_Create(t *testing.T) {
Description: "my subscription",
Condition: condition.NewTextCondition(
condition.NewKeyCondition(condition.NewCondition(false), "key0"),
"ok",
"ok", false,
),
},
},
Expand All @@ -81,7 +81,7 @@ func TestService_Create(t *testing.T) {
Description: "fail_auth",
Condition: condition.NewTextCondition(
condition.NewKeyCondition(condition.NewCondition(false), "key0"),
"ok",
"ok", false,
),
},
err: auth.ErrAuth,
Expand All @@ -91,7 +91,7 @@ func TestService_Create(t *testing.T) {
Description: "limit_reached",
Condition: condition.NewTextCondition(
condition.NewKeyCondition(condition.NewCondition(false), "key0"),
"ok",
"ok", false,
),
},
err: limits.ErrReached,
Expand Down Expand Up @@ -149,6 +149,7 @@ func TestService_Read(t *testing.T) {
NewBuilder().
MatchAttrKey("k1").
MatchText("p1").
MatchExact().
BuildTextCondition(),
},
).
Expand Down
40 changes: 32 additions & 8 deletions model/subscription/condition/builder.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
package condition

type Builder interface {

// Negation makes a resulting Condition negative.
// May be set for any resulting Condition type.
Negation() Builder

// GroupLogic defines the logic for a resulting GroupCondition. Default is GroupLogicAnd.
GroupLogic(l GroupLogic) Builder

// GroupChildren defines the nested Condition set for a resulting GroupCondition.
GroupChildren(children []Condition) Builder

// BuildGroupCondition builds a GroupCondition.
BuildGroupCondition() (c Condition)

// MatchAttrKey defines the incoming messages attribute key to match for a resulting KeyCondition.
// Default is empty string key. For a TextCondition empty key causes the matching against all attribute keys.
MatchAttrKey(k string) Builder

// MatchText defines the text search terms for a resulting TextCondition.
MatchText(p string) Builder

BuildGroupCondition() (c Condition)
// MatchExact enables the exact text matching criteria for a resulting TextCondition.
MatchExact() Builder

// BuildTextCondition builds a TextCondition.
BuildTextCondition() (c Condition)
}

type builder struct {
not bool
gl GroupLogic
gc []Condition
key string
term string
partial bool
not bool
gl GroupLogic
gc []Condition
key string
term string
exact bool
}

func NewBuilder() Builder {
Expand Down Expand Up @@ -49,6 +67,11 @@ func (b *builder) MatchText(term string) Builder {
return b
}

func (b *builder) MatchExact() Builder {
b.exact = true
return b
}

func (b *builder) BuildGroupCondition() (c Condition) {
c = condition{
Not: b.not,
Expand All @@ -70,7 +93,8 @@ func (b *builder) BuildTextCondition() (c Condition) {
Condition: c,
Key: b.key,
},
Term: b.term,
Term: b.term,
Exact: b.exact,
}
return
}
21 changes: 14 additions & 7 deletions model/subscription/condition/text_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,36 @@ type (
TextCondition interface {
KeyCondition
GetTerm() string
IsExact() bool
}

textCondition struct {
KeyCondition KeyCondition
Term string
Exact bool
}
)

func NewTextCondition(kc KeyCondition, pattern string) TextCondition {
func NewTextCondition(kc KeyCondition, pattern string, exact bool) TextCondition {
return textCondition{
KeyCondition: kc,
Term: pattern,
Exact: exact,
}
}

func (kc textCondition) IsNot() bool {
return kc.KeyCondition.IsNot()
func (tc textCondition) IsNot() bool {
return tc.KeyCondition.IsNot()
}

func (kc textCondition) GetKey() string {
return kc.KeyCondition.GetKey()
func (tc textCondition) GetKey() string {
return tc.KeyCondition.GetKey()
}

func (kc textCondition) GetTerm() string {
return kc.Term
func (tc textCondition) GetTerm() string {
return tc.Term
}

func (tc textCondition) IsExact() bool {
return tc.Exact
}

0 comments on commit 5194038

Please sign in to comment.