Normally one can query in the following way:
.query({
key1: value1,
...
keyN: valueN
})
But can I use instead of "hard" key values variables?
like following:
.query({
varKey1: value1,
...
varKeyN: valueN
})
Yes, it is possible.
Assuming varKey1, ... varKeyN are some variables:
.query({
[varKey1]: value1,
...
[varKeyN]: valueN
})
Related
I would need some help to handle configuration files in golang.
I renamed every fields and values for privacy (just saying)
I have this yaml configuration file :
Label1:
field1: value1
field2: value2
field3: value3
field4: value4
Label2:
field1: value1
field2: value2
field3: value3
field4: value4
Label3:
field1: value1
field2: value2
field3: value3
field4: value4
I'm using https://github.com/kylelemons/go-gypsy/tree/master/yaml to manually decode this yaml file into the structure below (manually walking the list of nodes). I don't have control over how many "Label" items will be on that file so I need a generic way of parsing it (and it is generic using the library mentioned above)
type Obj struct {
Field1 string `yaml:"field1"`
Field2 string `yaml:"field2"`
Field3 string `yaml:"field3"`
Field4 bool `yaml:"field4"`
}
type ObjConfig struct {
Objs map[string]Obj
}
I have several other configuration files, much simpler, which I'm able to decode using annotations such as :
type conf1 struct {
Field1 int `yaml:"field1"`
Field2 string `yaml:"field2"`
}
I'm using the automatic yaml Unmarshal (through Viper framework) for these and it's working fine.
It wasn't a big deal mixing these 2 ways of handling configuration files until now. But now I need to retrieve the file from a spring config server that I did set up instead of having the files locally. I'm using this library to do so :
https://github.com/Piszmog/cloudconfigclient
It's working well for structures annotated with the yaml:"fieldX", I'm able to deserialize it directly.
But for the first structure of course it's not working and I need a workaround. The library (cloudconfigclient) is using the classic yaml decoder so I can't write my own custom decoder. Looking at the library code it uses this code to deserialize the configuration file (with resp.Body being the yaml configuration file content) :
var dest interface{}
yaml.NewDecoder(resp.Body).Decode(dest)
I'm able to change the configuraton file structure though, as long as I can get the same data in it. Maybe I could use something like
Labels:
- Label1:
field1: value1
field2:value2
....
- Label2:
field1:value1
....
and use the yaml map handling from unmarshal ?
I'm obviously not experienced with golang, I'd love to get some help here.
To sum up, the question is how to decode the first yaml file using yaml.Unmarshal with the classic decoder (by either specific annotations that I don't know of, or changing the yaml structure itself).
I got it working by mapping it to a generic map[string]interface{} type
var configResponse map[string]interface{}
err = configClient.GetFileFromBranch(BRANCH, CONF_DIR, CONF_FILE, &configResponse )
and then handling the cast myself into the structure
type ObjConfig struct {
Objs map[string]Obj
}
Not the most easy to read nor resilient to error solution but it's working
I want to add a comment log in db.system.profile of mongodb, and it works in a constant variable, such as following.
#Query(value="{'field1':?0, 'field2': ?1}")
#Meta(comment="staticComment")
Mono<Quota> findByField1AndField2(String value1, String value2, Sort sort);
My question is if there's any way to assign comment in running time? or such as #Query to carry parameter in? I tried following, but it show up "{?2}" in comment of db.system.profile.
#Query(value="{'field1':?0, 'field2': ?1}")
#Meta(comment="{?2}")
Mono<Quota> findByField1AndField2(String value1, String value2, String dynamicalComment, Sort sort);
I need to set a properties in two different files through Ansible/Jinja2 template file. In one of the files the values should be comma-separated, in the other space-separated.
Currently I use two different variables:
values_space_separated = value1 value2 value3
values_comma_separated = value1,value2,value3
How can I avoid duplication?
Is there a way to convert a value of the variable on-the-fly?
You could always use the regex_replace filter.
So if you normally define the variable as:
values = value1,value2,value3
Then if you need it space separated instead then you could always just do this:
{{ values | regex_replace(',',' ') }}
I'm working on a configuration file parser and I need help parsing key: value pairs into a hash.
I have data in the form of: key: value key2: value2 another_key: another_value.
So far I have code in form of
line = line.strip!.split(':\s+')
which returns an array in the form of
["key:value"]["key2: value2"]["another_key: another_value"]
How can I turn these arrays into a single hash in the form of
{key=>value, key2=>value2, another_key=>another_value}
I'm not sure if the key:value pairs need to be in the form of a string or not. Whatever is easiest to work with.
Thanks for your help!
This is the solution I found:
line = line.strip.split(':')
hash = Hash[*line]
which results in the output{"key"=>"value"}, {"key2"=>"value2"}
Very very close to Cary's solution:
Hash[*line.delete(':').split]
Even simpler:
Hash[*line.gsub(':',' ').split]
# => {"key"=>"value", "key2"=>"value2", "another_key"=>"another_value"}
Assuming the key and value are single words, I'd probably do something like this:
Hash[line.scan(/(\w+):\s?(\w+)/)]
You can change the regex if it's not quite what you are looking for.
Say I have two variables (value1 and value2) and either could be nil, how can I create an element using XmlMarkup and only add the attributes that are not nil?
If I do this
xm = Builder::XmlMarkup.new
xm.item(:attribute1=>value1, :attribute2=>value2)
and both value1 and value2 are nil, I still get
<item attribute1="", attribute2=""/>
I have also tried to add the attributes after creating the element but had no success and I cannot figure out if this is even supported.
If it is not already apparent, I am a complete ruby beginner so any input would be appreciated.
I think something like this could work:
xm = Builder::XmlMarkup.new
attributes = {}
attributes[:attribute1] = value1 if value1
attributes[:attribute2] = value2 if value2
xm.item(attributes)
If you have more than a couple of attributes I can show you a way to minimize duplication with a similar method too.