couchbaselabs / couchbase-shell

Shell yeah!
http://couchbase.sh
Apache License 2.0
58 stars 13 forks source link

Loop support with fake and tera #53

Open ingenthr opened 3 years ago

ingenthr commented 3 years ago

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?

chvck commented 3 years ago

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 commented 3 years ago

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"
    }
}