Reading:
Creating YAML version of API Console Resource XML
Share:

Creating YAML version of API Console Resource XML

Metamug

//: # ()

Motivation

  • XML is more verbose, yaml syntax is clearer. YAML is more powerful than JSON when it comes to specifying complex data structures. It's a superset of JSON.
  • Developers can easily write SQL statements inside yaml document.
  • Skip the root element

Method

When designing the YAML version of Metamug API Console, we cannot directly map the yml file to xml resource format. It needs to be created to suit the yaml conventions. There are solutions to handle xml attributes in yaml, but for our conversion we are not going to resort to those means.

v: 1.1

get:
  sql:
     - select * from table1
     - select col1, col2 
       from table2
  api:
    url: https://api.example.com/test
    method: POST
    param:
      param1: 123
      param2: value2
    headers:
      accept: application/json
    body: some content to send in the api
post:
  sql:
     - select col1, col2 
       from table2
     - update table2 set col1='val1' where col2='val2'
  execute: com.metamug.file.Uploader

The above version translates to below xml, if a direct conversion is used.

<?xml version="1.0" encoding="UTF-8" ?>
<root>
    <v>1.1</v>
    <get>
        <sql>select * from table1</sql>
        <sql>select col1, col2 from table2</sql>
        <api>
            <url>https://api.example.com/test</url>
            <method>POST</method>
            <param>
                <param1>123</param1>
                <param2>value2</param2>
            </param>
            <headers>
                <accept>application/json</accept>
            </headers>
            <body>some content to send in the api</body>
        </api>
    </get>
    <post>
        <sql>select col1, col2 from table2</sql>
        <sql>update table2 set col1='val1' where col2='val2'</sql>
        <execute>com.metamug.file.Uploader</execute>
    </post>
</root>

We need to additionally apply specific conversions for attributes to match resource file format

Skipping the root element

In the above json format, you must have noticed, we have skipped the root Resource element. We can directly start with version and other usual information. This inspiration has been taken by looking at other formats

Handling XML escape characters in YML

When writing SQL statements, in case of XML it was required to escape xml characters with le, gt etc. Not anymore, now we can simply write sql queries without replacing escape characters. Now you must be thinking, how to escape yaml characters, in that case, wrapping with quotes would solve the problem.


Share this article:
Icon For Arrow-up