How to get the last element inside array using VTL? - vtl

I am writing AWS AppSync resolver. Where I do need to get the last element in my array. AWS AppSync supports VTL language.
Example:
#set($items=["color", "taste", "shape"])
#set($result="shape")
I am using $array.size() but didn't work. I don't see any option in Utility Helpers.
.
#set($result=$item[$item.size()-1])
Thanks,

The solution is to make sure you are making the arithmetic operation inside -> set($result=$item[$item.size()-1]);
Set the array size into a separate variable and use another variable to get the last index.
#set($length=$facts.size())
#set($lastIndex = $length - 1)
#set($fact=$facts.get($lastIndex))

Related

How to use variables in LUA 5.1 Touchosc

I'm trying, to wrap my head around Touchosc and script based on LUA 5.1.
I have a number of labels, called song0, song1, song2, and so on. I'm trying to set different values in these, using
local text = 'Smoke On The Water'
for i = 1, 2 do
self.children.pager1.children.main.children.song[i].values.text = text
end
but that gives me an error.
:-) I do need help.
Finn
Since you haven't provided the actual error, it's difficult to say what the problem is, but if I have to venture a guess, then try replacing ...children.song[i].values... with ...children["song"..i].values.... Since there is no table song, you just need to generate dynamic field names.

Integromat Scenario: One-shot module after iterating through a loop

I have created a scenario where I iterate through multiple modules with an array of data. This works fine.
After this completes, I want to run a module once before the scenario completes.
How do I add a module that won't get called in the loop?
There are few ways to achieve this,
Use Router to Create a new Route that will be triggered after the
first route is complete
Trigger new Scenario via Webhooks after you are done with the
scenario
If you are working with array, then using Array Aggregator or other
Aggregators will allow you to first complete the iteration and then
trigger the module you want to use
I am not sure exactly what you want to do after the iteration is complete, but setting the scenarios as displayed in the screenshot below should help you get started on this,
Using Router
For this you can create a router, the upper hand of the router is always executed first, so the iterator and other operations will be done there. After which, the next hand/route will be executed which will be the module you want to trigger at last.
However, If you want to pass some values from the first hand/route to the last one then you will need to set a variable and fetch it on the second route. See details here : https://www.integromat.com/en/help/converger
Using Aggregator Module
You can either use Array, Text or Numeric Aggregator to aggregate all the iteration operations and then trigger the module that you want to use at last.
As far as my knowledge goes, there is no Integromat default modules that can be configured before the scenario ends. We can leverage the Integromat API in future that is currently in development to do so.
I found a filter to be the most easy way of doing this. Essentially chekcing if this bundle position is equal to the total number of bundles!
If you're interested in doing something on the last iteration only, you can use a filter to check if the current bundle is equal to the total number of bundles
last bundle filter
They won't let me paste pics sigh

Does Karate Support Def Variable update for latest data [duplicate]

This question already has an answer here:
Karate API Testing - Reusing variables in different scenarios in the same feature file
(1 answer)
Closed 1 year ago.
I am doing a test case where it will call API and that data will use next API call as part of One Scenario.
I am passing testdata as part of example 4 records .Here I have under one scenario first Given API call output passing to second given API call.As part of comapare the results i need the first API call output data to compare with second API call results.
So is there any way to capture all four test records data first API call data in one variable (each time variable to update)
example :
*def var = 'hello'
var = var +'world'
Please need help
Please read the docs, copied below for convenience: https://github.com/intuit/karate#script-structure
Variables set using def in the Background will be re-set before every Scenario. If you are looking for a way to do something only once per Feature, take a look at callonce. On the other hand, if you are expecting a variable in the Background to be modified by one Scenario so that later ones can see the updated value - that is not how you should think of them, and you should combine your 'flow' into one scenario. Keep in mind that you should be able to comment-out a Scenario or skip some via tags without impacting any others. Note that the parallel runner will run Scenario-s in parallel, which means they can run in any order.
So please don't expect a variable in one Scenario to be update-able by another Scenario.
But within a Scenario if you want to "collect" data, there are many ways. For example try appending to a list - refer: https://github.com/intuit/karate#json-transforms
* def init = []
# do some API call
* karate.appendTo(init, response)

AirPrint - Make number of copies default to 2 and not just 1 copy

When I open the print options using AirPrint, I want to make it default to 2 copies and not just 1 copy.
Couldn't find any methods that could change the number of copies. Wondering if anyone has any ideas if this is possible since it seems that this printing option is pretty limited.
So I found my own solution. Basically under the UIPrintInteraction Controller there is a method called printItems. What I did is make an NSArray and put two copies of the object I wanted to print and used that method instead of printItem.
However, since my application used the printFormatter I was unable to use this method since in the IOS developer documentation it says
"If you set this property (printFormatter), UIPrintInteractionController sets the printingItems, printingItem, and printPageRenderer properties to nil. (Only one of these properties can be set for a print job.)"
So what I did was just doubled the html page I was formatting and I trusted that the formatting was correct for it to print two pages.
Another way would be to add the same printFormatter to page 1 and page 2 in a printPageRenderer.

How to access a scope if its name is being used as a query column

Dealing with some legacy code we came across a rather annoying situation. We are looping through a query with the <cfoutput query="x"> tag. That query has a column named 'url'. Within that loop we need to check if a key exists within the url scope. Since CF puts a priority on what's in the query over general page scopes I can't use a structKeyExists(url,"key") since as far as CF is concerned at this point, url is a string with the value from the current row of the query.
How can I break out of the query scope and inspect what's in my url?
As a temporary we are using isDefined("url.key"), but I would still like to know if there is a way to break out of the query scope.
Also can't really change the column, or even the column name in the query without a few hours of work tracking down an changing all references to it, so we're going to avoid that if at all possible.
EDIT:
There seems to be some confusion as to how this code is set up, and why the simple solutions don't apply. It would be hard for me to give a thorough example but I will try to clarify the situation.
There are many pages that would count as 'pageA' for the following example. Enough that changing how things work would require a change in scope and investment in time that's just not going to happen in the time allotted.
PageA runs a query with one of the columns being named url, then starts an output loop via cfoutput, inside that loop PageB is included. One PageA may have different variables in the URL scope than another PageA, actually they are the same, but may be named differently(varID=x in one case vid=x in another). Inside of PageB I need to use the value from that url scope, so I want to run through the different possible names (if key 'varID' exists in url, use it, otherwise use 'vid').
This is why I want to "punch through" the query scope to get the url structure, and not the url column from the query. Any other method seems to require modifying the many PageAs.
So the question is not how to solve this problem specifically, as there are many ways to do it, I would just really like to avoid them as they all add a lot of time in implementation and testing. The question remains, is there a way to access the url scope as a variable if url exists as a query column and you are in the query scope.
I thought it might work to create a function that returned the url scope, but upon testing it, even with a local-scoped query (which prevents the function using the query itself) the use of url inside the function is still corrupted:
<cffunction name="getUrlScope"><cfreturn Url /></cffunction>
...
<cfoutput query="x">
<cfif StructKeyExists( getUrlScope() , 'key' )>
<!--- still fails :( --->
There is however an undocumented (meaning unsupported and liable to change) option. If you dump getPageContext() you will see a bunch of functions that do interesting things, including dealing with scopes.
You can use getPageContext().SymTab_findBuiltinScope('URL') to get at the URL scope.
You can also use getPageContext().getCfScopes() to get an array of scopes. I'm not sure if the order is guaranteed fixed but it seems to be [cgi,?,url,form,cookie,?] checking on both CF10 and cflive (CF9), so possibly is.
(In CF8 there was the method getBuiltinScopes, which returned a struct instead of an array - this no longer appears to exist, reinforcing the whole unsupported and changeable nature of these methods.)
On Railo those don't work, but there is getPageContext.UrlScope() and similarly-named functions for the other scopes.
One solution would be to assign the url struct to a new variable outside of the cfoutput tag and then reference that variable instead of url. Example:
<cfset urlScope = url>
<cfoutput query="x">
<cfset keyExists = structKeyExists(urlScope, "key")>
</cfoutput>
My solution for this is always to alias the url column in the query as int
SELECT URL as qURL FROM myTable ...
IF you don't have access to the query (it's a stored precedure or used elswhere etc) you can always use query of a query to reselect it with your alias.
I don't care for the idea of creating a separate reference to URL outside the output - but that would also work. I just want to KNOW what is user input (i.e. comes from the URL or FORM) and what is generated internally (i.e. comes from a query).
Couldn't you move structKeyExists(url,"key") outside of the cfoutput block, and store that into a variable? Or do a structAppend to copy the url struct into another struct named something else?
Another approach is to replace your cfoutput block with a cfloop block.
<cfloop from="1' to = "#YourQuery.recordcount#" index = "idx">
<cfif StructKeyExits(url,"key")>
<cfoutput>
#url.key# is not the same as #YourQuery.url[idx]#
which can also be referenced like this #YourQuery["url"][idx]
etc

Resources