Skip to content

Commit

Permalink
feat!: Rename SortedSetGetScore -> SortedSetGetScores (#227)
Browse files Browse the repository at this point in the history
Also fix the sorted set example.
  • Loading branch information
schwern authored and cprice404 committed Mar 14, 2023
1 parent d5edfeb commit 5debbbb
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 60 deletions.
9 changes: 5 additions & 4 deletions examples/sortedset-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/momentohq/client-sdk-go/auth"
"github.com/momentohq/client-sdk-go/config"
"github.com/momentohq/client-sdk-go/momento"
"github.com/momentohq/client-sdk-go/responses"
)

const (
Expand Down Expand Up @@ -74,7 +75,7 @@ func getClient() momento.CacheClient {
panic(err)
}
client, err := momento.NewCacheClient(
config.LatestLaptopConfig(),
config.LaptopLatest(),
credProvider,
60*time.Second,
)
Expand All @@ -93,14 +94,14 @@ func setupCache(client momento.CacheClient, ctx context.Context) {
}
}

func displayElements(setName string, resp momento.SortedSetFetchResponse) {
func displayElements(setName string, resp responses.SortedSetFetchResponse) {
switch r := resp.(type) {
case *momento.SortedSetFetchHit:
case *responses.SortedSetFetchHit:
for _, e := range r.Elements {
fmt.Printf("setName: %s, value: %s, score: %f\n", setName, e.Value, e.Score)
}
fmt.Println("")
case *momento.SortedSetFetchMiss:
case *responses.SortedSetFetchMiss:
fmt.Println("we regret to inform you there is no such set")
os.Exit(1)
}
Expand Down
4 changes: 2 additions & 2 deletions momento/simple_cache_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type CacheClient interface {

SortedSetFetch(ctx context.Context, r *SortedSetFetchRequest) (responses.SortedSetFetchResponse, error)
SortedSetPut(ctx context.Context, r *SortedSetPutRequest) (responses.SortedSetPutResponse, error)
SortedSetGetScore(ctx context.Context, r *SortedSetGetScoreRequest) (responses.SortedSetGetScoreResponse, error)
SortedSetGetScores(ctx context.Context, r *SortedSetGetScoresRequest) (responses.SortedSetGetScoresResponse, error)
SortedSetRemove(ctx context.Context, r *SortedSetRemoveRequest) (responses.SortedSetRemoveResponse, error)
SortedSetGetRank(ctx context.Context, r *SortedSetGetRankRequest) (responses.SortedSetGetRankResponse, error)
SortedSetIncrementScore(ctx context.Context, r *SortedSetIncrementScoreRequest) (responses.SortedSetIncrementScoreResponse, error)
Expand Down Expand Up @@ -194,7 +194,7 @@ func (c defaultScsClient) SortedSetPut(ctx context.Context, r *SortedSetPutReque
return r.response, nil
}

func (c defaultScsClient) SortedSetGetScore(ctx context.Context, r *SortedSetGetScoreRequest) (responses.SortedSetGetScoreResponse, error) {
func (c defaultScsClient) SortedSetGetScores(ctx context.Context, r *SortedSetGetScoresRequest) (responses.SortedSetGetScoresResponse, error) {
if err := c.dataClient.makeRequest(ctx, r); err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ import (
pb "github.com/momentohq/client-sdk-go/internal/protos"
)

type SortedSetGetScoreRequest struct {
type SortedSetGetScoresRequest struct {
CacheName string
SetName string
ElementValues []Value

grpcRequest *pb.XSortedSetGetScoreRequest
grpcResponse *pb.XSortedSetGetScoreResponse
response responses.SortedSetGetScoreResponse
response responses.SortedSetGetScoresResponse
}

func (r *SortedSetGetScoreRequest) cacheName() string { return r.CacheName }
func (r *SortedSetGetScoresRequest) cacheName() string { return r.CacheName }

func (r *SortedSetGetScoreRequest) requestName() string { return "Sorted set get score" }
func (r *SortedSetGetScoresRequest) requestName() string { return "Sorted set get score" }

func (r *SortedSetGetScoreRequest) initGrpcRequest(scsDataClient) error {
func (r *SortedSetGetScoresRequest) initGrpcRequest(scsDataClient) error {
var err error

if _, err = prepareName(r.SetName, "Set name"); err != nil {
Expand All @@ -44,7 +44,7 @@ func (r *SortedSetGetScoreRequest) initGrpcRequest(scsDataClient) error {
return nil
}

func (r *SortedSetGetScoreRequest) makeGrpcRequest(metadata context.Context, client scsDataClient) (grpcResponse, error) {
func (r *SortedSetGetScoresRequest) makeGrpcRequest(metadata context.Context, client scsDataClient) (grpcResponse, error) {
resp, err := client.grpcClient.SortedSetGetScore(metadata, r.grpcRequest)
if err != nil {
return nil, err
Expand All @@ -55,14 +55,14 @@ func (r *SortedSetGetScoreRequest) makeGrpcRequest(metadata context.Context, cli
return resp, nil
}

func (r *SortedSetGetScoreRequest) interpretGrpcResponse() error {
func (r *SortedSetGetScoresRequest) interpretGrpcResponse() error {
switch grpcResp := r.grpcResponse.SortedSet.(type) {
case *pb.XSortedSetGetScoreResponse_Found:
r.response = &responses.SortedSetGetScoreHit{
r.response = &responses.SortedSetGetScoresHit{
Elements: convertSortedSetScoreElement(grpcResp.Found.GetElements()),
}
case *pb.XSortedSetGetScoreResponse_Missing:
r.response = &responses.SortedSetGetScoreMiss{}
r.response = &responses.SortedSetGetScoresMiss{}
default:
return errUnexpectedGrpcResponse(r, r.grpcResponse)
}
Expand Down
24 changes: 12 additions & 12 deletions momento/sorted_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ var _ = Describe("SortedSet", func() {

elements := []Value{element}
Expect(
client.SortedSetGetScore(ctx, &SortedSetGetScoreRequest{
client.SortedSetGetScores(ctx, &SortedSetGetScoresRequest{
CacheName: cacheName, SetName: collectionName, ElementValues: elements,
}),
).Error().To(HaveMomentoErrorCode(expectedError))
Expand Down Expand Up @@ -341,18 +341,18 @@ var _ = Describe("SortedSet", func() {
})
})

Describe(`SortedSetGetScore`, func() {
Describe(`SortedSetGetScores`, func() {
It(`Misses when the element does not exist`, func() {
Expect(
sharedContext.Client.SortedSetGetScore(
sharedContext.Client.SortedSetGetScores(
sharedContext.Ctx,
&SortedSetGetScoreRequest{
&SortedSetGetScoresRequest{
CacheName: sharedContext.CacheName,
SetName: sharedContext.CollectionName,
ElementValues: []Value{String("foo")},
},
),
).To(BeAssignableToTypeOf(&SortedSetGetScoreMiss{}))
).To(BeAssignableToTypeOf(&SortedSetGetScoresMiss{}))
})

It(`Gets the score`, func() {
Expand All @@ -365,9 +365,9 @@ var _ = Describe("SortedSet", func() {
)

Expect(
sharedContext.Client.SortedSetGetScore(
sharedContext.Client.SortedSetGetScores(
sharedContext.Ctx,
&SortedSetGetScoreRequest{
&SortedSetGetScoresRequest{
CacheName: sharedContext.CacheName,
SetName: sharedContext.CollectionName,
ElementValues: []Value{
Expand All @@ -376,7 +376,7 @@ var _ = Describe("SortedSet", func() {
},
),
).To(Equal(
&SortedSetGetScoreHit{
&SortedSetGetScoresHit{
Elements: []SortedSetScoreElement{
SortedSetScore(9999),
SortedSetScore(-9999),
Expand All @@ -388,9 +388,9 @@ var _ = Describe("SortedSet", func() {

It(`returns an error when element values are nil`, func() {
Expect(
sharedContext.Client.SortedSetGetScore(
sharedContext.Client.SortedSetGetScores(
sharedContext.Ctx,
&SortedSetGetScoreRequest{
&SortedSetGetScoresRequest{
CacheName: sharedContext.CacheName,
SetName: sharedContext.CollectionName,
ElementValues: nil,
Expand All @@ -399,9 +399,9 @@ var _ = Describe("SortedSet", func() {
).Error().To(HaveMomentoErrorCode(InvalidArgumentError))

Expect(
sharedContext.Client.SortedSetGetScore(
sharedContext.Client.SortedSetGetScores(
sharedContext.Ctx,
&SortedSetGetScoreRequest{
&SortedSetGetScoresRequest{
CacheName: sharedContext.CacheName,
SetName: sharedContext.CollectionName,
ElementValues: []Value{nil, String("aValue"), nil},
Expand Down
33 changes: 0 additions & 33 deletions responses/sorted_set_get_score.go

This file was deleted.

33 changes: 33 additions & 0 deletions responses/sorted_set_get_scores.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package responses

type SortedSetScoreElement interface {
isSortedSetScoreElement()
}

type SortedSetGetScoresResponse interface {
isSortedSetGetScoresResponse()
}

// SortedSetGetScoresMiss Miss Response to a cache SortedSetScore api request.
type SortedSetGetScoresMiss struct{}

func (SortedSetGetScoresMiss) isSortedSetGetScoresResponse() {}

// SortedSetGetScoresHit Hit Response to a cache SortedSetScore api request.
type SortedSetGetScoresHit struct {
Elements []SortedSetScoreElement
}

func (SortedSetGetScoresHit) isSortedSetGetScoresResponse() {}

type SortedSetScore float64

func (SortedSetScore) isSortedSetScoreElement() {}

type SortedSetScoreMiss struct{}

func (SortedSetScoreMiss) isSortedSetScoreElement() {}

type SortedSetScoreInvalid struct{}

func (SortedSetScoreInvalid) isSortedSetScoreElement() {}

0 comments on commit 5debbbb

Please sign in to comment.