Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doctype encoder #53

Merged
merged 5 commits into from
Nov 22, 2022
Merged

Add doctype encoder #53

merged 5 commits into from
Nov 22, 2022

Conversation

mabasic
Copy link
Contributor

@mabasic mabasic commented Sep 29, 2022

@armanbilge As you can see there is duplicate code, but I don't currently know how to fix that ...

Fixes: #44

I'm not really happy with this code. It should work, but it seems clunky. The http4s documentation on encoders is just terrible :( Why can't I just tell http4s that if it receives this type or this type do to this. How does it even get the function which matches. So many unknowns here.

@armanbilge
Copy link
Member

The http4s documentation on encoders is just terrible :( Why can't I just tell http4s that if it receives this type or this type do to this. How does it even get the function which matches. So many unknowns here.

If I remember correctly, are you relatively new to Scala? This works based on "implicits" (also known as "contextual abstractions" in Scala 3). The http4s documentations probably assumes some familiarity with the concept.

Long story short, it is able to choose the function automatically by matching the types. But this is a Scala technique—not specific to http4s.


Regarding the duplication, if you really want to avoid it I believe that both doctype and Fragment implement the geny.Writable interface. So if you write an encoder based on that common interface, then they can both use it.

https://github.com/com-lihaoyi/geny#writable

@mabasic
Copy link
Contributor Author

mabasic commented Sep 29, 2022

I'm very fresh to Scala. I've only started learning Scala 3 maybe a month ago. This is what I am working on: https://github.com/mabasic/izradaweba

This works based on "implicits" (also known as "contextual abstractions" in Scala 3). The http4s documentations probably assumes some familiarity with the concept.

I haven't yet come to that part in the Scala 3 book :). Reading the http4s documentation feels like reading an alien language, but the code produced seems very logical.

Regarding the duplication, if you really want to avoid it I believe that both doctype and Fragment implement the geny.Writable interface. So if you write an encoder based on that common interface, then they can both use it.

I know that doctype implements that interface, but I'm not sure about Frag. Even if they do they don't have the render method which generates a string.

https://github.com/com-lihaoyi/scalatags/search?q=Writable

It would seem that only doctype extends Writable... I really don't know why doctype is not the same as Frag or ConcreteHtmlTag[String] but completely different.

@armanbilge
Copy link
Member

I know that doctype implements that interface, but I'm not sure about Frag. Even if they do they don't have the render method which generates a string.

Damn. Sorry, I must have been confused. Then the duplication seems inevitable. perhaps lets just name the method differently?

@mabasic
Copy link
Contributor Author

mabasic commented Sep 29, 2022

perhaps lets just name the method differently?

Do you have something in mind?

@mabasic mabasic requested a review from armanbilge November 4, 2022 18:21
@mabasic
Copy link
Contributor Author

mabasic commented Nov 4, 2022

@armanbilge I have made the suggested changes and added tests for doctype. Can we merge this and make a release :) ?

P.S. Can I try to rewrite this library using Scala 3 or is there a reason why it is still on Scala 2?

@armanbilge armanbilge changed the title First attempt to implement doctype detection Add doctype encoder Nov 4, 2022
Copy link
Member

@armanbilge armanbilge left a comment

Choose a reason for hiding this comment

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

LGTM, nice work!

P.S. Can I try to rewrite this library using Scala 3 or is there a reason why it is still on Scala 2?

Feel free to make a Scala 3 only fork of the library. The reason it is still on Scala 2 (like most other open source Scala projects) is because it is important to continue supporting users who have not yet updated to Scala 3. This will be the situation for many years.

@armanbilge armanbilge merged commit 7834249 into http4s:series/0.25 Nov 22, 2022
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.

Does not work if doctype is returned instead of Frag
2 participants