Find the same key in other localization json-file - intellij-idea

I want to find the same key in an other jsons localization file.
My json’s looks like this.
{
"general": {
"main": {
"title": "This is a title",
"header": "This is a header",
"sub": {
"sub_thing_1": "Something",
"sub_thing_2": "Something2"
}
}
},
"other_things": {
"title": "Other title"
"sub": {
"sub_thing_1": "Something",
"sub_thing_2": "Something2"
}
}
}
As you can see the same key can be in more places
My thinking is, to generate a collection that contains, the depth values like this
eg: this "sub_thing_2" can be found at -> ["general", "main", "sub"]
and with this information i can find the exact key in other files.
And with this code I geathering the depth information for each key, it’s just a scratch, thinkering code fragement, I'm new to Kotlin by the way
psi?.accept(object : JsonRecursiveElementVisitor() {
override fun visitObject(o: JsonObject) {
val childrens = o.children
for (child in childrens) {
val childProp = child as JsonProperty
if (child.lastChild is JsonObject) {
depth.add(childProp.name)
visitObject(child.lastChild as JsonObject)
} else {
println(depth.toString() + childProp.name + ": " + childProp.lastChild.text)
}
}
if (depth.isNotEmpty()) {
val lastItem = depth.last();
depth.remove(lastItem)
}
}
})
My question is there is other easier way to do this maybe without the PSI or I'm in the right direction?

Related

Shopify Storefront API getProductMedia

I tried following the offical Shopify Documentation for retrieving ProductMedia.
My Query looks like this:
query getProductMediaById($id: ID!) {
product(id: $id) {
id
media(first: 10) {
edges {
node {
mediaContentType
alt
...mediaFieldsByType
}
}
}
}
}
fragment mediaFieldsByType on Media {
... on ExternalVideo {
id
embeddedUrl
}
... on MediaImage {
image {
...imageAttributes
}
}
... on Model3d {
sources {
url
mimeType
format
filesize
}
}
... on Video {
sources {
url
mimeType
format
height
width
}
}
}
fragment imageAttributes on Image {
altText
url
}
The only thing where I diverged from the official documentation is to put the image attributes to another fragment for code reuse.
But when I try to execute the query I get the following response:
{
"data": {
"product": {
"__typename": "Product",
"id": "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0LzY3NjcyOTczMzEzMDU=",
"media": {
"__typename": "MediaConnection",
"edges": [
{
"__typename": "MediaEdge",
"node": {
"__typename": "MediaImage",
"mediaContentType": "IMAGE",
"alt": ""
}
}
]
}
}
},
"loading": false,
"networkStatus": 7
}
Or to put it to words my response doesn't contain any information from the mediaFieldsByType fragment.
Any Idea what I'm doing wrong?

BigQueryIO.write DynamicDestination withCreateDisposition - Clustering fields

BigQueryIO.write.withCreateDisposition(CreateDisposition.CREATE_IF_NEEDED) along with DynamicDestinations we can write to the dynamic table and if the table does not exist it will create the table from TableSchema provided from DynamicDestinations.
I am not able to add clustering fields part of TableSchema model, because it does not have such a feature.
how we can add DynamicDestinations having TableSchema with clustering fields?
bigQuery API is one way to add cluster field to a table
Using this link you can test the API before writing your code
function execute() {
return gapi.client.bigquery.jobs.insert({
"resource": {
"configuration": {
"query": {
"clustering": {
"fields": [
"Field1",
"Field2"
]
},
"query": "select 5",
"destinationTable": {
"datasetId": "Id1",
"projectId": "Project1",
"tableId": "T1"
}
}
}
}
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
},
function(err) { console.error("Execute error", err); });
}
And This is a JS example on how to manipulate the parameters:
static setConfiguration(params, configuration) {
//To have a destination table we MUST have a tableId
if (params.destinationTable && params.destinationTable.tableId) {
configuration.query.destinationTable = params.destinationTable
}
if (params.clusteringFields) {
configuration.query.clustering = {fields: params.clusteringFields}
}
if (params.timePartitioning) {
configuration.query.timePartitioning = {
type: 'DAY',
field: params.timePartitioning
}
}
if (params.writeDisposition) {
configuration.query.writeDisposition = params.writeDisposition
}
if (params.queryPriority && params.queryPriority.toUpperCase() === "BATCH") {
configuration.query.priority = "BATCH"
}
if (params.useCache === false) {
configuration.query.useQueryCache = params.useCache
}
if (params.maxBillBytes) {
configuration.query.maximumBytesBilled = params.maxBillBytes
}
if (params.maxBillTier) {
configuration.query.maximumBillingTier = params.maxBillTier
}
}
Now after version 2.16.0, BigQueryIO does provide an option to add clusteringFields in dynamic destinations.
#Override
public TableDestination getTable(String eventName) {
return new TableDestination(tableSpec,
tableDescription, timePartitioning, clustering);
}
Notice that the 4th parameter is clustering, which you can use.

How to read a subsection from a firebase database

I am using this code to access a Firebase Database, but I am cannot figure out how to read the sub array of strings. (the JSON structure is below)). The code I have returns the top level items, but not the list of strings. Would someone be able to assist with this issue?
Here is my function to read from the DB:
func sizes(userId: String = Auth.auth().currentUser!.uid, success: #escaping ([Sizes]) -> ()) {
let ref = Router.sizes.reference()
let query = ref.queryOrdered(byChild: "name") //userId)
query.observe(.value, with: { snapshot in
var array = [Sizes]()
for child in snapshot.children {
if let size = Mapper<Sizes>().map(JSON: (child as! DataSnapshot).value as! [String : AnyObject]) {
array.append(size)
}
}
success(array)
})
}
My Firebase JSON is as follows:
{
"-SzCat_001": {
"name": "Womans",
"sizeCategories": {
"name": "Pants",
"sizeDescriptor": [
"00",
"0",
"2",
"4",
"6",
"8",
"10",
"12",
"XL"
]
}
}
}
And this is what I get returned?
[0] = {
name = "Womans"
sizeCategories = 0 values {} }
I am trying to figure out how to read the sizeCategories list of strings as a subarray of sizes.
Here is my definition of sizes and sizeCategories:
struct Sizes: Mappable {
var name: String = ""
var sizeCategories = [SizeCategories]()
init() {
}
init?(map: Map) {
}
mutating func mapping(map: Map) {
name <- map["name"]
sizeCategories <- map["sizeCategories"]
}
}
struct SizeCategories: Mappable {
var name: String = ""
var sizeDescriptor = [String]()
init() {
}
init?(map: Map) {
}
mutating func mapping(map: Map) {
name <- map["name"]
sizeDescriptor <- map["sizeDescriptor"]
}
}
Thanks for any help!!!
You're jumping through a lot of hoops to read the data here. You could just let allMyData = snapshot.value as! [String: AnyObject] and know that each internal value is also a [String: AnyObject]. But if you really want to destructure into something more typed with this mapping technique, have a look at your sizes definition:
var sizeCategories = [SizeCategories]()
This says "sizeCategories is an array of SizeCategories type". But your data is not structured as an array, it is a dict:
"sizeCategories": {
"name": "Pants",
"sizeDescriptor": [
"00",
"0",
"2",
"4",
"6",
"8",
"10",
"12",
"XL"
]
}
You need to adjust your definition and mapping method here for this field.

Indexing unknown nodes in Firebase

My data structure is like this:
firebase-endpoint/updates/<location_id>/<update_id>
each location has many updates that firebase adds as "array" elements.
How can I index on the "validFrom" property of each update if the location_id is unknown before insertion into the databse?
{
"rules": {
"updates": {
"<location_id>": { // WHAT IS THIS NODE SUPPOSED TO BE?
".indexOn": ["validFrom"]
}
}
}
}
data structure sample
{
"71a57e17cbfd0f524680221b9896d88c5ab400b3": {
"-KBHwULMDZ4EL_B48-if": {
"place_id": "71a57e17cbfd0f524680221b9896d88c5ab400b3",
"name": "Gymbox Bank",
"statusValueId": 2,
"update_id": "NOT_SET",
"user_id": "7017a0f5-04a7-498c-9ccd-c547728deffb",
"validFrom": 1456311760554,
"votes": 1
}
},
"d9a02ab407543155d86b84901c69797cb534ac17": {
"-KBHgPkz_buv7DzOFHbD": {
"place_id": "d9a02ab407543155d86b84901c69797cb534ac17",
"name": "The Ivy Chelsea Garden",
"update_id": "NOT_SET",
"user_id": "7017a0f5-04a7-498c-9ccd-c547728deffb",
"validFrom": 1456307547374,
"votes": 0
}
}
}
Update: I don't think this is a dupe of the said question becauase that question doesn't have a parent object with an unknown id as well. ie both <location_id> and <update_id> are free form keys and cannot be set by hand
I did a bit more digging in the docs and I think this should work:
{
"rules": {
"updates": {
"$location_id": { // $location_id should act like a wild card
".indexOn": ["validFrom"]
}
}
}
}

Generate links for collection resources for a specific single resource

I wrote a custom controller to handle a GET http://localhost:54000/api/v1/portfolios/{id}/evaluate request.
#RequestMapping(value = "/portfolios/{id}/evaluate", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> evaluate(#PathVariable Long id) {
Portfolio portfolio = portfolioService.evaluate(id);
if (portfolio == null) {
return ResponseEntity.notFound().build();
}
Resource<Portfolio> resource = new Resource<>(portfolio);
resource.add(entityLinks.linkForSingleResource(Portfolio.class, id).withSelfRel());
return ResponseEntity.ok(resource);
}
The current response is
{
"summary" : {
"count" : 24.166666666666668,
"yield" : 0.14921630094043895,
"minBankroll" : -6.090909090909091,
"sharpeRatio" : 0.7120933654645042,
"worstReturn" : -2.4545454545454533,
"losingSeason" : 3,
"return" : 3.6060606060606077
},
"_links" : {
"self" : {
"href" : "http://localhost:54000/api/v1/portfolios/4"
}
}
}
but I would like to add collection resources (summaries and systems) linked to that portfolio:
{
"summary": {
"count": 24.166666666666668,
"yield": 0.14921630094043895,
"minBankroll": -6.090909090909091,
"sharpeRatio": 0.7120933654645042,
"worstReturn": -2.4545454545454533,
"losingSeason": 3,
"return": 3.6060606060606077
},
"_links": {
"self": {
"href": "http://localhost:54000/api/v1/portfolios/4"
},
"portfolio": {
"href": "http://localhost:54000/api/v1/portfolios/4"
},
"summaries": {
"href": "http://localhost:54000/api/v1/portfolios/4/summaries"
},
"systems": {
"href": "http://localhost:54000/api/v1/portfolios/4/systems"
}
}
}
I did not find a way to generate those links with the RepositoryEntityLinks entityLinks object
You can always do something like this:
entityLinks.linkForSingleResource(Portfolio.class, id).slash("systems").withRel("systems");
And if your systems endpoint is implemented in a custom controller method you can use the ControllerLinkBuilder to generate a link to your controller method. Lets say you implemented the getSystems method with id parameter in MyControllerClass - then you can generate the link like this (linkTo and methodOn are static methods in ControllerLinkBuilder):
linkTo(methodOn(MyControllerClass.class).getSystems(id)).withRel("systems");