Leonardo Maia Pugliese
Holy Swift

Holy Swift

Testing your codables with one line Python script in Swift

Testing your codables with one line Python script in Swift

The Python one line solution

Leonardo Maia Pugliese's photo
Leonardo Maia Pugliese

Published on Oct 20, 2021

4 min read

Subscribe to my newsletter and never miss my upcoming articles

Hallo collega's, Leo hier.

The problem we will face today is something that every iOS Developer eventually encounters. You start your sprint (if you still do sprints and not a kanban-like development), and the backend is not ready yet. So you sit down with them and hopefully can outline how the json will look like. So you go back to your desk and think: How I can be sure that this codable parses the json correctly?

Today we will check how can we do this with Python! And without Python also.

The next two weeks will be pretty hard/intense for me. So maybe or not I'll have time/energy/willpower to post something here, I hope everything be back to normal in the from mid November and so on.

That's said, let's code! But first... The painting.

The Painting

This painting is called "Coal cars" an 1821 art piece from Théodore Géricault. Géricault's fiery, daring personality and short life, fit the mold of Romantic artists of his era and, along with his controversial paintings, profoundly influenced 19th-century art.

I choose this painting because it demonstrates hard working people in the coal mines, and my next weeks will be intense, so I found that fits into my actual situation.

The Problem

You have a json structure and need to test decoding them with your codables.

Imagine that you have a json structure to try to decode, like this one:

{
    "id": "1",
    "title": "Star Wars!",
    "genre": "Syfi"
}

For the sake of simplicity, this is not a complex json. But the idea is the same for a big one.

And you write a codable movie object like this:

struct Movie: Codable {
    let id: Int 
    let title: String
    let genre: String
}

How can you quickly know this is wrong?

Python Solution

The Python solution we will use a pretty little command in Terminal. We will first create a file, serve it with a Python server, so any Swift code can read it!

First create a file called index.json and put this into it:

{
    "id": "1",
    "title": "Star Wars!",
    "genre": "Syfi"
}

Open the Terminal and navigate to the folder you create the index.json file.

Now type this in Terminal : python -m SimpleHTTPServer 8100. But wait a minute, what is this doing? It's literally saying:

Hey Mr. Python can you use your module SimpleHTTPServer to create a HTTP server, serving everything in my folder on the port 8100, please?

And that's exactly what it does. With this simple statement you now have a HTTP server that you can reach with your Swift code and test your codable against it!

So now, in the Playgrounds you can just test:

let url = URL(string: "http://localhost:8100/index.json")!
let data = try! Data(contentsOf: url)
let decoder = JSONDecoder()

let movie = try! decoder.decode(Movie.self, from: data)

print(movie)

And the result is.... oh wait:

Screenshot 2021-10-20 at 18.03.46.png

It's saying our model is wrong in codingKey "id", it expected to decode Int but found a string/data instead. Let's fix it changing from Int to String!

struct Movie: Codable {
    let id: String
    let title: String
    let genre: String
}

Now it's perfect!

Screenshot 2021-10-20 at 18.04.17.png

This way when the backend are ready to test, you already know that at least the json both sides agreed is parsing correctly. Awesome right?

The python solution is great when you need to implement something into legacy code that is hard to modify the network layer, so you will need to add this new http request to the localhost and the codable, and you are ready to test. But of course if you have an easy networking layer, you can go with a simpler Swift solution that you will check below.

Only Swift Solution

If you can easily access the networking layer of your app you can just modify the data received like this:

let json = """
{
    "id": "1",
    "title": "Star Waors!",
    "genre": "Sci-Fi"
}
"""

let data = json.data(using: .utf8)
let movie = try? JSONDecoder().decode(Movie.self, from: data!)

print(movie)

And the result is pretty much the same!

Screenshot 2021-10-20 at 18.16.27.png

And that's it!

Summary

Today we study a fast way to create a HTTP Server in Python and how this can be useful to test your http requests. Also we checked a fully Swift way to check and parse your codable to be sure that your models are up to date with the backend.

That's all my people, I hope you liked as I enjoyed write this article. If you want to support this blog you can Buy Me a Coffee or just leave a comment saying hello. You can also sponsor posts and I'm open to writing freelancing! Just reach me in LinkedIn or Twitter for details.

Thanks for the reading and... That's all folks.

credits: image

Did you find this article valuable?

Support Leonardo Maia Pugliese by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this