Sorting JSON for unit testing

When you are creating unit tests for things that produce JSON, you will quickly find that you need to compare two JSON strings for equality. The order of key/value pairs within a JSON object is not defined, while the order within JSON arrays is. The spec (RFC 7159) says: An object is an unordered collection of zero or more name/value pairs… An array is an ordered sequence of zero or more values. ...

October 26, 2019 · Maurits van der Schee

Automatic REST API for SlimPHP 4 (minimal)

Last month we showed how to use PHP-CRUD-API (2k stars) as a library executed on an endpoint in the SlimPHP 4 framework (10k stars). In the previous post we used the recommended way to set up a SlimPHP 4 project and in this post we take a minimal approach (using as little code as possible). Install SlimPHP4 We download composer and use it to install the two projects: wget https://getcomposer.org/composer.phar php composer.phar require slim/slim:^4 php composer.phar require mevdschee/php-crud-api This is all that is needed. ...

September 26, 2019 · Maurits van der Schee

GopherCon 2019: videos online

GopherCon is the original Go conference. It debuted in 2014 and was celebrating it’s five-year anniversary last year. Like every year it was held in the Colorado Convention Center in Denver and it had about 1800 attendees. The videos are posted on the Gopher Academy Youtube channel and are also linked here: Thursday July 25 9:00 Welcome Gophers! Russ Cox - On the Path to Go 2 [34:57] Elena Morozova - How Uber Goes [23:31] 10:15 Morning Break ...

August 28, 2019 · Maurits van der Schee

Automatic REST API for SlimPHP 4

Today, about 4 years after the initial commit, the promise of “upload a single PHP file to add a REST API to your database” is still very much alive. It is now possible to use PHP-CRUD-API (2k stars) as a library executed on an endpoint in the SlimPHP 4 framework (10k stars). This is possible as they both adhere to the PHP-FIG’s (PHP Framework Interop Group) HTTP standard PSR-7. Install SlimPHP 4 You need to run the following commands on your Linux system: ...

August 16, 2019 · Maurits van der Schee

Script to undelete all files in Git

I have written a Bash script to quickly undelete all files that are deleted in a Git repository (on any previous commit). The script only recovers the last known state of files of which the filename is not currently in use. It is optimized so that it executes quickly when large numbers of files are deleted. Usage Copy the script into the repository that you want to undelete files in and run: ...

August 13, 2019 · Maurits van der Schee

API authorization strategy: use the DB

When building an API you may find the need to implement authorization in a generic way. Using the authorization implementation of your (relational) database is a well-documented, simple and proven strategy. The user that is used for the database connection should in this scenario depend on the authenticated user of the API (and it’s authorization). This post will explain how to apply this strategy. But before we start let’s take a step back and think about what it is that you may be authorizing. ...

July 29, 2019 · Maurits van der Schee

Database multi-tenancy strategies

When you are building a SaaS software product you need to chose a way to store the data of your customers. You can store everything in one database or you can create multiple databases. There are roughly four main approaches. This article discusses the up- and downsides of these approaches. Multi-tenancy: 4 approaches First let’s try to summarize the four identified approaches: All customers in one database (fully normalized) Every table has a CustomerID field (de-normalized) A database for every customer and one shared database Every customer has it’s own database Now let’s try to find some up- and downsides for each of them. ...

July 28, 2019 · Maurits van der Schee

Handling GeoJSON tiles in Leaflet

Leaflet is world’s most popular open-source JavaScript “Tiled web map” library offering Google maps like functionality on your own (or public) data. In the past weeks I have implemented a GeoJSON vector tile plugin for Leaflet. In this post I will explain when to use raster tiles, Mapbox Vector Tiles, GeoJSON tiles or a GeoJSON bounding box. Why tiles? When somebody has a free pan and zoom over a map, no two maps are the same, as you have a lot possible visible maps and sizes and zoom levels. For a single user this would be fine, but as soon as you have multiple users then you want to use some form of caching to reduce the load on the server. You could fix the map size and pan and zoom in fixed steps, but that does not feel good. This is why tiling is introduced. It allows you to have the caching advantages of fixed steps, while allowing the user custom map sizes and seamless pan and zoom (Google Maps has fixed zoom steps, Google Earth has seamless zoom). ...

July 20, 2019 · Maurits van der Schee

Lossless compression of PHP files

How small can a PHP file get? I was wondering this, while building PHP-CRUD-API, a full-featured API in a single PHP file. PHP has a really nice feature called ‘halt_compiler’, which allows you to have gzip contents in your PHP file. In the code below I’m applying that technique to PHP code. This allows you to reduce the size of a PHP file without losing any of it’s functionality. Why would you do this? The file gets smaller, but executing it will be slower as it is need to be uncompressed before it can be executed. I also think the opcode cache may have more trouble optimizing your code when you uncompress it on-the-fly. Despite these downsides, some people use similar techniques for obfuscating the source code they publish. So I guess you could use this script to prevent people from easily reading your source code. I have not found any other (good) use case (yet). ...

July 19, 2019 · Maurits van der Schee

Open source software pyramid

For us (professional software developers) there are open source libraries and tools that cover most of what we are doing. Even in commercial projects we can often leverage these as their licenses are often permissive towards commercial use. Which raises the question: When do you write your own code? On a GDS blog I ran into the “open source pyramid” as advocated by JP Rangaswami: For common problems use Open Source. For rare problems use Buy. For unique problems use Build. This is a pattern I have often encountered in commercial software companies. I have seen exceptions to this rule when the software is very important to the company (for instance part of the strategy or “core business” of the company). ...

June 11, 2019 · Maurits van der Schee