Skip to content

Commit

Permalink
feat!: Simplify passing values. (#135)
Browse files Browse the repository at this point in the history
* feat!: Do not expose AsBytes()
* refactor: Make our String or Bytes interface more intuitive to users.

Name it from the POV of the user passing things in, not from what is
received.

Bytes -> StringOrBytes
RawBytes -> Bytes
StringBytes -> String

* feat!: String("string") and Bytes([]bytes("abc"))
* Add a type alias for keys.
  • Loading branch information
schwern authored and cprice404 committed Mar 14, 2023
1 parent 337c90f commit 4771030
Show file tree
Hide file tree
Showing 23 changed files with 105 additions and 112 deletions.
2 changes: 1 addition & 1 deletion README.template.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ In cases where you get an error response, it can be treated as `momentoErr` usin
```go
_, err := client.Get(ctx, &momento.GetRequest{
CacheName: cacheName,
Key: &momento.StringBytes{Text: key},
Key: momento.String(key),
})

if err != nil {
Expand Down
23 changes: 12 additions & 11 deletions examples/list-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func pushFrontToList(value string) {
resp, err := client.ListPushFront(ctx, &momento.ListPushFrontRequest{
CacheName: cacheName,
ListName: listName,
Value: &momento.StringBytes{Text: value},
Value: momento.String(value),
TruncateBackToSize: 0,
CollectionTTL: utils.CollectionTTL{
Ttl: 5 * time.Second,
Expand All @@ -51,7 +51,7 @@ func pushBackToList(value string) {
resp, err := client.ListPushBack(ctx, &momento.ListPushBackRequest{
CacheName: cacheName,
ListName: listName,
Value: &momento.StringBytes{Text: value},
Value: momento.String(value),
TruncateFrontToSize: 0,
CollectionTTL: utils.CollectionTTL{
Ttl: 5 * time.Second,
Expand Down Expand Up @@ -101,7 +101,7 @@ func printListLength() {
}
}

func concatFront(values []momento.Bytes) {
func concatFront(values []momento.Value) {
resp, err := client.ListConcatenateFront(ctx, &momento.ListConcatenateFrontRequest{
CacheName: cacheName,
ListName: listName,
Expand All @@ -116,7 +116,7 @@ func concatFront(values []momento.Bytes) {
}
}

func concatBack(values []momento.Bytes) {
func concatBack(values []momento.Value) {
resp, err := client.ListConcatenateBack(ctx, &momento.ListConcatenateBackRequest{
CacheName: cacheName,
ListName: listName,
Expand All @@ -131,7 +131,7 @@ func concatBack(values []momento.Bytes) {
}
}

func removeValue(value momento.Bytes) {
func removeValue(value momento.Value) {
_, err := client.ListRemoveValue(ctx, &momento.ListRemoveValueRequest{
CacheName: cacheName,
ListName: listName,
Expand All @@ -140,7 +140,6 @@ func removeValue(value momento.Bytes) {
if err != nil {
panic(err)
}
fmt.Printf("\nremoved '%s' from list\n", string(value.AsBytes()))
}

func main() {
Expand Down Expand Up @@ -218,23 +217,23 @@ func main() {

pushFrontToList("list seed")

var values []momento.Bytes
var values []momento.Value
for i := 0; i < 5; i++ {
values = append(values, momento.StringBytes{Text: fmt.Sprintf("concat front %d", i)})
values = append(values, momento.String(fmt.Sprintf("concat front %d", i)))
}
concatFront(values)
printList()

values = nil
for i := 0; i < 5; i++ {
values = append(values, momento.StringBytes{Text: fmt.Sprintf("concat back %d", i)})
values = append(values, momento.String(fmt.Sprintf("concat back %d", i)))
}
concatBack(values)
printList()

_, err = client.Delete(ctx, &momento.DeleteRequest{
CacheName: cacheName,
Key: momento.StringBytes{Text: listName},
Key: momento.String(listName),
})
if err != nil {
panic(err)
Expand All @@ -248,7 +247,9 @@ func main() {
}
}
printList()
removeValue(momento.StringBytes{Text: "even"})
value := "even"
removeValue(momento.String(value))
fmt.Printf("\nremoved '%s' from list\n", value)
printList()

// Delete the cache
Expand Down
6 changes: 3 additions & 3 deletions examples/scalar-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func main() {
log.Printf("Setting key: %s, value: %s\n", key, value)
_, err = client.Set(ctx, &momento.SetRequest{
CacheName: cacheName,
Key: &momento.StringBytes{Text: key},
Value: &momento.StringBytes{Text: value},
Key: momento.String(key),
Value: momento.String(value),
})
if err != nil {
panic(err)
Expand All @@ -64,7 +64,7 @@ func main() {
log.Printf("Getting key: %s\n", key)
resp, err := client.Get(ctx, &momento.GetRequest{
CacheName: cacheName,
Key: &momento.StringBytes{Text: key},
Key: momento.String(key),
})
if err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion examples/sortedset-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {
CacheName: cacheName,
SetName: setName,
Elements: []*momento.SortedSetScoreRequestElement{{
Value: momento.StringBytes{Text: fmt.Sprintf("element-%d", i)},
Value: momento.String(fmt.Sprintf("element-%d", i)),
Score: float64(i),
}},
})
Expand Down
4 changes: 2 additions & 2 deletions momento/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type DeleteRequest struct {
// Name of the cache to get the item from to be deleted
CacheName string
// string or byte key to be used to delete the item.
Key Bytes
Key Key

grpcRequest *pb.XDeleteRequest
grpcResponse *pb.XDeleteResponse
Expand All @@ -31,7 +31,7 @@ type DeleteRequest struct {

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

func (r *DeleteRequest) key() Bytes { return r.Key }
func (r *DeleteRequest) key() Key { return r.Key }

func (r *DeleteRequest) requestName() string { return "Delete" }

Expand Down
4 changes: 2 additions & 2 deletions momento/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type GetRequest struct {
// Name of the cache to get the item from
CacheName string
// string or byte key to be used to store item
Key Bytes
Key Key

grpcRequest *pb.XGetRequest
grpcResponse *pb.XGetResponse
Expand All @@ -49,7 +49,7 @@ type GetRequest struct {

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

func (r *GetRequest) key() Bytes { return r.Key }
func (r *GetRequest) key() Key { return r.Key }

func (r *GetRequest) requestName() string { return "Get" }

Expand Down
4 changes: 2 additions & 2 deletions momento/list_concatenate_back.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (resp ListConcatenateBackSuccess) ListLength() uint32 {
type ListConcatenateBackRequest struct {
CacheName string
ListName string
Values []Bytes
Values []Value
TruncateFrontToSize uint32
CollectionTTL utils.CollectionTTL

Expand All @@ -40,7 +40,7 @@ type ListConcatenateBackRequest struct {

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

func (r *ListConcatenateBackRequest) values() []Bytes { return r.Values }
func (r *ListConcatenateBackRequest) values() []Value { return r.Values }

func (r *ListConcatenateBackRequest) ttl() time.Duration { return r.CollectionTTL.Ttl }

Expand Down
4 changes: 2 additions & 2 deletions momento/list_concatenate_front.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (resp ListConcatenateFrontSuccess) ListLength() uint32 {
type ListConcatenateFrontRequest struct {
CacheName string
ListName string
Values []Bytes
Values []Value
TruncateBackToSize uint32
CollectionTTL utils.CollectionTTL

Expand All @@ -40,7 +40,7 @@ type ListConcatenateFrontRequest struct {

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

func (r *ListConcatenateFrontRequest) values() []Bytes { return r.Values }
func (r *ListConcatenateFrontRequest) values() []Value { return r.Values }

func (r *ListConcatenateFrontRequest) ttl() time.Duration { return r.CollectionTTL.Ttl }

Expand Down
8 changes: 4 additions & 4 deletions momento/list_pop_back.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ type ListPopBackResponse interface {
}

type ListPopBackHit struct {
value Bytes
value Value
}

func (ListPopBackHit) isListPopBackResponse() {}

func (resp ListPopBackHit) ValueByte() []byte {
return resp.value.AsBytes()
return resp.value.asBytes()
}

func (resp ListPopBackHit) ValueString() string {
return string(resp.value.AsBytes())
return string(resp.value.asBytes())
}

type ListPopBackMiss struct{}
Expand Down Expand Up @@ -67,7 +67,7 @@ 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: Bytes(rtype.Found.Back)}
case *pb.XListPopBackResponse_Missing:
r.response = &ListPopBackMiss{}
default:
Expand Down
8 changes: 4 additions & 4 deletions momento/list_pop_front.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ type ListPopFrontResponse interface {
}

type ListPopFrontHit struct {
value Bytes
value Value
}

func (ListPopFrontHit) isListPopFrontResponse() {}

func (resp ListPopFrontHit) ValueByte() []byte {
return resp.value.AsBytes()
return resp.value.asBytes()
}

func (resp ListPopFrontHit) ValueString() string {
return string(resp.value.AsBytes())
return string(resp.value.asBytes())
}

type ListPopFrontMiss struct{}
Expand Down Expand Up @@ -67,7 +67,7 @@ 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: Bytes(rtype.Found.Front)}
case *pb.XListPopFrontResponse_Missing:
r.response = &ListPopFrontMiss{}
default:
Expand Down
6 changes: 3 additions & 3 deletions momento/list_push_back.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (resp ListPushBackSuccess) ListLength() uint32 {
type ListPushBackRequest struct {
CacheName string
ListName string
Value Bytes
Value Value
TruncateFrontToSize uint32
CollectionTTL utils.CollectionTTL

Expand All @@ -41,7 +41,7 @@ type ListPushBackRequest struct {

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

func (r *ListPushBackRequest) value() Bytes { return r.Value }
func (r *ListPushBackRequest) value() Value { return r.Value }

func (r *ListPushBackRequest) ttl() time.Duration { return r.CollectionTTL.Ttl }

Expand All @@ -61,7 +61,7 @@ func (r *ListPushBackRequest) initGrpcRequest(client scsDataClient) error {

r.grpcRequest = &pb.XListPushBackRequest{
ListName: []byte(r.ListName),
Value: r.Value.AsBytes(),
Value: r.Value.asBytes(),
TtlMilliseconds: ttl,
RefreshTtl: r.CollectionTTL.RefreshTtl,
TruncateFrontToSize: r.TruncateFrontToSize,
Expand Down
4 changes: 2 additions & 2 deletions momento/list_push_front.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (resp ListPushFrontSuccess) ListLength() uint32 {
type ListPushFrontRequest struct {
CacheName string
ListName string
Value Bytes
Value Value
TruncateBackToSize uint32
CollectionTTL utils.CollectionTTL

Expand All @@ -41,7 +41,7 @@ type ListPushFrontRequest struct {

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

func (r *ListPushFrontRequest) value() Bytes { return r.Value }
func (r *ListPushFrontRequest) value() Value { return r.Value }

func (r *ListPushFrontRequest) ttl() time.Duration { return r.CollectionTTL.Ttl }

Expand Down
4 changes: 2 additions & 2 deletions momento/list_remove_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (ListRemoveValueSuccess) isListRemoveValueResponse() {}
type ListRemoveValueRequest struct {
CacheName string
ListName string
Value Bytes
Value Value

grpcRequest *pb.XListRemoveRequest
grpcResponse *pb.XListRemoveResponse
Expand All @@ -30,7 +30,7 @@ type ListRemoveValueRequest struct {

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

func (r *ListRemoveValueRequest) value() Bytes { return r.Value }
func (r *ListRemoveValueRequest) value() Value { return r.Value }

func (r *ListRemoveValueRequest) requestName() string { return "ListRemoveValue" }

Expand Down
14 changes: 7 additions & 7 deletions momento/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ type hasCacheName interface {
}

type hasKey interface {
key() Bytes
key() Key
}

type hasValue interface {
value() Bytes
value() Value
}

type hasValues interface {
values() []Bytes
values() []Value
}

type hasScalarTTL interface {
Expand All @@ -64,7 +64,7 @@ func prepareCacheName(r hasCacheName) (string, error) {
}

func prepareKey(r hasKey) ([]byte, error) {
key := r.key().AsBytes()
key := r.key().asBytes()

if len(key) == 0 {
err := momentoerrors.NewMomentoSvcErr(momentoerrors.InvalidArgumentError, "key cannot be empty", nil)
Expand All @@ -74,7 +74,7 @@ func prepareKey(r hasKey) ([]byte, error) {
}

func prepareValue(r hasValue) ([]byte, momentoerrors.MomentoSvcErr) {
value := r.value().AsBytes()
value := r.value().asBytes()
if len(value) == 0 {
err := momentoerrors.NewMomentoSvcErr(momentoerrors.InvalidArgumentError, "value cannot be empty", nil)
return nil, convertMomentoSvcErrorToCustomerError(err)
Expand Down Expand Up @@ -113,10 +113,10 @@ func prepareCollectionTtl(ttl utils.CollectionTTL, defaultTtl time.Duration) (ui
return uint64(ttlDuration.Milliseconds()), ttl.RefreshTtl
}

func momentoBytesListToPrimitiveByteList(i []Bytes) [][]byte {
func momentoBytesListToPrimitiveByteList(i []Value) [][]byte {
var rList [][]byte
for _, mb := range i {
rList = append(rList, mb.AsBytes())
rList = append(rList, mb.asBytes())
}
return rList
}
8 changes: 4 additions & 4 deletions momento/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ type SetRequest struct {
// Name of the cache to store the item in.
CacheName string
// string or byte key to be used to store item.
Key Bytes
Key Key
// string ot byte value to be stored.
Value Bytes
Value Value
// Optional Time to live in cache in seconds.
// If not provided, then default TTL for the cache client instance is used.
TTL time.Duration
Expand All @@ -37,9 +37,9 @@ type SetRequest struct {

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

func (r *SetRequest) key() Bytes { return r.Key }
func (r *SetRequest) key() Key { return r.Key }

func (r *SetRequest) value() Bytes { return r.Value }
func (r *SetRequest) value() Value { return r.Value }

func (r *SetRequest) ttl() time.Duration { return r.TTL }

Expand Down
Loading

0 comments on commit 4771030

Please sign in to comment.