Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It seems that there are two approaches to schema encoding:

* Writing OpenAPI/Avro and then generating deserializing/serializing from that (like e.g. Avro[0] or Tie[1] or Typical)

* Writing the schema using an in-language DSL (e.g. Autodocodec[2])

If I have a single language codebase, why should I prefer the first approach? You can always make your in-language DSL serialize out to a dedicated language at a later point.

Typical isn't focused on JSON, so it doesn't seem like it is optimized for web. Not doing web makes it more likely that you don't need multiple language support.

You can limit the metaprogramming also: You don't strictly need GHC.Generics for the in-language DSL. But if you're generating code, it's always going to be opaque and hard to debug.

If you keep the DSL in-language, you don't need to generate stubs since you can use the languages own type system to enforce the mapping to the native records[2].

I have heard the argument that everything should be 'documentation first', which was given as an argument for using Tie. But I don't see why an in-language DSL can't provide enough detail. There is so much manually written OpenAPI out there, any of these approaches is vastly better than that.

I have been reading Designing Data Intensive Applications by Martin Kleppmann but it doesn't cover this trade-off. Which makes sense, since it isn't really a book on programming using DSLs.

[0]: https://hackage.haskell.org/package/avro#generating-code-fro...

[1]: https://github.com/scarf-sh/tie

[2]: https://github.com/NorfairKing/autodocodec#fully-featured-ex...



> If I have a single language codebase, why should I prefer the first approach?

Probably the most compelling reason is that a single language codebase might not be a single language codebase forever. But, as you suggested, the switch to a language-agnostic framework can be deferred until it becomes necessary.

However, there's a reason to use Typical specifically: asymmetric fields. This feature allows you to change your schema over time without breaking compatibility and without sacrificing type safety.

If you ever expect to have newer versions of the codebase reading messages that were generated by older versions of the codebase (or vice versa), this is a concern that will need to be addressed. This can happen when you have a system that isn't deployed atomically (e.g., microservices, web/mobile applications, etc.) or when you have persisted messages that can outlive a single version of the codebase (e.g., files or database records).

An embedded DSL could in principle provide asymmetric fields, but I'm not aware of any that do.

> Typical isn't focused on JSON, so it doesn't seem like it is optimized for web.

It just makes different trade-offs than most web-based systems, but that doesn't make it unsuitable for web use. We have comprehensive integration tests that run in the browser. Deserialized messages are simple passive data objects [1] that can be logged or inspected in the developer console.

[1] https://en.wikipedia.org/wiki/Passive_data_structure




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: