mustangostang / spyc

A simple YAML loader/dumper class for PHP
MIT License
706 stars 207 forks source link

Coorce empty map values to null #35

Open dergachev opened 10 years ago

dergachev commented 10 years ago

I'm not a yaml expert, but it seems that there might be a bug in handling of empty map values, eg:


---
1: a
2: 

Here's how spyc handles it when there's tilde (by correctly print NULL):

INPUT: 

---
1: a
2: ~

RESULT:

array(2) {
  [1]=>
  string(1) "a"
  [2]=>
  NULL
}

Here's how spyc handles empty map value, should print NULL, prints empty string:

INPUT:

---
1: a
2:

RESULT:

array(2) {
  [1]=>
  string(1) "a"
  [2]=>
  string(0) ""
}

php source code: https://gist.github.com/dergachev/c2b3c18d65edf00aa8fd

I haven't read the yaml spec, I'm quite sure libyaml behaves differently. And here's a random stack overflow thread that seems to confirm it: http://stackoverflow.com/questions/833927/yaml-dictionary-with-empty-value

dergachev commented 10 years ago

Potentially related: https://github.com/mustangostang/spyc/issues/20 https://github.com/mustangostang/spyc/commit/e1d6159fddab2e22fbfec54e491c3c232f2a30ce

dergachev commented 10 years ago

Sorry had typos in original bug description. Fixed now, let me know if it's not clear.

jackmcdade commented 7 years ago

The 1.0 spec is totally ambiguous when it comes to handling an empty map value. There simply is zero mention of it in the entire document. In 1.1 they add the addition of ~ or "empty". I feel like it's a logical solution, since a strict type is more useful than an empty string, but with Spyc behaving this way for so long I'm not sure it's worth changing the behavior as I'm fairly certain any application still relying on it (mine included) as accounted for it.

I would say this is not a bug, but still open to consensus as to how to treat it. And i know this is an old issue. But I'm trying to bring Spyc up to modern glory, since I rely on it every day with Statamic.

christhomas commented 7 years ago

well for example an empty map should be read from the yaml into an empty map and then written back to the file as an empty map.

but if I try this, my empty map is turned into an empty string, which when written back, is an empty string.

the correct result is, that they should remain the same, you should not be able to see any difference.

so either we need a way to tell spyc to do that cause right now it does not, or consider this a bug (which I think it is) then wonder how to fix it.

input file --> read into variable, immediately write into --> output file

there should be no functional difference between input file and output file.

jackmcdade commented 7 years ago

I agree completely.

On Nov 28, 2016, at 10:37 AM, Christopher Thomas notifications@github.com wrote:

well for example an empty map should be read from the yaml into an empty map and then written back to the file as an empty map.

but if I try this, my empty map is turned into an empty string, which when written back, is an empty string.

the correct result is, that they should remain the same, you should not be able to see any difference.

so either we need a way to tell spyc to do that cause right now it does not, or consider this a bug (which I think it is) then wonder how to fix it.

input file <-- read into variable, immediately write into --> output file

there should be no functional difference between input file and output file.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.