procore-oss / blueprinter

Simple, Fast, and Declarative Serialization Library for Ruby
MIT License
1.14k stars 109 forks source link

Allow a V2 view to specify its parent, regardless of how it's nested #487

Closed jhollinger closed 2 days ago

jhollinger commented 2 days ago

I've been thinking about an "impendance mismatch" between V1 and V2 that will make upgrading more difficult (whether manual or scripted). This is an attempt to patch over a common case. I'm not 100% sold on it myself - I'd like to keep the DSL as simple as possible. Feedback welcome.

Consider the following V1 Blueprint:

class WidgetBlueprint < Blueprinter::Base
  view :normal do
    field :name
  end

  view :extended do
    include_view :normal
    field :description
  end
end

There are currently two ways to represent this in V2: inheritance and partials.

# Inheritance has the drawback that the extended view's name will change to
# "normal.extended". This will require callsites to be updated with the new name. 
class WidgetBlueprint < Blueprinter::V2::Base
  view :normal do
    field :name

    view :extended do
      field :description
    end
  end
end

# Using partials avoids that, but looks...not great. A converted codebase would be
# full of Blueprints defined like this:
class WidgetBlueprint < Blueprinter::V2::Base
  partial :normal do
    field :name
  end

  view :normal do
    use :normal
  end

  view :extended do
    use :normal
    field :description
  end
end

What this PR adds

This PR allows a view to specify which view it should inherit from, without affecting the name:

class WidgetBlueprint < Blueprinter::V2::Base
  view :normal do
    field :name
  end

  view :extended, inherit: :normal do
    field :description
  end
end

Maybe a better idea??

Note that the above doesn't address cases where V1 views inherit from multiple views. Those would still need to resort to partials. What if every view implicitly defined a partial for itself? 🤯 I think that would solve everything. Much simpler to implement, too.

jhollinger commented 2 days ago

Dropped in favor of https://github.com/procore-oss/blueprinter/pull/488.