Skip to content

Commit

Permalink
feat: Change back to returning responses as pointers (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
schwern authored and cprice404 committed Mar 14, 2023
1 parent 226deee commit f5489e9
Show file tree
Hide file tree
Showing 25 changed files with 48 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.template.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Here's a quick example:

```go
switch r := resp.(type) {
case momento.GetHit:
case *momento.GetHit:
log.Printf("Lookup resulted in cahce HIT. value=%s\n", r.ValueString())
default:
// you can handle other cases via pattern matching in other `switch case`, or a default case
Expand Down
1 change: 1 addition & 0 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ require (
google.golang.org/grpc v1.52.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)

20 changes: 10 additions & 10 deletions examples/list-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func pushFrontToList(value string) {
}

switch r := resp.(type) {
case momento.ListPushFrontSuccess:
case *momento.ListPushFrontSuccess:
fmt.Printf("pushed with 5 sec TTL to front of list whose length is now %d\n", r.ListLength())
}
}
Expand All @@ -63,7 +63,7 @@ func pushBackToList(value string) {
}

switch r := resp.(type) {
case momento.ListPushBackSuccess:
case *momento.ListPushBackSuccess:
fmt.Printf("pushed with 5 sec TTL to back of list whose length is now %d\n", r.ListLength())
}
}
Expand All @@ -78,9 +78,9 @@ func printList() {
}

switch r := resp.(type) {
case momento.ListFetchHit:
case *momento.ListFetchHit:
fmt.Printf("\nlist fetch returned:\n\n\t%s\n", strings.Join(r.ValueListString(), "\n\t"))
case momento.ListFetchMiss:
case *momento.ListFetchMiss:
fmt.Println("\nlist fetch returned a MISS")
}
}
Expand All @@ -94,9 +94,9 @@ func printListLength() {
panic(err)
}
switch r := resp.(type) {
case momento.ListLengthMiss:
case *momento.ListLengthMiss:
fmt.Println("\nlist length returned a MISS")
case momento.ListLengthHit:
case *momento.ListLengthHit:
fmt.Printf("\ngot list length: %d", r.Length())
}
}
Expand All @@ -111,7 +111,7 @@ func concatFront(values []momento.Bytes) {
panic(err)
}
switch r := resp.(type) {
case momento.ListConcatenateFrontSuccess:
case *momento.ListConcatenateFrontSuccess:
fmt.Printf("\nconcatenated values to front. list is now length %d\n", r.ListLength())
}
}
Expand All @@ -126,7 +126,7 @@ func concatBack(values []momento.Bytes) {
panic(err)
}
switch r := resp.(type) {
case momento.ListConcatenateBackSuccess:
case *momento.ListConcatenateBackSuccess:
fmt.Printf("\nconcatenated values to back. list is now length %d\n", r.ListLength())
}
}
Expand Down Expand Up @@ -208,9 +208,9 @@ func main() {
panic(err)
}
switch r := resp.(type) {
case momento.ListPopFrontHit:
case *momento.ListPopFrontHit:
fmt.Printf("\npopped value '%s'\n", r.ValueString())
case momento.ListPopFrontMiss:
case *momento.ListPopFrontMiss:
fmt.Println("\npop from front returned MISS")
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func main() {
}

switch r := resp.(type) {
case momento.GetHit:
case *momento.GetHit:
log.Printf("Lookup resulted in cahce HIT. value=%s\n", r.ValueString())
case momento.GetMiss:
case *momento.GetMiss:
log.Printf("Look up did not find a value key=%s", key)
}

Expand Down
4 changes: 2 additions & 2 deletions examples/sortedset-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ func setupCache(client momento.ScsClient, ctx context.Context) {

func displayElements(setName string, resp momento.SortedSetFetchResponse) {
switch r := resp.(type) {
case momento.SortedSetFetchHit:
case *momento.SortedSetFetchHit:
for _, e := range r.Elements {
fmt.Printf("setName: %s, elementName: %s, score: %f\n", setName, e.Name, e.Score)
}
fmt.Println("")
case momento.SortedSetFetchMiss:
case *momento.SortedSetFetchMiss:
fmt.Println("we regret to inform you there is no such set")
os.Exit(1)
}
Expand Down
2 changes: 1 addition & 1 deletion momento/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ func (r *DeleteRequest) makeGrpcRequest(metadata context.Context, client scsData
}

func (r *DeleteRequest) interpretGrpcResponse() error {
r.response = DeleteSuccess{}
r.response = &DeleteSuccess{}
return nil
}
4 changes: 2 additions & 2 deletions momento/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ func (r *GetRequest) interpretGrpcResponse() error {
resp := r.grpcResponse

if resp.Result == pb.ECacheResult_Hit {
r.response = GetHit{value: resp.CacheBody}
r.response = &GetHit{value: resp.CacheBody}
return nil
} else if resp.Result == pb.ECacheResult_Miss {
r.response = GetMiss{}
r.response = &GetMiss{}
return nil
} else {
return errUnexpectedGrpcResponse
Expand Down
2 changes: 1 addition & 1 deletion momento/list_concatenate_back.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ func (r *ListConcatenateBackRequest) makeGrpcRequest(metadata context.Context, c

func (r *ListConcatenateBackRequest) interpretGrpcResponse() error {
resp := r.grpcResponse
r.response = ListConcatenateBackSuccess{listLength: resp.ListLength}
r.response = &ListConcatenateBackSuccess{listLength: resp.ListLength}
return nil
}
2 changes: 1 addition & 1 deletion momento/list_concatenate_front.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ func (r *ListConcatenateFrontRequest) makeGrpcRequest(metadata context.Context,

func (r *ListConcatenateFrontRequest) interpretGrpcResponse() error {
resp := r.grpcResponse
r.response = ListConcatenateFrontSuccess{listLength: resp.ListLength}
r.response = &ListConcatenateFrontSuccess{listLength: resp.ListLength}
return nil
}
4 changes: 2 additions & 2 deletions momento/list_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ func (r *ListFetchRequest) makeGrpcRequest(metadata context.Context, client scsD
func (r *ListFetchRequest) interpretGrpcResponse() error {
switch rtype := r.grpcResponse.List.(type) {
case *pb.XListFetchResponse_Found:
r.response = ListFetchHit{value: rtype.Found.Values}
r.response = &ListFetchHit{value: rtype.Found.Values}
case *pb.XListFetchResponse_Missing:
r.response = ListFetchMiss{}
r.response = &ListFetchMiss{}
default:
return errUnexpectedGrpcResponse
}
Expand Down
4 changes: 2 additions & 2 deletions momento/list_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ func (r *ListLengthRequest) interpretGrpcResponse() error {
resp := r.grpcResponse
switch rtype := resp.List.(type) {
case *pb.XListLengthResponse_Found:
r.response = ListLengthHit{value: rtype.Found.Length}
r.response = &ListLengthHit{value: rtype.Found.Length}
case *pb.XListLengthResponse_Missing:
r.response = ListLengthMiss{}
r.response = &ListLengthMiss{}
default:
return errUnexpectedGrpcResponse
}
Expand Down
4 changes: 2 additions & 2 deletions momento/list_pop_back.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ func (r *ListPopBackRequest) makeGrpcRequest(metadata context.Context, client sc
func (r *ListPopBackRequest) interpretGrpcResponse() error {
switch rtype := r.grpcResponse.List.(type) {
case *pb.XListPopBackResponse_Found:
r.response = ListPopBackHit{value: RawBytes{rtype.Found.Back}}
r.response = &ListPopBackHit{value: RawBytes{rtype.Found.Back}}
case *pb.XListPopBackResponse_Missing:
r.response = ListPopBackMiss{}
r.response = &ListPopBackMiss{}
default:
return errUnexpectedGrpcResponse
}
Expand Down
4 changes: 2 additions & 2 deletions momento/list_pop_front.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ func (r *ListPopFrontRequest) makeGrpcRequest(metadata context.Context, client s
func (r *ListPopFrontRequest) interpretGrpcResponse() error {
switch rtype := r.grpcResponse.List.(type) {
case *pb.XListPopFrontResponse_Found:
r.response = ListPopFrontHit{value: RawBytes{rtype.Found.Front}}
r.response = &ListPopFrontHit{value: RawBytes{rtype.Found.Front}}
case *pb.XListPopFrontResponse_Missing:
r.response = ListPopFrontMiss{}
r.response = &ListPopFrontMiss{}
default:
return errUnexpectedGrpcResponse
}
Expand Down
2 changes: 1 addition & 1 deletion momento/list_push_back.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ func (r *ListPushBackRequest) makeGrpcRequest(metadata context.Context, client s

func (r *ListPushBackRequest) interpretGrpcResponse() error {
resp := r.grpcResponse
r.response = ListPushBackSuccess{value: resp.ListLength}
r.response = &ListPushBackSuccess{value: resp.ListLength}
return nil
}
2 changes: 1 addition & 1 deletion momento/list_push_front.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,6 @@ func (r *ListPushFrontRequest) makeGrpcRequest(metadata context.Context, client

func (r *ListPushFrontRequest) interpretGrpcResponse() error {
resp := r.grpcResponse
r.response = ListPushFrontSuccess{value: resp.ListLength}
r.response = &ListPushFrontSuccess{value: resp.ListLength}
return nil
}
2 changes: 1 addition & 1 deletion momento/list_remove_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ func (r *ListRemoveValueRequest) makeGrpcRequest(metadata context.Context, clien
}

func (r *ListRemoveValueRequest) interpretGrpcResponse() error {
r.response = ListRemoveValueSuccess{}
r.response = &ListRemoveValueSuccess{}
return nil
}
2 changes: 1 addition & 1 deletion momento/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ func (r *SetRequest) makeGrpcRequest(metadata context.Context, client scsDataCli
}

func (r *SetRequest) interpretGrpcResponse() error {
r.response = SetSuccess{}
r.response = &SetSuccess{}
return nil
}
2 changes: 1 addition & 1 deletion momento/simple_cache_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (c ScsClient) ListRemoveValue(ctx context.Context, r *ListRemoveValueReques
return r.response, nil
}

func (c *ScsClient) Close() {
func (c ScsClient) Close() {
defer c.controlClient.Close()
defer c.dataClient.Close()
}
Expand Down
8 changes: 4 additions & 4 deletions momento/simple_cache_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestBasicHappyPathSDKFlow(t *testing.T) {
}

switch result := getResp.(type) {
case GetHit:
case *GetHit:
if !bytes.Equal(result.ValueByte(), value) {
t.Errorf(
"set byte value and returned byte value are not equal "+
Expand Down Expand Up @@ -143,7 +143,7 @@ func TestBasicHappyPathDelete(t *testing.T) {
}

switch result := getResp.(type) {
case GetHit:
case *GetHit:
if !bytes.Equal(result.ValueByte(), value) {
t.Errorf(
"set byte value and returned byte value are not equal "+
Expand Down Expand Up @@ -493,7 +493,7 @@ func TestSetGet(t *testing.T) {
t.Errorf("unexpected error occurred on getting cache err=%+v", err)
}
switch result := resp.(type) {
case GetHit:
case *GetHit:
if tt.value != result.ValueString() {
t.Errorf(
"set string value=%s is not the same as returned string value=%s",
Expand All @@ -515,7 +515,7 @@ func TestSetGet(t *testing.T) {
t.Errorf("unexpected error occurred on getting cache err=%+v", err)
}
switch result := resp.(type) {
case GetMiss:
case *GetMiss:
// We expect miss
default:
t.Errorf("expected miss but got responseType=%+v", result)
Expand Down
13 changes: 4 additions & 9 deletions momento/sorted_set_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,17 @@ func (r *SortedSetFetchRequest) makeGrpcRequest(metadata context.Context, client
func (r *SortedSetFetchRequest) interpretGrpcResponse() error {
grpcResp := r.grpcResponse

var resp SortedSetFetchResponse

// Convert from grpc struct to internal struct
switch r := grpcResp.SortedSet.(type) {
switch rsp := grpcResp.SortedSet.(type) {
case *pb.XSortedSetFetchResponse_Found:
resp = SortedSetFetchHit{
Elements: sortedSetGrpcElementToModel(r.Found.GetElements()),
r.response = &SortedSetFetchHit{
Elements: sortedSetGrpcElementToModel(rsp.Found.GetElements()),
}
case *pb.XSortedSetFetchResponse_Missing:
resp = SortedSetFetchMiss{}
r.response = &SortedSetFetchMiss{}
default:
return errUnexpectedGrpcResponse
}

r.response = resp

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion momento/sorted_set_get_rank.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (r *SortedSetGetRankRequest) interpretGrpcResponse() error {
Rank: rank.ElementRank.Rank,
}
case *pb.XSortedSetGetRankResponse_Missing:
resp = SortedSetGetRankMiss{}
resp = &SortedSetGetRankMiss{}
default:
return errUnexpectedGrpcResponse
}
Expand Down
2 changes: 1 addition & 1 deletion momento/sorted_set_increment.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ func (r *SortedSetIncrementRequest) makeGrpcRequest(metadata context.Context, cl
}

func (r *SortedSetIncrementRequest) interpretGrpcResponse() error {
r.response = SortedSetIncrementSuccess{}
r.response = &SortedSetIncrementSuccess{}
return nil
}
2 changes: 1 addition & 1 deletion momento/sorted_set_put.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (r *SortedSetPutRequest) makeGrpcRequest(metadata context.Context, client s
}

func (r *SortedSetPutRequest) interpretGrpcResponse() error {
r.response = SortedSetPutSuccess{}
r.response = &SortedSetPutSuccess{}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion momento/sorted_set_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@ func (r *SortedSetRemoveRequest) makeGrpcRequest(metadata context.Context, clien
}

func (r *SortedSetRemoveRequest) interpretGrpcResponse() error {
r.response = SortedSetRemoveSuccess{}
r.response = &SortedSetRemoveSuccess{}
return nil
}
4 changes: 2 additions & 2 deletions momento/topic_publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ type TopicPublishRequest struct {
}

type TopicPublishResponse interface {
isTopicPublichResponse()
isTopicPublishResponse()
}

type TopicPublishSuccess struct{}

func (TopicPublishSuccess) isTopicPublichResponse() {}
func (TopicPublishSuccess) isTopicPublishResponse() {}

type TopicValue interface {
isTopicValue()
Expand Down

0 comments on commit f5489e9

Please sign in to comment.