Umarshalling JSON should happen separate from http responses in Go

I had been writing some clever Go code that would unmarshal an http response to JSON all in one foul swoop. Somehow it didn’t occur to me until this very moment that this approach can only cover the happy path unmarshalling. If I had the not-so-crazy desire to unmarshal an error response to do something with that, it couldn’t work.

This necessitates a rewrite of my WordPress API client in my Go front-end app. This was overdue anyway since the client doesn’t have a sensible way of handling cookies. The best thing to do might be completely deleting the client and just having copied code: I keep finding use cases that my app needs that my client doesn’t cover. The option is a super bulky general purpose API client, or a collection of repetitive single-purpose functions that are easy to change. Intellectually, I know the second one is better but I may try to hammer away at the first option a little bit longer to discover more sharp edges.

Even still, realizing that an overly general purpose API client shouldn’t unmarshal its own responses may be an instance of me developing taste in Go. I’ll take that as a win. I may also need to reconsider a struct type for request params, though if I’m writing a function for each thing I need that might be unnecessary. I’m not sure if I hate duplicated code or unnecessary required arguments more.

Anyway, I’m going back to sleep, where my regrettable Go code will likely haunt me further.

Leave a comment