Allow byte array/slice be given as value 71/171/3 v0.1.1
authorMarco Tallskog <marco.tallskog@nokia.com>
Fri, 17 May 2019 10:05:00 +0000 (13:05 +0300)
committerMarco Tallskog <marco.tallskog@nokia.com>
Fri, 17 May 2019 11:09:56 +0000 (14:09 +0300)
An error was given if byte slice or array was given as a value, now they
are allowed. However, other types or slices are not allowed as value.

Change-Id: Ia0c4515498a1b0e03a002a7d4357adb6d262213a
Signed-off-by: Marco Tallskog <marco.tallskog@nokia.com>
sdl.go
sdl_test.go

diff --git a/sdl.go b/sdl.go
index 4d98904..9eee8c9 100644 (file)
--- a/sdl.go
+++ b/sdl.go
@@ -146,7 +146,12 @@ func (s *SdlInstance) setNamespaceToKeys(pairs ...interface{}) ([]interface{}, e
                                        }
                                }
                        } else {
-                               return []interface{}{}, errors.New("Key/value pairs doesn't match")
+                               if reflectType.Elem().Kind() == reflect.Uint8 {
+                                       retVal = append(retVal, v)
+                                       shouldBeKey = true
+                               } else {
+                                       return []interface{}{}, errors.New("Key/value pairs doesn't match")
+                               }
                        }
                case reflect.Array:
                        if shouldBeKey {
@@ -162,7 +167,12 @@ func (s *SdlInstance) setNamespaceToKeys(pairs ...interface{}) ([]interface{}, e
                                        }
                                }
                        } else {
-                               return []interface{}{}, errors.New("Key/value pairs doesn't match")
+                               if reflectType.Elem().Kind() == reflect.Uint8 {
+                                       retVal = append(retVal, v)
+                                       shouldBeKey = true
+                               } else {
+                                       return []interface{}{}, errors.New("Key/value pairs doesn't match")
+                               }
                        }
                default:
                        if shouldBeKey {
@@ -399,6 +409,9 @@ func (s *SdlInstance) RemoveAll() error {
        return err
 }
 
+//RemoveAllAndPublish removes all keys under the namespace and if successfull, it
+//will publish an event to given channel. This operation is not atomic, thus it is
+//not guaranteed that all keys are removed.
 func (s *SdlInstance) RemoveAllAndPublish(channelsAndEvents []string) error {
        keys, err := s.Keys(s.nsPrefix + "*")
        if err != nil {
index dd65755..a941f83 100644 (file)
@@ -215,6 +215,39 @@ func TestWriteOneKey(t *testing.T) {
        m.AssertExpectations(t)
 }
 
+func TestWriteByteSliceAsValue(t *testing.T) {
+       m, i := setup()
+
+       msetExpected := []interface{}{"{namespace},key1", []byte{1, 2, 3, 4, 5}}
+
+       m.On("MSet", msetExpected).Return(nil)
+       err := i.Set("key1", []byte{1, 2, 3, 4, 5})
+       assert.Nil(t, err)
+       m.AssertExpectations(t)
+}
+
+func TestWriteByteSliceAsValueMixed(t *testing.T) {
+       m, i := setup()
+
+       msetExpected := []interface{}{"{namespace},key1", []byte{1, 2, 3, 4, 5}, "{namespace},key2", "value2"}
+
+       m.On("MSet", msetExpected).Return(nil)
+       err := i.Set("key1", []byte{1, 2, 3, 4, 5}, []string{"key2", "value2"})
+       assert.Nil(t, err)
+       m.AssertExpectations(t)
+}
+
+func TestWriteByteArrayAsValue(t *testing.T) {
+       m, i := setup()
+
+       msetExpected := []interface{}{"{namespace},key1", [5]byte{1, 2, 3, 4, 5}}
+
+       m.On("MSet", msetExpected).Return(nil)
+       err := i.Set("key1", [5]byte{1, 2, 3, 4, 5})
+       assert.Nil(t, err)
+       m.AssertExpectations(t)
+}
+
 func TestWriteMixed(t *testing.T) {
        m, i := setup()