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

查看所有标签

猜你喜欢:

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

遗传算法原理及应用

遗传算法原理及应用

周明、孙树栋 / 国防工业出版社 / 1999-6 / 18.0

一起来看看 《遗传算法原理及应用》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具