Skip to content

[Serializer] Allow to pass csv encoder options in context #22537

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 1 commit into from
Apr 29, 2017
Merged

[Serializer] Allow to pass csv encoder options in context #22537

merged 1 commit into from
Apr 29, 2017

Conversation

ogizanagi
Copy link
Contributor

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets N/A
License MIT
Doc PR N/A

CSV contents typically are provided by one or many third-parties, not always allowing you to get control over the provided format. In case you need to import csv files with different formats, either you have to instantiate a decoder yourself/inject it instead of the main serializer instance, either you need another serializer instance with a differently configured csv encoder registered within.

This PR allows to configure any encoder option through the context, so you can keep injecting and using the same serializer instance.

} else {
$result[$parentKey.$key] = $value;
}
}
}

/**
* @param array $context
Copy link
Member

Choose a reason for hiding this comment

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

useless, just repeats the signature

@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Apr 26, 2017
@@ -21,6 +21,10 @@
class CsvEncoder implements EncoderInterface, DecoderInterface
{
const FORMAT = 'csv';
const DELIMITER_KEY = 'csv_delimiter';
Copy link
Member

Choose a reason for hiding this comment

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

Just asking: is there any reason to name these constants *_KEY instead of *_CHAR? I was checking the popular CSV library published by ThePhpLeague and they say that the separator, enclosure, etc. can only be 1 char: https://github.com/thephpleague/csv/blob/master/src/AbstractCsv.php#L42

Copy link
Contributor Author

@ogizanagi ogizanagi Apr 27, 2017

Choose a reason for hiding this comment

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

Actually the KEY suffix is not about csv at all. It's about the constant representing the key of the option to pass in the context argument. See DateTimeNormalizer::FORMAT_KEY for reference. 😄

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for my confusion!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps your confusion is legit and the naming should be more explicit though. I named it after DateTimeNormalizer::FORMAT_KEY, but that's currently the only occurence of such a suffixed constant in the serializer component. Other constants for options like in the AbstractNormalizer are simply named after the option itself, without any suffix.

Also in a different topic, some options like allow_extra_attributes, attributes, or key_type don't even have any constant. Maybe it's worth adding them and always ask for constants for new options in the future?

ping @dunglas.

Copy link
Member

Choose a reason for hiding this comment

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

Also in a different topic, some options like allow_extra_attributes, attributes, or key_type don't even have any constant. Maybe it's worth adding them and always ask for constants for new options in the future?

I definitely agree.

@fabpot
Copy link
Member

fabpot commented Apr 29, 2017

Thank you @ogizanagi.

@fabpot fabpot merged commit 10a76aa into symfony:master Apr 29, 2017
fabpot added a commit that referenced this pull request Apr 29, 2017
…ext (ogizanagi)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[Serializer] Allow to pass csv encoder options in context

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | N/A
| License       | MIT
| Doc PR        | N/A

CSV contents typically are provided by one or many third-parties, not always allowing you to get control over the provided format. In case you need to import csv files with different formats, either you have to instantiate a decoder yourself/inject it instead of the main serializer instance, either you need another serializer instance with a differently configured csv encoder registered within.

This PR allows to configure any encoder option through the context, so you can keep injecting and using the same serializer instance.

Commits
-------

10a76aa [Serializer] Allow to pass csv encoder options in context
fabpot added a commit that referenced this pull request Apr 29, 2017
…gizanagi)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[Serializer] Add missing normalizer options constants

| Q             | A
| ------------- | ---
| Branch?       | master (3.3)
| Bug fix?      | not really
| New feature?  | yesish, but for 3.3 as those options were added on this branch and not released yet
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #22537 (comment)
| License       | MIT
| Doc PR        | N/A

As seen in #22537 (comment).

@dunglas : I'm not sure about the exposing the `key_type` option as a constant in `ArrayDenormalizer`/`AbstractObjectNormalizer`, as it looks more or less like a detail of the AbstractObjectNormalizer implementation, but anyway it should be in 3.2 if we add it, so I haven't included it here.
However, I wonder if this option shouldn't directly accept a string too, rather than just a `Symfony\Component\PropertyInfo\Type` instance if we want to consider this option "public"?

Commits
-------

b0c414f [Serializer] Add missing normalizer options constants
symfony-splitter pushed a commit to symfony/serializer that referenced this pull request Apr 29, 2017
…gizanagi)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[Serializer] Add missing normalizer options constants

| Q             | A
| ------------- | ---
| Branch?       | master (3.3)
| Bug fix?      | not really
| New feature?  | yesish, but for 3.3 as those options were added on this branch and not released yet
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | symfony/symfony#22537 (comment)
| License       | MIT
| Doc PR        | N/A

As seen in symfony/symfony#22537 (comment).

@dunglas : I'm not sure about the exposing the `key_type` option as a constant in `ArrayDenormalizer`/`AbstractObjectNormalizer`, as it looks more or less like a detail of the AbstractObjectNormalizer implementation, but anyway it should be in 3.2 if we add it, so I haven't included it here.
However, I wonder if this option shouldn't directly accept a string too, rather than just a `Symfony\Component\PropertyInfo\Type` instance if we want to consider this option "public"?

Commits
-------

b0c414f2c8 [Serializer] Add missing normalizer options constants
@ogizanagi ogizanagi deleted the feature/serializer/csv_encoder_context_options branch April 29, 2017 16:01
@stof stof modified the milestones: 3.3, 3.4 Apr 30, 2017
@fabpot fabpot mentioned this pull request May 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants