Expand URI templates with Burrito

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

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

查看所有标签

猜你喜欢:

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

编程真好玩

编程真好玩

[英] 乔恩·伍德科克 / 余宙华 / 南海出版公司 / 2017-8-1 / 88.00元

在美国,编程已进入幼儿园和中小学课堂,是备受欢迎的课程之一。 在英国,编程被列入国家教学大纲,成为6~15岁孩子的必修课。 在芬兰,编程理念融入了小学的各门课程,孩子们可以随时随地学编程。 编程已经成为世界的通用语言,和听、说、读、写、算一样,是孩子必须掌握的技能。 Scratch是美国麻省理工学院设计开发的可视化少儿编程工具,全球1500多万孩子正在学习使用。它把枯燥乏味......一起来看看 《编程真好玩》 这本书的介绍吧!

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

在线压缩/解压 JS 代码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具