Multi-line regex only matches if begin & end are on the same line - grammar

I'm trying to add a bit of grammar for the Liquid language to my custom extension. Here is the bit of code in question:
{% if type == "record"
or type == "record2"
%}
records
{% else %}
no_records
{% endif %}
Then I have the following rule in my language repository:
"liquid_tags": {
"name": "source.qqql.liquid",
"begin": "\\{(\\%)",
"end": "(\\%)\\}",
"beginCaptures": {
"1": {"name": "source.qqql.liquid.tag"}
},
"endCaptures": {
"1": {"name": "source.qqql.liquid.tag"}
}
},
The problem is that the %} of the if statement is not captured. Only the other closing tags are captured because they're on the same line.

Related

Shopify section schema: different content for each present block

I'm just starting out with Shopify development, and I'm creating a custom section called USP Banner which has a single block type called column that can be added up to 4 times; and that column just has one text and one richtext field within it. The content of each column is identical on every page, but the section can appear in different places depending on the page itself.
I've given the column default values for the text & richtext and added the 4 columns that will be used into the presets definition of the schema; however because it's just the same column x 4, each column has the same content 4 times. What I'm aiming for is that when you add the section to any page, it prefills each column with different content.
I read through the Shopify docs and all I found was some vague allusion to having a settings object within the block presets, but I can't find any examples of that anywhere. I also found an old SO answer stating that presets can't contain default content.
Is there an easy way to add default content to repeating blocks within a section?
Although it's not made clear in the official docs, you can set default values for repeated blocks in the presets; but whereas the settings when defining the blocks are arrays of objects, the settings within the preset blocks are objects where each pre-determined field value uses the field's unique ID as its key. For example, a typical section schema might look like:
{% schema %}
{
"name": "USP Banner",
"blocks": [
{
"type": "column",
"name": "Column",
"limit": 4,
"settings": [
{
"type": "text",
"id": "heading",
"label": "Heading",
},
{
"type": "richtext",
"id": "text",
"label": "Text",
}
]
}
],
"presets": [
{
"name": "USP Banner",
"blocks": [
{
"type": "column"
},
{
"type": "column"
}
]
}
]
}
{% endschema %}
...so to create preset values for each block, we simply add settings to the preset definition like so:
"presets": [
{
"name": "USP Banner",
"blocks": [
{
"type": "column",
"settings": {
"heading": "Remarkable Value",
"text": "<p>Never knowingly undersold</p>"
}
},
{
"type": "column",
"settings": {
"heading": "5-Year Warranty",
"text": "<p>We're that confident in our products</p>"
}
}
]
}
]
...where the key for each field (heading & text in this case) is the ID of the defined fields in the blocks section at the top of the schema.

Liquid: successive random numbers excluding the first results

The purpose of the code is to ask random security questions using Liquid. I used the following code which creates a random number which is then replaced by a question:
{% assign min = 1 %}
{% assign max = 7 %}
{% assign diff = max | minus: min %}
{% assign randomNumber1 = "now" | date: "%N" | modulo: diff | plus: min %}
{{randomNumber1 | replace: "1", "Question 1" | replace: "2", "Question 2" | replace: "3", "Question 3" | replace: "4", "Question 4"| replace: "5", "Question 5"| replace: "6", "-Question 6"| replace: "7", "Question 7"}}
I copied this code three times to have three numbers/questions (with randomNumber1, randomNumber2, randomNumber3). The tricky part is that a question should not appear twice.
For that randomNumber2 must never be equal to randomNumber1 and randomNumber3 must never be equal to randomNumber1 and randomNumber2. Unfortunately I have not found an ideal solution to code these conditions.
Do you have one maybe? Thanks :)

Passing nested dict payloads from `config` to macros in DBT

Problem
I would like to pass a nested dict-of-dicts payload from a model's config to be used in a macro for processing. The payload should be a dictionary with an arbitrary number of keys, and parameters for each of these keys, e.g.:
{{ config(
payload = {
'inputs': {
'input1': {'param1': false, 'param2': 'test'},
'input2': {'param1': false, 'param2': 'test'},
},
'other_params': {
'interval': 1,
'unit': 'hour',
}
})
}}
However I am encountering an error in passing nested dictionary args in the config variable to a macro that uses the payload.
When I try to access the items in the input payload, with e.g. payload.get('inputs'), I get the following error:
Running with dbt=0.20.0
Encountered an error:
Compilation Error in model tf_sample (models/intermediate/development/tf_sample.sql)
'str object' has no attribute 'get'
> in macro test_payload (macros/test_payload.sql)
> called by model tf_sample (models/intermediate/development/tf_sample.sql
However, I believe this issue is specific to how the config variables are being handled, because if I pass the payload dict into the macro directly, the macro will parse successfully.
Question
How can I create a payload dict in this format to be passed to a macro?
Minimal Working Example of the Problem
Below I have created a MWE to demonstrate the issue:
Example macro: loop over the provided payload.get('inputs')
-- macros/test_payload.sql
{% macro test_payload(payload=config.get('payload')) -%}
{% for input, params in payload.get('inputs').items() %}
-- {{loop.index }}: {{input}}, {{params}}
{% endfor %}
{% endmacro %}
~
However this macro will not succeed to pull in the config.get('payload') in the way that I am expecting:
Example model calling test_payload() -- this will FAIL to parse
The below SQL calls the macro in the way I would like (and would expect to work):
-- model_1.sql --- This will FAIL to parse
{{
config(
payload = {
'inputs': {
'input1': {'param1': false, 'param2': 'test'},
'input2': {'param1': false, 'param2': 'test'},
},
'other_params': {
'interval': 1,
'unit': 'hour',
}
}
)
}}
-- this fails
{{ test_payload(config.get('payload')) }}
-- this also fails
{{ test_payload() }}
Example model calling test_payload() with an embedded dict; SUCCEEDS
However if I pass the payload in directly to the macro test_payload, I successfully get the expected output:
-- model_2.sql --- This will SUCCEED! But it doesn't store the payload in the config :(
{{ test_payload(payload={
'inputs': {
'input1': {'param1': false, 'param2': 'test'},
'input2': {'param1': false, 'param2': 'test'},
},
'other_params': {
'interval': 1,
'unit': 'hour',
}
}) }}
Result:
-- 1: input1, {'param1': False, 'param2': 'test'}
-- 2: input2, {'param1': False, 'param2': 'test'}
Do you need to have the payload variable inside config for any reason? If that is not the case, you can create variables outside of it and it will work, something like this:
{
set payload = {
'inputs': {
'input1': {'param1': false, 'param2': 'test'},
'input2': {'param1': false, 'param2': 'test'},
},
'other_params': {
'interval': 1,
'unit': 'hour',
}
}
}
{{ test_payload(payload) }}
Then, the macro definition won't need the default config.get('payload'):
{% macro test_payload(payload) -%}

How to validate Nested JSON Response

I am facing issue while validate Nested JSON response in API Testing using Karate Framework.
JSON Response:
Feed[
{ "item_type": "Cake" ,
"title": "Birthday Cake",
"Services":
[
{
"id": "1",
"name": {
"first_name": "Rahul",
"last_name": "Goyal"
}
},
{
"id": "2",
"name":{
"first_name": "Hitendra",
"last_name": "garg"
}
}
]
},
{
"item_type":"Cycle",
"title": "used by"
},
{
"item_type": "College"
"dept":
[
{"branch": "EC"},
{"branch": "CSE"},
{"branch": "CIVIL"}
]
},
]
}
Now i need to validate response based on Item type. as we can see nested JSON is different for different item_type.
I have tried with below solution
Schema Design for Item_type value cake
def Feed_Cake_Service_name={first_name: '#string',last_name: '#string'}
def Feed_Cake_Services= {id: '#string',name:#(Feed_Cake_Service_name)}
def Feed_Cake={item_type:'#string',title: '#string',Services: '#[] Feed_Cake_Services'}
def Feed_Cake_Response= {Feed: '#[] Feed_Cake'}
Schema Design for item_type Cycle
def Feed_Cycle={item_type:'#string',title:'#string'}
Schema Design for item type College
def Feed_College_Dept_Branch={branch:'#string'}
def Feed_College={item_type:'#string',dept: '[] Feed_College_Dept_Branch'}
now if i want to verify only item type Cake then i have written match like below
match response contains Feed_Cake_Response
but here my test case is getting failed. because it is comparing for all item type.
so here i have two question
1.) How we can compare particular item type schema
2.) How we can include all item type in one match equation since any item type can come in JSON response , and i want to validate all
Thanks
I'll just give you one hint. For the rest, read the documentation please:
* def item = { item_type: '#string', title: '##string', dept: '##[]', Services: '##[]' }
* match each response == item

Accessing nested values from YAML data in a Twig template

I am trying to access nested YML data in a Twig template. My YML data is structured like this:
card_default:
card_title: 'Card Title'
card_header: 'This is the card header on a multi-part card'
card_text: "Some quick example text to build on the card title and make up the bulk of the card's content."
card_link: 'https://github.com/phase2/particle'
card_background: primary
card_image_location: top
card_footer: "This is the card footer"
text_color: uk-dark
card_body: "This is some card body text"
card_style: default
card_image:
card_more_stuff in here....
... and then I call some of the data in a Twig template like this:
{% include '#molecules/card/_card.twig' with {
card_default: {
card_title: card_title,
card_text: card_text,
card_background: 'primary',
card_link: card_link,
card_link_text: card_link_text,
card_link_class: card_link_class,
}
} only %}
But that does not seem to work. I have a feeling the way I am trying to do this is not quite right but a search didn't give me any more insight. Essentially I want to access the values within card_default.
I can see all the data in the array if I dump with {{ dump(card_default) }}
array(14) { ["card_title"]=> string(10) "Card Title" ["card_header"]=> string(44) "This is the card header on a multi-part card" ["card_text"]=> string(94) "Some quick example text to build on the card title and make up the bulk of the card's content." ["card_link"]=> string(34) "https://github.com/phase2/particle" ["card_link_text"]=> string(9) "Read more" ["card_link_class"]=> string(27) "uk-button uk-button-default" ["card_background"]=> string(7) "primary" ["card_width"]=> int(25) ["card_image_location"]=> string(3) "top" ["card_footer"]=> string(23) "This is the card footer" ["list"]=> array(2) { ["list_flush"]=> bool(true) ["items"]=> array(3) { [0]=> array(1) { ["item_text"]=> string(15) "Cras justo odio" } [1]=> array(1) { ["item_text"]=> string(23) "Dapibus ac facilisis in" } [2]=> array(1) { ["item_text"]=> string(18) "Vestibulum at eros" } } } ["text_color"]=> string(7) "uk-dark" ["card_body"]=> string(27) "This is some card body text" ["card_style"]=> string(7) "default" }
The data is in the variable card_default, so it should be e.g. card_default.card_title
but instead of creating a whole new object you just could do this in 2 ways:
YAML
foo:
bar: 'foobar'
number: 42
main.twig
{% include "full.twig" with { 'foo' : foo } only %}
{% include "slim.twig" with foo only %}
full.twig
{{ foo.bar }}
slim.twig
{{ number }}
I figured this out, I just needed to map the nested items properly like so:
{% include '#molecules/card/_card.twig' with {
card_title: card_default.card_title,
card_text: card_default.card_text,
card_background: 'primary',
card_link: card_default.card_link,
card_link_text: card_default.card_link_text,
card_link_class: card_default.card_link_class,
} %}
In the above code, card_default is mapped in the variable portion of the array, i.e., after the colon. card_link: card_default.card_link,