Does mediaResponse really support array of mediaObject? - actions-on-google

As the document says, MediaResponse contains mediaObjects property, and mediaObjects is array of mediaObject, but when I tried to put multiple mediaObjects, I got this error:
MalformedResponse at
expected_inputs[0].input_prompt.rich_initial_prompt.items1.media_response:
Only 1 media_object is allowed. First media_object will be used while
rest will be filtered.
Then what is the point of having an array of mediaObject?

There are several places in the protocol that contain an array where only one object is permitted in the array. One assumes that the designers wanted to make it expandable in the future without having to add special casing.
In this case, it sorta makes sense - right now we can only send one media object as part of the reply. It might be reasonable that, in the future, we could send one or more without having to come back to our webhook.

Related

Node.CloneNode() not a function -dom-to-image.js

I want to create a .png file of a HTML page in angularjs and download it. For this I'm currently using dom-to-image.js and using the domToImage.toBlob function and passing the node element to it. But internally when it goes to dom-to-image.js it throws the error:
node.cloneNode() is not a function
Can anyone please assist me here?
Thanks
This error arises when you attempt to call cloneNode() on anything other than a single DOM node.
In this case, the error is coming from the dom-to-image library, which calls that method internally.
Note that without a code snippet, its hard to identify the precise issue with your code, but the point is, when you call domtoimage.toBlob(), you need to supply a single DOM node.
So double check what you are calling it with. If you are selecting by class, for instance, you could end up with more than one element.
Its best practice to be precise with which node you want to convert to a blob - use the id attribute, like this:
domtoimage.toBlob(document.getElementById('element')).then(...)
where element is the id of your target node.
Its also possible you're selecting with angular.element() or even using jQuery directly.
In both cases, this returns an Object -- which you can't call cloneNode() on.
Also note the following from the Angular docs for angular.element():
Note: All element references in AngularJS are always wrapped with jQuery or jqLite (such as the element argument in a directive's compile / link function). They are never raw DOM references.
Which means you would observe the same behavior in both cases, e.g. both of these:
domtoimage.toBlob($('#element')).then(...)
domtoimage.toBlob(angular.element('#element')).then(...)
would result in the error you see. You can index the Object before supplying it to domtoimage.toBlob(), perhaps like this:
domtoimage.toBlob($('#element')[0]).then(...)
domtoimage.toBlob(angular.element('#element')[0]).then(...)
and that would also work.
Also check out this post about "cloneNode is not a function".

NSComboBox with alternative ways of writing

I'm trying to implement a autocomplete function into a small app. I want to give the user the possibility to write down a city (from a long long list of cities) into a NSComboBox. That works fine, as long as the user is using the exact writing of the city inside my array of city names. But if they use, for whatever reason, a different spelling, it fails and the city is not found.
So if the user is looking for "Köln" for example, it's not a problem, but if he is looking for "Cologne" he wouldn't be able to find it.
For this I have, for each city an additional array of alternative spellings.
Now I would love NSComboBox (or any other type of TextField) to look not only in "city.name" but also within the array "city.alternativeNames". The shown value should as well represent what the user is writing.
I don't want to create an entry in the list of the pop-up part of NSComboBox for each alternative name, as that would make the list even longer than it is and would confuse people (cologne alone has 85 different spellings).
Thanks for your suggestions, I'm completely new to NSComboBox.
Interesting problem. I think probably you need to choose a different way to structure your data to make it easier.
Consider the lookup method (matching a string). Structuring your data for this case should account for the preferred spelling of each city (preferred by you, for the scrolled list).
How about a flat array of cities (to allow a simple search based on user spelling without also having to check each possible array of alternates and manage sorting them properly) but each has an optional (can be nil) "preferred spelling" pointer to the "correct" one. When displaying the options in the combo box, show the array filtered by those with no optional preferred spellings plus the currently-typed partial/full completion of the alternate spelling?
So a City has a name property and an optional preferred property. In your case, if Cologne is preferred, the Köln instance would have Cologne set as preferred. Köln would only appear in the list if the user typed it (even partially) and it would automatically be in the correct alpha-sorted position (assuming your cities are kept sorted).
Does this make sense or do I need to rephrase? Haven't had enough coffee this morning. :-)

Can the Post for a ODataContoller in WebAPI 2 take an IEnumerable of a complex object

I want to know if passing a collection to the post of an ODataController is even possible before I waste my time trying to figure out where my problem is.
I've seen variations of this question over the internet, but nothing that has led me to an answer.
I created the Post method on an ODataContoller where I want to pass in a IEnumerable of a complex object. However, when I debug the controller, the parameter is null.
When I take each individual element of the collection and pass it into a Controller's Post (that takes a single object), the object is accepted. So I know the individual objects are being formatted correctly.
Technically this is possible. If you get null as complexObjects in the following call
[HttpPost]
public IHttpActionResult CreateMany([FromBody] IEnumerable<ComplexObject> complexObjects)
{
// ...
}
it's probably due to a format error in the bodies json object. If you have a working single object you can post, you just need to wrap it in brackets ... an array is an array even if it cotains only one element. This assumes, you check your web api actions via postman, fiddler etc., where you can 'compose' the entire request. Alternatively you can use the output of a 'GET all' action (if you have one) as input.
Regarding another aspect, the REST-fulness of list creation, you may find RESTful way to create multiple items in one request interesting
Hope this helps.

make play-json read the empty string as None for a type of Option[T]

I'm attempting to parse json from the GitHub API with play-json, and encountering a problem with the merge_commit_sha field on Pull Requests (incidentally, I know this field is deprecated, but don't want to discuss that in this parsing problem!). Unfortunately merge_commit_sha field comes back as the empty string in some cases:
"merge_commit_sha": ""
This is how the field is declared in my case class:
merge_commit_sha: Option[ObjectId],
I have an implicit Format[ObjectId], which does not tolerate the empty string, because that's not a valid value for a Git hash id. I'm also using a play-json macro-generated Read[PullRequest], which I'd like to keep on using, in preference to individually declaring reads for every single field on pull requests.
As I've declared the field to be an Option, I'd like "merge_commit_sha": "" to be read as the value None, but this is not what currently happens - a string is present, so the Format[ObjectId] is invoked, and returns a JsFailure.
One thing I tried was declaring an implicit Format[Option[ObjectId]] with the required behaviour, but it didn't seem to get used by the macro-generated Read[PullRequest].
You can define a custom Reads and Writes yourself.
Using Json.format[MyType] uses a Scala macro. You may be able to hook into that. Although, 'extending' a macro for this one case class just seems wrong.
Custom Reads and Writes might be a little 'boilerplate-like' and boring, but they have their upsides.
For example if your json has a bunch of new fields on it, you wont get a JsError when validating or transforming it to a case class. You only take what you need from the JSON and create objects. It also allows for a separation between your internal model and what you're consuming, which in some cases is preferred.
I hope this helps,
Rhys
EDIT
After using some other JSON libs I may have found what you are looking for.
I know the question was asking specifically after Play JSON.
If you're able to move away from Play JSON, Look at spray-json-shapeless specifically JsNullBehaviour and JsNullNotNone REF.

Issue with duplicate search results and XMLParser array refresh

I use an NSXMLParser to parse ext. API data. I also use a Singleton shared array var to maintain info retrieved from ext API.
This info is dynamic and changesas the user types anything in UISearchBar
Now the thing is as soon as the user types anything, I clear off the existing array contents by using [retrievedArray removeAllObjects];
Thus this refreshed retrievedArray based on the current terms in the search bar.
This works fine if the user types slowly. However I get to see duplicate search results if the user types very fast. This, I am assuming, is because the retrievedArray contents do not get enough time to clear off.
I am still trying to resolve the issue. Please suggest some more fixes.
Could you please provide me the fix.
No, I don't think this is the case, unless you're doing search in a separate thread and clear array in another one. If not, then there is probably error in your search logic.
First of all I think a singleton approach may not be the best way to go for what you are doing. But make sure that you are synchronizing all mutable access to the array. Instead of allowing the singleton to return a NSMutableArray for any object and their mom (super) to use you need to have methods like addObject,removeObject,clear with #synchronize blocks or any kind of lock you decide on. I still see issues with this approach because the code calling addObject, remove and clear will all need to be synchronized as well. Maybe consider on each auto complete request you use a delegate or post a NSNotification containing a timestamp, the characters the user typed to get data, and a NSArray of results. At that time you can see if the response is still valid, discard any invalid responses, and update the user with just the contents of the most recent valid NSArray