JEXL – Get current date and time - jexl

I have to use JEXL expression and I need to get current year and month using JEXL. Is it possible?

You can use JETT which allow to invoke static methods in JEXL code:
ETT extends JEXL to allow references to static methods directly in JEXL Expressions
Then you can call (Java 8) LocalDateTime.now method to get current date and time:
${java.time.LocalDateTime.now()}

Related

Overriding DateTime.now() at a local context without using a third party lib in python

I am looking into ways for overriding datetime.now() or similar method in python. Ideally I would like to override on a per request basis or control cases where I want to freeze the datetime.now() and not necessarily all calls to datetime.now(). From what I understand, libraries like freezegun freeze all calls to datetime.now().
I want to implement a new version of Datettime, say Datettime.override.
Have two variables called frozenTime which has a frozen time value.
Implement datetime.provider(frozenTime) which returns either return default datetime impl or return datettime.override if frozenTime > 0

SOAPUI context variables - How does Groovy make this possible?

Sorry to all you Groovy dudes if this is a bit of a noob question.
In SOAPUI, i can create a Groovy script where i can define an arbitrary variable to the run context to retrieve at a later time.
context.previouslyUndefinedVariable = 3
def num = context.previouslyUndefinedVariable
What feature of Groovy allows previously undefined variables to be added to an object like this? I would like to learn more about it.
Many thanks in advance!
Groovy has the ability to dynamically add methods to a class through metaprogramming.
To learn more, see:
What is Groovy's MetaClass used for?
Groovy Goodness: Add Methods Dynamically to Classes with ExpandoMetaClass
Runtime and compile-time metaprogramming
The accepted answer is a bit of a poor explanation for how SoapUI is doing it.
In this case, context is always an instance of some SoapUI library java class (such as WsdlTestRunContext), and these are all implementations of Map. You can check context.getClass() and assert context in Map.
When you look up a property on a Map, Groovy uses the getAt and putAt methods. There are various syntaxes you can use. All of these are equivalent:
context.someUndef
context.'someUndef'
context[someUndef]
context['someUndef']
context.getAt('someUndef')
And
context.someUndef = 3
context.'someUndef' = 3
context[someUndef] = 3
context['someUndef'] = 3
context.putAt('someUndef', 3)
I like to use any of the above that include quote marks, so that Groovy-Eclipse doesn't flag it as a missing property.
It's also interesting that Groovy looks for a getAt() method before it checks for a get method being referred to as a property.
For example, consider evaluating "foo".class. The String instance doesn't have a property called class and it also doesn't have a method getAt(String), so the next thing it tries is to look for a "get" method with that name, i.e. getClass(), which it finds, and we get our result: String.
But with a map, ['class':'bar'].class refers to the method call getAt('class') first, which will be 'bar'. If we want to know what type of Map it is, we have to be more specific and write in full: ['class':'bar'].getClass() which will be LinkedHashMap.
We still have to specify getClass() even if that Map doesn't have a matching key, because ['foo':'bar'].class will still mean ['foo':'bar'].getAt('class'), which will be null.

Bitbucket - Groovy Pre-receive Hook

I am trying to understand the following code snippet following site.
<% refChanges.getCommits(repository).each { commit -> %>
- ${commit.author.name} | ${commit.displayId} | ${commit.message} | ${commit.authorTimestamp}
<% } %>
The script is using a getCommits method, but when I look at the documentation for the RefChange interface I do not see any such method.
I consider myself an expert Java developer, but I have no workable knowledge in Groovy, so I assume that I am misunderstanding Groovy or the BitBucket documentation (or both).
In Groovy it's possible to add methods to a class or interface at run-time via meta-programming. Since the RefChange interface does not include getCommits(), it must be that the method is being added after-the-fact. Based on their example code, it looks like they're using the meta-class.
Where is getCommits()?
For example, in Groovy the Collection interface gets the method findAll() (along with many other methods). I can confirm this as follows:
assert Collection.metaClass.metaMethods*.name.contains('findAll') == true
The code above grabs the names of all the meta methods and then uses contains() to see if a match is found. You can confirm the same for getCommits() in a similar way:
assert Collection.metaClass.metaMethods*.name.contains('getCommits') == true
Note that I specified Collection rather than RefChange because refChanges is a Collection of RefChange. And so I think Atlasssian stuck getCommits() into Collection as a convenience method.
How does it work?
To understand what's going, I'll remove the templating code:
refChanges.getCommits(repository).each { commit ->
"${commit.author.name} | ${commit.displayId} | ${commit.message} | ${commit.authorTimestamp}"
}
getCommits() returns a Collection of com.atlassian.bitbucket.commit.Commit.
Object.each(Closure) is added by the Groovy GDK (yes, into the Object class) and it calls the Closure repeatedly; each time with an element of the Collection.
Although it's not apparent in the example code, the line within the each(Closure) is a GString. Basically the expressions within the ${...} are evaluated and the whole thing is concatenated into a String.

Typelite: dates in DTO gets mapped to Date, but are actually ISO8601 strings

I use ServiceStack. In my C# DTO I have an attribute
public DateTime Created { get; set; }
which is mapped to TypeScript using TypeLITE http://type.litesolutions.net/
interface Instance {
Created: Date;
However, since I do not want to send JavaScript specific date format over the API, I use ISO dates using this ServiceStack setting
JsConfig.DateHandler = JsonDateHandler.ISO8601;
So, I have a mismatch here. One solution is to instead use the Typescript class
interface Instance {
Created: string;
and parse the date when used. Where would I put the call Date.parse('iso-date-string')? The ambitious solution is to have the TypeScript class twice, one with string, and one with Date.
In order to make TypeLITE generate string instead of Date for DateTime, what should I do?
The number of classes is rather small, so I can fix them up by hand. But, my idea is that I would have a special API call where the client can download the TypeScript declarations, and then, a complete automated TypeLITE would be much nicer.
You need to create a TypeLITE convertor for the DateTime:
TypeScript.Definitions()
.ForLoadedAssemblies()
.WithConvertor<DateTime>(t => "string")

Passing a String by Reference in Metro/C++?

I'm working on a C++ Metro style app and have to pass a string by reference (somehow). At first, I passed the String^ which doesn't work because strings are immutable how I have found out.
What would be a proper way to pass a string by reference?
Edit: OK, it seems that it's not that easy since the answers and comments suggest to use return values. But as far as I think this is not applicable in my situation: In this Metro app I have two pages and a string should be "shared" across those two pages.
So in the main page I do this in a click event:
this->Frame->Navigate(newPage, this->TestString);
In the OnNavigatedTo event of the second page I convert the second parameter to a String^ and change it. Then I use this->Frame->GoBack() to navigate back to the first page. There I'd like to have access to the changed string. Unfortunately, GoBack() doesn't allow to pass any parameters as far as I know.
You can use a tracking reference:
void ModifyTheParameter(String^% value) {
value = gcnew String("Blah");
}
That would modify the original variable you passed in as parameter (see MSDN for more info and examples). It would then be used just as any other method taking a String^ parameter.
But if possible, avoid using tracking references as parameters. I'd recommend just returning a String^ and assigning that to the original variable.
Yet another possibility: You could just create some kind of View-agnostic DataModel that contains your String (and possibly other data that you work with). You could then pass that DataModel to your method. Since the DataModel variable isn't changed (just a property of it), you wouldn't need to pass a reference to it.
See below an example of a function f which takes as a parameter a reference to a std::string.
std::string someString;
void f(std::string& s);
f(someString);

Resources