Validating array input in AppSync Resolver Template - aws-appsync

I have a query in my aws app-sync:
input TestingInput {
user_ids: [String]!
}
Now, I need to do 2 different validations here:
Validate that the user_ids is an array of strings.
Validate that all user_ids exists in database.
For 1, I can use $util.isList(), but it cannot verify the if there is empty string of not. Is there any way we can filter out the empty string from array and then check the length of array?
For 2, no idea as of right now

Velocity scripting has access to some Java API. In this case, they're lists, and strings. However, I'm not sure what Java version AppSync supports. From AWS documentation, it's being linked to Java SE 6. Go back to your problem, it's done by having a new list, and add non-empty ids into it.
#set( $nonEmptyUserIds = [] )
#foreach( $userId in $userIds )
#set( $trimmedUserId = $userId.trim() )
#if( $trimmedUserId.length() != 0 )
$util.qr( $nonEmptyUserIds.add($trimmedUserId) )
#end
#end
Since it's a check against your database, you need a datasource for your resolver, and a db query for the request mapping. If your db is a RDS, then you can use a SELECT. If it's a DyanmoDb, then BatchGetItem is the right choice.
In the response mapping, you should have db results to validate submitted user ids.

Related

CKan toolkit.get_action function is giving empty list when sorting by package_count

I'm new to CKAN and encountered a problem with template helpers. Particularly in my case, I will have to invoke toolkit.get_action('group_list') in my own template helper. However, when I add the constraint like the following:
results = toolkit.get_action('group_list')(data_dict={'sort': 'package_count desc',
'type': 'MyType',
'all_fields': True})
The results that I get back is an empty list. If I remove the 'sort' constraint from the data_dict, I can get the results of list the groups with 'MyType'. I don't know what caused this problem, because when I followed ckan toolkit official examples, it works for without any problems. However, what I can think of is this customized group might have its own schema such that package_count can not be used as a sort key. Since there's no error message, I can't make further assumption.
I figured out my own problem. The implementation I've showed in my question is correct. However, the database is not being populated properly. Basically you have to populate some packages for the particular groups. If you have no packages within each groups this function will return an empty query result. With the advice of #DRead, I researched the source code of _group_or_org_list(). If you take a look at this piece of code:
if sort_info and sort_info[0][0] == 'package_count':
query = model.Session.query(model.Group.id,
model.Group.name,
sqlalchemy.func.count(model.Group.id))
query = query.filter(model.Member.group_id == model.Group.id) \
.filter(model.Member.table_id == model.Package.id) \
.filter(model.Member.table_name == 'package') \
.filter(model.Package.state == 'active')
else:
query = model.Session.query(model.Group.id,
model.Group.name)
If you don't have packages created for groups, filter(model.Member.table_id == model.Package.id) will filter all groups out.

Given a Symfony 2.7 formType object with an added choice field, how can I access the choice strings?

I have created a form object in symfony, which is meant to process an api request. It is created in a controller via:
$myForm = new MyFormType($foo, $bar);
$form = $this->createForm($myForm, $entity, array('method' => 'POST', 'csrf_protection' => false));
Once these objects are created, I pass them into an external service which converts api data into data that's meant to be consumed by the form, like the following:
//I pass this in, so that, our api users don't have to pass in
//{uglyAPIVariable45: 721}, but rather {pretty_variable: "go left"}
$goodData = $this->convertOldData($form->getBuilderDataCopy(), $oldData);
One of the things this service does, is if the form has a choice, like a select tag, it attempts to convert any string passed to the api into the value in select tag. This way, if the user can pass over "East" if the select field has the options "North", "West", "South" and "East" associated with the values 0, 4, 5 and 10 and the converter should return 10. The user doesn't have to pass over one of those random values, they can just pass over the string. And if they passed over "Foo", it would return an error along with a description of what good choices they could pass over.
My problem is that, in production, I'm not able to get ahold of those values.
In development, I found that I could get my options using,
foreach($formBuilderDataCopy as $formElement){
$options = $formElement->getAttributes()['data_collector/passed_options']['choices'];
}
But when we moved to production, this code broke. The method, getAttributes, returned null suddenly. Admittedly, it seemed kind of janky to begin with, so I wanted to find a more valid approach to getting these choices, possibly even allowing me to bypass a different approach for 'choice', 'entity', 'timezone' et. al.
Unfortunately, looking through the source code and exploring it with get_class_methods and friends hasn't provided me with a solution this issue. Even when I get choices, the most I can get are values, but not the string options in the select box.
What's the appropriate approach to acquiring such information from my symphony forms?
As a side note, I have also tried:
$form->get('elementName')->getConfig()->getOption('choices');
But it also just returns a list of the ids to single numbers and not the strings that replace those numbers. e.g.
{3: 0, 5: 1, 10: 2, 11: 3...}
Create an entity Object for the form type. Then you can use the data as object. It must not been a doctrine entity.

wordpress 3.2.1 database query

i am trying to make a simple selection in a wordpress table (created by a plugin). the table is named reduceri , and has the following columns: id, post, category.
so, i am trying to take all the category values, when the post is equal to the current post id.
the way i am doing the query is:
$the_query = "
SELECT $wpdb->reduceri.category
FROM $wpdb->reduceri
WHERE $wpdb->reduceri.post = ".$post_id."
";
$my_reduceri = $wpdb->get_results($the_query);
but when i var_dump the $my_reduceri all i get is an empty array: array(0) { } even though there should actually be some results... any idea where i am wrong (in the query)?
thank you
Did you declared global $wpdb; before using this query?

PHP mongoDb driver , fetching data and then deleting it , is not working

This is the sample code :
$r = $coll->findOne();
$coll->remove(array("_id"=>$r["_id"])); // use the same object id as retreived from DB
$ret=$coll->findOne(array("_id"=>($r["_id"])));
var_dump($ret); // dumps the records that was supposed to be deleted
The records in the collection have MongoDB objectId, and not strings.
Same logic on console works fine and deleted the record correctly.
This is working for me. Here's the code:
$coll->drop();
print("Now have ".$coll->count()." items\n");
$coll->insert(array("x" => 'blah'));
$coll->insert(array("x" => "blahblah"));
print("Inserted ".$coll->count()." items\n");
$x = $coll->findOne();
print("Object X\n");
print_r($x);
$query_x = array('_id' => $x['_id']);
$coll->remove($query_x);
print("Removed 1 item, now have ".$coll->count()." items\n");
$y = $coll->findOne($query_x);
print("Object Y\n");
print_r($y);
Here's the output:
Now have 0 items
Inserted 2 items
Object X
Array
(
[_id] => MongoId Object
(
[$id] => 4d8d124b6803fa623b000000
)
[x] => blah
)
Removed 1 item, now have 1 items
Object Y
Are you sure there's not a typo somewhere?
Unlike the php == operator, mongo's equality operator always uses "Object equality" which is like php's identical comparison operator (===) or java's .equals(). While your code looks as though it should work (and it does work fine for me with a test dataset), something about your dataset may be causing php to cast the returned MongoId to a string. Read more about MongoId here.
Make sure that your query is supplying a MongoId for comparison by doing a var_dump of the query itself. Also, make sure that you are running the latest version of the PHP Mongo driver.
Since PHP is loosely typed it is most important to ensure that you cast all input values and search values to the expected and consistent data type otherwise it will surely not locate your expected document.
While using MongoDB within PHP I make it a point to cast everything intentionally in order to avoid any possible confusion or error.
Also, the mongodb-user group at groups.google.com is very good and responsive so if you are not already utilizing that resource I would definitely consider joining it.

Zend_Session: unserialize session data

I'm using session SaveHandler to persist session data in the database.
Sample session_data column from the database:
Messenger|a:1:{s:13:"page_messages";a:0:{}}userSession|a:1:{s:7:"referer";s:32:"http://cms.dev/user/profile/view";}Zend_Auth|a:1:{s:7:"storage";O:19:"User_Model_Identity":3:{s:2:"id";s:1:"1";s:8:"username";s:13:"administrator";s:4:"slug";s:13:"administrator";}}
I want to delete Zend_Auth object from this session data.
How can I unserialize those objects and remove object I need?
I suspect, that I don't have to write my custom parser, that Zend_Session already has a method to do this. I have tried different combinations of unserialize but it still returns false.
I'm using autoloader from ZF 1.10.2 and Doctrine 1.2
The code below will work, it is not mine, but in essence what it does is split the session string apart using the pipe as a delimiter, the unserialize the split chunks individually.
The problem is that the build in unserialize function in php doesn't understand the concatenated serialization.
function unserialize_session_data( $serialized_string ) {
$variables = array();
$a = preg_split("/(\w+)\|/", $serialized_string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
for($i=0;$i<count($a);$i=$i+2){
$variables[$a[$i]] = unserialize($a[$i+1]);
}
return($variables);
}