feat(Storage) : Implement Object Contexts with filter and validation#2
feat(Storage) : Implement Object Contexts with filter and validation#2salilg-eng wants to merge 64 commits intomainfrom
Conversation
|
/Gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces support for object contexts in Cloud Storage, enabling the attachment of key-payload pairs to objects for identification and filtering. The changes include updates to the service definition, connection handling, and the addition of validation logic for custom contexts. Feedback highlights the need for consistent validation across all object modification methods—such as update, copy, and rewrite—and suggests refining validation regex and error messages. Additionally, corrections were requested for a misleading internal comment and the incorrect placement of contexts within a unit test's metadata array.
| // during object creation context properties go into metadata | ||
| // but not into request body |
There was a problem hiding this comment.
This comment is misleading. In the Cloud Storage JSON API, contexts is a property of the Object resource. When calling insertObject, the metadata provided in $args['metadata'] represents the object resource and constitutes the request body. Thus, it is indeed part of the request body.
// during object creation context properties are part of the object resource
// and should be included in the request body.
thiyaguk09
left a comment
There was a problem hiding this comment.
Could you please add the missing tests?
| @@ -215,12 +217,284 @@ public function testObjectRetentionUnlockedMode() | |||
| $this->assertFalse($object->exists()); | |||
| } | |||
|
|
|||
| public function testObjectExists() | |||
There was a problem hiding this comment.
Do not remove the existing test testObjectExists.
There was a problem hiding this comment.
That was my bad I added
| $this->assertFalse($object->exists()); | ||
| } | ||
|
|
||
| public function testGetContextAndServerGenratedTimes() |
There was a problem hiding this comment.
Replaced with this testGetContextsWithServerTime
| { | ||
| $initialContexts = [ | ||
| 'custom' => [ | ||
| 'tag' => ['value' => 'orignal'], |
| $inherited = $object->rewrite(self::$bucket, ['name' => 'inherit-' . uniqid()]); | ||
| $info = $inherited->info(); | ||
|
|
||
| $this->assertEquals('orignal', $info['contexts']['custom']['tag']['value']); |
| ], | ||
| ]; | ||
|
|
||
| $object = $this->createObjectWithContexts($initialContexts); |
There was a problem hiding this comment.
Ensure the object has context first.
There was a problem hiding this comment.
Yes, I ensure that and according to added
|
|
||
| const CONTEXT_OBJECT_KEY = 'insert-key'; | ||
| const CONTEXT_OBJECT_VALUE = 'insert-val'; | ||
| const CONTEXT_OBJECT_PREFIX = 'object-contexts-'; |
There was a problem hiding this comment.
nits: If this is not being reused, go with a static value, similar to how it is added in other tests.
There was a problem hiding this comment.
Removed this constant and directly used static
| } | ||
| } | ||
|
|
||
| public function removeAndClearAllContextsDataProvider() |
There was a problem hiding this comment.
These scenarios are fundamentally wrong; you are performing the same operation twice with the same key.
There was a problem hiding this comment.
I can remove the expected value from dataProvider and compare directly in the method testRemoveAndClearAllObjectContexts.
| ); | ||
| } | ||
|
|
||
| public function testGetFiltersByPresenceOfKeyValuePair() |
There was a problem hiding this comment.
Which is a list operation? Why is it named "get" here? Please correct this.
| $iterator = $bucket->objects([ | ||
| 'filter' => $filter | ||
| ]); | ||
| $iterator->current(); |
There was a problem hiding this comment.
Schemes test without assert!
| 'metadata' => [ | ||
| 'contexts' => [ | ||
| 'custom' => [ | ||
| 'dept' => ['value' => 'eng', 'createTime' => '...'] |
There was a problem hiding this comment.
Use DateProvider instead of '...', and include all values in the get method since the update time is currently missing.
|
/Gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces support for object contexts in Google Cloud Storage, enabling users to attach and filter by key-payload pairs. The implementation includes metadata handling in the REST connection, validation logic for context keys and values during upload, and updates to the service definition for context-based filtering. Extensive unit and system tests were added to verify CRUD operations and filtering. Feedback suggests refactoring the validation logic into a shared trait to ensure consistency across other operations like update and copy, and improving the granularity of error messages for context value validation.
| if ($val !== '' && !preg_match('/^[a-zA-Z0-9][^"\/]*$/', $val)) { | ||
| throw new \InvalidArgumentException('Object context value must start with an alphanumeric.'); | ||
| } |
There was a problem hiding this comment.
For consistency with the key validation logic and to provide more specific error messages, consider splitting the value validation into separate checks. The current error message is also incomplete as it doesn't mention the restrictions on double quotes and forward slashes.
if ($val !== '') {
if (!preg_match('/^[a-zA-Z0-9]/', $val)) {
throw new \InvalidArgumentException('Object context value must start with an alphanumeric.');
}
if (preg_match('/["\/]/', $val)) {
throw new \InvalidArgumentException('Object context value cannot contain double quotes or forward slashes.');
}
}|
/Gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces support for object contexts in Google Cloud Storage, enabling users to attach, update, and filter objects based on custom key-value pairs. The changes span the service definition, REST connection handling, and core library classes, accompanied by extensive system and unit tests. Feedback focuses on improving the reusability of validation logic across different object operations, ensuring that null values are correctly handled to allow clearing contexts, and refining error messages and documentation for better consistency and accuracy.
| if ($val !== '' && !preg_match('/^[a-zA-Z0-9][^"\/]*$/', $val)) { | ||
| throw new \InvalidArgumentException('Object context value must start with an alphanumeric.'); | ||
| } |
There was a problem hiding this comment.
The error message is misleading. If the value starts with an alphanumeric character but contains a forward slash or a double quote, the regex will fail, but the exception message will only mention the alphanumeric requirement. It should be updated to reflect all constraints.
if ($val !== '' && !preg_match('/^[a-zA-Z0-9][^"\/]*$/', $val)) {
throw new \InvalidArgumentException('Object context value must start with an alphanumeric and cannot contain double quotes or forward slashes.');
}| * @type string $contexts.custom.{key}.value The value associated with the context. | ||
| * Must start with an alphanumeric character and cannot contain double quotes (`"`) | ||
| * or forward slashes (`/`). |
There was a problem hiding this comment.
The documentation for the context value here is inconsistent with the one in Bucket.php. It is missing the 'If not empty' qualifier, which is important because the validation logic allows empty strings.
* @type string $contexts.custom.{key}.value The value associated with the context.
* If not empty, must start with an alphanumeric character and cannot contain double quotes (`"`)
* or forward slashes (`/`).
operations, and complex filtering scenarios.