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)


Quick Dokku Cheat Sheet


#Get running containers:
docker ps

#Get all containers:
docker ps -a

#Stop container:
docker stop container_id

#Remove container:
docker rm container_id


#Start/Stop/Restart app:
dokku ps:start myapp; 
dokku ps:stop myapp; 
dokku ps:restart myapp

#Restart ALL:
dokku ps:restartall


#Read app log:
dokku logs myapp


#Add domain:
dokku domains:add myapp mydomain; 

#Remove domain:
dokku domains:remove myapp mydomain


#Set env var:
dokku config:set myapp MY_ENV_VAR='value'

#Unset env var:
dokku config:unset myapp MY_ENV_VAR

#Set global env var:
dokku config:set MY_ENV_VAR='value' --global

Swift refactoring XCode vs AppCode

Ok so I took up the challenge and started coding in Swift for Pins2Go (the mobile version of Pins) and I needed to simply change a test variable name…

Here’s what XCode told me (Can’t refactor Swift code. XCode can only refactor C and Objective C code.)

Well that didn’t seem very nice to me given how much Apple’s pushing Swift coding lately!

So I decided to try doing this in AppCode:

This JUST WORKED. Flawlessly. Smoothly.

AppCode: Epic win, XCode: Epic fail!!!