Skip to content

Avoid direct coupling to BlockBody instances for liquid-c replacement#1289

Merged
dylanahsmith merged 4 commits into
masterfrom
refactor-for-c-block-body
Sep 11, 2020
Merged

Avoid direct coupling to BlockBody instances for liquid-c replacement#1289
dylanahsmith merged 4 commits into
masterfrom
refactor-for-c-block-body

Conversation

@dylanahsmith
Copy link
Copy Markdown
Contributor

@dylanahsmith dylanahsmith commented Sep 8, 2020

Problem

We want to use a C data structure for the block body in liquid-c (Shopify/liquid-c#58) that will be exposed to ruby as Liquid::C::BlockBody, but currently Liquid::Document is directly coupled to Liquid::BlockBody. There are also several places where BlockBody.new is used directly, which doesn't provide a clean way to override in liquid-c to use Liquid::C::BlockBody. instead.

Solution

The first commit adds Liquid::Block#new_body that just returns BlockBody.new so that it can be overridden in liquid-c to return Liquid::C::BlockBody.new.

The second commit exposes some Liquid::BlockBody instance methods as class methods (raise_missing_tag_terminator, raise_missing_variable_terminator, render_node), since they don't depend on self and can be re-used for simplicity in liquid-c.

The third commit changes Liquid::Document to not inherit from Liquid::BlockBody, but instead to store a Liquid::BlockBody. It also creates the block body using a #new_body method that can be overridden by liquid-c.

This way liquid-c can return a body of a different class that wraps
a C implementation.
So they can be used from a Liquid::C::BlockBody
This way liquid-c can more cleanly use a Liquid::C::BlockBody object
for the block body by overriding Liquid::Document#new_body.
@dylanahsmith dylanahsmith force-pushed the refactor-for-c-block-body branch from e146906 to dfbbf87 Compare September 8, 2020 18:01
Comment thread lib/liquid/document.rb

def new_body
Liquid::BlockBody.new
end
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This repeats the implementation in Liquid::Block. If the two are always intended to do the same thing / be overridden together, how about we expose a single implementation somewhere higher up? (for example, Liquid.new_block_body) This way Liquid C only needs to override one method.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed a commit that extracted the implementation to override into ParseContext#new_block_body. I'll update the liquid-c PR to override that.

@dylanahsmith dylanahsmith merged commit 95ff059 into master Sep 11, 2020
@dylanahsmith dylanahsmith deleted the refactor-for-c-block-body branch September 11, 2020 13:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants