Skip to content

Latest commit

 

History

History
447 lines (376 loc) · 6.88 KB

supported-patterns.md

File metadata and controls

447 lines (376 loc) · 6.88 KB

Supported Patterns

You can compose a mapping template that has a pattern described here.

All of the patterns described here are tested in test/compose.test.ts.

Simple properties

You can describe an object with one or more properties.

import { composeMappingTemplate } from 'mapping-template-compose';

composeMappingTemplate([
  ['key', '"value"'],
  ['key2', '123'],
  ['key3', 'true'],
]);

will produce:

{
  "key": "value",
  "key2": 123,
  "key3": true
}

#if directive

Simgle #if

import { composeMappingTemplate, ifThen } from 'mapping-template-compose';

composeMappingTemplate([
  ifThen(
    '$input.params("flag") == "on"',
    [
      ['key', '"value"'],
      ['key2', '123'],
      ['key3', 'true'],
    ],
  ),
]);

will produce:

{
#if ($input.params("flag") == "on")
  "key": "value",
  "key2": 123,
  "key3": true
#end
}

#if followed by a property

import { composeMappingTemplate, ifThen } from 'mapping-template-compose';

composeMappingTemplate([
  ifThen(
    '$input.params("flag") == "on"',
    [['key', '"value"']],
  ),
  ['key2', '123'],
]);

will produce:

{
#if ($input.params("flag") == "on")
  "key": "value",
#end
  "key2": 123
}

#if following a property

import { composeMappingTemplate, ifThen } from 'mapping-template-compose';

composeMappingTemplate([
  ['key', '"value"'],
  ifThen(
    '$input.params("flag") == "on"',
    [
      ['key2', '123'],
      ['key3', 'true'],
    ],
  ),
]);

will produce:

{
  "key": "value"
#if ($input.params("flag") == "on")
  ,"key2": 123,
  "key3": true
#end
}

Consecutive #if

import { composeMappingTemplate, ifThen } from 'mapping-template-compose';

composeMappingTemplate([
  ifThen(
    '$input.params("flag") == "on"',
    [['key', '"value"']],
  ),
  ifThen(
    '$input.json("$.page") > 1',
    [['key2', '123']],
  ),
  ifThen(
    '$input.params("date") != ""',
    [['key3', 'true']],
  ),
]);

will produce:

{
#if ($input.params("flag") == "on")
  "key": "value"
#end
#if ($input.json("$.page") > 1)
#if ($input.params("flag") == "on")
  ,
#end
  "key2": 123
#end
#if ($input.params("date") != "")
#if (($input.params("flag") == "on") || ($input.json("$.page") > 1))
  ,
#end
  "key3": true
#end
}

Nested #if

import { composeMappingTemplate, ifThen } from 'mapping-template-compose';

composeMappingTemplate([
  ifThen(
    '$input.params("flag") == "on"',
    [
      ifThen(
        '$input.json("$.page") > 1',
        [['key', '"value"']],
      ),
    ],
  ),
  ifThen(
    '$input.params("date") != ""',
    [['key2', '123']]
  ),
]);

will produce:

{
#if ($input.params("flag") == "on")
#if ($input.json("$.page") > 1)
  "key": "value"
#end
#end
#if ($input.params("date") != "")
#if (($input.params("flag") == "on") && ($input.json("$.page") > 1))
  ,
#end
  "key2": 123
#end
}

Nested #if 2

import { componentMappingTemplate, ifThen } from 'mapping-template-compose';

composeMappingTemplate([
  ifThen(
    '$input.params("flag") == "on"',
    [['key', '"value"']],
  ),
  ifThen(
    '$input.json("$.page") > 1',
    [ifThen(
      '$input.params("date") != ""',
      [['key2', '123']],
    )],
  ),
]);

will produce:

{
#if ($input.params("flag") == "on")
  "key": "value"
#end
#if ($input.json("$.page") > 1)
#if ($input.params("date") != "")
#if ($input.params("flag") == "on")
  ,
#end
  "key2": 123
#end
#end
}

#if-#else directive

Simple #if-#else

import { composeMappingTemplate, ifThenElse } from 'mapping-template-compose';

composeMappingTemplate([
  ifThenElse(
    '$input.params("flag") == "on"',
    [['key', '"value"']],
    [['key2', '123']],
  ),
]);

will produce:

{
#if ($input.params("flag") == "on")
  "key": "value"
#else
  "key2": 123
#end
}

#if-#else followed by a property

import { composeMappingTemplate, ifThenElse } from 'mapping-template-compose';

composeMappingTemplate([
  ifThenElse(
    '$input.params("flag") == "on"',
    [['key', '"value"']],
    [['key2', '123']],
  ),
  ['key3', 'true'],
]);

will produce:

{
#if ($input.params("flag") == "on")
  "key": "value",
#else
  "key2": 123,
#end
  "key3": 'true'
}

#if-#else following a property

import { composeMappingTemplate, ifThenElse } from 'mapping-template-compose';

composeMappingTemplate([
  ['key', '"value"'],
  ifThenElse(
    '$input.params("flag") == "on"',
    [['key2', '123']],
    [['key3', 'true']],
  ),
]);

will produce:

{
  "key": "value",
#if ($input.params("flag") == "on")
  "key2": 123
#else
  "key3": true
#end
}

#if-#else nesting #if and followed by #if

import { composeMappingTemplate, ifThen, ifThenElse } from 'mapping-template-compose';

composeMappingTemplate([
  ifThenElse(
    '$input.params("flag") == "on"',
    [ifThen(
      '$input.json("page") > 1',
      [['key', '"value"']],
    )],
    [ifThen(
      '$input.params("date") != ""',
      [['key2', '123']],
    )],
  ),
  ifThen(
    '$input.json("amount") > 1000',
    [['key3', 'true']],
  ),
]);

will produce:

{
#if ($input.params("flag") == "on")
#if ($input.json("page") > 1)
  "key": "value"
#end
#else
#if ($input.params("date") != "")
  "key2": 123
#end
#end
#if ($input.json("amount") > 1000)
#if ((($input.params("flag") == "on") && ($input.json("page") > 1)) || ((!($input.params("flag") == "on")) && ($input.params("date") != "")))
  ,
#end
  "key3": true
#end
}

#if-#else nesting #if and following #if

import { composeMappingTemplate, ifThen, ifThenElse } from 'mapping-template-compose';

composeMappingTemplate([
  ifThen(
    '$input.params("flag") == "on"',
    [['key', '"value"']],
  ),
  ifThenElse(
    '$input.json("page") > 1',
    [ifThen(
      '$input.params("date") != ""',
      [['key2', '123']],
    )],
    [ifThen(
      '$input.json("amount") > 1000',
      [['key3', 'true']],
    )],
  ),
]);

will produce:

{
#if ($input.params("flag") == "on")
  "key": "value"
#end
#if ($input.json("page") > 1)
#if ($input.params("date") != "")
#if ($input.params("flag") == "on")
  ,
#end
  "key2": 123
#end
#else
#if ($input.json("amount") > 1000)
#if ($input.params("flag") == "on")
  ,
#end
  "key3": true
#end
#end
}

Embedding a composed object in another mapping template

You can embed a result of composeMappingTemplate as a property value in another mapping template:

import { composeMappingTemplate, ifThen } from 'mapping-template-compose';

composeMappingTemplate([
  ['embedded', composeMappingTemplate([
    ['key', '"value"'],
    ifThen(
      '$input.params("flag") == "on"',
      [['key2', '123']],
    ),
  ])],
  ['key3', 'true'],
]);

will produce:

{
  "embedded": {
    "key": "value"
  #if ($input.params("flag") == "on")
    ,"key2": 123
  #end
  },
  "key3": true
}