Go reading a nested JSON without structs the easy way…

Let’s face it sometimes all we need is just to get something out of a JSON response without actually needing to go all the way and create structs to reflect its internals.

The quick and dirty solution to this is a map with a string as a key and an interface{} (think Object in any other language) which can then be accessed with mymap[“key”]

and if needed cast on the fly like this: mymap[“key”].(string)

Problem is this all works super easy and out of the box with a one level json. The issue is when you would need to access something more internal. Let’s take this JSON example:

"data": {
"gid": "1140824417098551",
"resource_type": "task",
"created_at": "2019-09-20T07:17:05.161Z",
"modified_at": "2019-09-20T07:17:05.161Z",
"name": "test from postman",
"resource_subtype": "default_task",
"notes": "notes from postman",
"assignee": {
"gid": "1139046208196844",
"resource_type": "user",
"name": "Fabio Russo"
"completed": false,
"assignee_status": "inbox",
"completed_at": null,
"due_on": null,
"due_at": null,
"projects": [
"gid": "1139411274392065",
"resource_type": "project",
"name": "test"
"start_on": null,
"tags": [],

With some omissis this is a typical response when adding a task with the Asana API. Let’s say I just need to print out or store the task id (in this json “gid”).
If I just do it like this:

var response map[string]interface{}
respBody, err := ioutil.ReadAll(resp.Body)
if err = json.Unmarshal(respBody, &response); err != nil {
//error code here

Now let’s say I’d love to do something that other languages allow such as:  responseID := response["data"]["gid"]

But that’s asking too much. And if I do it in two steps: responseData := response["data"]

I can’t just do  responseGID := responseData["gid"]

because responseData is an interface{} and not a map[string]interface{} aka a json object we can inspect.
The solution is simple: just cast the responseData into what it really is: a map[string]interface{} because we know it is.

I end up with a response object that I can use like this:

responseData := response["data"].(map[string]interface{})
taskID := responseData["gid"].(string)

Which will totally work as long as we know the json structure in advance (which we would need to even in the case we’d be using structs for the job).

Go decode base64 image.. the easy way

I was banging my head against this for hours and then I found the solution.

While attempting to Decode a jpg image from base64 I kept getting a missing SOI marker error.

And then I found out that it’s way easier to just use image.Decode (the generic version from the “image” package directly) which will basically decode anything you throw at it 🙂

base64Data := b64.NewDecoder(b64.StdEncoding, strings.NewReader(userData))
img, str, err := image.Decode(base64Data)
if err != nil {
    log.Println("Error decoding image: " + err.Error() + " str: " + str)

Maybe this can save somebody a little time.

Connecting Go app to App Engine Cloud SQL (Postgres)

Just putting this here because I’ve been looking for hours without finding the right connection string. So basically if you’re using sqlx for example you’re going to need a connection string for your go app.

The easiest thing is to place this as an environment var in your app.yaml. In order to do this your app.yaml might look like this:

runtime: go111
  DATABASE_URL: "postgres://username:password@/databasename?host=/cloudsql/example:us-central1:example123"

After /cloudsql/ you’ll have your instancename (hint just run gcloud sql instances describe yourproject) from the command line and you’ll find a “connectionName” key, that’s the one

the trick was to 1) not use a host but /databasename directly and 2) using host= and not socket or unix_socket

N.B. This will work with a STANDARD GAE + Cloud Postgres

In the Go code it’s as simple as:

url := os.Getenv("DATABASE_URL")
DB, err = sqlx.Open("postgres", url)

Fixing a Matias switch with keychatter on a KPB V80

So I have this beautiful matias quiet click keyboard that I sort of love for its tactility. The sounds is slightly less impressive though, but still way better than my floss modded IBM M Spacesaver. But the real bummer with this keyword is, you guessed it, keychatter. Now the reason for this is most probably the way the switches are lubed and not properly tested by QA. I have several keys that drive me mad. Let me show you how to fix this if you’re in the same condition.

  • remove the keycap of the switch to “repair” and the ones next to it so you have a little wiggle room

  • use two toothpicks to gently allow the upper part of the switch to be pulled up and released. Do this gently, very very very gently.

  • once the switch is disassembled remove the tactile leaf and clean it with alcohol

disassemble and clean matias switch

  • air spray the bottom of the switch

  • reassemble with care

That should be it. I was able to repair some of the keys and have a way better typing experience now. Hope this helps somebody. There are tons of videos on youtube on how to disassemble alps and matias switches. If the keychatter persists you probably need a new switch and get soldering.

Adding a 10g spring to a topre 45 keyboard

So I’m back to crazy keyboard mods. I have a topre Novatouch that is 45 grams actuation force. It’s nice but it’s definitely lighter and a little less “thock” than a 45 gram uniform Realforce. So I decided I’d try to add a 10g spring. The actuation on a topre board depends on the rubber dome and not on the spring force like with cherry compatible switches so adding an external spring will change tactility and feedback, not just add force. I knew that the feel would be slightly different but… I’m not offended by it in the least. When you hit a topre key you usually face a little bit of resistance high up on the keystroke and then, after a tactile bump, the rest of the travel goes down real fast until you hear the “thock” sound (ok, I don’t, not really because I’ve also silence-modded this baby). With the external spring you need more strength (should be around 55gram actuation ideally) but the weird thing is that instead of the initial resistance-bump-drop-to-the-bottom thing there now is more of a uniform resistance, slighter bump and the key jumps back up. This is much more similar to an IBM Model M but without the sound or (if you exclude the tactile feedback) to a cherry mx black old school. So it’s a completely different beast. I like it, but it’s not topre any more. It’s a ModelM/Topre/MX Black hybrid and (as fatigued as my fingers are at the end of the day) I LOVE IT.

From SQL to ORM and back

Let’s face it, we’ve all been there. We started our career with SQL, found it cumbersome and difficult to remember, had too many DataManager classes around to encapsulate queries and got tired of all that work. Then suddenly we were hit by the light at the end of the tunnel.. an ORM.. in my case Hibernate was love at first sight. No more fussing with SQL dialects, no more Stored Procedures on Oracle, something reusable, write once run on any DB, real java style. And then just sometimes you’d hit a wall where some really crazy query or nested selects weren’t as easy without having to really know every nuance of the library, and even then it sometimes wouldn’t work. And then there was the Heap growing like crazy, garbage collection to tame, too many objects being pulled into memory during a massive select. Of course there was also data mapping, but that was even more xml, no thanks. Then came Ruby on Rails with ActiveRecord and all its beauty and ease. Everything was a breeze. Except it wasn’t and sometimes you’d still need to do some nasty native query. But hey it was nice. Most of the time. Until of course it took more code and workarounds to make AR behave like a very specific native query that just couldn’t be mapped. And maybe it ended up with more code, more loops and more memory than necessary. And don’t even get me started on performance! And so recently (like a couple of years now) I came to know Go. Go is plain but in a subtle way. But database wise it doesn’t come with a lot of extras attached. So I went back to having structs mapped to tables (well not only) and to using plain old native SQL (yes I have the luxury of saying I want Postgres because Heroku or other services will just allow me to). And so everything became sort of “simple” again. Of course there’s more work involved. I have query files that are pure SQL and call them from my code. I need to cycle through results or just assign a query result to an array and all. But.. and this is very important, I have two big wins: 1) I have complete control 2) I have super performant code

The only downside is you need to really know your SQL.. but hey… SQL is something that will never die, just like C and Linux. So learning it is investing in a lifelong skill anyway. Plus the amount of control is simply stunning. No more walls. No more coding around a barrier or lack of implementation in a library. This feels like home. Yep.

NSImage tinted as easily as a UIImage

I was looking for a simple way of tinting an NSImage the way I’m used to doing with UIImage but it appears that Cocoa is a little behind Cocoa Touch in this regard. I found many different solutions but none of them worked out of the box with Swift 4. This is what I came up with eventually and works as expected.

extension NSImage {
    func tinting(with tintColor: NSColor) -> NSImage {
        guard let cgImage = self.cgImage(forProposedRect: nil, context: nil, hints: nil) else { return self }
        return NSImage(size: size, flipped: false) { bounds in
            guard let context = NSGraphicsContext.current?.cgContext else { return false }
            context.clip(to: bounds, mask: cgImage)
            return true

NSColor from/to HTML colors

Here’s a nice little extension I wrote to create NSColors given an html string and vice versa getting an html string from an existing NSColor. Hope it’s useful.

extension NSColor {
    public convenience init?(htmlString: String) {
        let hexColor = htmlString.replacingOccurrences(of: "#", with: "")
        let scanner = Scanner(string: hexColor)
        var hexNumber: UInt64 = 0
        if scanner.scanHexInt64(&hexNumber) {
            let red = CGFloat(((hexNumber & 0xFF0000) >> 16)) / 255.0
            let green = CGFloat(((hexNumber & 0x00FF00) >> 8)) / 255.0
            let blue = CGFloat(((hexNumber & 0x0000FF) >> 0)) / 255.0
            self.init(red: red, green: green, blue: blue, alpha: 1.0)

        return nil
    func toHTMLString() -> String {
        var r: CGFloat = 0
        var g: CGFloat = 0
        var b: CGFloat = 0
        var a: CGFloat = 0
        self.getRed(&r, green: &g, blue: &b, alpha: &a)
        return String(
            format: "#%02X%02X%02X",
            Int(r * 0xff),
            Int(g * 0xff),
            Int(b * 0xff)


Multisite WordPress with A2

After looking at several options I settled with A2 hosting at around 12$/month but at least I have a bunch of domains on a single network wordpress installation and everything is working great.

Here’s the steps involved:

  • Go to your CPanel and install WordPress from Softaculous (not the optimized version)
  • Make sure it’s networking is turned on
  • To add an existing domain go to CPanel-> Addon Domains and let’s add an alias to our domain like this:

  • Then it will be automatically added to the DNS zone editor
  • If you have the domain on another provider (mine’s godaddy for example) you’ll need to set the dns to point to A2

  • Now back to your wordpress on A2 (yoursite/wp-admin). Go to My Sites, Network Admin->Sites

  • Make sure that you have your main domain / a desired wordpress folder. This is a hack basically. The real wp network site will live in that folder

  • The real trick happens in the Settings. The siteurl can be pointed to the actual domain we registered in the cpanel dns zone section

  • Now install the MU WP Domain mapping plugin https://codex.wordpress.org/WordPress_Multisite_Domain_Mapping and follow the installation guide.
  • Now the last step is to go to My Sites -> Settings -> Domains and add the unique site id (number .. you can see it when opening My Sites site in the url as the id parameter) and the new domain there (primary should be set to no otherwise it will simply redirect to your main site! Happened to me and made me waste a little time so beware ):

All set. If you visit your domain you should see your wordpress blog. Enjoy!

Time to wash some records

During our vacation I spent some time buying used records in Amsterdam. I ended up with quite some good stuff among which Porcupine and Ocean Rain by Echo and the Bunnymen, Japanese Whispers and Staring at the sea by the Cure, Closer by Joy Division, Keith Jarret’s Koln concert and the list goes on and on. Needless to say most of them, even if in great condition, needed a thorough washing. I use the Disco Antistat and, having finished their cleaning fluid, I decided to try and make some homemade based on the London Jazz Collector’s recipe. I know this was meant for RCMs but it does work with the Antistat too. I must say that the results were pretty good. At least as good as with the original fluid but with no residue at all! It does take more time to dry though, but that’s ok. All in all I’m really happy with it and, being a lot cheaper, I can just throw it away every 15 or so vinyls cleaned.