Skip to content

Instantly share code, notes, and snippets.

@ybur-yug
Last active August 29, 2015 14:23
Show Gist options
  • Save ybur-yug/700e4803f52e08ae84d8 to your computer and use it in GitHub Desktop.
Save ybur-yug/700e4803f52e08ae84d8 to your computer and use it in GitHub Desktop.

seriously im blanking so hard on this, check it:

2.2.1 :001 > require 'gkv'
 => true 
2.2.1 :002 > db = Gkv::Database.new
 => #<Gkv::Database:0x000000016395d8> 
2.2.1 :003 > db.set('test', {key: 'val'})
 => "test" 
2.2.1 :004 > db.get('test')
Psych::SyntaxError: (<unknown>): did not find expected node content while parsing a flow node at line 1 column 2
    from /home/bobby/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:370:in `parse'
    from /home/bobby/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:370:in `parse_stream'
    from /home/bobby/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:318:in `parse'
    from /home/bobby/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:245:in `load'
    from /home/bobby/.rvm/gems/ruby-2.2.1/gems/gkv-0.2.1/lib/gkv/database.rb:21:in `get'
    from (irb):4
    from /home/bobby/.rvm/rubies/ruby-2.2.1/bin/irb:11:in `<main>'
2.2.1 :005 > db.set('test2', "{key: 'val'}")
 => "test2" 
2.2.1 :006 > db.get('test2')
 => {"key"=>"val"} 
2.2.1 :007 > db.get('test2').class
 => Hash

Using the old hash syntax:

2.2.1 :005 > db.set('a', {:key => 'value'})
 => "a" 
2.2.1 :006 > db.get('a')
Psych::SyntaxError: (<unknown>): did not find expected node content while parsing a flow node at line 1 column 2
	from /home/bobby/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:370:in `parse'
	from /home/bobby/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:370:in `parse_stream'
	from /home/bobby/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:318:in `parse'
	from /home/bobby/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:245:in `load'
	from /home/bobby/.rvm/gems/ruby-2.2.1/gems/gkv-0.2.1/lib/gkv/database.rb:21:in `get'
	from (irb):6
	from /home/bobby/.rvm/rubies/ruby-2.2.1/bin/irb:11:in `<main>'

I'm loading it as strings into YAML's lib:

if we set it as a hash that is a string, it reads out as a hash properly BUT if we input the hash as an actual hash it breaks with that Psych error EVEN THOUGH i coerce all the input to strings from the source:

...
    def get(key)
      if $ITEMS.keys.include? key
        YAML.load(Gkv::GitFunctions.cat_file($ITEMS[key].last))
      else
        raise KeyError
      end
    end

    def set(key, value)
      update_items(key, value.to_s)
      key
    end
...

and update items:

...    
  def update_items(key, value)
      if $ITEMS.keys.include? key
        history = $ITEMS[key]
        history << Gkv::GitFunctions.hash_object(value.to_s)
        $ITEMS[key] = history
      else
        $ITEMS[key] = [Gkv::GitFunctions.hash_object(value.to_s)]
      end
    end
  end
...

meh

@dustinbrownman
Copy link

I think it's a Psych error. I just ran it locally and it works fine... (though it outputs as a string) Here's what I did:

irb(main):001:0> require 'gkv'
=> true
irb(main):002:0> db = Gkv::Database.new
=> #<Gkv::Database:0x007fa5a11f5100>
irb(main):003:0> db.set('test', {key: 'val'})
=> {:key=>"val"}
irb(main):004:0> db.get('test')
=> "{:key=>\"val\"}"
irb(main):005:0> db.get('test').class
=> String
irb(main):006:0> 

¯\ (ツ)

@ybur-yug
Copy link
Author

I have not released the gem onto rubygems in the latest capacity. Did you build it from source on master @dustinbrownman?

@ybur-yug
Copy link
Author

it returns the key without coersion so I think you might be on an older version, could be wrong though

@ybur-yug
Copy link
Author

It also returns the key and not value now which leads me to believe this is probably the case (just remembered this change)

@dustinbrownman
Copy link

Ah, I cloned and thought I had installed it from that repo, but I guess not...I'll try again.

@dustinbrownman
Copy link

So yeah, building the gem is an important step, haha.

I tried it out and got the same error. Looking more into it.

@ybur-yug
Copy link
Author

I think maybe the object ID may change and somehow break it with the multiple coercions? I'm not sure at all....I even to_s'd ALL THE THINGS and got nothing. But if I input a raw string....bam magic it outputs as a dick. #dynamictypeproblemz

@ybur-yug
Copy link
Author

PS the test in master right now passes a string so that it passes. Was doing it in an experiment. But yeah. Rspec after clone can give you a sanbox

@dustinbrownman
Copy link

Okay so I use pry to look at what's happening in that Gkv::Database#get method and here's what I saw:

[11] pry(#<Gkv::Database>)> $ITEMS
=> {"test"=>["d6a24399cce1029078defdf413699a8b604e5854"]}
[12] pry(#<Gkv::Database>)> $ITEMS[key]
=> ["d6a24399cce1029078defdf413699a8b604e5854"]
[13] pry(#<Gkv::Database>)> $ITEMS[key].last
=> "d6a24399cce1029078defdf413699a8b604e5854"
[14] pry(#<Gkv::Database>)> Gkv::GitFunctions.cat_file($ITEMS[key].last)
=> "{:key=>\"value\"}"

Notice how that string/hash is returned using the hash rocket syntax. I copied that string and passed it to YAML.load and that's where the Psych error kicked in.

[15] pry(#<Gkv::Database>)> YAML.load("{:key=>\"value\"}")
Psych::SyntaxError: (<unknown>): did not find expected node content while parsing a flow node at line 1 column 2
from /Users/dustin/.rubies/ruby-2.2.0/lib/ruby/2.2.0/psych.rb:370:in `parse'

So I changed the string to reflect the symbolly hash, and here's what I got:

[20] pry(#<Gkv::Database>)> YAML.load("{key: \"value\"}")
=> {"key"=>"value"}

Strange stuff. To really understand what's happening, I'd need to dig into exactly what gets written in that temp file, but I think this is all I have for tonight. I hope this helps get things rolling and isn't just info you already knew :)

@ybur-yug
Copy link
Author

I fucking love you Dustin. I did the same but thought the lexer/parser would be able to interchangeably handle them. Complete oversight. I can now handle this, I think. But its almost 4am here so I will sleep before I do this. Lol

@ybur-yug
Copy link
Author

Also on the note of the tempfile its whatever string is inputted...I use the tempfile because in that manner I dont have to deal with system perms etc. Its not a 'true' tempfile (ie not the TempFile class in the std lib) but the 2 preconceptions of this tool is A) you have a git repo and B) you have rw perms so that is why I keep it local (this could be a horrible decision or a good one, I dont know. I just did it this way because it worked. I had initially tried to pipe it in with --stdin but it worked in irb and not in the gem so I did this as a workaround)

@ybur-yug
Copy link
Author

...I've been nerd-sniped by a damned hashrocket.

@ybur-yug
Copy link
Author

PS pretty pl0x free an hour of your life within the next month to pair with me on something random

@dustinbrownman
Copy link

Damn hashrockets! And I will!

@ybur-yug
Copy link
Author

I added a note using the hashrocket syntax and i/o @dustinbrownman

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