Is there a Groovier way to access properties file? - gradle

One can access properties file like that:
def props = new Properties()
new File('my.props').withInputStream { props.load(it) }
assert props.foo == 'bar'
I think that's quite cumbersome. Isn't there a groovier way?
// does not compile
def props = Properties.from(new File('my.props'))
assert props.foo == 'bar'

I believe the answer is no.
The doc for Groovy JDK enhancements does not contain java.util.Properties (compared to, say, java.io.File). This article implies prior art for a home-grown solution.

You can always utilize metaprogramming:
Properties.metaClass.static.from = { File f ->
def p = new Properties()
f.withInputStream { p.load(it) }
p
}
p = Properties.from(new File('a.properties'))
assert p['a'] == '10'

I'm not aware of any shortcut to create a properties from a file. I'd like to suggest a simple solution without prior variable declaration:
p = new File('my.props').withReader { reader ->
new Properties().with {
load reader
it
}
}

Related

Grails not able to add an object property. How would this be achieved?

Class A {
String nameA
B b
}
Class B {
String nameB
}
So within my controller right now I have: def temp = AService.get(id) and I want to add a B object to temp.
I have seen things like: temp.addToB({b params}) but it did not work for me. How can I achieve this?
I have seen things like temp.addToB({b params}) but I have had not
success.
If you have a reference to an instance of A and you want to initialize the b property in that instance, you can use Groovy's property assignment support to do that.
A someA = new A()
someA.b = new B()
A someA = aService.get(id)
someA.b = new B()
A someA = aService.get(id)
someA.b = bService.get(someOtherId)
Etc.

How to handle Dynamic xpath in Groovy?

in the below example the xpath had been in given in the code.
String xml = Recall.getXml()
def telephoneNumbers = new XmlSlurper().parseText(xml)​​​​​​​​
def outputBuilder = new groovy.xml.StreamingMarkupBuilder()
String telephoneXml = outputBuilder.bind { mkp.yield telephoneNumbers.telephone }
would like to know any possible solution to specify the same xpath through a variable. like below.
String telephoneXml = outputBuilder.bind { mkp.yield ${xpath} }
Thanks in advance.
to be clear this is not xpath. in groovy it's named gpath, and it's a groovy expression.
You can use Eval class to evaluate groovy expression from string:
def xml = '''<root>
<telephone>1234567899</telephone>
<cell>1234567890</cell>
</root>'''
def telephoneNumbers=new XmlSlurper().parseText(xml)
def outputBuilder = new groovy.xml.StreamingMarkupBuilder()
def gpath = "xml.telephone"
String telephoneXml = outputBuilder.bind { mkp.yield Eval.me('xml',telephoneNumbers,gpath) }

ElasticSearch Nest Create Fields Object

Looking at the docs we should be able to create a new source filter like so
new SearchRequest<Project>
{
Source = new SourceFilter
{
Include = Fields<Project>(p => p.Name, prop => prop.StartedOn)
}
}
The issue I'm facing is that Fields isn't typed and doesn't have a constructor.
How do I go about making a Fields for use in sourceFilters, queries etc?
You can find Fields<> method in class Infer, so change example code to
new SearchRequest<Project>
{
Source = new SourceFilter
{
Include = Infer.Fields<Project>(p => p.Name, prop => prop.StartedOn)
}
}
Also you can import this static class in your cs file with using static Nest.Infer;, so you will be able to use this example as it is.
Hope it helps.

How to use spring properties in gradle.build?

How can I use properties configured in resources/application.properties in gradle.build? I would like to get something like this :
flyway {
url = MAP_WITH_PROPERTIES['spring.datasource.url']
user = MAP_WITH_PROPERTIES['spring.datasource.username']
}
import java.util.Properties
def props = new Properties()
file('src/main/resources/application.properties').withInputStream {
props.load(it)
}
def url = props['spring.datasource.url']
def user = props['spring.datasource.username']
You can load properties and use them this way:
ext.ApplicationProps = new Properties()
ApplicationProps.load(new FileInputStream("src/main/resources/application.properties"))
And use it as follows:
flyway {
url = ApplicationProps['spring.datasource.url']
user = ApplicationProps['spring.datasource.username']
}
Just note, that path to the properties is defined from the root and may vary if you have a multimodule project.

Is there an equivalent in Dart of the instance_variable_set method in Ruby?

If not, is there anything like this on the horizon?
This is the one feature of JavaScript, Ruby, and Perl that I can't live without. I know you can fake it with a hash member, but I want to be able to create (arbitrary) "first class" members from a parser.
Currently there's nothing that can set a field that doesn't yet exist. The mirror API can be used to set fields that already exist, and may eventually be extended to support defining new fields dynamically.
You can also use the "noSuchMethod" method on a class to intercept setter / getter, and store the received value in a map.
For example (I can't remember the syntax exactly...):
class Foo {
var _dynamicProperties = new Map<String,Object>();
noSuchMethod(String function_name, List args) {
if (args.length == 0 && function_name.startsWith("get:")) {
// Synthetic getter
var property = function_name.replaceFirst("get:", "");
if (_dynamicProperties.containsKey(property)) {
return _dynamicProperties[property];
}
}
else if (args.length == 1 && function_name.startsWith("set:")) {
// Synthetic setter
var property = function_name.replaceFirst("set:", "");
// If the property doesn't exist, it will only be added
_dynamicProperties[property] = args[0];
return _dynamicProperties[property];
}
super.noSuchMethod(function_name, args)
}
}
And then you can use this in your code as follows:
var foo = new Foo();
foo.bar = "Hello, World!";
print(foo.bar);
Of course, this can lead to typos that will not be checked by the type checker, e.g.:
foo.bar = "Hello";
foo.baz = "Hello, World!"; // Typo, meant to update foo.bar.
There are ways you have type-checker validation by using redirecting factory constructors and an implied interface, but then it starts to get complicated.
Side note: This is what JsonObject uses to convert a JSON map to a class type syntax.

Resources