<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Laurent Bovet</title>
    <description>The latest articles on DEV Community by Laurent Bovet (@lbovet).</description>
    <link>https://dev.to/lbovet</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F327085%2F55612f8a-2146-47e9-a106-befd8bcb7f04.jpeg</url>
      <title>DEV Community: Laurent Bovet</title>
      <link>https://dev.to/lbovet</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lbovet"/>
    <language>en</language>
    <item>
      <title>Painless YAML Templating</title>
      <dc:creator>Laurent Bovet</dc:creator>
      <pubDate>Thu, 30 Jan 2020 13:00:52 +0000</pubDate>
      <link>https://dev.to/lbovet/painless-yaml-templating-2n2m</link>
      <guid>https://dev.to/lbovet/painless-yaml-templating-2n2m</guid>
      <description>&lt;h2&gt;
  
  
  ❤️ YAML ?
&lt;/h2&gt;

&lt;p&gt;YAML is ubiquitous. Wether you love it or hate it, you have no choice but using it. It established itself as &lt;em&gt;the&lt;/em&gt; configuration format for all things cloud/devops/serverless (choose your buzzword).&lt;/p&gt;

&lt;p&gt;The main characteristic of YAML is the role played by semantic indentation. It makes lazy humans save typing braces to delimit block they would indent anyway.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;tbody&gt;&lt;tr&gt;
&lt;th&gt;YAML&lt;/th&gt;
&lt;th&gt;JSON&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;

&lt;pre&gt;
hello:
  foo: bar            


&lt;/pre&gt;
&lt;/td&gt;
&lt;td&gt;

&lt;pre&gt;{
  "hello": {
    "foo": "bar"       
  }
}
&lt;/pre&gt;

&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
    

&lt;blockquote&gt;
&lt;p&gt;Humans like YAML because it focuses on the content&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Complexity Increase
&lt;/h2&gt;

&lt;p&gt;Automation is at the heart of everything in the raise of Infrastructure-as-Code, Cloud, DevOps, GitOps, etc. &lt;br&gt;
Containerization also contributes to the exponentially growing number of configuration combinations.&lt;/p&gt;

&lt;p&gt;Inevitably, a need for structure and factorization arised.&lt;/p&gt;
&lt;h2&gt;
  
  
  Text Templating to the Rescue
&lt;/h2&gt;

&lt;p&gt;That's why most of the tools ingesting YAML configuration support some kind of templating.&lt;/p&gt;

&lt;p&gt;The first generation approach relies on existing text templating libraries (&lt;a href="http://jinja.palletsprojects.com/"&gt;Jinja&lt;/a&gt;, &lt;a href="https://golang.org/pkg/text/template/"&gt;Go templates&lt;/a&gt;, &lt;a href="https://helm.sh/docs/chart_template_guide/"&gt;Helm&lt;/a&gt;, ...).&lt;br&gt;
It is a clever but lazy move because it does not work well when indentation comes into play.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Whoever struggled with &lt;code&gt;toYaml&lt;/code&gt; in Helm charts is a victim of YAML text templating.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;From a user experience point of view, the opening and closing symbols that YAML avoided come back in the templating language.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;
  &lt;span class="pi"&gt;{{&lt;/span&gt; &lt;span class="nv"&gt;if eq .Values.favorite.drink "coffee"&lt;/span&gt; &lt;span class="pi"&gt;}}&lt;/span&gt;
  &lt;span class="na"&gt;mug&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
  &lt;span class="pi"&gt;{{&lt;/span&gt; &lt;span class="nv"&gt;end&lt;/span&gt; &lt;span class="pi"&gt;}}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Alternative: a Dedicated Language
&lt;/h2&gt;

&lt;p&gt;To avoid the drawbacks of text templating, one can adopt a configuration language providing the programmatic features needed to tackle the complexity and generate the YAML. This is typically the approach taken by &lt;a href="https://jsonnet.org/"&gt;Jsonnet&lt;/a&gt;. It is a powerful language that natively understands the data structure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsonnet"&gt;&lt;code&gt;  &lt;span class="nx"&gt;Martini&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;ingredients&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;$&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'Tom Collins'&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;ingredients&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;qty&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'Dry White Vermouth'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;qty&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It can output in many formats, including YAML. &lt;/p&gt;

&lt;p&gt;So, everyone should adopt this no? Why is it not the case?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Try to switch to a new language, you will just add a new one to your already bloated Babel tower.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It is difficult to adopt Jsonnet because it cannot be introduced progressively. You have to make a dramatic switch in order to get the benefits. Few organizations are capable of driving such changes in a top-down way.&lt;/p&gt;

&lt;p&gt;As all the examples and litterature about YAML-based tools are written in YAML, using Jsonnet imposes to systematically translate them.&lt;/p&gt;

&lt;p&gt;And note that these unwanted opening and closing braces are back again...&lt;/p&gt;

&lt;h2&gt;
  
  
  Enters Structural YAML Templating
&lt;/h2&gt;

&lt;p&gt;So, the truth is in a middle way.&lt;/p&gt;

&lt;p&gt;Could we write plain YAML and add programmatic features to it in some undisturbing way?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://get-ytt.io/"&gt;YTT&lt;/a&gt; does this with a language written in YAML comments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;  &lt;span class="c1"&gt;#@ for/end echo in data.values.echos:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;#@ name(echo)&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;hashicorp/http-echo&lt;/span&gt;
    &lt;span class="na"&gt;args&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="c1"&gt;#@ "-listen=:" + str(echo.port)&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="c1"&gt;#@ "-text=" + echo.text&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The structure of YAML is preserved and understood by the tool.&lt;br&gt;
Is this the ultimate definitive way to solve the problem? &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;As a developer, don't you a feel slight discomfort when you write code inside comments?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As YTT is designed as replacement of text templating for configuration files, it provides a set of operations optimized for the task. Some features like overlays are powerful, some others like a single source for template values are limitating.&lt;/p&gt;
&lt;h2&gt;
  
  
  Leveraging YAML Tags
&lt;/h2&gt;

&lt;p&gt;Thinking about this, I reminded &lt;a href="https://github.com/wfrog/wfrog/tree/master/wfcommon/config"&gt;some work I did with YAML about ten years ago&lt;/a&gt;. It is a dependency injection system for Python &lt;em&gt;à la&lt;/em&gt; Spring Framework. It uses the YAML tag system to create a configuration corresponding to Spring's Application Context. This YAML, thanks to the tag system and anchors is directly deserialized as wired singletons forming the application structure.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;YAML's strength is in the tags&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With YAML tags in and mind inspiration from YTT and Jsonnet, came the design of &lt;a href="https://yglu.io"&gt;Yglu ᕄ !?&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Input&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;greeting&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;!-&lt;/span&gt; &lt;span class="s"&gt;Hello&lt;/span&gt;
&lt;span class="na"&gt;greeter&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;!()&lt;/span&gt;
  &lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;!?&lt;/span&gt; &lt;span class="s"&gt;len($)&lt;/span&gt;
  &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;!?&lt;/span&gt; &lt;span class="s"&gt;$_.greeting + ', ' + $&lt;/span&gt;  
&lt;span class="na"&gt;names&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;!-&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;world&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;foo&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;bar&lt;/span&gt;
&lt;span class="na"&gt;messages&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="kt"&gt;!for&lt;/span&gt; &lt;span class="s"&gt;$_.names&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;!()&lt;/span&gt;
     &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="kt"&gt;!?&lt;/span&gt; &lt;span class="s"&gt;($_.greeter)($)&lt;/span&gt;          
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Output&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;messages&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;
  &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Hello, world&lt;/span&gt;        
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;
  &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Hello, foo&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;
  &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Hello, bar&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Find more examples in the &lt;a href="https://yglu.io"&gt;online playground&lt;/a&gt; and the &lt;a href="https://github.com/lbovet/yglu/tree/master/tests/samples"&gt;test samples&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;With the tag system and a powerful expression language (&lt;a href="http://yaql.readthedocs.io/"&gt;YAQL&lt;/a&gt;, in this case), YAML structural templating features can be truly idiomatic.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrap-Up
&lt;/h2&gt;

&lt;p&gt;Leveraging YAML tags for creating a balanced DSL with a functional expression language brings YAML templating to a next level. &lt;/p&gt;

&lt;p&gt;It allows for introducing YAML factorization progressively, integrate with existing YAML tooling and provide an idiomatic user experience.&lt;/p&gt;

&lt;p&gt;It is probably the correct base for the tools to come. Maybe with other expression languages, but certainly with YAML tags.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://yglu.io"&gt;&lt;b&gt;Yglu ᕄ !?&lt;/b&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>yaml</category>
      <category>templating</category>
      <category>devops</category>
      <category>kubernetes</category>
    </item>
  </channel>
</rss>
