using resValue from applicationVariants.all - android-gradle-plugin

Has anyone used resValue to set values while iterating through each variant?
Couldn't get something like this to work
applicationVariants.all { variant ->
//none of these work
variant.mergedFLavor.setResValue("integer","key","value")
variant.mergedFlavor.resValue "integer", "key", "value"
}
I have the following variant structure:
buildTypes{
debug {...}
stage {...}
release{...}
}
productFlavors {
flavorDimensions "country", "store"
uk {
flavorDimension "country"
}
us {
flavorDimension "country"
}
amazon {
flavorDimension "store"
}
google {
flavorDimension "store"
}
}
I was looking to setting a tracking_id for each [country] where build type is release. And setting a unique code for each [country]+[store] combination.
I know the country/store code could be done using resource files in the following directories:
src/ukAmazon/res
src/ukGoogle/res
src/usAmazon/res
src/usGoogle/res
And tracking id in directories:
src/ukAmazonRelease/res
src/ukGoogleRelease/res
src/usAmazonRelease/res
src/usGoogleRelease/res
But it would be nice to keep everything in script and avoid duplicating res files. It will start getting even messier when more countries come in.

For someone who still feel confused, just modify your script to something like below and it will work:
applicationVariants.all { variant ->
variant.resValue "integer", "key", "value"
}

Found this:
https://code.google.com/p/android/issues/detail?id=67416
So it looks like it's not there yet. There is a hacky workaround mentioned.

Related

How to extend the jsonschema validator?

For my project I need a new property for json schemas.
I named it "isPropertyOf" and basically what I want is that if I have this:
fruits = {
"banana": "yellow fruit",
"apple": "round fruit"
}
schema = {
"type": "object",
"properties": {
"fav_fruit": {
"type": "string",
"isPropertyOf": fruits
}
}
}
Then schema would validate only objects like {"fav_fruit":"banana"} or {"fav_fruit":"apple"}, but not {"fav_fruit":"salami"}
(I know that for this very example using an enum would make more sense, but assume "fruits" is also used for other stuff and I would rather avoid redundancy)
I read docs about this and figured I need to use jsonschema.validators.extend. I tried something like that:
def is_property_of_callable(validator_instance, property_value, instance, schema):
keys = [k for k in property_value]
return instance in keys
mapping = {"isPropertyOf": is_property_of_callable}
my_validator = jsonschema.validators.extend(jsonschema.Draft7Validator, mapping)
instance = {"fav_fruit": "tobacco"}
my_validator(schema).is_valid(instance)
I was ready to see something go wrong, but apparently the validator wasn't seeing any issue. I tried with an obviously wrong instance that even the standard validator of jsonschema wouldn't accept
my_validator(schema).is_valid({"fav_fruit": 0})
But apparently it looked ok to it.
I thought maybe the way I added this property was so broken that it was making the validator accept anything, so I tried a minimal case of validator extension:
minimal_validator = jsonschema.validators.extend(jsonschema.Draft7Validator, {})
minimal_validator(schema).is_valid(instance)
And this one is also happy with 0 being my favourite fruit.
What am I doing wrong here? How can I make that work?

How To Get Particular Security Advisory Repository in Graphql

I have Tried
I have tried this code
`# Type queries into this side of the screen, and you will
# see intelligent typeaheads aware of the current GraphQL type schema,
# live syntax, and validation errors highlighted within the text.
# We'll get you started with a simple query showing your username!
query {
securityAdvisories(orderBy: {field: PUBLISHED_AT, direction: DESC}, first: 2) {
nodes {
description
ghsaId
summary
publishedAt
}
}
}
And got the below response
{
"data": {
"securityAdvisories": {
"nodes": [
{
"description": "In Symfony before 2.7.51, 2.8.x before 2.8.50, 3.x before 3.4.26, 4.x before 4.1.12, and 4.2.x before 4.2.7, when service ids allow user input, this could allow for SQL Injection and remote code execution. This is related to symfony/dependency-injection.",
"ghsaId": "GHSA-pgwj-prpq-jpc2",
"summary": "Critical severity vulnerability that affects symfony/dependency-injection",
"publishedAt": "2019-11-18T17:27:31Z"
},
{
"description": "Tapestry processes assets `/assets/ctx` using classes chain `StaticFilesFilter -> AssetDispatcher -> ContextResource`, which doesn't filter the character `\\`, so attacker can perform a path traversal attack to read any files on Windows platform.",
"ghsaId": "GHSA-89r3-rcpj-h7w6",
"summary": "Moderate severity vulnerability that affects org.apache.tapestry:tapestry-core",
"publishedAt": "2019-11-18T17:19:03Z"
}
]
}
}
}
But i want to get the response for specific security advisory like this
i.e i want to get graphql response for specific id for below example url ID is GHSA-wmx6-vxcf-c3gr
Thanks!
The simplest way would be to use the securityAdvisory() query.
query {
securityAdvisory(ghsaId: "GHSA-wmx6-vxcf-c3gr") {
ghsaId
summary
}
}
If you need to use the securityAdvisories() query for some reason, you simply have to add an identifier:. The following query should get the distinct entry for GHSA-wmx6-vxcf-c3gr.
query {
securityAdvisory(ghsaId: "GHSA-wmx6-vxcf-c3gr") {
ghsaId
summary
}
}

How do I delete a node in appsettings.json using appsettings.Development.json?

I am struggling a bit with how the appsettings.Development.json overrides or otherwise merges with the appsettings.json. I am not sure how to "clear" a node out of appsettings.json by using the appsettings.Development.json file.
For reference, I am using the default builder as seen here https://github.com/aspnet/MetaPackages/blob/rel/2.0.0-preview1/src/Microsoft.AspNetCore/WebHost.cs#L159-L160
appsettings.json
{
"Policy": {
"roles": [
{
"name": "inventoryAdmin",
"subjects": [ "bob", "alice" ],
"identityRoles": [ "ActiveDirectory-Role-Manager" ]
},
]
}
}
Given that example, why can I not do the following in my:
appsettings.Development.Json:
{ "Policy": { "Roles": [] } }
or
{ "Policy": { "Roles": null } }
When I check the output via something like Configuration.Get<PolicyServer.Local.Policy>().Roles I still get 3 roles back.
This question is hopefully going to guide me on how I can override a node and not just clear it. So I am hoping to start simple and work my way there.
All of the settings that go into your IConfiguration instance are simply key-value pairs. Take the following, simplified example JSON:
{
"Roles": [
{ "Name": "Role1", "Subjects": [ "Alice", "Bob" ] },
{ "Name": "Role2", "Subjects": [ "Charlie" ] }
]
}
Although this is essentially a tree structure, it maps into the following key-value pairs when added to your IConfiguration instance (there are some additional empty values here, but they're not part of this discussion):
Roles =
Roles:0:Name = Role1
Roles:0:Subjects:0 = Alice
Roles:0:Subjects:1 = Bob
Roles:1:Name = Role2
Roles:1:Subjects:0 = Charlie
You can see that this mimics the hierarchy of your JSON, where the names are object properties and the numbers are indexes into arrays. That first one is important: There's a key of Roles which has no value, because values can only be simple strings and its just a parent in itself.
Now, when you add an extra JSON file to the IConfiguration instance setup, it maps to a new set of key-value pairs that get applied on top of those that exist. Take the following additional JSON:
{
"Roles": []
}
This simply overwrites the existing Roles key and sets it to, well, the same value it already has: nothing. The same applies if you use null in your JSON file - that's just how this stuff works.
In terms of a solution here, I suggest seeing if you can rework your appsettings.json approach. For example, you might be able to put the role configuration itself into e.g. an appsettings.Production.json file and leave the default version blank so that it doesn't exist in your development environment. In other words, try and model your different appsettings.json files to be additive themselves.

How to override an ASP.NET Core configuration array setting reducing length of the array

I have an ASP.NET Core app with appsettings.json config file. One of the settings in the file is represented by the array of objects, like this:
{
"Globalization": {
"Languages": [
{
"DisplayName": "Ru",
"Code": "ru"
},
{
"DisplayName": "En",
"Code": "en"
}
]
}
}
In our CI system we use environment variables to override the configuration settings from file. It turns out that I can only override existing items or add new items to the array, but I can’t reduce the number of items using index notation ("Globalization__Languages__0__DisplayName" etc.).
And it’s the same with appsettings.{Environment}.json, I still have two language options even if I have just one item in it.
Of course I can make the base config empty or invent some other workaround, but am I missing something? Is there any way to neatly override the settings reducing the number of items (preferably with the help of environment variables)?
Microsoft community made a very bad design decision to make arrays work in that way (IMHO).
As a solution we worked it out as the follows:
{
"Globalization": {
"Languages": "ru|Ru,en|En"
}
}
and then:
var langs = Configuration.GetSection("Globalization:Languages")
.Value.Split(',')
.Select(x => x.Split('|'))
.Select(x => new { Code = x[0], DisplayName = x[1] })
.ToArray();
My proposal is here.

Is RestKit the only framework that has JSON to Objective-C objects mapping?

I am looking for a library or framework that does JSON to Objective-C relational object mapping.
i.e. I need to map JSON containing objects, array of objects and dictionaries of objects to my custom objects.
something like:
DataObject {
"user" : {
"name":"Peter",
"id":1234
}
"place": "UK"
"job": {
"title" : "CTO",
"salary" : 1234567
}
"employess": [
{
"name":"Carlton",
"id":1235
},
{
"name":"Hugo",
"id":12346
}]
}
So there is a DataObject a UserObject and an employees array consisting of UserObjects.
I would like for the mapping from the JSON to my DataObject to happen "automatically", of course meant as I would like to describe the objects and there relations in the Object class and have the mapping done from this, instead of manually mapping each nested object.
(First level native objective-c properties are easily done with setValue:forKey and other KVO methods, but from there on it gets complicated).
I have been testing out RestKit but it seems there is no way to pick and choose which functionality you need from that framework, it is either all of it or none of it, and I do find it does too much for my needs.
Are anyone familiar with a library etc. out there that can do this?
Thank you in advance.
To map JSON to Objective-C objects, I have tried RestKit. I used it a while ago, so my criticisms might not apply anymore.
What I found out: nice idea. Not too complicated to plug-in. If it works, great for you. But if not, or if you need to push it a bit further, good luck to debug.
I regret the time I invested in it.
I am only looking for JSON to Objective-C objects, not the other way around.
I found a link on SO to JTObjectMapping - but can't find the original answer. Seems more lightweight and close to what I was searching, but I did not had the opportunity to try it.
An other similar class: jastor.
I prefer the approach of this two classes over RestKit, as it only takes care of one job, whereas RestKit tried to handle everything.
What you have posted above isn't valid JSON. If you made it valid JSON what you want to do is impossible without a specific schema, eg.
{
"DataObject": {
"user": {
"name": "Peter",
"id": 1234
},
"place": "UK",
"job": {
"title": "CTO",
"salary": 1234567
}
}
}
Is Dataobject a dictionary or an Object? What about User or Job? What is User is an instance of NSUser and job is an NSDictionary?
On the other hand, if you have a known schema:-
[
{
"class": "DataObject",
"properties": {
"user": {
"class": "User",
"properties": {
"name": "Peter",
"id": 1234
}
},
"place": "UK",
"job": {
"title": "CTO",
"salary": 1234567
}
}
}
]
you don't need a framework as it is trivial to map to your objects yourself once you have valid JSON. Pseudocode:-
createWithDict(dict) {
var newOb = create new(dict["class"]);
dict.properties.each( val, key ) {
if(val is dictionary && val.hasproperty("class"))
val = createWithDict(val)
newOb[key] = val
}
return newOb;
}