Skip to content

Commit

Permalink
Merge pull request #6 from awakari/num-conds
Browse files Browse the repository at this point in the history
feat: support number conditions
  • Loading branch information
akurilov committed Aug 13, 2023
2 parents ac46890 + bbf275e commit d933374
Show file tree
Hide file tree
Showing 19 changed files with 614 additions and 189 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ func main() {
Description: "my subscription",
Enabled: true,
Condition: condition.NewBuilder().
MatchAttrKey("tags").
MatchText("SpaceX").
AttributeKey("tags").
AnyOfWords("SpaceX").
BuildTextCondition(),
}
subId, err = client.CreateSubscription(ctx, userId, subData)
Expand Down
10 changes: 9 additions & 1 deletion api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,15 @@ func TestClient_ReadSubscription(t *testing.T) {
Enabled: true,
Condition: condition.
NewBuilder().
BuildTextCondition(),
Any([]condition.Condition{
condition.
NewBuilder().BuildTextCondition(),
condition.
NewBuilder().
LessThanOrEqual(42).
BuildNumberCondition(),
}).
BuildGroupCondition(),
},
},
"subs API not set": {
Expand Down
2 changes: 1 addition & 1 deletion api/grpc/limits/service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/grpc/permits/service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/grpc/reader/service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/grpc/resolver/service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/grpc/subject/subject.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions api/grpc/subscriptions/client_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ func (cm clientMock) Read(ctx context.Context, req *ReadRequest, opts ...grpc.Ca
},
},
{
Cond: &Condition_Tc{
Tc: &TextCondition{
Key: "k1",
Term: "p1",
Exact: true,
Cond: &Condition_Nc{
Nc: &NumberCondition{
Key: "k1",
Op: Operation_Gt,
Val: -42.1,
},
},
},
Expand Down
53 changes: 52 additions & 1 deletion api/grpc/subscriptions/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,39 @@ func encodeCondition(src condition.Condition) (dst *Condition) {
Exact: c.IsExact(),
},
}
case condition.NumberCondition:
dstOp := encodeNumOp(c.GetOperation())
dst.Cond = &Condition_Nc{
Nc: &NumberCondition{
Key: c.GetKey(),
Op: dstOp,
Val: c.GetValue(),
},
}
}
return
}

func encodeNumOp(src condition.NumOp) (dst Operation) {
switch src {
case condition.NumOpGt:
dst = Operation_Gt
case condition.NumOpGte:
dst = Operation_Gte
case condition.NumOpEq:
dst = Operation_Eq
case condition.NumOpLte:
dst = Operation_Lte
case condition.NumOpLt:
dst = Operation_Lt
default:
dst = Operation_Undefined
}
return
}

func decodeCondition(src *Condition) (dst condition.Condition, err error) {
gc, tc := src.GetGc(), src.GetTc()
gc, nc, tc := src.GetGc(), src.GetNc(), src.GetTc()
switch {
case gc != nil:
var group []condition.Condition
Expand All @@ -171,6 +198,12 @@ func decodeCondition(src *Condition) (dst condition.Condition, err error) {
group,
)
}
case nc != nil:
dstOp := decodeNumOp(nc.Op)
dst = condition.NewNumberCondition(
condition.NewKeyCondition(condition.NewCondition(src.Not), nc.GetKey()),
dstOp, nc.Val,
)
case tc != nil:
dst = condition.NewTextCondition(
condition.NewKeyCondition(condition.NewCondition(src.Not), tc.GetKey()),
Expand All @@ -182,6 +215,24 @@ func decodeCondition(src *Condition) (dst condition.Condition, err error) {
return
}

func decodeNumOp(src Operation) (dst condition.NumOp) {
switch src {
case Operation_Gt:
dst = condition.NumOpGt
case Operation_Gte:
dst = condition.NumOpGte
case Operation_Eq:
dst = condition.NumOpEq
case Operation_Lte:
dst = condition.NumOpLte
case Operation_Lt:
dst = condition.NumOpLt
default:
dst = condition.NumOpUndefined
}
return
}

func decodeError(src error) (dst error) {
switch {
case src == nil:
Expand Down
Loading

0 comments on commit d933374

Please sign in to comment.