Skip to content

Commit

Permalink
feat!: Normalize dictionaries. (#248)
Browse files Browse the repository at this point in the history
* feat!: Replace ElemField/ElemValue with Field/Value

This is the last place that prefixes with "Elem" or "Element".

* feat!: Element -> DictionaryElement

To distinguish from all the other elements.

* fix: prepareDictionaryElements typo

Co-authored-by: pgautier404 <pgautier404@users.noreply.github.com>

* doc: Reword the DictionaryElement docs.

---------

Co-authored-by: pgautier404 <pgautier404@users.noreply.github.com>
  • Loading branch information
2 people authored and cprice404 committed Mar 14, 2023
1 parent 31de897 commit 231aaa2
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 62 deletions.
8 changes: 4 additions & 4 deletions momento/cache_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,10 @@ func (c defaultScsClient) DictionarySetField(ctx context.Context, r *DictionaryS
)
}

var elements []Element
elements = append(elements, Element{
ElemField: r.Field,
ElemValue: r.Value,
var elements []DictionaryElement
elements = append(elements, DictionaryElement{
Field: r.Field,
Value: r.Value,
})
newRequest := &DictionarySetFieldsRequest{
CacheName: r.CacheName,
Expand Down
12 changes: 6 additions & 6 deletions momento/dictionary_set_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
type DictionarySetFieldsRequest struct {
CacheName string
DictionaryName string
Elements []Element
Elements []DictionaryElement
Ttl *utils.CollectionTtl

grpcRequest *pb.XDictionarySetRequest
Expand All @@ -23,7 +23,7 @@ type DictionarySetFieldsRequest struct {

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

func (r *DictionarySetFieldsRequest) elements() []Element { return r.Elements }
func (r *DictionarySetFieldsRequest) dictionaryElements() []DictionaryElement { return r.Elements }

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

Expand All @@ -38,16 +38,16 @@ func (r *DictionarySetFieldsRequest) initGrpcRequest(client scsDataClient) error
return err
}

var elements []Element
if elements, err = prepareElements(r); err != nil {
var elements []DictionaryElement
if elements, err = prepareDictionaryElements(r); err != nil {
return err
}

var pbElements []*pb.XDictionaryFieldValuePair
for _, v := range elements {
pbElements = append(pbElements, &pb.XDictionaryFieldValuePair{
Field: v.ElemField.asBytes(),
Value: v.ElemValue.asBytes(),
Field: v.Field.asBytes(),
Value: v.Value.asBytes(),
})
}

Expand Down
50 changes: 25 additions & 25 deletions momento/dictionary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ var _ = Describe("Dictionary methods", func() {
})

DescribeTable("add string fields and string and bytes values for set fields happy path",
func(elements []Element, expectedItemsStringValue map[string]string, expectedItemsByteValue map[string][]byte) {
func(elements []DictionaryElement, expectedItemsStringValue map[string]string, expectedItemsByteValue map[string][]byte) {
Expect(
sharedContext.Client.DictionarySetFields(sharedContext.Ctx, &DictionarySetFieldsRequest{
CacheName: sharedContext.CacheName,
Expand Down Expand Up @@ -198,27 +198,27 @@ var _ = Describe("Dictionary methods", func() {
},
Entry(
"with string values",
[]Element{
{ElemField: String("myField1"), ElemValue: String("myValue1")},
{ElemField: String("myField2"), ElemValue: String("myValue2")},
[]DictionaryElement{
{Field: String("myField1"), Value: String("myValue1")},
{Field: String("myField2"), Value: String("myValue2")},
},
map[string]string{"myField1": "myValue1", "myField2": "myValue2"},
map[string][]byte{"myField1": []byte("myValue1"), "myField2": []byte("myValue2")},
),
Entry(
"with byte values",
[]Element{
{ElemField: Bytes("myField1"), ElemValue: Bytes("myValue1")},
{ElemField: Bytes("myField2"), ElemValue: Bytes("myValue2")},
[]DictionaryElement{
{Field: Bytes("myField1"), Value: Bytes("myValue1")},
{Field: Bytes("myField2"), Value: Bytes("myValue2")},
},
map[string]string{"myField1": "myValue1", "myField2": "myValue2"},
map[string][]byte{"myField1": []byte("myValue1"), "myField2": []byte("myValue2")},
),
Entry(
"with mixed values",
[]Element{
{ElemField: Bytes("myField1"), ElemValue: String("myValue1")},
{ElemField: String("myField2"), ElemValue: Bytes("myValue2")},
[]DictionaryElement{
{Field: Bytes("myField1"), Value: String("myValue1")},
{Field: String("myField2"), Value: Bytes("myValue2")},
},
map[string]string{"myField1": "myValue1", "myField2": "myValue2"},
map[string][]byte{"myField1": []byte("myValue1"), "myField2": []byte("myValue2")},
Expand All @@ -230,9 +230,9 @@ var _ = Describe("Dictionary methods", func() {
sharedContext.Client.DictionarySetFields(sharedContext.Ctx, &DictionarySetFieldsRequest{
CacheName: sharedContext.CacheName,
DictionaryName: sharedContext.CollectionName,
Elements: []Element{
{ElemField: String("myField"), ElemValue: String("myValue")},
{ElemField: String(""), ElemValue: String("myOtherValue")},
Elements: []DictionaryElement{
{Field: String("myField"), Value: String("myValue")},
{Field: String(""), Value: String("myOtherValue")},
},
}),
)
Expand All @@ -243,9 +243,9 @@ var _ = Describe("Dictionary methods", func() {
sharedContext.Client.DictionarySetFields(sharedContext.Ctx, &DictionarySetFieldsRequest{
CacheName: sharedContext.CacheName,
DictionaryName: sharedContext.CollectionName,
Elements: []Element{
{ElemField: String("myField"), ElemValue: String("myValue")},
{ElemField: String("myOtherField"), ElemValue: nil},
Elements: []DictionaryElement{
{Field: String("myField"), Value: String("myValue")},
{Field: String("myOtherField"), Value: nil},
},
}),
).Error().To(HaveMomentoErrorCode(InvalidArgumentError))
Expand All @@ -255,29 +255,29 @@ var _ = Describe("Dictionary methods", func() {

It("converts from a map with string values to element slice", func() {
theMap := map[string]string{"myField1": "myValue1", "myField2": "myValue2"}
expected := []Element{
{ElemField: String("myField1"), ElemValue: String("myValue1")},
{ElemField: String("myField2"), ElemValue: String("myValue2")},
expected := []DictionaryElement{
{Field: String("myField1"), Value: String("myValue1")},
{Field: String("myField2"), Value: String("myValue2")},
}
elems := ElementsFromMapStringString(theMap)
Expect(elems).To(ConsistOf(expected))
})

It("converts from a map with bytes values to element slice", func() {
theMap := map[string][]byte{"myField1": []byte("myValue1"), "myField2": []byte("myValue2")}
expected := []Element{
{ElemField: String("myField1"), ElemValue: Bytes("myValue1")},
{ElemField: String("myField2"), ElemValue: Bytes("myValue2")},
expected := []DictionaryElement{
{Field: String("myField1"), Value: Bytes("myValue1")},
{Field: String("myField2"), Value: Bytes("myValue2")},
}
elems := ElementsFromMapStringBytes(theMap)
Expect(elems).To(ConsistOf(expected))
})

It("converts from a map with Value values to element slice", func() {
theMap := map[string]Value{"myField1": String("myValue1"), "myField2": Bytes("myValue2")}
expected := []Element{
{ElemField: String("myField1"), ElemValue: String("myValue1")},
{ElemField: String("myField2"), ElemValue: Bytes("myValue2")},
expected := []DictionaryElement{
{Field: String("myField1"), Value: String("myValue1")},
{Field: String("myField2"), Value: Bytes("myValue2")},
}
elems := ElementsFromMapStringValue(theMap)
Expect(elems).To(ConsistOf(expected))
Expand Down
44 changes: 22 additions & 22 deletions momento/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ type hasFields interface {
fields() []Value
}

type hasElements interface {
elements() []Element
type hasDictionaryElements interface {
dictionaryElements() []DictionaryElement
}

type hasTtl interface {
Expand Down Expand Up @@ -170,18 +170,18 @@ func prepareValues(r hasValues) ([][]byte, momentoerrors.MomentoSvcErr) {
return values, nil
}

func prepareElements(r hasElements) ([]Element, error) {
for _, v := range r.elements() {
if v.ElemValue == nil || v.ElemField == nil {
func prepareDictionaryElements(r hasDictionaryElements) ([]DictionaryElement, error) {
for _, v := range r.dictionaryElements() {
if v.Value == nil || v.Field == nil {
return nil, buildError(
momentoerrors.InvalidArgumentError, "element fields and values may not be nil", nil,
)
}
if err := validateNotEmpty(v.ElemField.asBytes(), "element field"); err != nil {
if err := validateNotEmpty(v.Field.asBytes(), "element field"); err != nil {
return nil, err
}
}
return r.elements(), nil
return r.dictionaryElements(), nil
}

func prepareCollectionTtl(r hasCollectionTtl, defaultTtl time.Duration) (uint64, bool, error) {
Expand Down Expand Up @@ -233,34 +233,34 @@ func validateNotEmpty(field []byte, label string) error {
return nil
}

func ElementsFromMapStringString(theMap map[string]string) []Element {
var elements []Element
func ElementsFromMapStringString(theMap map[string]string) []DictionaryElement {
var elements []DictionaryElement
for k, v := range theMap {
elements = append(elements, Element{
ElemField: String(k),
ElemValue: String(v),
elements = append(elements, DictionaryElement{
Field: String(k),
Value: String(v),
})
}
return elements
}

func ElementsFromMapStringBytes(theMap map[string][]byte) []Element {
var elements []Element
func ElementsFromMapStringBytes(theMap map[string][]byte) []DictionaryElement {
var elements []DictionaryElement
for k, v := range theMap {
elements = append(elements, Element{
ElemField: String(k),
ElemValue: Bytes(v),
elements = append(elements, DictionaryElement{
Field: String(k),
Value: Bytes(v),
})
}
return elements
}

func ElementsFromMapStringValue(theMap map[string]Value) []Element {
var elements []Element
func ElementsFromMapStringValue(theMap map[string]Value) []DictionaryElement {
var elements []DictionaryElement
for k, v := range theMap {
elements = append(elements, Element{
ElemField: String(k),
ElemValue: v,
elements = append(elements, DictionaryElement{
Field: String(k),
Value: v,
})
}
return elements
Expand Down
9 changes: 4 additions & 5 deletions momento/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ func (v String) asString() string {
return string(v)
}

// Element Type to hold field/value pairs for use with the DictionarySetFields operation.
// The Value type is used for ElemField and ElemValue and allows both strings and bytes
// to be used for fields and values.
type Element struct {
ElemField, ElemValue Value
// Type to hold field/value elements in dictionaries for use with DictionarySetFields.
// Field and Value are both Value type which allows both Strings and Bytes.
type DictionaryElement struct {
Field, Value Value
}

0 comments on commit 231aaa2

Please sign in to comment.