convert nil to empty string #28

Merged
merged 2 commits into from Feb 17, 2014

Conversation

Projects
None yet
3 participants
Contributor

rjocoleman commented Feb 16, 2014

Currently if a key's value is nil when using TOML::Generator.new(hash).body a undefined method error is thrown: NoMethodError: undefined method 'to_toml' for nil:NilClass

This PR converts supports nil.to_toml by converting it into an empty string.

Collaborator

parkr commented Feb 17, 2014

Can you please show where in the spec we should expect nil to be "" once read in?

Contributor

rjocoleman commented Feb 17, 2014

My interpretation of mojombo/toml#30 is that a key with a nil value should not be present, my rational is that if it's present it should be handled rather than the current error and nil is functionally equivalent to "" in many cases - at least mine.

Perhaps a better solution would be:

other_pairs.each do |pair|
  key, val = pair
  if key.include? '.'
    raise SyntaxError, "Periods are not allowed in keys (failed on key: #{key.inspect})"
  end
  unless val.nil?
    @body += "#{key} = #{format(val)}\n"
  end
end

at https://github.com/jm/toml/blob/master/lib/toml/generator.rb#L54

Collaborator

parkr commented Feb 17, 2014

nil is functionally equivalent to "" in many cases

>> if nil
..   print "whee"
.. end
# nothing prints
>> if ""
..   print "whee"
.. end
"whee"

nil and "" don't look functionally equivalent to me. If anything, NilClass#to_toml should return itself. nil is falsey and "" is truthy so we need to respect that difference.

Perhaps a better solution would be ...

In mojombo/toml#30, it looks like mojombo decided a key with no value should be an empty hash table, so we should set the value for keys without any value to be an empty hash table.

Contributor

rjocoleman commented Feb 17, 2014

nil and "" don't look functionally equivalent to me. If anything, NilClass#to_toml should return itself. nil is falsey and "" is truthy so we need to respect that difference.

Agreed, I should have qualified my statement to be limited for uses as configuration where often nil and "" are treated the same. Opinions and differing use cases aside this NilClass#to_toml should be handled per the spec.

In mojombo/toml#30, it looks like mojombo decided a key with no value should be an empty hash table, so we should set the value for keys without any value to be an empty hash table.

It was said that an empty key group should be an empty hash table. This term is defined in v0.1.0 and redefined as tables in v0.2.0

My following of the thread is still that a key with no value is omitted.

I've updated my PR to handle these.

require 'toml'

hash = {
  "bar" => nil,
  "foo" => {}
}

doc = TOML::Generator.new(hash).body
# => "\n[foo]\n"
Collaborator

parkr commented Feb 17, 2014

Ok, cool. Thanks! 👍

Can you please write a quick test for this? Thank you!

Contributor

rjocoleman commented Feb 17, 2014

I've added the empty table and a nil value to the hash in test/test_generator.rb and then removed the nil value from the expected result..
I believe this covers it but I don't use minitest so I could be missing something.

Collaborator

parkr commented Feb 17, 2014

Awesome! LGTM. @jm?

Owner

jm commented Feb 17, 2014

👍 Looks good to me!

@parkr parkr added a commit that referenced this pull request Feb 17, 2014

@parkr parkr Merge pull request #28 from rjocoleman/nil
convert nil to empty string
deeb624

@parkr parkr merged commit deeb624 into jm:master Feb 17, 2014

parkr added this to the 0.2.0 milestone Feb 17, 2014

parkr referenced this pull request Feb 17, 2014

Merged

Release v0.1.1 #29

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment