Skip to content
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

refactor: remove annotations support #5

Merged
merged 26 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3d14989
refactor: remove `doctrine/annotations`
pizzaminded Mar 17, 2024
d7cb047
refactor: remove `doctrine/annotations` from ItemMappingReader
pizzaminded Mar 17, 2024
c50c25b
refactor: remove `doctrine/annotations` from ItemMappingReader
pizzaminded Mar 17, 2024
fc07e45
refactor: extract to separate method
pizzaminded Mar 17, 2024
af7ba49
refactor: remove more occurences of annotation reader
pizzaminded Mar 17, 2024
2732b7f
fix: remove annotation reader from test suite
pizzaminded Mar 17, 2024
05bbf23
refactor: change constructor arguments
pizzaminded Mar 17, 2024
cebb2e7
fix: switch test class from annotations to attributes
pizzaminded Mar 17, 2024
0b58675
fix: no need to keep these files in separate file
pizzaminded Mar 17, 2024
382dea6
fix: switch test class from annotations to attributes
pizzaminded Mar 17, 2024
29fca69
fix: switch test class from annotations to attributes
pizzaminded Mar 17, 2024
bad9faa
fix: get rid of AbstractAttribute
pizzaminded Mar 17, 2024
56c60ab
fix: switch test class from annotations to attributes
pizzaminded Mar 17, 2024
b9242d2
fix: define variable
pizzaminded Mar 17, 2024
39e5a94
refactor: remove last occurence of annotation reader
pizzaminded Mar 17, 2024
f283c2a
refactor: change constructor arguments
pizzaminded Mar 17, 2024
b954687
fix: switch test classes from annotations to attributes
pizzaminded Mar 17, 2024
5750db3
refactor: change constructor arguments
pizzaminded Mar 17, 2024
8167fb9
refactor: fix typehint
pizzaminded Mar 17, 2024
a110597
fix: switch test class from annotations to attributes
pizzaminded Mar 17, 2024
0a3041f
fix: change the function to split arrays
pizzaminded Jun 15, 2024
4a4c4b8
fix: convert `SetValue` to plain arrays during hydration
pizzaminded Jun 16, 2024
1f8a238
feat: add `nodash` filter
pizzaminded Jun 16, 2024
757c6fb
fix: get rid of annotations
pizzaminded Aug 17, 2024
e58c171
remove braces
pizzaminded Aug 17, 2024
23bfd13
update README.md
pizzaminded Aug 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ Work with AWS DynamoDB and Single-Table-Designed tables in your apps.

Requires `aws/aws-sdk-php` with `3.*` version.

## Getting started

### Installation

`composer require dynamite/dynamite`

### Configuration

## Some important things you need to know:

### Your table schema
Expand All @@ -14,23 +22,7 @@ Requires `aws/aws-sdk-php` with `3.*` version.
- Dynamite assumes that tables/indexes are created and active.

### You need to bring your own ID Generator
There are some proposals you can use:

#### 1. [ramsey/uuid](https://github.com/ramsey/uuid)
- Literally an UUID
- Really low chance of collision

#### 2. [robinvdvleuten/ulid](https://github.com/robinvdvleuten/php-ulid) ([**see ULID spec**](https://github.com/ulid/spec))
- Time-based
- Lexicographically sortable
- UUID compliant
- Really low chance of collision
- Higher precision than KSUID
#### 3. [tuupola/ksuid](https://github.com/tuupola/ksuid) ([**Read more about KSUID**](https://github.com/segmentio/ksuid))
- Time-based
- Lexicographically sortable
- Naturally ordered
- Really low chance of collision
See `docs/bring-your-own-id.md` for more information.

## The unordered roadmap to `v1.0.0`
- [x] Creating an item
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"require": {
"php": ">= 8 | 8.1",
"guzzlehttp/guzzle": "> 7",
"doctrine/annotations": "^1.13",
"aws/aws-sdk-php": "^3.216",
"symfony/polyfill-php80": "^1.23"
},
Expand Down
44 changes: 12 additions & 32 deletions src/Dynamite/Configuration/DuplicateTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,27 @@

namespace Dynamite\Configuration;


use Doctrine\Common\Annotations\Annotation\Target;

/**
* When spotted in configuration, takes props from main item and writes them to new record.
* This enforces persisting items via batchWriteItem.
*
* @Annotation
* @Target({"CLASS"})
*
* @author pizzaminded <[email protected]>
* @license MIT
*/
#[\Attribute(flags: \Attribute::TARGET_CLASS)]
#[\Attribute(flags: \Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class DuplicateTo
{
public string $pk;

public ?string $sk;

/**
* Applies transformations to Primary key attributes.
* @Enum({"UPPER", "LOWER"})
* @deprecated - soon to be removed - use {upper:propName} or {lower:propName} filter
*/
public ?string $transform = null;

/**
* Properties from base entity that should be put in duplicated item
* @var array<string, mixed>
*/
public array $props = [];
public function __construct(
protected string $pk,
protected ?string $sk,
/**
* Properties from base entity that should be put in duplicated item
* @var array<string>
*/
protected array $props = []
)
{
}

/**
* @return array
Expand All @@ -53,13 +42,4 @@ public function getSortKeyFormat(): string
{
return $this->sk;
}

/**
* @return string|null
*/
public function getTransform(): ?string
{
return $this->transform;
}

}
32 changes: 16 additions & 16 deletions src/Dynamite/Configuration/NestedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@
#[\Attribute(flags: \Attribute::TARGET_CLASS)]
class NestedItem
{
/**
* When set, payload from persistence layer will be passed as a first argument
* Method must be public and static
* @var string|null
*/
private ?string $deserializeMethod;

/**
* When set, values from properties will be ignored and given method will be called
* Function cannot be static, must be public and return array
* @var string|null
*/
private ?string $serializeMethod;

public function __construct(array $props)
public function __construct(
/**
* When set, payload from persistence layer will be passed as a first argument
* Method must be public and static
* @var string|null
*/
private ?string $deserializeMethod = null,

/**
* When set, values from properties will be ignored and given method will be called
* Function cannot be static, must be public and return array
* @var string|null
*/
private ?string $serializeMethod = null
)
{
$this->deserializeMethod = $props['deserializeMethod'] ?? null;
$this->serializeMethod = $props['serializeMethod'] ?? null;

}

/**
Expand Down
29 changes: 26 additions & 3 deletions src/Dynamite/Configuration/NestedItemAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,40 @@
* @license MIT
*/
#[\Attribute(flags: \Attribute::TARGET_PROPERTY)]
class NestedItemAttribute extends AbstractAttribute
class NestedItemAttribute implements AttributeInterface
{

/**
* @throws ConfigurationException
*/
public function __construct(
protected string $type,
protected string $name,
) {
$this->assertType($this->type);
}

/**
* @param string $type
* @return void
* @throws ConfigurationException
*/
protected function assertType(string $type)
protected function assertType(string $type): void
{
if (!class_exists($type)) {
throw new ConfigurationException(sprintf('Class "%s" does not exists and cannot be used as a type in "%s" annotation.', $type, self::class));
throw new ConfigurationException(sprintf('Class "%s" does not exists and cannot be used as a type in "%s" attribute.', $type, self::class));
}
}

public function getType(): string
{
return $this->type;
}

public function getName(): string
{
return $this->name;
}


}
38 changes: 15 additions & 23 deletions src/Dynamite/Configuration/NestedValueObjectAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,29 @@
* A object-with-single-property (or array of them) which will be converted to scalar value (or array of them).
* Use cases: Value objects with single values (eg: Colour, Currency)
*
* @Annotation
* @author pizzaminded <[email protected]>
* @license MIT
*/
#[\Attribute(flags: \Attribute::TARGET_PROPERTY)]
class NestedValueObjectAttribute extends AbstractAttribute
{
public function __construct(
string $type,
string $name,
/**
* Which property should hold value going to persistence layer?
* @var string
*/
protected string $property,
protected bool $collection = false
) {

/**
* Which property should hold value going to persistence layer?
* @var string
*/
protected string $property;

/**
* Does it store a collection of classes?
* @var bool
*/
protected bool $collection = false;


public function __construct(array $props)
{
parent::__construct($props);
$this->assertPropertiesPresence($props, ['property']);

if (isset($props['collection'])) {
$this->collection = (bool)$props['collection'];
}
parent::__construct([
'type' => $type,
'property' => $property,
'name' => $name,

$this->property = $props['property'];
]);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Dynamite/ItemRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public function __construct(

/**
* When passing a string to $partitionKey or $sortKey, Dynamite will pass them to DB directly.
* But when passing an array, it will build an key using data from SortKeyFormat and PrimaryKeyFormat annotations.
* Remember to pass all "fragments" of key in array in given format:
* But when passing an array, it will build an key using data from SortKeyFormat and PrimaryKeyFormat attributes.
* Remember to pass all fragments of key in array in given format:
* [ fieldName => value ].
*
* @param array<string, string>|string $partitionKey
Expand Down
5 changes: 5 additions & 0 deletions src/Dynamite/ItemSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Dynamite;

use Aws\DynamoDb\SetValue;
use Dynamite\Configuration\Attribute;
use Dynamite\Configuration\NestedItemAttribute;
use Dynamite\Configuration\NestedValueObjectAttribute;
Expand Down Expand Up @@ -135,6 +136,10 @@ public function hydrateObject(string $className, ItemMapping $itemMapping, array
}
}

if($propValue instanceof SetValue) {
$propValue = $propValue->toArray();
}

$propertyReflection->setValue($instantiatedObject, $propValue);
continue;
}
Expand Down
Loading
Loading