Dumping templates with Marshal

138 views
Skip to first unread message

Nicolas Hock Isaza

unread,
Sep 4, 2012, 11:08:13 PM9/4/12
to liquid-t...@googlegroups.com
Hello, I'm having a problem when saving the `Marshal.dump` on the DB and recovering from there. I'm using Postgres and Ruby 1.9.3.

Here's my code:

Part has a `liquid_source` and a `template_source`. The liquid column is the actual text with the liquid template. The other one is the compiled (dumped) source.

So, I have this:

p = Part.first
p.liquid_source = "Hello {{name}}"
p.liquid_source.encoding => #<Encoding:UTF-8>

p.template_source = Marshal.dump(Liquid::Template.parse(p.liquid_source)).force_encoding('UTF-8')
=> "\u0004\bo:\u0015Liquid::Template\u0006:\n@rooto:\u0015Liquid::Document\u0006:\u000E@nodelist[\aI\"\vHello \u0006:\u0006ETo:\u0015Liquid::Variable\b:\f@markupI\"\tname\u0006;\tT:\n@nameI\"\tname\u0006;\tT:\r@filters[\u0000"

# Notice the `\u0000` at the end


If I save the part (returns true) it won't save the `\u000` part leaving the dump incomplete. So when I try to restore it:

Marshal.load(p.reload.template_source)

  Part Load (1.0ms) SELECT "parts".* FROM "parts" WHERE "parts"."id" = $1 LIMIT 1 [["id", 1]]
ArgumentError: marshal data too short
  from /Users/tesla/code/templates/app/models/part.rb:9:in `load'
from /Users/tesla/code/templates/app/models/part.rb:9:in `template'
from (irb):24
from /Users/tesla/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
  from /Users/tesla/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
from /Users/tesla/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
  from script/rails:6:in `<main>'

The 'whole' example is here: https://gist.github.com/3629583

I honestly don't know if the bug is in Liquid/Marshal or the Pg adapter. I noticed the last \u0000 is missing at the end (and if added the load works). Since that's the 'nil' char Postgres will probably just not allow it (I have no idea though).

Thank you very much
Reply all
Reply to author
Forward
0 new messages