-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
Support renaming properties in flattened sub-objects #48
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6c78fa1
Tweak the field usage tracking to allow for custom property names in …
Crell 734d579
Update README to recommend how to handle value objects.
Crell fe3e2ea
Add support for a flattening prefix, which allows using the same chil…
Crell f077f4c
Ensure flatten prefixing works recursively.
Crell e86b61f
Document flattenPrefix and its implications for value objects.
Crell 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 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 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 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 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 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 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,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Crell\Serde\Records\ValueObjects; | ||
|
||
use Crell\Serde\Attributes\Field; | ||
|
||
class Age | ||
{ | ||
public function __construct(#[Field(serializedName: 'age')] public int $value) | ||
{ | ||
if ($this->value < 0) { | ||
throw new \InvalidArgumentException('Age cannot be negative.'); | ||
} | ||
} | ||
} |
This file contains 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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Crell\Serde\Records\ValueObjects; | ||
|
||
use Crell\Serde\Attributes\Field; | ||
|
||
class Email | ||
{ | ||
public function __construct(#[Field(serializedName: 'email')] public string $value) {} | ||
} |
This file contains 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,15 @@ | ||
<?php | ||
|
||
namespace Crell\Serde\Records\ValueObjects; | ||
|
||
use Crell\Serde\Attributes\Field; | ||
|
||
class JobDescription | ||
{ | ||
public function __construct( | ||
#[Field(flatten: true, flattenPrefix: 'min_')] | ||
public Age $minAge, | ||
#[Field(flatten: true, flattenPrefix: 'max_')] | ||
public Age $maxAge, | ||
) {} | ||
} |
This file contains 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,10 @@ | ||
<?php | ||
|
||
namespace Crell\Serde\Records\ValueObjects; | ||
|
||
class JobEntry | ||
{ | ||
public function __construct( | ||
public JobDescription $description, | ||
) {} | ||
} |
This file contains 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,13 @@ | ||
<?php | ||
|
||
namespace Crell\Serde\Records\ValueObjects; | ||
|
||
use Crell\Serde\Attributes\Field; | ||
|
||
class JobEntryFlattened | ||
{ | ||
public function __construct( | ||
#[Field(flatten: true)] | ||
public JobDescription $description, | ||
) {} | ||
} |
This file contains 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,13 @@ | ||
<?php | ||
|
||
namespace Crell\Serde\Records\ValueObjects; | ||
|
||
use Crell\Serde\Attributes\Field; | ||
|
||
class JobEntryFlattenedPrefixed | ||
{ | ||
public function __construct( | ||
#[Field(flatten: true, flattenPrefix: 'desc_')] | ||
public JobDescription $description, | ||
) {} | ||
} |
This file contains 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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Crell\Serde\Records\ValueObjects; | ||
|
||
use Crell\Serde\Attributes\Field; | ||
|
||
class Person | ||
{ | ||
public function __construct( | ||
public string $name, | ||
#[Field(flatten: true)] | ||
public Age $age, | ||
#[Field(flatten: true)] | ||
public Email $email, | ||
) {} | ||
} |
Oops, something went wrong.
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.
This way it (also applicable for
Age
) forces properties to have a specific name, which depending on the context may be ambiguous, or even worse, it may be necessary to have, let's imagine, 2 email objects.Ideally the property name would be dictated by the parent object,
People
in this case, or as a fallback, theserializedName
to be infered there.Another way to put it, is to consider this scenario:
Forcing the
serializedName
on the value object makes this object impossible to deserialize/serialize, as far as I understand.Does my perspective on this make sense?
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.
Hm, yes, it does break down if the value object is used twice. However, I'm not really sure how to have an alternate name injected from the parent.
Part of the challenge is that there are certain approaches that would only work for a single-property value object, but there's nothing inherent in value objects that makes them single-value only. (You could have a phone number class that stores the area code separately internally, for whatever reason.) So any solution would need to handle that, too.
Which suggests, maybe using
renameWith: new Prefix(...)
on the parent class. But then the question is how that interacts with the names in the value object. I'm not sure what interaction there makes sense without some completely separate pathway.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.
I'm delighted to see this PR.
But as already noted before, setting the field's name into the child object might be troublesome. You can't say how the value object will be used.
For example, considering the type Age, a field of that type called
age
could exist, but also aminimumAge
orwhatever
. Also, multiple fields could use the same type, so different names are needed.It is the parent class, then, that should determine the name.
What about a new attribute, as I put on the issue?
In this way, Serde knows how to serialize/hydrate. But this adds an extra dependency, and I prefer Vanilla PHP when possible.
As an alternative, why not use a flag like
simple
?I imagine two possible values:
true
when the child value object uses the default fieldvalue
string
when the child uses a custom field nameOr better, why not merge
flatten
andsimple
behaviours in a single property calledsimplify
?The field name or the
serializedName
might be used as a name.