<story><title>Go, REST APIs, and Pointers</title><url>https://willnorris.com/2014/05/go-rest-apis-and-pointers</url></story><parent_chain></parent_chain><comment><author>nickknw</author><text>Here is a practical everyday example where languages that can represent emptiness in the type system come in handy.&lt;p&gt;He ends up using pointers and mentions: &amp;quot;Using pointers also means that clients of the library will need to perform their own nil checks where appropriate to prevent panics.&amp;quot;&lt;p&gt;Looking forward to using Rust :)</text></comment>
<story><title>Go, REST APIs, and Pointers</title><url>https://willnorris.com/2014/05/go-rest-apis-and-pointers</url></story><parent_chain></parent_chain><comment><author>buro9</author><text>PATCH is a nightmare in reality, especially with the default zero nature of Go.&lt;p&gt;We opted for the JSON PATCH notation in RFC 6902: &lt;a href=&quot;http://tools.ietf.org/html/rfc6902&quot; rel=&quot;nofollow&quot;&gt;http:&amp;#x2F;&amp;#x2F;tools.ietf.org&amp;#x2F;html&amp;#x2F;rfc6902&lt;/a&gt;&lt;p&gt;Essentially there&amp;#x27;s a standard format for instructions on how to modify an existing JSON document, like this:&lt;p&gt;&lt;pre&gt;&lt;code&gt;   PATCH &amp;#x2F;my&amp;#x2F;data HTTP&amp;#x2F;1.1
   Host: example.org
   Content-Length: 326
   Content-Type: application&amp;#x2F;json-patch+json
   If-Match: &amp;quot;abc123&amp;quot;

   [
     { &amp;quot;op&amp;quot;: &amp;quot;test&amp;quot;, &amp;quot;path&amp;quot;: &amp;quot;&amp;#x2F;a&amp;#x2F;b&amp;#x2F;c&amp;quot;, &amp;quot;value&amp;quot;: &amp;quot;foo&amp;quot; },
     { &amp;quot;op&amp;quot;: &amp;quot;remove&amp;quot;, &amp;quot;path&amp;quot;: &amp;quot;&amp;#x2F;a&amp;#x2F;b&amp;#x2F;c&amp;quot; },
     { &amp;quot;op&amp;quot;: &amp;quot;add&amp;quot;, &amp;quot;path&amp;quot;: &amp;quot;&amp;#x2F;a&amp;#x2F;b&amp;#x2F;c&amp;quot;, &amp;quot;value&amp;quot;: [ &amp;quot;foo&amp;quot;, &amp;quot;bar&amp;quot; ] },
     { &amp;quot;op&amp;quot;: &amp;quot;replace&amp;quot;, &amp;quot;path&amp;quot;: &amp;quot;&amp;#x2F;a&amp;#x2F;b&amp;#x2F;c&amp;quot;, &amp;quot;value&amp;quot;: 42 },
     { &amp;quot;op&amp;quot;: &amp;quot;move&amp;quot;, &amp;quot;from&amp;quot;: &amp;quot;&amp;#x2F;a&amp;#x2F;b&amp;#x2F;c&amp;quot;, &amp;quot;path&amp;quot;: &amp;quot;&amp;#x2F;a&amp;#x2F;b&amp;#x2F;d&amp;quot; },
     { &amp;quot;op&amp;quot;: &amp;quot;copy&amp;quot;, &amp;quot;from&amp;quot;: &amp;quot;&amp;#x2F;a&amp;#x2F;b&amp;#x2F;d&amp;quot;, &amp;quot;path&amp;quot;: &amp;quot;&amp;#x2F;a&amp;#x2F;b&amp;#x2F;e&amp;quot; }
   ]
&lt;/code&gt;&lt;/pre&gt;
We&amp;#x27;ve implemented the operations &amp;quot;add&amp;quot;, &amp;quot;remove&amp;quot; and &amp;quot;replace&amp;quot; in our REST API.&lt;p&gt;We don&amp;#x27;t have a meaningful way of doing &amp;quot;move&amp;quot; and &amp;quot;copy&amp;quot;, and &amp;quot;test&amp;quot; can be done by performing a GET and looking at the document (and that can be used in the If-Match).&lt;p&gt;You can see the documentation for our stuff here: &lt;a href=&quot;http://microcosm-cc.github.io/#events-single-patch&quot; rel=&quot;nofollow&quot;&gt;http:&amp;#x2F;&amp;#x2F;microcosm-cc.github.io&amp;#x2F;#events-single-patch&lt;/a&gt;&lt;p&gt;And that gives away why we chose to do this... booleans and the default value of false. We wanted it to be more explicit and no room for accidental expression of a value anywhere, regardless of the callee language&amp;#x2F;environment or ours. JSON PATCH makes this very explicit.&lt;p&gt;Of course there&amp;#x27;s the mild inconvenience of handling the value type, but that&amp;#x27;s relatively easily overcome.&lt;p&gt;What was really interesting was handling permissions for the PATCH instructions.&lt;p&gt;For example a user might have permission to issue a PATCH that changed a string, but only the admin could issue a PATCH that updated some special part of a resource.&lt;p&gt;PATCH is relatively easy with the above, and very predictable... fine-grained permissions of which part of a document someone can update... that&amp;#x27;s definitely where the fun is.</text></comment>