<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Tobiasz Małecki on Medium]]></title>
        <description><![CDATA[Stories by Tobiasz Małecki on Medium]]></description>
        <link>https://medium.com/@amatalai?source=rss-a110ccc971fa------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Mqcd0othsBWYyETI.jpeg</url>
            <title>Stories by Tobiasz Małecki on Medium</title>
            <link>https://medium.com/@amatalai?source=rss-a110ccc971fa------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 19:30:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amatalai/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How I changed the default structure of Elixir project to suit my preferences more]]></title>
            <link>https://medium.com/@amatalai/how-i-changed-the-default-structure-of-elixir-project-to-suit-my-preferences-more-8e0a7dc88ef3?source=rss-a110ccc971fa------2</link>
            <guid isPermaLink="false">https://medium.com/p/8e0a7dc88ef3</guid>
            <category><![CDATA[phoenix-framework]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Tobiasz Małecki]]></dc:creator>
            <pubDate>Fri, 18 Oct 2019 11:16:58 GMT</pubDate>
            <atom:updated>2019-10-18T11:16:58.748Z</atom:updated>
            <content:encoded><![CDATA[<p>Reasonable defaults can make your life easier. I think that most programmers agree with that. Does that mean we should stick to them forever? Can a particular solution be suitable for everything and everyone?</p><p>This article tells the story of my experiment, which proved that sometimes it is worth breaking well-established standards.</p><h4>To get some context</h4><p>Let’s start with the standard elixir project.</p><pre>.<br>├── config<br>│   └── config.exs<br>├── lib<br>│   ├── foo<br>│   │   ├── bar.ex<br>│   │   └── baz.ex<br>│   └── foo.ex<br>├── mix.exs<br>└── test<br>    ├── foo<br>    │   ├── bar_test.exs<br>    │   └── baz_test.exs<br>    ├── foo_test.exs<br>    └── test_helper.exs</pre><p>It’s the output of mix new foo with two additional modules: Foo.Bar and Foo.Baz added manually. You can imagine this is your favorite small open-source library.</p><p>In my opinion, it has the following advantages:</p><ul><li>compiles the content of lib out of box</li><li>has everything that is needed to run tests</li><li>looks familiar to many developers</li><li>is easy to be released to hex (the content of lib is added to the package, the content of test isn’t)</li></ul><h4>That’s great. So… what’s your problem?</h4><p>I don’t like having separate lib and test folders in phoenix projects. Ones that are big and not meant to be pushed to hex.</p><p>I’m bad at remembering shortcuts and names. On the other hand, I’m pretty good at remembering where individual files are stored (especially when the project has a clear division into small contexts). However, I don’t want to go through the second tree with the same structure to get to the tests. It has always been a bit annoying.</p><p>One day while reading the documentation of mix test task, I found out the following options: :test_paths and :test_pattern. The mere fact that such options exist in Elixir codebase means that they were useful to someone. I decided to check if they would also be useful for me.</p><p>First, I changed :test_paths value to [&quot;lib&quot;]. It allowed me to keep a file with tests next to the file with implementation. It also forced me to move .test_helper.exs to the root of lib, but one additional file isn’t a problem. What bothered me more was the alphabetical order. Of two files with the same prefix, the one ending with _test.exs was higher than its .ex counterpart. It made the files tree in my editor look weird. Luckily, it was easy to fix by changing :test_pattern value to &quot;*.test.exs&quot;. From now on, files with tests had to be named like this: my_module.test.exs.</p><pre>.<br>├── config<br>│   └── config.exs<br>├── lib<br>│   ├── foo<br>│   │   ├── bar.ex<br>│   │   ├── bar.test.exs<br>│   │   ├── baz.ex<br>│   │   └── baz.test.exs<br>│   ├── foo.ex<br>│   ├── foo.test.exs<br>│   └── test_helper.exs<br>└── mix.exs</pre><p>I was pleased with what I was able to achieve.</p><h4>Ok. Is that all?</h4><p>No.</p><p>After a few weeks of work in this way, new advantages appeared:</p><ul><li>Code review has become easier because changes to tests are right under changes to implementation (long-gone days of repeatedly scrolling the whole merge request on Gitlab up and down).</li><li>Faster refactoring (move single folder when changing namespaces)</li><li>Well-written tests are a form of documentation. It is good to have them in an easily accessible place.</li><li>It’s visible if a module is important or not. The file generated by a framework or library doesn’t have an additional .test.exs file associated with it.</li></ul><p>It also has some disadvantages:</p><ul><li>I have to set manually :test_paths and :test_pattern for every new umbrella application.</li><li>It may not be obvious where to put integration tests when there is no test folder.</li></ul><p>However, these are minor ones comparing to how more comfortable my work has become.</p><p>I’m aware this topic may be controversial for some people. I encourage you to leave me a comment if you think that I’ve missed something.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8e0a7dc88ef3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Quick look at new :gen_server handle_continue callback]]></title>
            <link>https://medium.com/appunite-edu-collection/quick-look-at-new-gen-server-handle-continue-callback-2ef0408391ff?source=rss-a110ccc971fa------2</link>
            <guid isPermaLink="false">https://medium.com/p/2ef0408391ff</guid>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Tobiasz Małecki]]></dc:creator>
            <pubDate>Wed, 20 Jun 2018 07:32:22 GMT</pubDate>
            <atom:updated>2018-06-20T08:56:04.768Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZvX4zKk4PAsPgvB3PvE-7Q.png" /></figure><p>19 June 2018. Erlang OTP 21.0 has been released today and I’m going to describe how new :<strong>gen_server</strong> handle_continue callback works.</p><h3>Setup</h3><p>Erlang 21.0 and Elixir 1.6.6-otp-21 installed using <a href="https://github.com/asdf-vm/asdf">asdf version manager</a></p><h3>Let’s write something basic</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd3ae513957c94926b3a7e1873efcbbe/href">https://medium.com/media/bd3ae513957c94926b3a7e1873efcbbe/href</a></iframe><p>Initial state of our <strong>GenServer</strong> is 0 and it implements single handle_info that increments state by 1 and logs new value.</p><p>Inside init callback we are sending three :inc_and_log messages to <strong>GenServer</strong> itself and logging additional message, so when we start it we can see following logs:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/079c5ca897db4755224fe4f7cd59dd54/href">https://medium.com/media/079c5ca897db4755224fe4f7cd59dd54/href</a></iframe><p>Nothing special so far.</p><h3>Continue</h3><p>Let’s modify handle_info return value and add new handle_continue callback.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/221370ac45df6c7b3c43ec01c8c4bcb2/href">https://medium.com/media/221370ac45df6c7b3c43ec01c8c4bcb2/href</a></iframe><p>When we start our <strong>GenServer</strong> now, we can see different values in logs:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56f9b18dcd4b3ca7282850d50eb2e749/href">https://medium.com/media/56f9b18dcd4b3ca7282850d50eb2e749/href</a></iframe><p>The reason for this is that when any callback returns {:continue, term} then handle_continue is invoked immediately. In other words after every handle_info there was handle_continue which incremented state value one more time.</p><h3>More realistic use case</h3><p>Let’s imagine call that returns current state and calculates new one.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3f36a506d8dc545a144aa7f8553aa6af/href">https://medium.com/media/3f36a506d8dc545a144aa7f8553aa6af/href</a></iframe><p>Process invoking call doesn’t need to know new state, but it’s blocked until calculation ends.<br>It also can’t be solved by moving calculation to handle_info callback because it can’t be guaranteed that there are no other messages waiting for being processed.</p><p>It’s a kind of situation where handle_continue is very convenient:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c9e54a5a0e74e7e5ff0c8f36b3ff65b/href">https://medium.com/media/3c9e54a5a0e74e7e5ff0c8f36b3ff65b/href</a></iframe><h3>Last thing</h3><p>There&#39;s no support for handle_continue in elixir yet. It means that @impl true before handle_continue will result in compilation warning.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2ef0408391ff" width="1" height="1" alt=""><hr><p><a href="https://medium.com/appunite-edu-collection/quick-look-at-new-gen-server-handle-continue-callback-2ef0408391ff">Quick look at new :gen_server handle_continue callback</a> was originally published in <a href="https://medium.com/appunite-edu-collection">Appunite Labs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Phoenix Framework and protobufs]]></title>
            <link>https://medium.com/appunite-edu-collection/phoenix-framework-and-protobufs-faaf7482a23e?source=rss-a110ccc971fa------2</link>
            <guid isPermaLink="false">https://medium.com/p/faaf7482a23e</guid>
            <category><![CDATA[protobuf]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[phoenix-framework]]></category>
            <category><![CDATA[phoenix]]></category>
            <dc:creator><![CDATA[Tobiasz Małecki]]></dc:creator>
            <pubDate>Wed, 06 Sep 2017 11:28:25 GMT</pubDate>
            <atom:updated>2017-09-11T08:06:38.239Z</atom:updated>
            <content:encoded><![CDATA[<p>Although I could write a lot about why Google Protocol Buffers are better than json (trust me, they are), in this article I’m going to focus on tips that will make it easier to work with them.</p><h4>Elixir protobuf library</h4><p>As I write this article there is not much choice in this matter. <br>We are using library called <a href="https://github.com/bitwalker/exprotobuf"><em>exprotobuf</em></a><em>. </em>It’s not perfect (based on erlang library, error messages are not prettiest I’ve ever seen), but provides everything we needed.</p><p>Some good advice to start with:</p><ol><li>Create separate umbrella app for storing protobuf schemas (files with <em>.proto</em> extention) and protobuf modules (those with structs generated by exprotobuf). It’ll make your project much cleaner, also sooner or later you will find separate namespace very useful.</li><li>Protobuf modules will not be recompiled automatically when protobuf schema changes. To achieve this you need to add @external_resource module attribute for every <em>.proto</em> file. It may be good idea to create macro to make this less repetitive.</li><li>Remember that you need to recompile package <a href="https://github.com/elixir-plug/mime"><em>mime</em></a> after adding <em>application/x-protobuf</em> mime type in config.</li></ol><h4>Decoding requests</h4><p>Main difference between decoding json and protobufs is the need to specify which message you are currently decoding. The most convenient method for this is to create a plug which takes protobuf module as argument.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dff773d80c48560352a205cd982d6d3e/href">https://medium.com/media/dff773d80c48560352a205cd982d6d3e/href</a></iframe><p>It can be used in controller or router depending how much specific/universal your messages are.<br>As you can see in line 14 decoded protobuf struct is added to conn assigns and may be accessed from there.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6bc162f4ed97d5dcbe32075e2ada065d/href">https://medium.com/media/6bc162f4ed97d5dcbe32075e2ada065d/href</a></iframe><h4>Encoding responses</h4><p>The most obvious way to send protobuf response is to encode value and use<br><em>send_resp/2</em></p><p>It worked for us for very long time, but in life of every developer comes such a moment when he starts a new project. It’s good time to try different approach.</p><p>Let’s start with simple module:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99543bc8254019ef97e028b8cf868ae1/href">https://medium.com/media/99543bc8254019ef97e028b8cf868ae1/href</a></iframe><p>and add following config:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3dd26bf92fc17d7fb8ff0f59cfd89731/href">https://medium.com/media/3dd26bf92fc17d7fb8ff0f59cfd89731/href</a></iframe><p>It allows us to use views like this one:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/517904b2413469f31be0252266ccd450/href">https://medium.com/media/517904b2413469f31be0252266ccd450/href</a></iframe><p>without calling any <em>encode</em> function.</p><p>I’m still surprised how easy it was to setup this.</p><h4>Something you may not expect</h4><p>As experienced developer you probably use some kind of error tracking.<br>In our case it’s Sentry.</p><p>Unfortunately one day during the development our staging returned <em>500 internal server error</em>, but there wasn’t even the slightest sign of it among other captured errors. I won’t lie, it took me days to understand what happend.</p><p>Protobuf feature called <em>oneof</em> is represented by exprotobuf as two-element tuple. Sentry client (which is using json) couldn’t encode it properly.<br>When I discovered this, the fix was rather simple:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a12face60aa57be6038bd1526327e40/href">https://medium.com/media/9a12face60aa57be6038bd1526327e40/href</a></iframe><p>If second element of two-element tuple is protobuf struct whole is converted to list, otherwise it isn’t encoded, as we do not want to trigger any undesirable side-effects.</p><p><strong>Note</strong>: to make this work, you should avoid basic types (like string) inside oneof.</p><h4>The End</h4><p>Okay, I think it’s enough as for first article written by someone who hates to write such things.</p><p>There are still some uncovered topics. Probably most interesting one is how to use protobufs with phoenix channels.<br>Also, you may be interested in package called <a href="https://github.com/amatalai/proto_response"><em>proto_response</em></a>. It provides test helper similar to json_response or html_response, but for protobufs.</p><p>Don’t hesitate to leave a comment if you have question or know better solutions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=faaf7482a23e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/appunite-edu-collection/phoenix-framework-and-protobufs-faaf7482a23e">Phoenix Framework and protobufs</a> was originally published in <a href="https://medium.com/appunite-edu-collection">Appunite Labs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>