Is there anybody who has written a universal action for iterating through all params values and setting these values on an object?
I want to write something like this:
def updateSomeObject = {obj->
for (def key : params.keySet()) {
if (obj.hasProperty(key) != null) {
def strValue = params[key]
obj[key] = strValue
}
}
but this works only for String values. In my case there are one to one associations, so it has to work with objects too.
I would like not to set properties (their names) to object, which values are null.
I use this to loop grails params:
Collection<?> keys = params.keySet()
for (Object key : keys) {
//check if key=action and key=controller which is grails default params
if (!key.equals("action") && !key.equals("controller")) {
println key //print out params-name
println params.get(key) //print out params-value
}
}
Hope that help...
It looks like you're trying to bind request parameters to an object. You really shouldn't need to write your own code to do this, as the Grails controllers provide a bindData method that does this already.
Is this what you want to do?
obj.properties = params
Hope that helps
Related
I am trying to send following parameters in GET method call:
{
query = {
"$or" = (
{
name = yes;
},
{
status = Open;
}
);
};
}
But it seems it is not returning the proper URL:
baseURL/objects?query%5B%24or%5D%5B%5D%5Bname%5D=yes&query%5B%24or%5D%5B%5D%5Bstatus%5D=Open
I was expecting to "Or" my data, but it is doing "And".
I am using AFURLRequestSerialization class.
I have followed this SO, but it gives me all the object without applying any query.
AFNetworking GET parameters with JSON (NSDictionary) string contained in URL key parameter
It was working properly in POST call, but in GET it is not working as expected.
I have resolved this by converting dictionary against query key to a string and adding this string as a value of key query in the dictionary.
So my parameters will look like:
parameters: {
query = "{\"$or\":[{\"name\":\"yes\"},{\"status\":\"Open\"}]}";
}
Then I am passing this dictionary to AFNetworking.
According to the documentation, it needs to follows the Form Post rules at: https://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4. When looking at that information it did not give me much to work with in terms of complex objects or maps.
Right now, If I have a list for example: Each item in the list needs to be stringified.
var params = {"list": [1,2,3]};
// needs to be stringed.
params["list"] = params["list"].map((item)=>item.toString()).toList();
Simple. Also all base items need to be a string as well
var params = {"number": 1, "boolean": true};
params = params.forEach((k,v)=> params[k].toString());
But how do we handle maps?
var params = {"map": {"a":1,"b":"foo","c":false,"d":[]}};
// ??
It seems that after testing in my app and in dart pad, you need to make sure everything is strings, so i am trying to come up with a way to effectively cover lists, maps, and maybe more complex objects for encoding.
var params = {};
params["list"] = [1,2,3];
params["number"] = 1;
params["boolean"] = true;
params["map"] = {"a":1,"b":"foo","c":false,"d":[]};
params.forEach((String key, dynamic value){
if(value is List){
params[key] = value.map((v)=>v.toString()).toList();
}else if(value is Map){
// ????
}else{
params[key] = value.toString();
}
//maybe have an additional one for custom classes, but if they are being passed around they should already have their own JSON Parsing implementations.
}
Ideally, the result of this would be passed into:
Uri myUri = new Uri(queryParameters: params);
and right now, while i solved the list issue, it doesn't like receiving maps. Part of me just wanted to stringify the map as a whole, but i wasn't not sure if there was a better way. I know that when someone accidentally stringified the array, it was not giving me: ?id=1&id=2 but instead ?id=%5B1%2C2%5D which was not correct.
I don't think there is any special support for maps. Query parameters itself is a map from string to string or string to list-of-strings.
Everything else need to be brought into this format first before you can pass it as query parameter.
A simple approach would be to JSON encode the map and pass the resulting string as a single query parameter.
i need to filter array result (get by parsed json).
if i know the exact id i can select in the json using
#my_art = ele_art.select { |articolo| articolo['id'] == 456 }
Now I have an array of ids called #myarray and i need to select in ele_art only the items with id in the array
Reading the array i have:
[279, 276]
i tried with
#my_art = ele_art.select { |articolo| articolo['id'] == #myarray }
or
#my_art = ele_art.select { |articolo| articolo['id'] in #myarray }
with no luck!
how can i solve?
#my_array is and array of ids, so, in that case you need to check if articolo['id'] is included in #myarray. For those cases, the Array class in Ruby has the include? method, which receives an object and returns true/false if the object is included or not in the array.
So, in your case try something like:
#my_art = ele_art.select { |articolo| #myarray.include?(articolo['id']) }
I'm trying to not set a value ONLY if it's present in the JSON. I want to do something like this:
if (map["doc.type"] != nil) {
picturePath <- map["doc.type"]
}
I'm using this ObjectMapper framework:
https://github.com/Hearst-DD/ObjectMapper
<- is mapping operator which returns optional type. If key "doc.type" is not presented picturePath is nil.
If you would like to full fill picturePath based on different keys, the best way is probably to use helpers variables and then assign picturePath based on them.
e.g.:
var optionalPicturePath: String? = map["doc.type"]
if optionalPicturePath != nil {
picturePath = optionalPicturePath
}
Is there any easy way to extract the parameters of the referrer url as contained in Request.UrlReferrer? Is there another way to get the parameters used by the referrer?
Query?blahID=3&name=blah
I am refering to getting blahID and name from the url. It can be done with a bunch of string manipulations, but was hoping there was an easier way.
Use HttpUtility.ParseQueryString from System.Web. Something like this should work:
string blahID = string.Empty;
if(Request.UrlReferrer != null)
{
var q = HttpUtility.ParseQueryString(Request.UrlReferrer.Query);
blahID = q["blahID"];
}