8000 [Yaml] Improve YAML boolean escaping by petert82 · Pull Request #13262 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Yaml] Improve YAML boolean escaping #13262

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 6 commits into from
Jan 16, 2015
Merged

Conversation

petert82
Copy link
Contributor
@petert82 petert82 commented Jan 5, 2015
Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #13209
License MIT
Doc PR None

This PR ensures that PHP values which would be interpreted as booleans in older versions of the YAML spec are escaped with single quotes when dumped by the Dumper.

For example, dumping this:

array(
    'country_code' => 'no',
    'speaks_norwegian' => 'y',
    'heating' => 'on',
)

Will produce this YAML:

country_code: 'no'
speaks_norwegian: 'y'
heating: 'on'

- Moves dumping single-quoting logic into Yaml\Escaper
- Ensures that PHP values which would be interpreted as booleans in
  older versions of the YAML spec are escaped with single quotes when
  dumped by the Dumper.
@petert82 petert82 force-pushed the yaml-boolean-escaping branch from 893db8d to 3760e67 Compare January 5, 2015 11:50
@larowlan
Copy link
Contributor
larowlan commented Jan 6, 2015

This will move towards interoperability with the pecl yaml extension (which uses libyaml, which is Yaml 1.1 spec). Which is something Drupal wants to make possible for performance reasons - see https://www.drupal.org/node/1920902 - in our current patch we are using a callback in our parsing for booleans to treat other than true/false as strings. One other issue we've seen between pecl/yaml and symfony/yaml is that - in a sequence is treated as a nested sequence by pecl/yaml but as a string by symfony/yaml - for example http://cgit.drupalcode.org/drupal/tree/core/profiles/standard/config/install/editor.editor.full_html.yml#n15

*/
private static function isValueRequiresSingleQuoting($value)
{
return in_array(strtolower($value), array('null', '~', 'true', 'false', 'y', 'n', 'yes', 'no', 'on', 'off'));
Copy link
Member

Choose a reason for hiding this comment

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

Note to myself and others: even if we do not support 'y', 'n', ... as valid Booleans, it does not hurt to escape them here for interoperability.

Copy link
Member

Choose a reason for hiding this comment

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

I suggest adding it as a comment to avoid removing it by mistake in the future

@stof
Copy link
Member
stof commented Jan 6, 2015

One other issue we've seen between pecl/yaml and symfony/yaml is that - in a sequence is treated as a nested sequence by pecl/yaml but as a string by symfony/yaml

Can you open an issue for that ?

Remove duplicate 'require' for symfony/symfony PR
@larowlan
Copy link
Contributor
larowlan commented Jan 6, 2015

Can you open an issue for that ?

Let me confirm it first, I suspect it might have been hand-edited, but if not - will open

Add comment as requested - see comment for stof
* @param string $value A PHP value
* @return bool True if the value would require single quotes.
*/
private static function isValueRequiresSingleQuoting($value)
Copy link
Member

Choose a reason for hiding this comment

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

doesValueRequireSingleQuoting?

Copy link
Member

Choose a reason for hiding this comment

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

Actually, I'm wondering if we really need those 2 private methods. What about inlining them with the right comments instead? Especially as they can be called quite a lot.

@fabpot
Copy link
Member
fabpot commented Jan 16, 2015

👍 after my small comment is addressed.

@petert82 petert82 force-pushed the yaml-boolean-escaping branch from 29dc388 to 8fa056b Compare January 16, 2015 13:15
@petert82
Copy link
Contributor Author

@fabpot They're gone. :)

@fabpot
Copy link
Member
fabpot commented Jan 16, 2015

Thank you @petert82.

@fabpot fabpot merged commit 8fa056b into symfony:2.3 Jan 16, 2015
fabpot added a commit that referenced this pull request Jan 16, 2015
This PR was merged into the 2.3 branch.

Discussion
----------

[Yaml] Improve YAML boolean escaping

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #13209
| License       | MIT
| Doc PR        | None

This PR ensures that PHP [values which would be interpreted as booleans][1] in older versions of the YAML spec are escaped with single quotes when dumped by the Dumper.

For example, dumping this:

```php
array(
    'country_code' => 'no',
    'speaks_norwegian' => 'y',
    'heating' => 'on',
)
```

Will produce this YAML:

```yaml
country_code: 'no'
speaks_norwegian: 'y'
heating: 'on'
```

[1]: http://yaml.org/type/bool.html

Commits
-------

8fa056b Inline private 'is quoting required' methods in Escaper
afe827a Merge pull request #2 from larowlan/patch-2
a0ec0fe Add comment as requested
1e0633e Merge pull request #1 from larowlan/patch-1
81a8090 Remove duplicate 'require'
3760e67 [Yaml] Improve YAML boolean escaping
@petert82 petert82 deleted the yaml-boolean-escaping branch January 16, 2015 14:09

// Determines if a PHP value is entirely composed of a value that would
// require single quoting in YAML.
return in_array(strtolower($value), array('null', '~', 'true', 'false', 'y', 'n', 'yes', 'no', 'on', 'off'));
Copy link
Member

Choose a reason for hiding this comment

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

I would do the in_array check before the regex check. It is likely to be faster

fabpot added a commit that referenced this pull request Jan 16, 2015
… (xabbuh)

This PR was merged into the 2.3 branch.

Discussion
----------

[Yaml] execute cheaper checks before more expensive ones

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

Minor improvements to the checks as suggested by @stof in #13262.

Commits
-------

cd4349d execute cheaper checks before more expensive ones
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0