Giter Site home page Giter Site logo

state file gets confused about taste-tester HOT 5 OPEN

facebook avatar facebook commented on April 28, 2024
state file gets confused

from taste-tester.

Comments (5)

malmond77 avatar malmond77 commented on April 28, 2024

Now I re-read the code in #116 , I am perplexed. I am not confident that it actually does anything. outside of a minor formatting change, this is assigning state = Mash.new only to be usually overwritten with a hash from JSON.parse.

The mash structure is cool, but I am not sure it helps here. I think a simpler solution to this problem is to flatten the key .to_s in the write method. How does that sound?

from taste-tester.

jaymzh avatar jaymzh commented on April 28, 2024

no, you silly man. That's what mashes do... they treat string and symbol keys as the same thing. That's literally the point of them, so it always collapses them and we only ever write out strings, and always ever read in strings without constantly converting manually.

from taste-tester.

malmond77 avatar malmond77 commented on April 28, 2024

I get that mashes handle keys differently. That's not the issue. The issue I'm raising is that simply assigning a new mash to state before trying to read a json file / falling back on an empty hash. The resulting data type of state is always a hash not a mash as you intended. Example:

$ irb
irb(main):001:0> require 'chef/mash'
=> true
irb(main):002:0> require 'json'
=> true
irb(main):003:0> m = Mash.new
=> {}
irb(main):004:0> m.class
=> Mash
irb(main):005:0> m = JSON.parse(File.read('/data/users/malmond/.taste-tester/prod/ref.json'))
=> {"port"=>5093, "ssl"=>true, "ssh"=>false, "logging"=>true, "bundle"=>"compatible", "ref"=>"bb50da1a52bd0e9e1ab1d03649ed27690445e092", "last_upload_time"=>"2019-08-15 10:24:41"}
irb(main):006:0> m.class
=> Hash
irb(main):007:0> m = {}
=> {}
irb(main):008:0> m.class
=> Hash

I don't think the code in #116 actually does anything. If anything, the solution here is even simpler:

     def write(key, val)
-      merge({ key => val })
+      merge({ key.to_s => val })
     end

This flattens all keys to strings, all the time.

from taste-tester.

jaymzh avatar jaymzh commented on April 28, 2024

Of course it's JSON... but it collapses all the keys. Every time. That's the point of mashes. What's written out is JSON, it's read into a mash, the keys merge properly. You CANNOT find a case with the current code where you'll get a :state and a "state" key, I'll bet money on it.

from taste-tester.

jaymzh avatar jaymzh commented on April 28, 2024
irb(main):002:0> m = Mash.new
=> {}
irb(main):003:0> m[:stuff] = 'bar'
=> "bar"
irb(main):004:0> m.merge!(JSON.parse('{"stuff": "wonk"}'))
=> {"stuff"=>"wonk"}

This is the whole reason that Chef wrote Mash. Let Mash do that for you so you're not doing it yourself.

from taste-tester.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.