How to disable sorting of return array? - rest

I have a custom rest action like this (in a class that extends yii/rest/ActiveController):
public function actionTest()
{
return ["9" => "Nine", "1" => "one"];
}
When calling the API, the array output is in reverse order, ie:
{
"1": "One"
"9": "Nine",
}
I would like to have it in the original (expected) order...
Seems like the array was sorted somewhere after the array was returned in the action, but I can't figure out where. This only happens when the array key is an integer, an array like this is sorted as expected:
["id-9" => "Nine", "id-1" => "one"]
Have tried using an ArrayDataProvider setting 'sort' = false, but that made no difference.

Since you're exporting it as json and looking at that, from this question - Keeping dictionary keys of JSON Object in order in Objective C, the answer left by Henning Makholm says that:
In JSON objects, by definition, the order of the key-value pairs is not meaningful. The specification allows a JSON producer to permute them any way it want, even at random -- and does not require a parser to preserve the ordering. RFC 4627 says:
An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.
So json has no ordering, since it uses a dictionary as its data structure and typically dictionaries will have no implicit or explicit ordering due to the way they hash the keys for quick access.
It may be that the actual underlying representation in your program is ordered, but the json output has no such guarantee.
One way to fix this would be to move to a different data structure where order is preserved.

Related

Get keys From Interface

I know the title says interface array, but that's how the data is being printed.
I have a field in mongodb called "devices" which is of type object. This object contains a bunch of random key values pairs. The keys are just randomly generated integers and the value are a string.
So I've written some code which retrieves the data from the db, after I've retrieved the data I want to get all the keys from theses objects and store them in an array and I can't seem to figure out how to do that.
First I assume my issue lies with the way I'm getting the data since I'm storing it into an interface{} and not an array. The data prints out like an array but when I change the structure to retrieve an array it comes back empty so I scrapped that idea.
Function
type Data struct {
Devices interface{} `json:"devices" bson:"devices"`
}
client := db.ConnectClient()
col := client.Database("Users").Collection("User")
var deviceIds Data
_ = col.FindOne(context.TODO(), bson.D{}).Decode(&deviceIds)
log.Print(deviceIds.Devices)
Output
2020/10/29 21:28:07 [{123456789 Plant} {456753121 Money Bringer} {798745321 Hello}]
Also I have tried changing that struct to
type Data struct {
Devices map[int]interface{} `json:"devices" bson:"devices"`
}
which gives an output of
2020/10/29 21:35:10 map[123456789:Plant 456753121:Money Bringer 798745321:Hello]
but again I don't know how to extract the keys from them
So as I just finished writing this question I figured out the map was the correct way to get my data.
Posted the question and answering it myself because I thought it would be useful for others
type Data struct {
Devices map[int]interface{} `json:"devices" bson:"devices"`
}
for key, value := range deviceIds.Devices {
fmt.Println(key, value)
}
Changing the struct to the above map and then looping through it with range worked perfectly

Search Array with depending order

I got a question regarding filtering an array.
Let's assume I got an array of country names:
let countries = [Albania, Bahrain, Barbados, Denmark, France, Zimbabwe]
now I want to filter this array to check if it contains a certain String let say "ba".
I can do this easily with
countries = countries.filter{ $0.contains("ba")}
which returns
Albania, Bahrain, Barbados, Zimbabwe
But I actually want the order of letters to matter. Therefore, the result "Albania" and "Zimbabwe" should not appear and only "Bahrain" and "Barbados" as their name starts with an Ba.
Is there any way to do this to avoid a huge for loop going through all entites checking individually for each character?
Use .hasPrefix instead of .contains, like this:
print(countries.filter{ $0.hasPrefix("Ba") })
Note that this is case sensitive. BTW, in your example, the problem was not missing order of letters but the fact that .contains respects case as do most methods in swift.
You can take care of the case and the filtering in one step:
let countries = ["Albania", "Bahrain", "Barbados", "Denmark", "France", "Zimbabwe"]
let filtered = countries.filter { $0.lowercased().hasPrefix("ba") } // -> ["Bahrain", "Barbados"]
This lowercases the country names before applying the filter's test, but doesn't change the original array, so the results have the same case.
This is important because you might incorrectly want to do this in two steps for readability:
countries
.map { $0.lowercased()}
.filter { $0.hasPrefix("ba")}
But this returns ["bahrain", "barbados"] because the filter is being applied to the now lowercased array.

Dereference a ReferenceField in Mongoengine

I am trying to dereference a reference field on my Flask backend and return the complete object with that certain field dereferenced.
The field I am trying to dereference is defined like this:
vouches_received = db.ListField(db.ReferenceField('Vouch'))
The way I am trying to dereference it is like this:
unverified_vouches = []
for vouch in usr.vouches_received:
unverified_vouches.append(vouch.to_mongo())
usr.vouches_received = unverified_vouches
However, when I then do:
usr.to_json()
On the object, then I get a ValidationError like so:
ValidationError: u'{...}' is not a valid ObjectId, it must be a
12-byte input of type 'str' or a 24-character hex string
The 3 dots (...) is basically the document dereferenced, it has mostly Strings, a Date Field, and some other reference fields I do not wish to dereference.
I am aware this is a valid error, as it is expecting an ObjectID for the reference field, but then arises the question, how do I succeed at dereferencing that field and return the document.
Thanks
The ListField is expecting elements of ObjectId and because you've de-referenced them it throws that error. I'm not sure this is the most elegant way but could you convert the usr.to_json() to a dict and then replace the vouches_received list with a deferenced list afterwards - I can't test it but something like?
user_dict = json.loads(usr.to_json())
unverified_vouches = []
for vouch in usr.vouches_received:
user_dict['vouches_received'].append(vouch.to_mongo())
usr_json = json.dumps(user_dict)
A better solution may be to use an EmbededDocument.

I would like to store the values in array list after iterating the select options , is it possible in Watir

I am trying to store the values of select list in an array variable
a = b.options.each {|option| puts option.attribute_value "value" }
Output :
IN PROGRESS
UPCOMING
FINAL
POSTPONED
CANCELLED
a.to_i
Is it possible to store all values which getting from attribute and store in An array
The element collections in Watir include the Enumerable module, which gives a lot of useful methods for iterating. In particular, it includes a map method that will perform a block on each element and collect the result in an array.
To store the value of all options, you can simply do:
total_list_values = #browser.options.map(&:value)
#=> ["IN PROGRESS", "UPCOMING", "FINAL", "POSTPONED", "CANCELLED"]
I coded it like this and its worked, posted if anyone wanted this
total_list_values = Array.new
body = #browser.options
body.options.each do |option|
total_list_values << option.value
end

Comparing ObjectID's in Jade and MongoDB

Jade Template Code
each member in team.members
if member == user._id
- flag=true;
Here, team is a document in the collection 'teams' in Mongo DB, and 'members' is a property having Arrays as value.
Here values of #{member} are 53fecc27bace957811633dcc and 5412a08fa51c105017b9f1fe respectively. Both are Object Id's.
Value of #{user.id} is 53fecc27bace957811633dcc
However, this comparison returns as false and flag is never set to true.
Why is it so?
EDITED FOR CLARITY:
The mongodb-native driver uses a custom ObjectID type. Even if the ObjectIDs are the same, member and user._id would still refer to two distinct objects, and therefore the comparison fails.
Here's a simplified example of what I'm talking about. In the example below I have a constructor function for a TestObject (simulating the mongo ObjectID). I instantiate two objects (i.e. x1 and x2) both with the same parameter. While x1 and x2 are instances of the same object and both were created with the same arguments, they are actually just two different objects with the same data/payload. Therefore, the first direct comparison fails, because they in fact are not the same. To take the simulation a bit further, I added a prototype method .equals() which ensures that both objects are instances of TestObject and the payloads are identical.
function TestObject(data) {
this.payload = data;
}
TestObject.prototype.equals = function(anotherObject) {
return (anotherObject instanceof TestObject) && (this.payload === anotherObject.payload);
};
var x1 = new TestObject(1), x2 = new TestObject(1);
console.log(x1 == x2); // <-- false
console.log(x1.equals(x2)); // <-- true
You can compare them by converting the ObjectIDs to strings.
each member in team.members
if member.toString() == user._id.toString()
- flag=true;
Another possible solution would be to use the ObjectId .equal() method:
each member in team.members
if member.equals(user._id)
- flag=true;
MongoDB objectIds are probably not simple strings. As per docs, they are BSON types.
It's possible that member and user._id are two different objects that is why a "reference equals" comparison is failing, even though their string representations are the same.
On a related note, it's a bad idea to use MongoDB generated object IDs in your application. A more robust design would be to have your own identifier for every record type.