Consider the following code:
$version = 'latest'
package { 'mypkg':
ensure => $version;
}
Will this do the same thing as:
package { 'mypkg':
ensure => latest;
}
Yes, those will do the same thing.
Puppet allows simple strings to be unquoted. latest and "latest" are equivalent. Passing a variable that holds the same value is also equivalent.
Note that the style guide only suggests using unquoted bare words in limited circumstances. If there's any question at all, it's better to put single-quotes (') around a string.
Related
I found this in Groovy Syntax documentation at 4.6.1. Special cases:
As slashy strings were mostly designed to make regexp easier so a few
things that are errors in GStrings like $() or $5 will work with
slashy strings.
What $() syntax means? give some usage examples please
I also found it at Define the Contract Locally in the Repository of the Fraud Detection Service:
body([ // (4)
"client.id": $(regex('[0-9]{10}')),
loanAmount : 99999
])
but I don't understand what $() means when used with regex('[0-9]{10}').
It means nothing (or what you make of it). There are two places, you
are addressing, but they have nothing to do with each other.
The docs just mention this as "you can use slashy strings to write
things, that would give you an error with a GString" - the same is true
for just using '-Strings.
E.g.
"hello $()"
Gives this error:
unknown recognition error type: groovyjarjarantlr4.v4.runtime.LexerNoViableAltException
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/tmp/x.groovy: 1: token recognition error at: '(' # line 1, column 9.
"hello $()"
The parser either wants a { or any char, that is a valid first char
for a variable (neither ( nor 5 is).
The other place you encountered $() (in Spring cloud contract), this
is just a function with the name $.
Form the docs 8. Contract DSL:
You can set the properties inside the body either with the value method or, if you use the Groovy map notation, with $()
So this is just a function, with a very short name.
E.g. you can try this yourself:
void $(x) { println x }
$("Hello")
I'm trying to update my terraform files from V0.11 to V0.12 and I have some questions.
What's the best way to concatenate variables with strings?
In V0.11 I use this: ${var.name}-STRING-${var.name2}, in V0.12 can I use this: var.name"-STRING-"var.name2 or must I use another ways to concatenate variables and strings?
In v0.12 for interpolations like this:
"${var.example}"
You should use now:
var.example
In your example, In v0.12 you should keep using the previous syntax from v0.11:
"${var.name}-STRING-${var.name2}"
There is a great section in Terraform documentation about migrating to v0.12
To concatenate, check few example below:
If you want to add '#' to string:
value = "${var.username}#${aws_instance.my-instance.public_dns}"
Output: abc#ec2-184-72-11-141.us-west-1.compute.amazonaws.com
To create link:
value = "http://${aws_instance.my-instance.public_dns}:90"
Output: http://ec2-184-72-11-141.us-west-1.compute.amazonaws.com:90
Former C Programmers might like this function better:
format("%s-STRING-%s", var.name, var.name2)
For me, it's less clunky than the old $-Syntax
List of available functions is available at https://www.terraform.io/docs/configuration/functions/join.html
For Python programmers I would suggest using join as another readable alternative:
join("-", [var.name, "STRING", var.name2])
For some of my projects, I have had to use the viper package to use configuration.
The package requires you to add the mapstructure:"fieldname" to identify and set your configuration object's fields correctly, but I have also had to add other tags for other purposes, leading to something looking like the following :
type MyStruct struct {
MyField string `mapstructure:"myField" json:"myField" yaml:"myField"`
}
As you can see, it is quite redundant for me to write tag:"myField" for each of my tag, so I was wondering if there was any way to "bundle" them up and reduce the verbosity, with something like this mapstructure,json,yaml:"myField"
Or is it simply not possible and you must specify every tag separately ?
Struct tags are arbitrary string literals. Data stored in struct tags may look like whatever you want them to be, but if you don't follow the conventions, you'll have to write your own parser / processing logic. If you follow the conventions, you may use StructTag.Get() and StructTag.Lookup() to easily get tag values.
The conventions do not support "merging" multiple tags, so just write them all out.
The conventions, quoted from reflect.StructTag:
By convention, tag strings are a concatenation of optionally space-separated key:"value" pairs. Each key is a non-empty string consisting of non-control characters other than space (U+0020 ' '), quote (U+0022 '"'), and colon (U+003A ':'). Each value is quoted using U+0022 '"' characters and Go string literal syntax.
See related question: What are the use(s) for tags in Go?
Groovy allows a quoted function identifier such as
def "my cool method"()
{
"foo"
}
but I can't find this in the specification. Is it a groovy feature or maybe some lib relies on quotes being used in identifiers? Or is it an omission in the official doc?
It is an integral part of Groovy to be able to do this. If it is missing in the documentation, then it is a documentation bug at most. Basically the same as written at http://groovy-lang.org/syntax.html#_quoted_identifiers is also valid for function names.
I am using Gradle in my project. I have a task for doing some extra configuration with my war. I need to build a string to use in my task like, lets say I have:
task extraStuff{
doStuff 'org.springframework:spring-web:3.0.6.RELEASE#war'
}
This works fine. What I need to do is define version (actually already defined in properties file) and use this in the task like:
springVersion=3.0.6.RELEASE
task extraStuff{
doStuff 'org.springframework:spring-web:${springVersion}#war'
}
My problem is spring version is not recognised as variable. So how can I pass it inside the string?
If you're developing an Android application using Gradle, you can declare a variable (i.e holding a dependency version) thanks to the keyword def like below:
def version = '1.2'
dependencies {
compile "groupId:artifactId:${version}"
}
I think the problem may lay on string literal delimiters:
The string literals are defined exactly as in groovy so enclose it in single or double quotes (e.g. "3.0.6.RELEASE");
Gstrings are not parsed in single quotes strings (both single '...' or triple '''...''' ones) if i recall correctly;
So the code will be:
springVersion = '3.0.6.RELEASE' //or with double quotes "..."
task extraStuff{
doStuff "org.springframework:spring-web:${springVersion}#war"
}
On android there are actually 2 possibilities how to achieve this. It really depends which suits your needs. Those two possibilities have their pros and cons. You can use def variable or ext{} block. Variable def is awesome because it lets you click on the variable and points exactly where it is defined in the file compared to ext{} block which does NOT points to that exact variable. On the other hand ext{} has one good advantage and that is you can refer variables from project_name/build.gradle to project_name/app/build.gradle which in some cases is very useful BUT as I said if you click on that variable lets say only inside only one file it wont points out to the definition of that variable which is very bad because it takes you more search time if your dependency list grows.
1) def option which is propably best and saves you search time.
def lifecycle = '2.0.0'
dependencies {
implementation 'androidx.lifecycle:lifecycle-extensions:$lifecycle'
}
2) second ext{} block. Its kinda ok if dependency list is not huge.
ext {
lifecycle = '1.1.1'
}
dependencies {
implementation 'androidx.lifecycle:lifecycle-extensions:$lifecycle'
}
3) In some cases if you want to share variables between project_name/build.gradle and project_name/app/build.gradle use ext{}
in project_name/build.gradle you define kotlin_shared_variable:
buildscript {
ext.kotlin_shared_variable = '1.3.41'
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_shared_variable"
}
}
which you can use in project_name/app/build.gradle
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_shared_variable"
}
and of course you can combine them.
see here.
Double-quoted strings are plain java.lang.String if there’s no interpolated expression, but are groovy.lang.GString instances if interpolation is present.
Gradle uses Groovy as a DSL. Here "${springVersion}" is a placeholder, what you want is to interpolate, so you should use the double quote, only the double quote in GString has the capability to interpolate.
You can also define variables in the gradle.properties file at the root of your project. You don't have to use double quotes in that file. You would need to add the following line:
lifecycle=2.0.0