From 085b911028cbafff355d259abd7cbd5cf03952c1 Mon Sep 17 00:00:00 2001 From: yrizhkov Date: Wed, 27 Nov 2024 19:12:52 +0200 Subject: [PATCH 1/6] Parse nil keys properly in query operations --- value.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/value.go b/value.go index 1b60a8a4..297581a3 100644 --- a/value.go +++ b/value.go @@ -1275,6 +1275,9 @@ func bytesToKeyValue(pType int, buf []byte, offset int, length int) (Value, Erro } return ListValue(v), nil + case ParticleType.NULL: + return NewNullValue(), nil + default: return nil, newError(types.PARSE_ERROR, fmt.Sprintf("ParticleType %d not recognized. Please file a github issue.", pType)) } From f9b52d793d1ccb18b3189d414f4fa7e5b1ce2e58 Mon Sep 17 00:00:00 2001 From: yrizhkov Date: Thu, 28 Nov 2024 16:22:58 +0200 Subject: [PATCH 2/6] return nil instead of NewNullValue() --- value.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/value.go b/value.go index 297581a3..455c03f3 100644 --- a/value.go +++ b/value.go @@ -1276,7 +1276,7 @@ func bytesToKeyValue(pType int, buf []byte, offset int, length int) (Value, Erro return ListValue(v), nil case ParticleType.NULL: - return NewNullValue(), nil + return nil, nil default: return nil, newError(types.PARSE_ERROR, fmt.Sprintf("ParticleType %d not recognized. Please file a github issue.", pType)) From 3323487287319fb31b4291b1ff30e9bd06255aa2 Mon Sep 17 00:00:00 2001 From: Khosrow Afroozeh Date: Thu, 28 Nov 2024 15:58:57 +0100 Subject: [PATCH 3/6] Do not allow nil values in Key constructors to convert to NullValue --- key.go | 11 ++++++++++- key_helper.go | 15 +++++++++++++++ key_test.go | 5 ++++- 3 files changed, 29 insertions(+), 2 deletions(-) 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"))) }) From 49e1e9db4ab28395e490ceee7b94ee11b9f85b48 Mon Sep 17 00:00:00 2001 From: Khosrow Afroozeh Date: Thu, 28 Nov 2024 16:24:13 +0100 Subject: [PATCH 4/6] Added todos for better error messages in a major version --- key_helper.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/key_helper.go b/key_helper.go index ec36b20c..82dc459a 100644 --- a/key_helper.go +++ b/key_helper.go @@ -150,6 +150,7 @@ func (vb *keyWriter) writeKey(val Value) Error { return nil } + // TODO: Replace the error message with fmt.Sprintf("Key Generation Error. Value type not supported: %T", val) return newError(types.PARAMETER_ERROR, "Key Generation Error. Value not supported: "+val.String()) } @@ -165,5 +166,6 @@ func verifyKey(val Value) Error { return nil } + // TODO: Replace the error message with fmt.Sprintf("Key Generation Error. Value type not supported: %T", val) return newError(types.PARAMETER_ERROR, "Key Generation Error. Value not supported: "+val.String()) } From 11d861fab4515a6ee10a9efeae9aa6b0e6f0d36e Mon Sep 17 00:00:00 2001 From: Khosrow Afroozeh Date: Thu, 28 Nov 2024 17:31:20 +0100 Subject: [PATCH 5/6] Reverts nil handling in Key --- key.go | 11 +---------- key_helper.go | 16 ---------------- key_test.go | 5 +---- 3 files changed, 2 insertions(+), 30 deletions(-) diff --git a/key.go b/key.go index 0b526959..3b62bf0d 100644 --- a/key.go +++ b/key.go @@ -112,19 +112,10 @@ 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: userKey, + userKey: NewValue(key), } if err := newKey.SetDigest(digest); err != nil { diff --git a/key_helper.go b/key_helper.go index 82dc459a..f237fc89 100644 --- a/key_helper.go +++ b/key_helper.go @@ -153,19 +153,3 @@ func (vb *keyWriter) writeKey(val Value) Error { // TODO: Replace the error message with fmt.Sprintf("Key Generation Error. Value type not supported: %T", val) 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 - } - - // TODO: Replace the error message with fmt.Sprintf("Key Generation Error. Value type not supported: %T", val) - return newError(types.PARAMETER_ERROR, "Key Generation Error. Value not supported: "+val.String()) -} diff --git a/key_test.go b/key_test.go index b752a609..541e80e5 100644 --- a/key_test.go +++ b/key_test.go @@ -132,10 +132,7 @@ var _ = gg.Describe("Key Test", func() { gm.Expect(err, nil) gm.Expect(key.Digest()).To(gm.Equal([]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")) + key, _ = as.NewKeyWithDigest("namespace", "set", []interface{}{}, []byte("01234567890123456789")) gm.Expect(key.Digest()).To(gm.Equal([]byte("01234567890123456789"))) }) From 28b36ed22be2820b8946838cb1a2c68298867d5a Mon Sep 17 00:00:00 2001 From: Khosrow Afroozeh Date: Thu, 28 Nov 2024 17:31:29 +0100 Subject: [PATCH 6/6] Revert "return nil instead of NewNullValue()" This reverts commit f9b52d793d1ccb18b3189d414f4fa7e5b1ce2e58. --- value.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/value.go b/value.go index 455c03f3..297581a3 100644 --- a/value.go +++ b/value.go @@ -1276,7 +1276,7 @@ func bytesToKeyValue(pType int, buf []byte, offset int, length int) (Value, Erro return ListValue(v), nil case ParticleType.NULL: - return nil, nil + return NewNullValue(), nil default: return nil, newError(types.PARSE_ERROR, fmt.Sprintf("ParticleType %d not recognized. Please file a github issue.", pType))