Expand URI templates with Burrito

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

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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

数字民主的迷思

数字民主的迷思

[美] 马修·辛德曼 / 唐杰 / 中国政法大学出版社 / 2015-12-25 / CNY 39.00

马修·辛德曼著的《数字民主的迷思》主要讨论互联网对美国政治的影响,聚焦的是“民主化”这一课题。针对公众关于网络民主的美好想象与过分狂热,它通过对在线竞选、链接结构、流量模式、搜索引擎使用、博客与博主、内容生产的“规模经济”等主题的深入处理,借助大量数据图表与分析,勾勒出互联网政治的种种局限性。尤其表明,网络政治信息仍然为一小群精英与机构所创造和过滤,在网络的每一个层次和领域都仍然遵循着“赢家通吃”......一起来看看 《数字民主的迷思》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具