From: Marco Tallskog Date: Fri, 17 May 2019 10:05:00 +0000 (+0300) Subject: Allow byte array/slice be given as value X-Git-Tag: v0.1.1 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=a66fcfb6cc2918cb06945ebb2f84f0c9f440ec09;p=ric-plt%2Fsdlgo.git Allow byte array/slice be given as value 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 --- diff --git a/sdl.go b/sdl.go index 4d98904..9eee8c9 100644 --- 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 { diff --git a/sdl_test.go b/sdl_test.go index dd65755..a941f83 100644 --- a/sdl_test.go +++ b/sdl_test.go @@ -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()