Skip to content

Conversation

@rmarronnier
Copy link
Contributor

Purpose

This is a working POC as an alternative solution to #1948 (it's not a configuration per se).

I've added several format as examples and let the md files as documentation (they can be removed, content extracted etc) to show clearly what this would look like.

I find the syntax neat and simple enough. What do you think ?

Checklist

  • - An issue already exists detailing the issue/or feature request that this PR fixes
  • - All specs are formatted with crystal tool format spec src
  • - Inline documentation has been added and/or updated
  • - Lucky builds on docker with ./script/setup
  • - All builds and specs pass on docker with ./script/test

@jwoertink
Copy link
Member

I've starting looking over this one, but there is quite a bit going on, so it's a little difficult to wrap my head around what all is happening. It seems like maybe this is changing how actions are handled? I'd really like to avoid that from changing and just keep this about defining what method doing the serialization for the serializers i.e. JSON serializers use to_json, XML serializers use to_xml or whatever, etc...

I appreciate the PR and stoked to start adding some new stuff! I'll try to finish up an actual review soon! 🙌

@jwoertink
Copy link
Member

I keep thinking about this... I wonder if we can actually make it a lot more simple.... I was looking at this recent PR on Crystal https://github.com/crystal-lang/crystal/pull/15939/files and it got me thinking... What if your serialization was just based on a simple require?

require "lucky/serializable/json"

abstract struct BaseSerializer
  include Lucky::Serializable
end

Then that would just have

module Lucky::Serializable
  def to_json(io)
    render.to_json
  end
end

Then if you wanted your own custom serialization method, you would just open Lucky::Serializable and add your method.

module Lucky::Serializable
  def to_yaml(io)
    render.to_yaml
  end
end

class Data::Index < BrowserAction
  get "/data" do
    serializer = DataSerializer.new(1, 2, 3)
    if yaml?
       yaml(serializer)
     elsif json?
       json(serializer)
     else
       plain_text(serializer.render.to_s)
     end
  end
end

Lucky already has the built-in methods used for handling the action output. Serializers just need to know how to convert your render to some other output.

@rmarronnier
Copy link
Contributor Author

I agree with you :

module Lucky::Serializable
def to_yaml(io)
render.to_yaml
end
end

makes so much sense. What I liked about this PR was especially the render_format macro to be able to easily declare a new render format (and the nice cool way to have foobar @users in the action).
I don't necessarily want, and won't fight for, to have new built-in formats (like yaml or csv) in Lucky::Renderable (it's more like examples of the powerful macro).
Feel free to steal code from this for a new PR to implement #1948

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.

2 participants