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

feat: Add possibility to recursive merge data in Context #307

Conversation

TheoD02
Copy link
Contributor

@TheoD02 TheoD02 commented Mar 3, 2024

Description

Overview

This pull request addresses the need for improved array merging behavior, specifically focusing on preserving nested values while merging arrays.

Examples

defaultContext Function

For example, the defaultContext function is updated to return a Context object with a nested array. The nested key contains an array with a merge key, which in turn contains another array.

This structure is designed to demonstrate the merging behavior of nested arrays.

The replaced key is intended to be replaced, while the value key is expected to be preserved.

#[AsContext(default: true, name: 'my_default')]
function defaultContext(): Context
{
    return new Context([
        'name' => 'my_default',
        'production' => false,
        'foo' => 'bar',
        'nested' => [
            'merge' => [
                'key' => [
                    'value' => 'should keep this',
                    'replaced' => 'should be replaced',
                ],
                'another' => 'should keep',
            ],
            'another' => 'should keep',
        ],
    ]);
}

productionContext Function

The productionContext function is based on the defaultContext function, with the intention of modifying the Context object to reflect a production environment.

In the case of the nested array, the replaced key is replaced with a new value, and a new key is added to the key array.

#[AsContext(name: 'production')]
function productionContext(): Context
{
    return defaultContext()
        ->withData(
            [
                'name' => 'production',
                'production' => true,
                'nested' => [
                    'merge' => [
                        'key' => [
                            'replaced' => 'replaced value',
                            'new' => 'new value',
                        ],
                    ],
                ],
            ],
            recursive: true  // Enable recursive merging for nested arrays
        );
}

Outcome

Actual

In the case of actual behavior, the Context object returned by the productionContext function is as follows (or now with recursive set to false):

[
    'name' => 'production',
    'production' => true,
    'foo' => 'bar',
    'nested' => [
        'merge' => [
            'key' => [
                'replaced' => 'replaced value',
                'new' => 'new value',
            ],
        ],
    ],
]

New Behavior

With the updated behavior, the Context object returned by the productionContext function is as follows, and recursive set to true:

[
    'name' => 'production',
    'production' => true,
    'foo' => 'bar',
    'nested' => [
        'merge' => [
            'key' => [
                'value' => 'should keep this',
                'replaced' => 'replaced value',
                'new' => 'new value',
            ],
            'another' => 'should keep',
        ],
        'another' => 'should keep',
    ],
]

Recursive Merging

Note

Actually the recursive option is by default true.

Is this a good idea to change the default behavior? or better to set false by default and let the user decide to set it to true?

@TheoD02 TheoD02 force-pushed the feat/add_possibility_to_recursive_merge_data_in_context branch 2 times, most recently from 32fc721 to a839027 Compare March 3, 2024 22:06
Copy link
Member

@lyrixx lyrixx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I Like the feature!

src/Context.php Show resolved Hide resolved
Copy link
Member

@pyrech pyrech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (apart the question about array_merge_recursive)

I can't figure what the best default value for $recursive, both are legit in my mind.

Should we throw a warning in case $recursive is given but keepExisting is false ?

@TheoD02
Copy link
Contributor Author

TheoD02 commented Mar 4, 2024

LGTM (apart the question about array_merge_recursive)

I can't figure what the best default value for $recursive, both are legit in my mind.

Should we throw a warning in case $recursive is given but keepExisting is false ?

IDK, I leave it up to you to decide on the potential impact of existing contexts.

Yes, the current implementation doesn't care about the value when $keepExisting is false. A throw seems more coherent

@TheoD02 TheoD02 force-pushed the feat/add_possibility_to_recursive_merge_data_in_context branch 2 times, most recently from b248eed to 8288444 Compare March 4, 2024 12:35
@TheoD02
Copy link
Contributor Author

TheoD02 commented Mar 4, 2024

Rebased and added a conditional check when $recursive is defined to true, and $keepExisting defined to false to warn the user 😄

Copy link
Member

@lyrixx lyrixx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼 thanks

@TheoD02 TheoD02 force-pushed the feat/add_possibility_to_recursive_merge_data_in_context branch from 8288444 to d2c5372 Compare March 4, 2024 14:12
@TheoD02
Copy link
Contributor Author

TheoD02 commented Mar 4, 2024

Done, fixed the changelog conflict, if no more feedback it's ready 😄

Copy link
Member

@pyrech pyrech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 🎉

@pyrech pyrech merged commit 9551726 into jolicode:main Mar 4, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants