generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 60
feat: integrate metadata support for short-term-memory (STM) #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| from enum import Enum | ||
| from typing import Optional, TypedDict, Union, NotRequired | ||
|
|
||
| class StringValue(TypedDict): | ||
| """Value associated with the `eventMetadata` key.""" | ||
| stringValue: str | ||
|
|
||
| @staticmethod | ||
| def build(value: str) -> 'StringValue': | ||
| return { | ||
| "stringValue": value | ||
| } | ||
|
|
||
| MetadataValue = Union[StringValue] | ||
| """ | ||
| Union type representing metadata values. | ||
|
|
||
| Variants: | ||
| - StringValue: {"stringValue": str} - String metadata value | ||
| """ | ||
|
|
||
| MetadataKey = Union[str] | ||
| """ | ||
| Union type representing metadata key. | ||
| """ | ||
|
|
||
| class LeftExpression(TypedDict): | ||
| """ | ||
| Left operand of the event metadata filter expression. | ||
| """ | ||
| metadataKey: MetadataKey | ||
|
|
||
| @staticmethod | ||
| def build(key: str) -> 'LeftExpression': | ||
| """Builds the `metadataKey` for `LeftExpression`""" | ||
| return { | ||
| "metadataKey": key | ||
| } | ||
|
|
||
| class OperatorType(Enum): | ||
| """ | ||
| Operator applied to the event metadata filter expression. | ||
|
|
||
| Currently supports: | ||
| - `EQUALS_TO` | ||
| - `EXISTS` | ||
| - `NOT_EXISTS` | ||
| """ | ||
| EQUALS_TO = "EQUALS_TO" | ||
| EXISTS = "EXISTS" | ||
| NOT_EXISTS = "NOT_EXISTS" | ||
|
|
||
| class RightExpression(TypedDict): | ||
| """ | ||
| Right operand of the event metadata filter expression. | ||
|
|
||
| Variants: | ||
| - StringValue: {"metadataValue": {"stringValue": str}} | ||
| """ | ||
| metadataValue: MetadataValue | ||
|
|
||
| @staticmethod | ||
| def build(value: str) -> 'RightExpression': | ||
| """Builds the `RightExpression` for `stringValue` type""" | ||
| return {"metadataValue": StringValue.build(value)} | ||
|
|
||
| class EventMetadataFilter(TypedDict): | ||
| """ | ||
| Filter expression for retrieving events based on metadata associated with an event. | ||
|
|
||
| Args: | ||
| left: `LeftExpression` of the event metadata filter expression. | ||
| operator: `OperatorType` applied to the event metadata filter expression. | ||
| right: Optional `RightExpression` of the event metadata filter expression. | ||
| """ | ||
| left: LeftExpression | ||
| operator: OperatorType | ||
| right: NotRequired[RightExpression] | ||
|
|
||
| def build_expression(left_operand: LeftExpression, operator: OperatorType, right_operand: Optional[RightExpression] = None) -> 'EventMetadataFilter': | ||
| """ | ||
| This method builds the required event metadata filter expression into the `EventMetadataFilterExpression` type when querying listEvents. | ||
|
|
||
| Args: | ||
| left_operand: Left operand of the event metadata filter expression | ||
| operator: Operator applied to the event metadata filter expression | ||
| right_operand: Optional right_operand of the event metadata filter expression. | ||
|
|
||
| Example: | ||
| ``` | ||
| left_operand = LeftExpression.build_key(key='location') | ||
| operator = OperatorType.EQUALS_TO | ||
| right_operand = RightExpression.build_string_value(value='NYC') | ||
| ``` | ||
|
|
||
| #### Response Object: | ||
| ``` | ||
| { | ||
| 'left': { | ||
| 'metadataKey': 'location' | ||
| }, | ||
| 'operator': 'EQUALS_TO', | ||
| 'right': { | ||
| 'metadataValue': { | ||
| 'stringValue': 'NYC' | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
| """ | ||
| filter = { | ||
| 'left': left_operand, | ||
| 'operator': operator.value | ||
| } | ||
|
|
||
| if right_operand: | ||
| filter['right'] = right_operand | ||
| return filter | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it be possible to have the syntax like
LeftExpression(key='location')and then we infer the type of from inspecting the type? Like we use the stringValue by checking that the
keyargument` is a string?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this is a TypedDict - we cannot - as it's supposed to be a type hint for dicts with specific structures - it doesn't provide runtime type conversion or inference.
For inferring the type - if needed, we can create a class method - which then invokes the respective methods.
Example:
(Where we'd have other new
TypedDictclasses when we introduce new types (e.g: NumberValue))If at any point, we'd want to support other functionalities for each of these types or for
LeftExpressionorRightExpressionas an overall feature - the customer would still have to invoke the respective methods manually without any inference - where those methods would still end up being@staticmethod.I think having it as above is more intuitive and allows the customer to understand what key type needs to be built.