Kevin Beaulieu

Eq-Go

Go code semantic diff tool (2021)

View Project on GitHub

The idea for this project arose out of necessity while working on a contribution for gqlgen, a GraphQL server library for Go. A large part of the library is a code generator that takes a GraphQL schema and outputs Go code for model types and resolver functions. My contribution to the library was to give users more control over the way in which the generated code is organized (see GitHub issue for more details). To test that my change was correct, I wanted to be able to verify whether the generated code from the original implementation was equivalent to that of my new implementation. By "equivalent", I mean that the generated code might be broken up into different files, and variable/function definitions might be ordered differently, but the code should otherwise be functionally equivalent. After finding that no such tool already existed for Go, I set out to build my own.

The main challenges in building eq-go were understanding the grammar of Go code in detail, building comparators for every type of token in the grammar, and building sorting algorithms for tokens that can be ordered in multiple ways without affecting their behavior (e.g., import statements, struct properties, and function lists).

eq-go is now available as a Go package for use in other projects, and as a command-line tool for comparing Go code files locally. It is currently being used in gqlgen to verify that generated code is equivalent across different modes of operation.

My Work

Check out my open-source contributions on GitHub.