Expand URI templates with Burrito

栏目: IT技术 · 发布时间: 5年前

RFC 6570 defines a URI template as “a compact sequence of characters for describing a range of Uniform Resource Identifiers through variable expansion.” Burrito is a Haskell library that lets you parse and expand those templates. Here’s a short example to show how it works:

>>> import Burrito
>>> let Just template = parse "http://user.example/~{name}"
>>> expand [("name", stringValue "fred")] template
"http://user.example/~fred"
>>> expand [("name", stringValue "mark")] template
"http://user.example/~mark"

Motivation

Two Haskell libraries already exist for working with URI templates. Why did I bother writing Burrito?

  • uri-template was uploaded before the RFC was published. It hasn’t been updated since.

  • uri-templater implements the RFC. Unfortunately it uses a bunch of dependencies and it doesn’t appear to be actively maintained.

These were a problem for me because I use URI templating in my github-release package. I want github-release to be on Stackage, so it needs to build with the latest versions of its dependencies. I could’ve forked either of the above packages and brought them up to date, but the RFC wasn’t too intimidating so I decided to start from scratch.

Testing

Turns out the RFC was more complicated than I expected. In order to make sure I got everything right, I ended up with 902 test cases . In fact this is what took the bulk of the time developing the library.

Having these tests give me a high degree of confidence that I’m faithfully implementing the RFC. They also allowed me to easily refactor my code after getting everything to work the first time. Believe me, the first implementation was not pretty to look at.

I would’ve liked to implement some property-based tests for this, but I couldn’t think of any interesting properties to write. If you know of some, please let me know!

Naming

I’m surprised that the name “burrito” wasn’t taken on Hackage already. Burritos are commonly used when writing monad tutorials, or at least that’s how the jokes go.

Anyway I chose the name “burrito” because I like to pick project names that have associated emoji: :burrito:. Also “burrito” has all the right letters (U, R, I, and T) in the right order.

Conclusion

If you find yourself needing to expand URI templates in Haskell, please consider using Burrito . I’ve tried hard to make it work correctly and be easy to use. Be sure to open an issue if something can be improved!


以上所述就是小编给大家介绍的《Expand URI templates with Burrito》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Head First PHP & MySQL(中文版)

Head First PHP & MySQL(中文版)

Lynn Beighley、Michael Morrison / 苏金国、徐阳 / 中国电力 / 2010-6 / 98.00元

通过《深入浅出PHP&MySQL(影印版)》,你将学习:准备好把你的静态HTML网页提升到下一个层次并使用PHP和MySQL建立数据库驱动的网站了吗?《深入浅出PHP& MysQL》是一本快捷实用的指南,让你的动态网站快速运行。自己动手建立实际应用程序,从视频游戏高分留言板到在线交友网站。当你完成后,你将可以进行验证表单、使用会话ID和cookies工作、执行数据库查询和联接、处理文件I/0操作等......一起来看看 《Head First PHP & MySQL(中文版)》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

SHA 加密
SHA 加密

SHA 加密工具