<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Passing Curiosity: Posts tagged work</title>
    <link href="https://passingcuriosity.com/tags/work/work.xml" rel="self" />
    <link href="https://passingcuriosity.com" />
    <id>https://passingcuriosity.com/tags/work/work.xml</id>
    <author>
        <name>Thomas Sutton</name>
        
        <email>me@thomas-sutton.id.au</email>
        
    </author>
    <updated>2015-12-29T00:00:00Z</updated>
    <entry>
    <title>Some ScalaCheck generators fail too much</title>
    <link href="https://passingcuriosity.com/2015/scalacheck-discarded-tests/" />
    <id>https://passingcuriosity.com/2015/scalacheck-discarded-tests/</id>
    <published>2015-12-29T00:00:00Z</published>
    <updated>2015-12-29T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>Most of the code I’ll be working with in my new job (BTW blog: I have
a new job) is written in <a href="http://www.scala-lang.org/">Scala</a> and uses property based testing
with <a href="https://scalacheck.org/">ScalaCheck</a>. Yesterday I ran into a problem with an existing
test suite that suddenly began failing with too many discarded tests:</p>
<pre><code>[info] FormattersSpec
[info]   Formatters are invertible for:
[info]     + Mapping
[info]     + Identifier
[info]
[error]     x Metadata
[error]  Gave up after only 39 passed tests. 197 tests were discarded. (FormattersSpec.scala:11)</code></pre>
<p>This test generates random <code>Metadata</code> values and makes sure that they
can be serialised and deserialised correctly (i.e. values can be
round-tripped). The property being test here is identical, only the
<code>Arbitrary</code>, <code>Serialise</code>, and <code>Deserialise</code> instances vary in each
case. The truly odd thing is that the pertinent code looks like this:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode scala"><code class="sourceCode scala"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="cf">case</span> <span class="kw">class</span> <span class="fu">Identifier</span><span class="op">(</span>name<span class="op">:</span> <span class="ex">String</span><span class="op">)</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="cf">case</span> <span class="kw">class</span> <span class="fu">Metadata</span> <span class="op">(</span>id<span class="op">:</span> Identifier<span class="op">,</span> maps<span class="op">:</span> <span class="ex">Set</span><span class="op">[</span>Identifier<span class="op">])</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="kw">implicit</span> <span class="kw">val</span> ArbIdentifier <span class="op">=</span> <span class="fu">Arbitrary</span><span class="op">(</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a>  <span class="cf">for</span> <span class="op">{</span></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a>    name  <span class="op">&lt;-</span> arbitrary<span class="op">[</span><span class="ex">String</span><span class="op">]</span></span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a>  <span class="op">}</span> <span class="cf">yield</span> <span class="fu">Identifier</span><span class="op">(</span>name<span class="op">)</span></span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a><span class="op">)</span></span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a><span class="kw">implicit</span> <span class="kw">val</span> ArbMetadata <span class="op">=</span> <span class="fu">Arbitrary</span><span class="op">(</span></span>
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a>  <span class="cf">for</span> <span class="op">{</span></span>
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a>    identifier <span class="op">&lt;-</span> arbitrary<span class="op">[</span>Identifier<span class="op">]</span></span>
<span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a>    mappings   <span class="op">&lt;-</span> arbitrary<span class="op">[</span><span class="ex">Set</span><span class="op">[</span>Identifier<span class="op">]]</span></span>
<span id="cb2-14"><a href="#cb2-14" aria-hidden="true" tabindex="-1"></a>  <span class="op">}</span> <span class="cf">yield</span> <span class="fu">Metadata</span><span class="op">(</span>identifier<span class="op">,</span> mappings<span class="op">)</span></span>
<span id="cb2-15"><a href="#cb2-15" aria-hidden="true" tabindex="-1"></a><span class="op">)</span></span></code></pre></div>
<p>My first step was redefining a few related <code>Arbitrary</code> instances to
avoid using <code>suchThat</code> (which discards invalid values) but this didn’t
fix the problem. Eventually I tried redefining <code>ArbMetadata</code> like
this:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode scala"><code class="sourceCode scala"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="kw">implicit</span> <span class="kw">val</span> ArbMetadata <span class="op">=</span> <span class="fu">Arbitrary</span><span class="op">(</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>  <span class="cf">for</span> <span class="op">{</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a>    identifier <span class="op">&lt;-</span> arbitrary<span class="op">[</span>Identifier<span class="op">]</span></span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a>    mappings   <span class="op">&lt;-</span> Gen<span class="op">.</span><span class="fu">const</span><span class="op">(</span><span class="ex">Set</span><span class="op">.</span>empty<span class="op">[</span><span class="ex">Set</span><span class="op">[</span>Identifier<span class="op">]])</span></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a>  <span class="op">}</span> <span class="cf">yield</span> <span class="fu">Metadata</span><span class="op">(</span>identifier<span class="op">,</span> mappings<span class="op">)</span></span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a><span class="op">)</span></span></code></pre></div>
<p>and the problem went away. Trying to use <code class="sourceCode scala">arbitrary<span class="op">[</span><span class="ex">Set</span><span class="op">[</span>Identifier<span class="op">]]</span></code>
in various ways in the Scala REPL confirmed that it is the problem; we
can easily generate as large a <code>List[Identifier]</code> as we like, but a
<code>Set[Identifier]</code> fails fairly frequently:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode scala"><code class="sourceCode scala"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="co">// This always generates a Some[List[Identifier]] value.</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>Gen<span class="op">.</span><span class="fu">listOfN</span><span class="op">(</span><span class="dv">100</span><span class="op">,</span> arbitrary<span class="op">[</span>Identifier<span class="op">]).</span><span class="fu">map</span><span class="op">(</span>_<span class="op">.</span>length<span class="op">).</span>sample</span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="co">// Sometimes we get a Some[List[Set[Identifier]]] and others None.</span></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a>Gen<span class="op">.</span><span class="fu">listOfN</span><span class="op">(</span><span class="dv">100</span><span class="op">,</span> arbitrary<span class="op">[</span><span class="ex">Set</span><span class="op">[</span>Identifier<span class="op">]]).</span><span class="fu">map</span><span class="op">(</span>_<span class="op">.</span>length<span class="op">).</span>sample</span></code></pre></div>
<p>It appears as though whatever mechanism is used by <code>arbitrary[Set[_]]</code>
to construct the sets, it doesn’t fails when the generator for the
value type returns duplicate elements. You can confirm this easily by
trying <code>arbitrary[Set[Unit]]</code>; any <code>Gen[Unit]</code> has no choice but to
return a the single value of type <code>Unit</code> (or to fail) and, as
expected, this almost never succeeds. Replacing the problematic
<code>arbitrary[Set[Identifier]]</code> in the original code with
<code class="sourceCode scala">arbitrary<span class="op">[</span><span class="bu">Seq</span><span class="op">[</span>Identifier<span class="op">]].</span><span class="fu">map</span><span class="op">(</span>_<span class="op">.</span>toSet<span class="op">)</span></code> resolves the issue:
constructing a set from a list of possibly duplicate <code>Identifier</code>s
always works.</p>
<p>After a bit of reading in the ScalaCheck source code it <em>seems</em> as
though the root cause of this problem is some instance of
<code class="sourceCode scala">CanBuildFrom<span class="op">[</span><span class="ex">Set</span><span class="op">[</span>_<span class="op">],</span> A<span class="op">,</span> <span class="ex">Set</span><span class="op">[</span>A<span class="op">]]</span></code> but I’ve no idea how to go
about figure out which one or why it’s broken. In any case, I now know
a bit more about working with Scala.</p>
<p>For more information, see the <a href="https://github.com/rickynils/scalacheck/issues/89">ScalaCheck issue #89</a>.</p>]]></summary>
</entry>
<entry>
    <title>Testing multiple GHC versions on Travis CI</title>
    <link href="https://passingcuriosity.com/2015/testing-multiple-ghc-versions-on-travis-ci/" />
    <id>https://passingcuriosity.com/2015/testing-multiple-ghc-versions-on-travis-ci/</id>
    <published>2015-04-04T00:00:00Z</published>
    <updated>2015-04-04T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>GHC 7.10.1 was recently released and, if nothing else, that means there’s a new
version of the base library so a lot of developers will need to bump the upper
bound specified in their cabal files. Having multiple versions of the compiler
installed isn’t all that difficult, but actually building an testing a cabal
package with multiple compiler versions <em>is</em> pretty tedious. It’s easy enough
to do on Travis CI though and with just a little bit of cargo-culting, you too
can be extending your set of supported GHC versions.</p>
<h2 id="multiple-versions-of-ghc-on-travis-ci">Multiple versions of GHC on Travis CI</h2>
<p><a href="https://travis-ci.org/">Travis CI</a>, for those who aren’t familiar with it, is a continuous
integration service with pretty tight integration with <a href="https://github.com/">GitHub</a>. Using it is
pretty straightforward: you add a YAML file to your repository describing how
to test your project and then you turn it on. Using <a href="http://docs.travis-ci.com/user/environment-variables/#Matrix-Variables">matrix variables</a> in
your YAML file you can specify multiple values for various aspects of your
build process and Travis CI will run your job multiple times - once for each
combination of values. The standard approach to testing with <a href="https://github.com/hvr/multi-ghc-travis">multiple GHC
versions</a> on Travis CI uses this to specify which versions of GHC and Cabal
to install and use in the build; specify four versions and get run four times.
Magic!</p>
<p>The <code>.travis.yml</code> file you use to do this is a little more complex than the
usual one saying “make my Haskell go!”, but you can generally just copy it
around from project to project. I edited <a href="https://github.com/thsutton/edit-distance-vector/blob/master/.travis.yml">my <code>.travis.yml</code> file</a> slightly to
tweak the way Travis CI sends me email and to select the versions of GHC that I
care about and now <a href="https://travis-ci.org/thsutton/edit-distance-vector">every build for that project</a> automatically covers all
the cases I care about.</p>
<h2 id="so-what">So what?</h2>
<p>My <code>edit-distance-vector</code> package is very simple: it’s one module with 166
lines (including comments and white space) and 100 lines of tests (again,
including comments and white space). Here are the issues picked up by testing
with four versions of GHC:</p>
<ol type="1">
<li><p>Obviously, the version bounds on the base library need to be broadened. I’ve
used <code>base &gt;=4.5 &amp;&amp; &lt;4.9</code> now.</p></li>
<li><p>Next I learn that the <a href="http://hackage.haskell.org/package/base-4.8.0.0/docs/Data-Monoid.html#t:Sum">Sum</a> type didn’t have a <a href="http://hackage.haskell.org/package/base-4.8.0.0/docs/Prelude.html#t:Num">Num</a> instance in
earlier versions. This means that constants like <code>1</code> can’t have types like
<code>Sum Int</code> so I’ve just applied the <code>Sum</code> constructor manually: <code>Sum 1</code>.</p></li>
<li><p>Then I learned that importing a module <code>hiding</code> something that it doesn’t
export used to be an error (it is now a warning).</p></li>
</ol>
<p>The <a href="https://github.com/thsutton/edit-distance-vector/commit/d287d8b97deee5cb3b3e2fe74e155226c40b96a4">commit</a> fixing these issues is pretty trivial but made the library
usable in a wider range of environments. Yay!</p>
<p>I think I’ll be using this by default in new Haskell repositories; when my code
doesn’t work with some version of GHC I’d like it to be because I <em>decided</em> to
do it, not just that I didn’t know.</p>]]></summary>
</entry>
<entry>
    <title>Released some Haskell packages</title>
    <link href="https://passingcuriosity.com/2015/released-haskell-packages/" />
    <id>https://passingcuriosity.com/2015/released-haskell-packages/</id>
    <published>2015-04-03T00:00:00Z</published>
    <updated>2015-04-03T00:00:00Z</updated>
    <summary type="html"><![CDATA[<h2 id="edit-distance-vector">edit-distance-vector</h2>
<p>The <a href="https://hackage.haskell.org/package/edit-distance-vector">edit-distance-vector</a> package is a small library for calculating the
optimal edit script and cost to transform one sequence of values into another.
The implementation uses the <a href="https://en.wikipedia.org/wiki/Wagner–Fischer_algorithm">Wagner-Fischer</a> algorithm and the rather fun
<a href="https://hackage.haskell.org/package/vector/docs/Data-Vector.html#v:constructN"><code>constructN</code></a> function.</p>
<p>I have a draft blog post on the way about the details of this package but until
that’s done you’ll have to make do with the <a href="https://hackage.haskell.org/package/edit-distance-vector-1.0/docs/Data-Vector-Distance.html">documentation</a>.</p>
<h2 id="aeson-diff">aeson-diff</h2>
<p>The <a href="https://hackage.haskell.org/package/aeson-diff">aeson-diff</a> package includes a library and two command-line programs
for extracting the differences between two JSON documents and for applying
these changes. The commands are:</p>
<ul>
<li><p><code>aeson-diff</code> which compares two JSON documents and generates a patch
describing the differences between them; and</p></li>
<li><p><code>aeson-patch</code> which takes a JSON document and updates it according to patch.</p></li>
</ul>
<p>I find the <code>aeson-diff</code> command quite useful for comparing different versions
of the JSON documents spewed out by several systems I have to deal with at
work.</p>]]></summary>
</entry>
<entry>
    <title>Reading from processes safely in Haskell</title>
    <link href="https://passingcuriosity.com/2015/haskell-reading-process-safe-deadlock/" />
    <id>https://passingcuriosity.com/2015/haskell-reading-process-safe-deadlock/</id>
    <published>2015-03-04T00:00:00Z</published>
    <updated>2015-03-04T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>Quite a bit of my work lately has been implementing HTTP interfaces to existing
systems. In a few cases this required invoking existing command-line tools and
parsing their output. The naive approach to invoking a <a href="https://hackage.haskell.org/package/process">process</a> in Haskell
and reading its output goes something like this:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">import</span> <span class="dt">System.Exit</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="kw">import</span> <span class="dt">System.Process</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="ot">main ::</span> <span class="dt">IO</span> ()</span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>main <span class="ot">=</span> <span class="kw">do</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a>    <span class="kw">let</span> p <span class="ot">=</span> (shell <span class="st">&quot;cat /usr/share/dict/words&quot;</span>)</span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a>            { std_in  <span class="ot">=</span> <span class="dt">Inherit</span></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a>            , std_out <span class="ot">=</span> <span class="dt">CreatePipe</span></span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a>            , std_err <span class="ot">=</span> <span class="dt">Inherit</span></span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a>            }</span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a>    (<span class="dt">Nothing</span>, <span class="dt">Just</span> out, <span class="dt">Nothing</span>, ph) <span class="ot">&lt;-</span> createProcess p</span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a>    ec <span class="ot">&lt;-</span> waitForProcess ph</span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a>    <span class="kw">case</span> ph <span class="kw">of</span></span>
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a>        <span class="dt">ExitSuccess</span>   <span class="ot">-&gt;</span> hGetContents out <span class="op">&gt;&gt;=</span> <span class="fu">print</span></span>
<span id="cb1-15"><a href="#cb1-15" aria-hidden="true" tabindex="-1"></a>        <span class="dt">ExitFailure</span> _ <span class="ot">-&gt;</span> <span class="fu">error</span> <span class="st">&quot;Bad things happened. :-(&quot;</span></span></code></pre></div>
<p>There is a potential problem in this code: we wait until the process has
terminated before reading the <code>Handle</code> allowing its output to accumulate in the
pipe buffer managed by the operating system in the mean time. This buffer has
a fixed size on most systems (this is a good thing!); when it fills up, the
writing process will go to sleep until the reader has consumed some data and
freed some buffer space to hold the next write. Alas, the reader (the Haskell
code above) is sleeping, waiting for the writer to terminate. The reader is
sleeping, waiting for the writer to terminate; and the writer is sleeping,
waiting for the reader to read. This is a <a href="https://en.wikipedia.org/wiki/Deadlock">deadlock</a>!</p>
<p>The solution is to do the Right Thing (tm) and take care of any buffering
behaviour we want ourselves. Thankfully this is pretty straightforward and it’s
the sort of code you generally only need to write once. The very simplest case
– reading from a process with a single output <code>Handle</code> – looks like this:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="ot">gatherOutput ::</span> <span class="dt">ProcessHandle</span> <span class="ot">-&gt;</span> <span class="dt">Handle</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">ExitCode</span>, <span class="dt">ByteString</span>)</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>gatherOutput ph h <span class="ot">=</span> work <span class="fu">mempty</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>  <span class="kw">where</span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>    work acc <span class="ot">=</span> <span class="kw">do</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a>        <span class="co">-- Read any outstanding input.</span></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a>        bs <span class="ot">&lt;-</span> BS.hGetNonBlocking h (<span class="dv">64</span> <span class="op">*</span> <span class="dv">1024</span>)</span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a>        <span class="kw">let</span> acc' <span class="ot">=</span> acc <span class="op">&lt;&gt;</span> bs</span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a>        <span class="co">-- Check on the process.</span></span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a>        s <span class="ot">&lt;-</span> getProcessExitCode ph</span>
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a>        <span class="co">-- Exit or loop.</span></span>
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a>        <span class="kw">case</span> s <span class="kw">of</span></span>
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a>            <span class="dt">Nothing</span> <span class="ot">-&gt;</span> work acc'</span>
<span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a>            <span class="dt">Just</span> ec <span class="ot">-&gt;</span> <span class="kw">do</span></span>
<span id="cb2-14"><a href="#cb2-14" aria-hidden="true" tabindex="-1"></a>                <span class="co">-- Get any last bit written between the read and the status</span></span>
<span id="cb2-15"><a href="#cb2-15" aria-hidden="true" tabindex="-1"></a>                <span class="co">-- check.</span></span>
<span id="cb2-16"><a href="#cb2-16" aria-hidden="true" tabindex="-1"></a>                <span class="fu">last</span> <span class="ot">&lt;-</span> BS.hGetContents h</span>
<span id="cb2-17"><a href="#cb2-17" aria-hidden="true" tabindex="-1"></a>                <span class="fu">return</span> (ec, acc' <span class="op">&lt;&gt;</span> <span class="fu">last</span>)</span></code></pre></div>
<p>This is essentially a loop which reads some input from the <code>Handle</code> (possibly
an empty string), checks to see if the process has terminated, and either
returns the accumulated input or loops again. Extending this to gather the
output of two handles (like <code>stderr</code> and <code>stdout</code>) is relatively
straightforward.</p>]]></summary>
</entry>
<entry>
    <title>Multiple JSON encodings in Haskell</title>
    <link href="https://passingcuriosity.com/2015/multiple-json-representations-haskell/" />
    <id>https://passingcuriosity.com/2015/multiple-json-representations-haskell/</id>
    <published>2015-02-12T00:00:00Z</published>
    <updated>2015-02-12T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>I’m currently working on a small RESTful API to control a system with
a command-line interface. The command produces JSON output but it’s not really
ideal to expose in an API. This post describes the approach I took to
supporting two different JSON encodings for the same set of data types – one
for communicating with API clients and another for communicating with the
upstream system.</p>
<p>I’ll start with some data types to represent the data my API manages. In this
post I’ll use the example of a painting robot. The robot can carry several
colours of paint but can only paint with one “active” colour at a time. Here
are some data types to represent these details:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">newtype</span> <span class="dt">ColourName</span> <span class="ot">=</span> <span class="dt">ColourName</span> {<span class="ot"> unColourName ::</span> <span class="dt">Text</span> }</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a>  <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Show</span>)</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Colour</span> <span class="ot">=</span> <span class="dt">Colour</span></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>    {<span class="ot"> colorName ::</span> <span class="dt">ColourName</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a>    ,<span class="ot"> colourRGB ::</span> (<span class="dt">Word8</span>, <span class="dt">Word8</span>, <span class="dt">Word8</span>)</span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a>    }</span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a>  <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Show</span>)</span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a><span class="kw">newtype</span> <span class="dt">RobotName</span> <span class="ot">=</span> <span class="dt">RobotName</span> {<span class="ot"> unRobotName ::</span> <span class="dt">Text</span> }</span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a>  <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Show</span>)</span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Robot</span> <span class="ot">=</span> <span class="dt">Robot</span></span>
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a>    {<span class="ot"> robotName ::</span> <span class="dt">RobotName</span></span>
<span id="cb1-15"><a href="#cb1-15" aria-hidden="true" tabindex="-1"></a>    ,<span class="ot"> robotActiveColour ::</span> <span class="dt">ColourName</span></span>
<span id="cb1-16"><a href="#cb1-16" aria-hidden="true" tabindex="-1"></a>    ,<span class="ot"> robotAvailableColours ::</span> [<span class="dt">Colour</span>]</span>
<span id="cb1-17"><a href="#cb1-17" aria-hidden="true" tabindex="-1"></a>    }</span></code></pre></div>
<h2 id="json-for-the-api-clients">JSON for the API clients</h2>
<p>The JSON encoding of <code>Robot</code> that I’d like to provide to API clients is pretty
straightforward:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode json"><code class="sourceCode json"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span> <span class="dt">&quot;name&quot;</span> <span class="fu">:</span> <span class="st">&quot;Rosie the robot&quot;</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="fu">,</span> <span class="dt">&quot;activeColour&quot;</span> <span class="fu">:</span> <span class="st">&quot;red&quot;</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="fu">,</span> <span class="dt">&quot;availableColours&quot;</span> <span class="fu">:</span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>    <span class="fu">{</span> <span class="dt">&quot;red&quot;</span>   <span class="fu">:</span> <span class="fu">{</span> <span class="dt">&quot;R&quot;</span><span class="fu">:</span> <span class="dv">255</span><span class="fu">,</span> <span class="dt">&quot;G&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">,</span> <span class="dt">&quot;B&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">}</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a>    <span class="fu">,</span> <span class="dt">&quot;green&quot;</span> <span class="fu">:</span> <span class="fu">{</span> <span class="dt">&quot;R&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">,</span> <span class="dt">&quot;G&quot;</span><span class="fu">:</span> <span class="dv">255</span><span class="fu">,</span> <span class="dt">&quot;B&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">}</span></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a>    <span class="fu">,</span> <span class="dt">&quot;blue&quot;</span>  <span class="fu">:</span> <span class="fu">{</span> <span class="dt">&quot;R&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">,</span> <span class="dt">&quot;G&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">,</span> <span class="dt">&quot;B&quot;</span><span class="fu">:</span> <span class="dv">255</span><span class="fu">}</span></span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a>    <span class="fu">}</span></span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a><span class="fu">}</span></span></code></pre></div>
<p>The Haskell code to parse this JSON using <a href="https://hackage.haskell.org/package/aeson">aeson</a> is straightforward too
(though please note that the instances derived for the <code>newtype</code> are only safe
to use <em>within</em> a larger JSON structure as they result in bare JSON strings,
not objects or arrays):</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="kw">deriving</span> <span class="kw">instance</span> <span class="dt">FromJSON</span> <span class="dt">ColourName</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="kw">deriving</span> <span class="kw">instance</span> <span class="dt">ToJSON</span> <span class="dt">ColourName</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="kw">instance</span> <span class="dt">FromJSON</span> [<span class="dt">Colour</span>] <span class="kw">where</span></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a>    parseJSON (<span class="dt">Object</span> v) <span class="ot">=</span> <span class="fu">mapM</span> (<span class="fu">uncurry</span> colour) <span class="op">$</span> HashMap.toList v</span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a>      <span class="kw">where</span></span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a>        colour name (<span class="dt">Object</span> o) <span class="ot">=</span> <span class="dt">Colour</span></span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a>                <span class="op">&lt;$&gt;</span> parseJSON (<span class="dt">String</span> name)</span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a>                <span class="op">&lt;*&gt;</span> ((,,) <span class="op">&lt;$&gt;</span> o <span class="op">.:</span> <span class="st">&quot;R&quot;</span> <span class="op">&lt;*&gt;</span> o <span class="op">.:</span> <span class="st">&quot;G&quot;</span> <span class="op">&lt;*&gt;</span> o <span class="op">.:</span> <span class="st">&quot;B&quot;</span>)</span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a>        colour _ _ <span class="ot">=</span> <span class="fu">fail</span> <span class="st">&quot;Colour must be a JSON object&quot;</span></span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a>    parseJSON _ <span class="ot">=</span> <span class="fu">fail</span> <span class="st">&quot;Colours must be a JSON object&quot;</span></span>
<span id="cb3-12"><a href="#cb3-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-13"><a href="#cb3-13" aria-hidden="true" tabindex="-1"></a><span class="kw">deriving</span> <span class="kw">instance</span> <span class="dt">FromJSON</span> <span class="dt">RobotName</span></span>
<span id="cb3-14"><a href="#cb3-14" aria-hidden="true" tabindex="-1"></a><span class="kw">deriving</span> <span class="kw">instance</span> <span class="dt">ToJSON</span> <span class="dt">RobotName</span></span>
<span id="cb3-15"><a href="#cb3-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-16"><a href="#cb3-16" aria-hidden="true" tabindex="-1"></a><span class="kw">instance</span> <span class="dt">FromJSON</span> <span class="dt">Robot</span> <span class="kw">where</span></span>
<span id="cb3-17"><a href="#cb3-17" aria-hidden="true" tabindex="-1"></a>    parseJSON (<span class="dt">Object</span> v) <span class="ot">=</span> <span class="dt">Robot</span></span>
<span id="cb3-18"><a href="#cb3-18" aria-hidden="true" tabindex="-1"></a>        <span class="op">&lt;$&gt;</span> v <span class="op">.:</span> <span class="st">&quot;name&quot;</span></span>
<span id="cb3-19"><a href="#cb3-19" aria-hidden="true" tabindex="-1"></a>        <span class="op">&lt;*&gt;</span> v <span class="op">.:</span> <span class="st">&quot;activeColour&quot;</span></span>
<span id="cb3-20"><a href="#cb3-20" aria-hidden="true" tabindex="-1"></a>        <span class="op">&lt;*&gt;</span> v <span class="op">.:</span> <span class="st">&quot;availableColours&quot;</span></span>
<span id="cb3-21"><a href="#cb3-21" aria-hidden="true" tabindex="-1"></a>    parseJSON _ <span class="ot">=</span> <span class="fu">fail</span> <span class="st">&quot;Robot must be a JSON object&quot;</span></span></code></pre></div>
<p>To talk to the upstream system I’ll use the <a href="https://hackage.haskell.org/package/process">process</a> library to execute
a command which produces JSON on its standard output. A simple function to
invoke a command, parse the JSON, and return the value (or an error) keeps the
boilerplate contained:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>shellOutJSON</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="ot">    ::</span> (<span class="dt">MonadError</span> <span class="dt">String</span> m, <span class="dt">MonadIO</span> m, <span class="dt">FromJSON</span> a)</span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a>    <span class="ot">=&gt;</span> <span class="dt">String</span></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a>    <span class="ot">-&gt;</span> [<span class="dt">String</span>]</span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a>    <span class="ot">-&gt;</span> m a</span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a>shellOutJSON cmd args <span class="ot">=</span> <span class="kw">do</span></span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a>    <span class="co">-- Execute the command.</span></span>
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a>    (exit_code, out, _err) <span class="ot">&lt;-</span> liftIO <span class="op">$</span> readProcessWithExitCode cmd args <span class="st">&quot;&quot;</span></span>
<span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-10"><a href="#cb4-10" aria-hidden="true" tabindex="-1"></a>    <span class="co">-- Check it succeeded.</span></span>
<span id="cb4-11"><a href="#cb4-11" aria-hidden="true" tabindex="-1"></a>    output <span class="ot">&lt;-</span> <span class="kw">case</span> exit_code <span class="kw">of</span></span>
<span id="cb4-12"><a href="#cb4-12" aria-hidden="true" tabindex="-1"></a>        <span class="dt">ExitSuccess</span> <span class="ot">-&gt;</span> <span class="fu">return</span> <span class="op">$</span> BS.pack out</span>
<span id="cb4-13"><a href="#cb4-13" aria-hidden="true" tabindex="-1"></a>        <span class="dt">ExitFailure</span> err <span class="ot">-&gt;</span> throwError <span class="op">$</span></span>
<span id="cb4-14"><a href="#cb4-14" aria-hidden="true" tabindex="-1"></a>            <span class="st">&quot;Could not execute command: error &quot;</span> <span class="op">&lt;&gt;</span> <span class="fu">show</span> err</span>
<span id="cb4-15"><a href="#cb4-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-16"><a href="#cb4-16" aria-hidden="true" tabindex="-1"></a>    <span class="co">-- Decode the JSON.</span></span>
<span id="cb4-17"><a href="#cb4-17" aria-hidden="true" tabindex="-1"></a>    <span class="kw">case</span> eitherDecode output <span class="kw">of</span></span>
<span id="cb4-18"><a href="#cb4-18" aria-hidden="true" tabindex="-1"></a>        <span class="dt">Left</span> e <span class="ot">-&gt;</span> throwError <span class="op">$</span> <span class="st">&quot;Error decoding JSON: &quot;</span> <span class="op">&lt;&gt;</span> e</span>
<span id="cb4-19"><a href="#cb4-19" aria-hidden="true" tabindex="-1"></a>        <span class="dt">Right</span> v <span class="ot">-&gt;</span> <span class="fu">return</span> v</span></code></pre></div>
<p>It’s important to note that the <em>call site</em> is responsible for fixing the type
<code>a</code> of value to be parsed from the JSON. This means that <code>shellOutJSON</code> will
happily attempt to parse the JSON into <em>any</em> type you ask it to (so long as it
has a <code>FromJSON</code> instance), whether or not you should expect the command to
produce such JSON. The obvious potential problem – a caller asking for data in
the wrong format – occurred twice in a dozen lines of code in my current
project.</p>
<h2 id="json-for-the-upstream-system">JSON for the upstream system</h2>
<p>The second JSON encoding is the one used to communicate with the command-line
application. The main difference from the API encoding is that it represents
the active colour by adding a <code>status</code> property to each colours; exactly one of
them is <code>active</code> and the rest are <code>available</code>. Rosie the robot is looks like
this:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode json"><code class="sourceCode json"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span> <span class="dt">&quot;name&quot;</span> <span class="fu">:</span> <span class="st">&quot;Rosie the robot&quot;</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="fu">,</span> <span class="dt">&quot;colours&quot;</span> <span class="fu">:</span></span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a>    <span class="fu">{</span> <span class="dt">&quot;red&quot;</span>   <span class="fu">:</span> <span class="fu">{</span> <span class="dt">&quot;R&quot;</span><span class="fu">:</span> <span class="dv">255</span><span class="fu">,</span> <span class="dt">&quot;G&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">,</span> <span class="dt">&quot;B&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">,</span> <span class="dt">&quot;status&quot;</span><span class="fu">:</span> <span class="st">&quot;active&quot;</span><span class="fu">}</span></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a>    <span class="fu">,</span> <span class="dt">&quot;green&quot;</span> <span class="fu">:</span> <span class="fu">{</span> <span class="dt">&quot;R&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">,</span> <span class="dt">&quot;G&quot;</span><span class="fu">:</span> <span class="dv">255</span><span class="fu">,</span> <span class="dt">&quot;B&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">,</span> <span class="dt">&quot;status&quot;</span><span class="fu">:</span> <span class="st">&quot;available&quot;</span><span class="fu">}</span></span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a>    <span class="fu">,</span> <span class="dt">&quot;blue&quot;</span>  <span class="fu">:</span> <span class="fu">{</span> <span class="dt">&quot;R&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">,</span> <span class="dt">&quot;G&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">,</span> <span class="dt">&quot;B&quot;</span><span class="fu">:</span> <span class="dv">255</span><span class="fu">,</span> <span class="dt">&quot;status&quot;</span><span class="fu">:</span> <span class="st">&quot;available&quot;</span><span class="fu">}</span></span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a>    <span class="fu">}</span></span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a><span class="fu">}</span></span></code></pre></div>
<p>This is structure is great if you are using the data to output a nice table for
a human to read but not so great in an API.</p>
<p>This additional format could be implemented with new data types to represent
robots and colours and a few conversion functions (probably using the excellent
<a href="https://hackage.haskell.org/package/lens">lens</a> package) to represent the weirdly formatted versions of our types. Or
I could keep the same data types but create a <code>newtype</code> wrapper around each of
them with new <code>FromJSON</code> instances implementing the new format.</p>
<p>Instead I’ll add a “wrapper” type with which to distinguish a normal <code>Robot</code>
from one which should be formatted for the upstream system.</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Upstream</span> a <span class="ot">=</span> <span class="dt">Upstream</span> {<span class="ot"> unwrapUpstream ::</span> a }</span></code></pre></div>
<p>This new type doesn’t “do” anything, it just tags the value it wraps and lets
me distinguish a <code>Robot</code> from an <code>Upstream Robot</code> which should be formatted for
the API and the upstream system respectively. (This is not strictly true: it
does take up memory and does cost an additional pointer dereference to
traverse). With the new <code>Upstream</code> type I can write a second <code>FromJSON</code>
instance each of my types.</p>
<p>If there is no special upstream format for a type the new instance can just
call the existing instance and stuff the result in an <code>Upstream</code> wrapper:</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="kw">instance</span> <span class="dt">FromJSON</span> (<span class="dt">Upstream</span> [<span class="dt">Colour</span>]) <span class="kw">where</span></span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>    parseJSON j <span class="ot">=</span> <span class="dt">Upstream</span> <span class="op">&lt;$&gt;</span> parseJSON j</span></code></pre></div>
<p>When the upstream encoding and the API encoding do differ, I write a <code>FromJSON</code>
instance in exactly the same way I normally would (making sure to use the
<code>Upstream</code> version of any other <code>FromJSON</code> instances I use):</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="kw">instance</span> <span class="dt">FromJSON</span> (<span class="dt">Upstream</span> <span class="dt">Robot</span>) <span class="kw">where</span></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a>    parseJSON (<span class="dt">Object</span> v) <span class="ot">=</span> <span class="dt">Upstream</span> <span class="op">&lt;$&gt;</span></span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a>        (<span class="dt">Robot</span></span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a>            <span class="op">&lt;$&gt;</span>  v <span class="op">.:</span> <span class="st">&quot;name&quot;</span></span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a>            <span class="op">&lt;*&gt;</span> (v <span class="op">.:</span> <span class="st">&quot;colours&quot;</span> <span class="op">&gt;&gt;=</span> activeColours <span class="op">&gt;&gt;=</span> exactlyOne)</span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a>            <span class="op">&lt;*&gt;</span> (unwrapUpstream <span class="op">&lt;$&gt;</span> v <span class="op">.:</span> <span class="st">&quot;colours&quot;</span>))</span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a>      <span class="kw">where</span></span>
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a>        <span class="co">-- Parse a JSON object of colours into a list of 'ColourName's which</span></span>
<span id="cb8-9"><a href="#cb8-9" aria-hidden="true" tabindex="-1"></a>        <span class="co">-- have @status == &quot;active&quot;.</span></span>
<span id="cb8-10"><a href="#cb8-10" aria-hidden="true" tabindex="-1"></a><span class="ot">        activeColours ::</span> <span class="dt">Value</span> <span class="ot">-&gt;</span> <span class="dt">Parser</span> [<span class="dt">ColourName</span>]</span>
<span id="cb8-11"><a href="#cb8-11" aria-hidden="true" tabindex="-1"></a>        activeColours (<span class="dt">Object</span> o) <span class="ot">=</span> (<span class="fu">fmap</span> <span class="fu">fst</span> <span class="op">.</span> <span class="fu">filter</span> <span class="fu">snd</span>) <span class="op">&lt;$&gt;</span></span>
<span id="cb8-12"><a href="#cb8-12" aria-hidden="true" tabindex="-1"></a>            <span class="fu">mapM</span> (<span class="fu">uncurry</span> colour) (HM.toList o)</span>
<span id="cb8-13"><a href="#cb8-13" aria-hidden="true" tabindex="-1"></a>        activeColours _ <span class="ot">=</span> <span class="fu">fail</span> <span class="st">&quot;Colours must be a JSON object.&quot;</span></span>
<span id="cb8-14"><a href="#cb8-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-15"><a href="#cb8-15" aria-hidden="true" tabindex="-1"></a>        <span class="co">-- Given a name and a JSON value, parse a pair containing the name and</span></span>
<span id="cb8-16"><a href="#cb8-16" aria-hidden="true" tabindex="-1"></a>        <span class="co">-- whether the colour has @status == &quot;active&quot;@.</span></span>
<span id="cb8-17"><a href="#cb8-17" aria-hidden="true" tabindex="-1"></a><span class="ot">        colour ::</span> <span class="dt">Text</span> <span class="ot">-&gt;</span> <span class="dt">Value</span> <span class="ot">-&gt;</span> <span class="dt">Parser</span> (<span class="dt">ColourName</span>, <span class="dt">Bool</span>)</span>
<span id="cb8-18"><a href="#cb8-18" aria-hidden="true" tabindex="-1"></a>        colour name (<span class="dt">Object</span> o) <span class="ot">=</span> (,)</span>
<span id="cb8-19"><a href="#cb8-19" aria-hidden="true" tabindex="-1"></a>            <span class="op">&lt;$&gt;</span> parseJSON (<span class="dt">String</span> name)</span>
<span id="cb8-20"><a href="#cb8-20" aria-hidden="true" tabindex="-1"></a>            <span class="op">&lt;*&gt;</span> ((<span class="dt">String</span> <span class="st">&quot;active&quot;</span> <span class="op">==</span>) <span class="op">&lt;$&gt;</span> (o <span class="op">.:</span> <span class="st">&quot;status&quot;</span>))</span>
<span id="cb8-21"><a href="#cb8-21" aria-hidden="true" tabindex="-1"></a>        colour _ _ <span class="ot">=</span> <span class="fu">fail</span> <span class="st">&quot;Colour must be a JSON object.&quot;</span></span>
<span id="cb8-22"><a href="#cb8-22" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-23"><a href="#cb8-23" aria-hidden="true" tabindex="-1"></a>    parseJSON _ <span class="ot">=</span> <span class="fu">fail</span> <span class="st">&quot;Robot must be a JSON object&quot;</span></span>
<span id="cb8-24"><a href="#cb8-24" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-25"><a href="#cb8-25" aria-hidden="true" tabindex="-1"></a><span class="co">-- | Parser to check that a list contains exactly one value.</span></span>
<span id="cb8-26"><a href="#cb8-26" aria-hidden="true" tabindex="-1"></a><span class="ot">exactlyOne ::</span> [a] <span class="ot">-&gt;</span> <span class="dt">Parser</span> a</span>
<span id="cb8-27"><a href="#cb8-27" aria-hidden="true" tabindex="-1"></a>exactlyOne [] <span class="ot">=</span> <span class="fu">fail</span> <span class="st">&quot;Missing value&quot;</span></span>
<span id="cb8-28"><a href="#cb8-28" aria-hidden="true" tabindex="-1"></a>exactlyOne [a] <span class="ot">=</span> <span class="fu">pure</span> a</span>
<span id="cb8-29"><a href="#cb8-29" aria-hidden="true" tabindex="-1"></a>exactlyOne _ <span class="ot">=</span> <span class="fu">fail</span> <span class="st">&quot;More than one value&quot;</span></span></code></pre></div>
<p>With all these instances written I can update <code>shellOutJSON</code> to use the
<code>Upstream</code> instances when it interacts with the command-line program. Two small
changes – adding <code>Upstream</code> to the <code>FromJSON</code> constraint and the “success”
pattern match – are enough to ensure that <em>all</em> communication with the
upstream system uses the <code>Upstream</code> JSON encoding:</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>shellOutJSON</span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a><span class="ot">    ::</span> (<span class="dt">MonadError</span> <span class="dt">String</span> m, <span class="dt">MonadIO</span> m, <span class="dt">FromJSON</span> (<span class="dt">Upstream</span> a))</span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a>    <span class="ot">=&gt;</span> [<span class="dt">String</span>]</span>
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a>    <span class="ot">-&gt;</span> m a</span>
<span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a>shellOutJSON cmd <span class="ot">=</span> <span class="kw">do</span></span>
<span id="cb9-6"><a href="#cb9-6" aria-hidden="true" tabindex="-1"></a>    <span class="co">-- Execute the command.</span></span>
<span id="cb9-7"><a href="#cb9-7" aria-hidden="true" tabindex="-1"></a>    (exit_code, out, _err) <span class="ot">&lt;-</span> liftIO <span class="op">$</span> readProcessWithExitCode cmd [] <span class="st">&quot;&quot;</span></span>
<span id="cb9-8"><a href="#cb9-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-9"><a href="#cb9-9" aria-hidden="true" tabindex="-1"></a>    <span class="co">-- Check it succeeded.</span></span>
<span id="cb9-10"><a href="#cb9-10" aria-hidden="true" tabindex="-1"></a>    output <span class="ot">&lt;-</span> <span class="kw">case</span> exit_code <span class="kw">of</span></span>
<span id="cb9-11"><a href="#cb9-11" aria-hidden="true" tabindex="-1"></a>        <span class="dt">ExitSuccess</span> <span class="ot">-&gt;</span> <span class="fu">return</span> <span class="op">$</span> BS.pack out</span>
<span id="cb9-12"><a href="#cb9-12" aria-hidden="true" tabindex="-1"></a>        <span class="dt">ExitFailure</span> err <span class="ot">-&gt;</span> throwError <span class="op">$</span></span>
<span id="cb9-13"><a href="#cb9-13" aria-hidden="true" tabindex="-1"></a>            <span class="st">&quot;Could not execute command: errno = &quot;</span> <span class="op">&lt;&gt;</span> <span class="fu">show</span> err</span>
<span id="cb9-14"><a href="#cb9-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-15"><a href="#cb9-15" aria-hidden="true" tabindex="-1"></a>    <span class="co">-- Decode the JSON.</span></span>
<span id="cb9-16"><a href="#cb9-16" aria-hidden="true" tabindex="-1"></a>    <span class="kw">case</span> eitherDecode output <span class="kw">of</span></span>
<span id="cb9-17"><a href="#cb9-17" aria-hidden="true" tabindex="-1"></a>        <span class="dt">Left</span> e <span class="ot">-&gt;</span> throwError <span class="op">$</span> </span>
<span id="cb9-18"><a href="#cb9-18" aria-hidden="true" tabindex="-1"></a>            <span class="st">&quot;Error decoding JSON: &quot;</span> <span class="op">&lt;&gt;</span> e</span>
<span id="cb9-19"><a href="#cb9-19" aria-hidden="true" tabindex="-1"></a>        <span class="dt">Right</span> (<span class="dt">Upstream</span> v) <span class="ot">-&gt;</span> <span class="fu">return</span> v</span></code></pre></div>
<p>Now any call to <code>shellOutJSON</code> will automatically parse using the correct JSON
encoding and any existing code using <code>shellOutJSON</code> doesn’t have to change.
Even better, any call which needs a type without an <code>Upstream</code> instance of
<code>FromJSON</code> will result in a type error at run time:</p>
<pre><code>lib/Server.hs:115:5:
    Could not deduce (FromJSON (Upstream Colour))
      arising from a use of ‘shellOutJSON’
    from the context (MonadError String m, MonadIO m)
      bound by the type signature for
                 getColour :: (MonadError String m, MonadIO m) =&gt;
                                      ColourName -&gt; m Colour
      at lib/Server.hs:(109,8)-(112,18)
    In a stmt of a 'do' block: shellOutJSON cmd [&quot;colour&quot;, &quot;list&quot;, colour_name]
    In the expression:
      shellOutJSON cmd [&quot;colour&quot;, &quot;list&quot;, color_name]
    In an equation for ‘getColour’:
        getColour name
          = do { let colour_name = T.unpack $ unColourName name
                 shellOutJSON cmd [&quot;colour&quot;, ....] }</code></pre>
<p>The second line of the error tells you exactly what’s missing: the compiler
can’t find a <code>FromJSON</code> instance for <code>Upstream Colour</code>.</p>
<h2 id="conclusion">Conclusion</h2>
<p>By using a “wrapper” type like <code>Upstream a</code> I reduced the amount of code I need
to write and maintain (in particular, there’s no converting back and forth
between <code>Colour</code> and <code>WeirdlyFormattedColour</code> data types). The values of my
various types are clearly still related and <code>Upstream</code> is completely agnostic
to the type being wrapped – an <code>Upstream Robot</code> is just a <code>Robot</code> inside an
<code>Upstream</code> and neither the <code>Robot</code> not the <code>Upstream</code> cares about the other
part at all.</p>
<p>Making the wrapper parametric like this (as opposed to, for example, creating
a different <code>newtype</code> wrapper around each of the particular types) makes i
possible to write code which – like the modified <code>shellOutJSON</code> – doesn’t
care about the <em>what</em> is being wrapped, just that it <em>is</em> wrapped.</p>
<p>Adding and removing the <code>Upstream</code> wrapper at the system boundary minimises the
amount code which can incorrectly use the wrong representation and, in
particular, makes it impossible for these bugs to happen in the many places
I use <code>shellOutJSON</code>. This forces me to define wrapped <code>FromJSON</code> instances for
<em>all</em> the types, even the ones that use the same JSON representation, but this
is a price I’m willing to pay for an interface that makes a class of errors
impossible.</p>
<p>Using this approach in my current project made the code shorter, simpler (in
terms of number of data types and functions defined), fixed two “wrong format”
bugs, and made it impossible to reintroduce them.</p>]]></summary>
</entry>
<entry>
    <title>Haskell at Work</title>
    <link href="https://passingcuriosity.com/2015/haskell-at-work/" />
    <id>https://passingcuriosity.com/2015/haskell-at-work/</id>
    <published>2015-01-28T00:00:00Z</published>
    <updated>2015-01-28T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>I recently (nearly 7 months ago) started work at <a href="https://www.anchor.net.au/">Anchor Hosting</a> as
a software developer in the engineering department. Anchor is a hosting
company, and its engineering group works on a variety of different things like
time-series data storage and analysis, APIs and automation, business
applications, and development tools.</p>
<p>I’ll describe some of our development tooling in this post and leave discussion
of the “real” systems we work on for a later post.</p>
<h3 id="building-and-deploying">Building and deploying</h3>
<p>Given we’re a Haskell shop, we’ve needed to figure out how to build, deploy,
and manage systems written in Haskell. I’m still not sold on the whole
containerisation mania which seems to be sweeping certain parts of the IT world
(and certainly not for general purpose multi-tenancy). Nevertheless, we’re
finding <a href="https://www.docker.com/">Docker</a> quite useful.</p>
<p>We have a Docker image containing GHC and cabal configured to use the
<a href="http://www.stackage.org/">Stackage</a> package set, with a bunch of frequently used Haskell
packages already installed. This image is built automatically using a <a href="https://github.com/anchor/docker-haskell" title="Build a Docker image with GHC and Stackage">small
set of scripts</a>.</p>
<p>Most of the systems we build operate as services (HTTP servers, agents on
message queue, etc.) and we “package” them as Docker images too. We have
<a href="https://github.com/anchor/docker-build" title="Build a Haskell package and make a Docker image of it.">another set of scripts</a> which use the Haskell image to build
a cabal package, extract the artefacts, and stuff them into a new Docker image.
This approach results in an image which is significantly smaller than it
otherwise would be.</p>
<p>Both sets of scripts can be used manually but they are also used in
<a href="http://jenkins-ci.org/">Jenkins</a> jobs. Like everything mentioned so far Jenkins and its
builders all run in Docker too, so we also have some scripts to build <a href="https://github.com/anchor/docker-jenkins" title="Build Docker images for Jenkins servers and builders.">Jenkins
Docker images</a>.</p>
<p>All of these Docker images are run on <a href="https://coreos.com/">CoreOS</a> servers hosted on
<a href="http://www.anchor.com.au/opencloud/" title="Ultra-high performance, public OpenStack cloud.">Anchor OpenCloud</a>, Anchor’s new <a href="http://www.openstack.org/">OpenStack</a> deployment. Generally, we
run each service as a Docker container managed by a <code>systemd</code> unit with its
configuration and data files (such as they are) mounted in from the host file
system. Each <code>systemd</code> unit deletes any old container and pulls the latest
image before starting the service, so upgrading an instance is easy: just
restart the <code>systemd</code> unit.</p>
<h3 id="development-tools">Development tools</h3>
<p>Various members of the team all use different operating systems (various
flavours of BSD, Linux, and Mac OS X), editors (vim, emacs, Sublime Text) and
have different opinions and habits about coding style, etc. To help manage
improve the consistency and, hopefully, quality of our code, we developed
<a href="https://github.com/anchor/git-vogue/">git-vogue</a>. <code>git-vogue</code> runs as a pre-commit hook (currently for <code>git</code>,
but it can be extended) and runs a range of checks over the modified or,
optionally, all files in the repository. This isn’t perfect but has helped
improve our code quite considerably.</p>]]></summary>
</entry>

</feed>
