Golang - Parsing nested JSON - go

I'm using the go-worker to process resque jobs. A job has a payload which has a nested JSON structure like this:
[{
"key-a":"val-a",
"key-b":"val-b",
"files":[{
"key-a": [
{"a":"b","c": "d"},
{"e":"f","g": "h"}
],
"key-b": [
{"a":"b","c": "d"},
{"e":"f","g": "h"}
]
}]
}]
Now go-worker gives me args ...interface{} which represents that JSON payload, not the actual JSON text. Is there an idiomatic way to convert that(args) to the correct types (could use another package to do this.) Using type assertions manually seems a bit tedious for such a thing.

If it's really giving you the actual go objects (i.e. a bunch of map[string]interface{} and not the json string itself) then there probably isn't much you can do besides a bunch of type assertions.
You could re-marshall it to json then parse it again into the correct structs, but that's a bit of a hack (and I have no idea if it would be performant or not).

Related

JMeter - If Controller not working for certain string contains condition?

Background:
I have an API that returns a response like so:
{
"status": 1,
"errorCode": null,
"message": null,
"data": [
{
"id": 33,
"snapshotId": 2,
"ceId": 29,
"month": "Feb",
"corpRcvPayAmt": 100000,
"wthRcvPayAmt": -90000
},
{
"id": 31,
"snapshotId": 2,
"ceId": 29,
"month": "Jan",
"corpRcvPayAmt": 0,
"wthRcvPayAmt": 0
}
]
}
The data node can vary from 0 size to 12 (empty to 1 per month). The only constant field for data is the "month" node. Other field values are changing.
Goal:
I want to do either a PUT or a POST request, depending on whether or not a specific month is already present in data.
For this, I'm capturing the entire data field into a variable using a Regular Expression Extractor like this:
With the sample response above, calling ${data} will give me {"id":33,"snapshotId":2,"ceId":29,"month":"Feb","corpRcvPayAmt":100000.00,"wthRcvPayAmt":-90000.00},{"id":31,"snapshotId":2,"ceId":29,"month":"Jan","corpRcvPayAmt":0.00,"wthRcvPayAmt":0.00}
Now using this field, I just want to do a simple if condition like so: if ${data} contains ${month} do something (PUT request). I'm currently trying to do it like this:
Problem:
The condition ${__groovy("${data}".contains("Jan"))} does not seem to work.
I've already tried doing the following:
change the condition to ${__groovy("${month}".contains("Jan"))} just to see if the syntax is correct. This works just fine.
Add a debug sampler inside the if controller using the condition ${__groovy("${month}".contains("Jan"))} to print ${data}. This also prints the data as expected.
I also tried using various js syntax for the if condition but so far groovy is the ongiving the best results.
With this I can confirm the following:
${month} and ${data} are extracted and stored properly
groovy syntax for string contains is working properly
for some reason, the condition ${__groovy("${data}".contains("Jan"))} is not working.
What am I missing here? Thanks in advance.
Don't reference JMeter Functions and/or Variables in Groovy scripts as:
There could be a conflict with GString Template
For JSR223 Test Elements it causes compilation cache feature malfunction reducing the performance
Your variable might simply resolve into something which will cause Groovy script compilation failure and/or malfunction. You can check jmeter.log file for the actual error details.
So instead of using ${data} go for vars.get("data") instead, vars is a shorthand for JMeterVariables class instance and this is the best way of getting a JMeter Variable value from the Groovy script.
The whole function should look like:
${__groovy(vars.get('data').contains("Jan"),)}
Also be aware that it's better to use JSON Extractor for obtaining values from JSON responses.

"1 != 1" error from immutable react unit tests

Running some unit tests while learning react and I'm seeing this fail message
expected Map { "items": List [] } to equal Map { "items": List [] }
My question is... what? I'm using fromJS for both the action and evaluation but this matches the tutorial I'm following and seems to be working elsewhere in the project. What am I missing here?
Looks like you have an object of type Map, and you are comparing if both have the same reference in memory. Since they don't have the same reference in memory because each object was created with the keyword new Map(... then first Map object with memory reference XYZ != second Map object with memory reference ABC.
I hope it makes sense.
Cheers,
Figured it out, I was missing chai.use(chaiImmutable); from my test entry point. Jorge's assertion was correct, but apparently this line is what allows value-wise comparison of immutable objects.
Thanks!

Creating an instance of a structure with unexported sub-structures in Go

I am trying to manually create an instance of the type ReactionAddedEvent given here, in nlopes' Go Slack library. However, the sub-type reactionItem is unexported, which leads me to receive the error ./bot_test.go:111: cannot refer to unexported name slack.reactionItem when trying to instantiate the object.
Here is my code:
m := &slack.ReactionAddedEvent{
Item: &slack.reactionItem{
File: &slack.File{
Preview: "Test",
URLPrivate: "http://google.com",
},
},
Reaction: "white_check_mark",
}
When I remove the identifier &slack.reactionItem from line 2 in that snippet, I get instead the error: ./bot_test.go:112: missing type in composite literal, obviously.
Is there any way for me to instantiate an object of this type with the parameters I need?
First, if slack here refers to the nlopes library, the slack.ReactionAddedEvent structure's Item field is not a pointer, so you can't store an address of a slack.reactionItem struct into that field anyway. Second, the File field of slack.reactionItem is a string, not a structure.
Third, even if the above isn't/weren't the case, if the types are not exported, but the fields themselves are, you can't assemble the structure in a single literal. Instead, you'll have to manually set those fields after creating the structure variable:
m := &slack.ReactionAddedEvent{Reaction: "white_check_mark"}
m.Item.File.Preview = "Test"
m.Item.File.URLPrivate = "http://google.com"
But again, if you're using the nlopes library, that won't work because the File field isn't actually a structure:
https://github.com/nlopes/slack/blob/master/websocket_reactions.go
And fourth, if the type isn't exported, that's probably a good sign that you shouldn't be manipulating objects of that type. In this case, in the nlopes library, those structures are only intended to be used for unmarshalling and then handling events from JSON messages.

Video Inside Silhouette using kinect

I am using this git
I am getting error data/config.json does not exist or could not be read.
How do I create json file or load one?
The configuration json file is missing from the source code, however, checking out how the json is parsed in ConfigManager should allow you to create one from scratch.
Just looking at what the property names and types are in that class you can work out something like this:
{
"clips":[
{
"silhouette":"add your silhouette filename here",
"background":"add your background filename here",
"duration":0
}
],
"useKinect":true,
"useGpu":false,
"name":"Your Application Name here",
"resizeSilhouette":false,
"mirrorSilhouette":false,
"resizeSilouhette":false,
"overlayVideo":true,
"useActionClips":false,
"silhouettePadding":{
"top":5,
"right":5,
"bottom":5,
"left":5
},
"centerOfMass":true,
"showTime":true,
"smoothSilhouette":0,
"crossfade":0,
"silhouetteCache":{
"enabled":false,
"minFrames":3,
"maxFrames":10
},
"scale":{
"width":640,
"height":480
},
"osc":{
"enabled":false,
"serverPort":12000,
"clientAddress":"127.0.0.1",
"clientPort":12001,
"channels":0
},
"actions":{
"frequency":1,
"clips":[
"clipName1","clipName2"
]
}
}
If you save this as config.json in your sketch's data folder it should load.
However, bare in mind this may also crash as I've just placed some dummy placeholder data to give you an idea. Fill in the data you think you know what it should be for your project and figure out the rest as you go along.
Unfortunately the github project you decided to use isn't documented, so this means you will have to go through all the source code and understand it before you get to use it.

How do I parsing data from JSON object?

I'm just starting to dabble in consuming a JSON web service, and I am having a little trouble working out the best way to get to the actual data elements.
I am receiving a response which has been converted into a Ruby hash using the JSON.parse method. The hash looks like this:
{"response"=>{"code"=>2002, "payload"=>{"topic"=>[{"name"=>"Topic Name", "url"=>"http://www.something.com/topic", "hero_image"=>{"image_id"=>"05rfbwV0Nggp8", "hero_image_id"=>"0d600BZ7MZgLJ", "hero_image_url"=>"http://img.something.com/imageserve/0d600BZ7MZgLJ/60x60.jpg"}, "type"=>"PERSON", "search_score"=>10.0, "topic_id"=>"0eG10W4e3Aapo"}]}, "message"=>"Success"}}
What I would like to know, is what is the easiest way to get to the "topic" data so I can do something like:
topic.name = json_resp.name
topic.img = jsob_resp.hero_image_url
etc
You can use Hashie's Mash . One of the best twitter clients for ruby uses it, and the resulting interface is very clean and easy to use. I've wrapped over Delicious rss api with it in less than 60 lines.
As usuall, the specs show very clearly how to use it.

Resources