magynhard/yaml_extend

ext_load_file mutates/removes some falsey keys

Closed this issue · 5 comments

Please see the failing spec here: #13

Hi @patrickberkeley,

thank you for your contribution.

At first I thought, that there may be a bug, but the YAML library itself has the same behaviour, so it happend not by my extending gem.

After some research I found out, that your described behaviour is indeed a part of the official YAML specification:
https://yaml.org/type/bool.html

All of the following key words are transformed into booleans:

YAML 1.1 specification

 y|Y|yes|Yes|YES|n|N|no|No|NO
|true|True|TRUE|false|False|FALSE
|on|On|ON|off|Off|OFF

YAML 1.2 specification:

true | True | TRUE | false | False | FALSE

Ruby YAML apparently implements the 1.1 specification.

If you want to use keys or values with these key words not to be transformed into their boolean values, you have to quote them.

I just tried an simple example in IRB:

require 'yaml'
require 'yaml_extend'

YAML.load 'no: so'
# => {false=>"so"} 

File.write "t.yml","no: so"
# => 6

YAML.load_file "t.yml"
# => {false=>"so"} 

YAML.ext_load_file "t.yml"
# => {false=>"so"}

I did not recognize your description, but maybe you have another constellation where a error may happen. Even a simple sample with inheritance worked fine.

You are invited to give an example that does not work in your case, then i will see what I can do for you. 👍🏻

@magynhard you are correct. The issue I'm seeing is if I have multiple keys that are evaluated as false, the last one wins and the previous ones don't appear in the output e.g.,

require 'yaml'

YAML.load '{ no: "no", false: "false" }'
# => {false=>"false"} 

YAML.load '{ false: "false", no: "no" }'
# => {false=>"no"} 

This is normal behavior, but the whole yaml-changing-keys thing is new to me.

@patrickberkeley this behaviour of YAML was also new to me, to thanks for inspiring me. 👍🏻