diff --git a/key.go b/key.go index 3b62bf0d..0b526959 100644 --- a/key.go +++ b/key.go @@ -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 { diff --git a/key_helper.go b/key_helper.go index 38ead4ff..ec36b20c 100644 --- a/key_helper.go +++ b/key_helper.go @@ -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()) +} diff --git a/key_test.go b/key_test.go index 541e80e5..b752a609 100644 --- a/key_test.go +++ b/key_test.go @@ -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"))) })