Skip to content

Commit

Permalink
Merge pull request #2 from awakari/switch-to-text-conditions
Browse files Browse the repository at this point in the history
switch to text conditions
  • Loading branch information
akurilov authored Jun 30, 2023
2 parents 3189206 + b3d30b1 commit e43b9b7
Show file tree
Hide file tree
Showing 16 changed files with 567 additions and 639 deletions.
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,24 +276,21 @@ func main() {
var subId string
var err error
subData := subscription.Data{
Metadata: subscription.Metadata{
Description: "my subscription",
Enabled: true,
},
Description: "my subscription",
Enabled: true,
Condition: condition.NewBuilder().
MatchAttrKey("tags").
MatchAttrValuePattern("SpaceX").
MatchAttrValuePartial().
BuildKiwiTreeCondition(),
MatchText("SpaceX").
BuildTextCondition(),
}
subId, err = client.CreateSubscription(ctx, userId, subData)

// Update the subscription mutable fields
md := subscription.Metadata{
upd := subscription.Data{
Description: "my disabled subscription",
Enabled: false,
}
err = client.UpdateSubscriptionMetadata(ctx, userId, subId, md)
err = client.UpdateSubscriptionMetadata(ctx, userId, subId, upd)

// Delete the subscription
err = client.DeleteSubscription(ctx, userId, subId)
Expand Down
10 changes: 5 additions & 5 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ type Client interface {
// ReadSubscription specified by the id. Returns ErrNotFound if subscription is missing.
ReadSubscription(ctx context.Context, userId, subId string) (subData subscription.Data, err error)

// UpdateSubscriptionMetadata updates the mutable part of the subscription.Data
UpdateSubscriptionMetadata(ctx context.Context, userId, subId string, md subscription.Metadata) (err error)
// UpdateSubscription replaces the existing subscription.Data fields.
UpdateSubscription(ctx context.Context, userId, subId string, subData subscription.Data) (err error)

// DeleteSubscription and all associated conditions those not in use by any other subscription.
// Returns ErrNotFound if a subscription with the specified id is missing.
Expand Down Expand Up @@ -143,11 +143,11 @@ func (c client) ReadSubscription(ctx context.Context, userId, subId string) (sub
return
}

func (c client) UpdateSubscriptionMetadata(ctx context.Context, userId, subId string, md subscription.Metadata) (err error) {
func (c client) UpdateSubscription(ctx context.Context, userId, subId string, subData subscription.Data) (err error) {
if c.svcSubs == nil {
err = fmt.Errorf("%w: UpdateSubscriptionMetadata(...)", ErrApiDisabled)
err = fmt.Errorf("%w: UpdateSubscription(...)", ErrApiDisabled)
} else {
err = c.svcSubs.UpdateMetadata(ctx, userId, subId, md)
err = c.svcSubs.Update(ctx, userId, subId, subData)
}
return
}
Expand Down
14 changes: 5 additions & 9 deletions api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,7 @@ func TestClient_CreateSubscription(t *testing.T) {
}
ctx := context.TODO()
subData := subscription.Data{
Metadata: subscription.Metadata{
Description: c.descr,
},
Description: c.descr,
}
id, err := cl.CreateSubscription(ctx, "user0", subData)
assert.Equal(t, c.id, id)
Expand All @@ -403,13 +401,11 @@ func TestClient_ReadSubscription(t *testing.T) {
svcSubs: subscriptions.NewServiceMock(),
subId: "sub0",
subData: subscription.Data{
Metadata: subscription.Metadata{
Description: "my subscription",
Enabled: true,
},
Description: "my subscription",
Enabled: true,
Condition: condition.
NewBuilder().
BuildKiwiTreeCondition(),
BuildTextCondition(),
},
},
"subs API not set": {
Expand Down Expand Up @@ -482,7 +478,7 @@ func TestClient_UpdateSubscriptionMetadata(t *testing.T) {
svcSubs: c.svcSubs,
}
ctx := context.TODO()
err := cl.UpdateSubscriptionMetadata(ctx, "user0", c.subId, subscription.Metadata{})
err := cl.UpdateSubscription(ctx, "user0", c.subId, subscription.Data{})
assert.ErrorIs(t, err, c.err)
assert.Nil(t, cl.Close())
})
Expand Down
35 changes: 15 additions & 20 deletions api/grpc/subscriptions/client_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
)

type clientMock struct{}
Expand All @@ -16,7 +15,7 @@ func newClientMock() ServiceClient {

func (cm clientMock) Create(ctx context.Context, req *CreateRequest, opts ...grpc.CallOption) (resp *CreateResponse, err error) {
resp = &CreateResponse{}
switch req.Md.Description {
switch req.Description {
case "fail":
err = status.Error(codes.Internal, "internal failure")
case "fail_auth":
Expand All @@ -43,31 +42,27 @@ func (cm clientMock) Read(ctx context.Context, req *ReadRequest, opts ...grpc.Ca
case "missing":
err = status.Error(codes.NotFound, "subscription not found")
default:
resp.Md = &Metadata{
Description: "subscription",
Enabled: true,
}
resp.Description = "subscription"
resp.Enabled = true
resp.Cond = &ConditionOutput{
Cond: &ConditionOutput_Gc{
Gc: &GroupConditionOutput{
Logic: GroupLogic_Or,
Group: []*ConditionOutput{
{
Not: true,
Cond: &ConditionOutput_Kc{
Kc: &KiwiConditionOutput{
Key: "k0",
Pattern: "p0",
Partial: false,
Cond: &ConditionOutput_Tc{
Tc: &TextConditionOutput{
Key: "k0",
Term: "p0",
},
},
},
{
Cond: &ConditionOutput_Kc{
Kc: &KiwiConditionOutput{
Key: "k1",
Pattern: "p1",
Partial: true,
Cond: &ConditionOutput_Tc{
Tc: &TextConditionOutput{
Key: "k1",
Term: "p1",
},
},
},
Expand All @@ -79,8 +74,8 @@ func (cm clientMock) Read(ctx context.Context, req *ReadRequest, opts ...grpc.Ca
return
}

func (cm clientMock) UpdateMetadata(ctx context.Context, req *UpdateMetadataRequest, opts ...grpc.CallOption) (resp *emptypb.Empty, err error) {
resp = &emptypb.Empty{}
func (cm clientMock) Update(ctx context.Context, req *UpdateRequest, opts ...grpc.CallOption) (resp *UpdateResponse, err error) {
resp = &UpdateResponse{}
switch req.Id {
case "fail":
err = status.Error(codes.Internal, "internal failure")
Expand All @@ -92,8 +87,8 @@ func (cm clientMock) UpdateMetadata(ctx context.Context, req *UpdateMetadataRequ
return
}

func (cm clientMock) Delete(ctx context.Context, req *DeleteRequest, opts ...grpc.CallOption) (resp *emptypb.Empty, err error) {
resp = &emptypb.Empty{}
func (cm clientMock) Delete(ctx context.Context, req *DeleteRequest, opts ...grpc.CallOption) (resp *DeleteResponse, err error) {
resp = &DeleteResponse{}
switch req.Id {
case "fail":
err = status.Error(codes.Internal, "internal failure")
Expand Down
58 changes: 23 additions & 35 deletions api/grpc/subscriptions/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ type Service interface {
// Read returns the subscription specified by the id. Returns ErrNotFound if subscription is missing.
Read(ctx context.Context, userId, subId string) (subData subscription.Data, err error)

// UpdateMetadata updates the mutable part of the subscription.Data
UpdateMetadata(ctx context.Context, userId, subId string, md subscription.Metadata) (err error)
// Update the mutable part of the subscription.Data
Update(ctx context.Context, userId, subId string, subData subscription.Data) (err error)

// Delete the specified subscription all associated conditions those not in use by any other subscription.
// Returns ErrNotFound if a subscription with the specified id is missing.
Expand Down Expand Up @@ -56,8 +56,9 @@ func NewService(client ServiceClient) Service {
func (svc service) Create(ctx context.Context, userId string, subData subscription.Data) (id string, err error) {
ctx = auth.SetOutgoingAuthInfo(ctx, userId)
req := CreateRequest{
Md: encodeMetadata(subData.Metadata),
Cond: encodeCondition(subData.Condition),
Cond: encodeCondition(subData.Condition),
Description: subData.Description,
Enabled: subData.Enabled,
}
var resp *CreateResponse
resp, err = svc.client.Create(ctx, &req)
Expand All @@ -77,22 +78,21 @@ func (svc service) Read(ctx context.Context, userId, subId string) (subData subs
resp, err = svc.client.Read(ctx, &req)
err = decodeError(err)
if err == nil {
subData.Metadata = subscription.Metadata{
Description: resp.Md.Description,
Enabled: resp.Md.Enabled,
}
subData.Condition, err = decodeCondition(resp.Cond)
subData.Description = resp.Description
subData.Enabled = resp.Enabled
}
return
}

func (svc service) UpdateMetadata(ctx context.Context, userId, subId string, md subscription.Metadata) (err error) {
func (svc service) Update(ctx context.Context, userId, subId string, data subscription.Data) (err error) {
ctx = auth.SetOutgoingAuthInfo(ctx, userId)
req := UpdateMetadataRequest{
Id: subId,
Md: encodeMetadata(md),
req := UpdateRequest{
Id: subId,
Description: data.Description,
Enabled: data.Enabled,
}
_, err = svc.client.UpdateMetadata(ctx, &req)
_, err = svc.client.Update(ctx, &req)
err = decodeError(err)
return
}
Expand Down Expand Up @@ -122,14 +122,6 @@ func (svc service) SearchOwn(ctx context.Context, userId string, limit uint32, c
return
}

func encodeMetadata(src subscription.Metadata) (dst *Metadata) {
dst = &Metadata{
Description: src.Description,
Enabled: src.Enabled,
}
return
}

func encodeCondition(src condition.Condition) (dst *ConditionInput) {
dst = &ConditionInput{
Not: src.IsNot(),
Expand All @@ -147,20 +139,19 @@ func encodeCondition(src condition.Condition) (dst *ConditionInput) {
Group: dstGroup,
},
}
case condition.KiwiTreeCondition:
dst.Cond = &ConditionInput_Ktc{
Ktc: &KiwiTreeConditionInput{
Key: c.GetKey(),
Pattern: c.GetPattern(),
Partial: c.IsPartial(),
case condition.TextCondition:
dst.Cond = &ConditionInput_Tc{
Tc: &TextConditionInput{
Key: c.GetKey(),
Term: c.GetTerm(),
},
}
}
return
}

func decodeCondition(src *ConditionOutput) (dst condition.Condition, err error) {
gc, ktc := src.GetGc(), src.GetKc()
gc, tc := src.GetGc(), src.GetTc()
switch {
case gc != nil:
var group []condition.Condition
Expand All @@ -179,13 +170,10 @@ func decodeCondition(src *ConditionOutput) (dst condition.Condition, err error)
group,
)
}
case ktc != nil:
dst = condition.NewKiwiTreeCondition(
condition.NewKiwiCondition(
condition.NewKeyCondition(condition.NewCondition(src.Not), ktc.GetKey()),
ktc.GetPartial(),
ktc.GetPattern(),
),
case tc != nil:
dst = condition.NewTextCondition(
condition.NewKeyCondition(condition.NewCondition(src.Not), tc.GetKey()),
tc.GetTerm(),
)
default:
err = fmt.Errorf("%w: unsupported condition type", ErrInternal)
Expand Down
Loading

0 comments on commit e43b9b7

Please sign in to comment.