Complete Go for Professional Developers

JSON Response Writer Refactor

Melkey
Twitch
Complete Go for Professional Developers

Lesson Description

The "JSON Response Writer Refactor" Lesson is part of the full, Complete Go for Professional Developers course featured in this preview video. Here's what you'd learn in this lesson:

Melkey creates a utils package and codes some helper functions to eliminate repeated code in the API handlers. A ReadIDParam function is created to get the ID parameter from incoming requests. Logging and JSON formatters are also added to the utils package.

Preview
Close

Transcript from the "JSON Response Writer Refactor" Lesson

[00:00:00]
>> Melkey: So at this point in time, we can actually go to the next section. But it won't be very good of me to do that because there's lots of smelly things happening in a code base, okay. There's lots of DRI, there's lots of just nastiness that I want to address now that we have this opportunity to kind of go in between this section and the following one which is going to be testing.

[00:00:23]
Now, this particular sub module section is going to be too long, but what it will do is it will introduce a new utils package for us to use, all right. And this utils package will do a few things for us. If you noticed, if you go back, the JSON getting back looks very ugly.

[00:00:45]
The JSON get back is just it's not organized, it doesn't look like pure JSON. It's no indents, it looks very not nice. And then another thing is, in all of our handlers, we are repeating the read ID parameter function over three times now. And I'm actually a person who isn't super particular DRI, if you have to do it, go ahead and do it.

[00:01:05]
But in this case, it makes no sense, because it's just updated to update in three different places now, which is kind of just annoying. So what we're gonna do now is gonna create a utils function. We're gonna address writing JSON in an easier way, in much more maintainable way.

[00:01:21]
I'm gonna add a read ID param function, I'm gonna clean up the handler specifications that we have right now. So in the internal folder, let's go ahead and make a new folder called utils, and then within utils, go ahead and make a new file called utils.go, okay. I'm gonna call this package utils, okay.

[00:01:46]
And the first thing we're going to create is func writeJSON, like so. This writeJSON is gonna take a few arguments, first, it's gonna be the writer. So it's gonna be HTTP response writer, like so. It's gonna take a status, which can be a type int and a data type.

[00:02:04]
Now, it's gonna be a really cool thing that we're gonna realize how we can navigate through different types in Go, but here we're gonna introduce a new type called Envelope, okay? And this Envelope is going to be a map which takes a string as key and then an interface as a value.

[00:02:24]
It's particularly a naked interface or empty interface. Now you may be asking, why and what does this mean? Well, the empty interface is goes way of basically having the any type. And go does have the actual any type, I believe now, I think this was introduced later on in Go, call it for what it is.

[00:02:48]
I'm gonna use just the interface type, I think my editor actually also recommends to use any sometimes. But I'm gonna use just this empty interface, and then here, data is gonna be of type, envelope, like so. And so why does this matter? Well, now for Ray JSON, we can simply give it data which is going to be a map, which is a type envelope with, let's say the string says error, and then we can also pass it the error message or the actual error type, or on successes.

[00:03:16]
We can say, this is your workout and pass it the workout struct for successful creations. So let's see how that looks, so here we're gonna just do some variable js, okay, I'm going to say json.MarshalIndent. I'm gonna pass in data, an empty string here, and then two tabs, or just one tab.

[00:03:38]
The tabbing means how much you wanna tab your JSON fields, okay. If error does not equal to nil, we can go ahead and return an error. And that just reminds me, make sure your right JSON is returning an error type like so.
>> Melkey: Okay. We wanna add new lines to the JSON, so we can do append js, and we can do single quotes, forward slash and to break the lines.

[00:04:15]
>> Melkey: And then here we can just do W header, we'll set content dash, type, application, slash JSON, like so, W dot right header can do the status that we bring in. So again, status is an int, W.rightjs, like so. And then we can return, no. And while we're here, let's go ahead and also create the func read ID param function.

[00:04:49]
So this is gonna take the pointer to HTTP request like so-and-so, return an int 64 and an error. And here we'll do ID param is going to be chi.URLParam,r, please slug we're extracting is ID. If ID param is empty, we can simply do return, just put any default value as zero and put errors.new.

[00:05:20]
We're gonna create our own error type here, we can just say invalid ID parameter. All right, then we can transform this id param into a base 10 int 64 type, because right now it is a string. So we can do string conversion, parse int we can pass it ID param, the base 10 into 64 check the error.

[00:05:45]
If error does not equal to nil, we can simply just again return zero and do errors.new invalid ID parameter type.
>> Melkey: And at the end here is return ID and no.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now