Skip to content

Commit

Permalink
chore: consolidate SortedSetPutElement to SortedSetElement, add util (#…
Browse files Browse the repository at this point in the history
…279)

* chore: consolidate SortedSetPutElement to SortedSetElement, add util

This commit renames SortedSetPutElement to SortedSetElement, for
consistency with the Dictionary API.  Also introduces a helper function
for creating an array of SortedSetPutElements from a map, like we
support for Dictionaries.
  • Loading branch information
cprice404 authored Mar 13, 2023
1 parent 17d1e20 commit 2ed735f
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 103 deletions.
4 changes: 2 additions & 2 deletions momento/cache_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type CacheClient interface {

// SortedSetFetch fetches the elements in the given sorted set by index rank or by score.
SortedSetFetch(ctx context.Context, r *SortedSetFetchRequest) (responses.SortedSetFetchResponse, error)
// SortedSetPutElement adds an element to the given sorted set. If the element already exists,
// SortedSetElement adds an element to the given sorted set. If the element already exists,
// its score is updated. Creates the sorted set if it does not exist.
SortedSetPutElement(ctx context.Context, r *SortedSetPutElementRequest) (responses.SortedSetPutElementResponse, error)
// SortedSetPutElements adds elements to the given sorted set. If an element already exists,
Expand Down Expand Up @@ -289,7 +289,7 @@ func (c defaultScsClient) SortedSetPutElement(ctx context.Context, r *SortedSetP
newRequest := &SortedSetPutElementsRequest{
CacheName: r.CacheName,
SetName: r.SetName,
Elements: []SortedSetPutElement{{Value: r.Value, Score: r.Score}},
Elements: []SortedSetElement{{Value: r.Value, Score: r.Score}},
Ttl: r.Ttl,
}
if err := c.dataClient.makeRequest(ctx, newRequest); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions momento/momento_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ func HaveListLength(length int) types.GomegaMatcher {
)
}

func HaveSortedSetElements(expected []responses.SortedSetElement) types.GomegaMatcher {
func HaveSortedSetElements(expected []responses.SortedSetBytesElement) types.GomegaMatcher {
return WithTransform(
func(fetchResp responses.SortedSetFetchResponse) ([]responses.SortedSetElement, error) {
func(fetchResp responses.SortedSetFetchResponse) ([]responses.SortedSetBytesElement, error) {
switch rtype := fetchResp.(type) {
case *responses.SortedSetFetchHit:
return rtype.ValueByteElements(), nil
return rtype.ValueBytesElements(), nil
default:
return nil, fmt.Errorf("expected SortedSetFetchHit, but got %T", fetchResp)
}
Expand Down
49 changes: 0 additions & 49 deletions momento/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,52 +239,3 @@ func validateNotNil(value Value, label string) error {

return nil
}

// DictionaryElementsFromMap converts a map[string]string to an array of momento DictionaryElements.
//
// DictionaryElements are used as input to DictionarySetFields.
func DictionaryElementsFromMap(theMap map[string]string) []DictionaryElement {
return DictionaryElementsFromMapStringString(theMap)
}

// DictionaryElementsFromMapStringString converts a map[string]string to an array of momento DictionaryElements.
//
// DictionaryElements are used as input to DictionarySetFields.
func DictionaryElementsFromMapStringString(theMap map[string]string) []DictionaryElement {
var elements []DictionaryElement
for k, v := range theMap {
elements = append(elements, DictionaryElement{
Field: String(k),
Value: String(v),
})
}
return elements
}

// DictionaryElementsFromMapStringBytes converts a map[string][]byte to an array of momento DictionaryElements.
//
// DictionaryElements are used as input to DictionarySetFields.
func DictionaryElementsFromMapStringBytes(theMap map[string][]byte) []DictionaryElement {
var elements []DictionaryElement
for k, v := range theMap {
elements = append(elements, DictionaryElement{
Field: String(k),
Value: Bytes(v),
})
}
return elements
}

// DictionaryElementsFromMapStringValue converts a map[string]momento.Value to an array of momento DictionaryElements.
//
// DictionaryElements are used as input to DictionarySetFields.
func DictionaryElementsFromMapStringValue(theMap map[string]Value) []DictionaryElement {
var elements []DictionaryElement
for k, v := range theMap {
elements = append(elements, DictionaryElement{
Field: String(k),
Value: v,
})
}
return elements
}
6 changes: 3 additions & 3 deletions momento/sorted_set_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ func (r *SortedSetFetchRequest) interpretGrpcResponse() error {
return nil
}

func sortedSetGrpcElementToModel(grpcSetElements []*pb.XSortedSetElement) []responses.SortedSetElement {
var returnList []responses.SortedSetElement
func sortedSetGrpcElementToModel(grpcSetElements []*pb.XSortedSetElement) []responses.SortedSetBytesElement {
var returnList []responses.SortedSetBytesElement
for _, element := range grpcSetElements {
returnList = append(returnList, responses.SortedSetElement{
returnList = append(returnList, responses.SortedSetBytesElement{
Value: element.Value,
Score: element.Score,
})
Expand Down
9 changes: 2 additions & 7 deletions momento/sorted_set_put_elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@ import (
"github.com/momentohq/client-sdk-go/utils"
)

type SortedSetPutElement struct {
Value Value
Score float64
}

type SortedSetPutElementsRequest struct {
CacheName string
SetName string
Elements []SortedSetPutElement
Elements []SortedSetElement
Ttl *utils.CollectionTtl

grpcRequest *pb.XSortedSetPutRequest
Expand Down Expand Up @@ -72,7 +67,7 @@ func (r *SortedSetPutElementsRequest) interpretGrpcResponse() error {
return nil
}

func convertSortedSetElementsToGrpc(modelSetElements []SortedSetPutElement) []*pb.XSortedSetElement {
func convertSortedSetElementsToGrpc(modelSetElements []SortedSetElement) []*pb.XSortedSetElement {
var returnList []*pb.XSortedSetElement
for _, el := range modelSetElements {
returnList = append(returnList, &pb.XSortedSetElement{
Expand Down
Loading

0 comments on commit 2ed735f

Please sign in to comment.