couchbase-shell icon indicating copy to clipboard operation
couchbase-shell copied to clipboard

Loop support with fake and tera

Open ingenthr opened this issue 5 years ago • 2 comments

based on tera, I tried this…

👤ingenthr at 🏠local in 🗄 default
> fake --template order-wip.tera --num-rows 5 | get content | get order_items
error: Error: expected `,` or `]` at line 11 column 26

👤ingenthr at 🏠local in 🗄 default
> open order-wip.tera
{
    "id": "{{ uuid() }}",
    "content": {
        "order_by": "{{ userName() }}",
        "address": "{{ numberWithFormat(format='^####') ~ ' ' ~ 
                       streetName() ~ '\n' ~
                       cityName() ~ ', ' ~
                       stateAbbr() ~ ' ' ~
                       postCode()  }}",
        "order_items": [
                         {% for i in range(end=5) %}
                         { "qty": "{{ numberWithFormat(format='^#')}}",
                           "item": "{{ words(num=3) }}" }
                         {% endfor %}
                       ],
        "type": "order"
    }
}

But it does not work. There does not seem to be looping support?

ingenthr avatar Sep 28 '20 21:09 ingenthr

I think that looping does work but it's going to be tricky to generate valid JSON. In your loop your objects need to be separated by a comma:

                         {% for i in range(end=5) %}
                         { "qty": "{{ numberWithFormat(format='^#')}}",
                           "item": "{{ words(num=3) }}" },
                         {% endfor %}

The problem is then that it'll still generate invalid JSON because it'll generate a trailing comma.

chvck avatar Oct 08 '20 12:10 chvck

The following works but is... icky:

{
    "id": "{{ uuid() }}",
    "content": {
        "order_by": "{{ userName() }}",
        "address": "{{ numberWithFormat(format='^####') ~ ' ' ~
                       streetName() ~ '\n' ~
                       cityName() ~ ', ' ~
                       stateAbbr() ~ ' ' ~
                       postCode()  }}",
        "order_items": [
                         {% for i in range(end=5) %}
                         { "qty": "{{ numberWithFormat(format='^#')}}",
                           "item": "{{ words(num=3) }}" }
                         {% if i < 4 %}
                           ,
                         {% endif %}
                         {% endfor %}
                       ],
        "type": "order"
    }
}

chvck avatar Oct 08 '20 13:10 chvck