Skip to content

Commit

Permalink
Do not allow nil values in Key constructors to convert to NullValue
Browse files Browse the repository at this point in the history
  • Loading branch information
khaf committed Nov 28, 2024
1 parent f9b52d7 commit 3323487
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
11 changes: 10 additions & 1 deletion key.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,19 @@ func NewKey(namespace string, setName string, key interface{}) (*Key, Error) {
// NewKeyWithDigest initializes a key from namespace, optional set name and user key.
// The server handles record identifiers by digest only.
func NewKeyWithDigest(namespace string, setName string, key interface{}, digest []byte) (*Key, Error) {
var userKey Value = nil
if key != nil {
userKey = NewValue(key)
// make sure the key type is valid
if err := verifyKey(userKey); err != nil {
return nil, err
}
}

newKey := &Key{
namespace: namespace,
setName: setName,
userKey: NewValue(key),
userKey: userKey,
}

if err := newKey.SetDigest(digest); err != nil {
Expand Down
15 changes: 15 additions & 0 deletions key_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,18 @@ func (vb *keyWriter) writeKey(val Value) Error {

return newError(types.PARAMETER_ERROR, "Key Generation Error. Value not supported: "+val.String())
}

func verifyKey(val Value) Error {
switch val.(type) {
case IntegerValue:
return nil
case LongValue:
return nil
case StringValue:
return nil
case BytesValue:
return nil
}

return newError(types.PARAMETER_ERROR, "Key Generation Error. Value not supported: "+val.String())
}
5 changes: 4 additions & 1 deletion key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ var _ = gg.Describe("Key Test", func() {
gm.Expect(err, nil)
gm.Expect(key.Digest()).To(gm.Equal([]byte("01234567890123456789")))

key, _ = as.NewKeyWithDigest("namespace", "set", []interface{}{}, []byte("01234567890123456789"))
key, err = as.NewKeyWithDigest("namespace", "set", []interface{}{}, []byte("01234567890123456789"))
gm.Expect(err).To(gm.HaveOccurred())

key, err = as.NewKeyWithDigest("namespace", "set", nil, []byte("01234567890123456789"))
gm.Expect(key.Digest()).To(gm.Equal([]byte("01234567890123456789")))
})

Expand Down

0 comments on commit 3323487

Please sign in to comment.