What is Copygen?

Copygen is a command-line code generator that generates type-to-type and field-to-field struct code without adding any reflection or dependencies to your project.
Apr 13, 2022
3 min
10/25/2021

While creating an automated royalty management solution, I needed a way to speed up programming development (that would also be subject to change in the future). Instead of compromising the application's design by sacrificing iterability for short-term code-speed, I decided to use code generation: Generated code would allow the program to maintain a resilient design at no cost; with the added bonus of being usable on future projects.

To understand how Copygen plays a role in this process, you need to understand how a maintainable application is designed. Just as there are multiple ways to build a house, there are also multiple ways to build a software application. In real life, a person can haphazardly put a hut together and call it a day, but the hut will be knocked over as soon as the wind blows. In contrast, a well-designed fortress will stand up against the strongest circumstances that even missiles struggle to destroy. One structure is easier to build (with regards to time), but the other is easier to maintain.

The significance of Copygen lies where it fits as a tool to generate maintainable code; which has many benefits (especially with regards to collaboration). The use-case for Copygen is described here: When to Use Copygen. Copygen's customizability gives it many potential use cases. However, Copygen's main purpose is to save you time by generating boilerplate code to map objects together.

Objective

Create a customizable code generator for Go that generates type-to-type and field-to-field struct code without adding any reflection or dependencies to a project.

How It Works

All information pertaining to the technical details of Copygen can be found on Github: https://github.com/switchupcb/copygen.

Project Timeline

The project timeline can be viewed using Git Commit History: I spent 14 days creating version 0.1.0 of Copygen which was released on September 29th, 2021. Upon completion, I created a post on Reddit which led to a user asking me to compare Copygen vs. Goverter. The consensus was that Copygen maintained more capability, but a worse User Interface (UI) so I took inspiration… I'd spend 6 more days optimizing the project and implementing an improved UI which solidified Copygen’s dominance as a field-to-field code generator (in Go). Changes at this scale and speed were only possible due to the application design decisions I’d made in the first version.

Copygen version 0.2.0 was released on October 5th, 2021. There were a few edge cases that popped up over the course of the month that were promptly resolved and support for customization templates (using the Go language rather than go/templates) was added in version 0.2.3. Unfortunately, the interpreter I used to implement this feature did not support Go modules. In order to resolve this issue, I created a pull request (on September 27th, 2021) that wasn't resolved until much later.

Copygen version 0.3.0 was started on April 5th, 2022 and released on April 13th... all with a broken keyboard. This version heavily improved the stability of the code generator and added a host of new features. It also allowed developers to create custom loaders, matchers, and generator functions, and pass files as they would normally programmatically. Copygen would be recognized by AwesomeGo — a curated list of Go projects — on April 24th.

Development of Disgo allowed me to use Copygen at scale. This uncovered a host of issues that were resolved in version 0.4.0 (June 27, 2022). The release of Disgo Beta led to the creation of Copygen Usecases, which showcased How Disgo Uses Copygen (July 27). Up to this point, work on the interpreter was practically non-existent due to the maintainers ignoring it. A user prompted us to revisit the pull request (from September 21), which led to a discovery on how to support Go modules. However, this has yet to be implemented upstream.

You can track the release progress of Copygen on Github.

Outcome

I’ve used Copygen on a number of upcoming projects in order to keep them maintainable while saving time spent typing. Copygen allows me to spend this time on other tasks while keeping my applications performant. While it’s designed to reduce field-to-field boilerplate code (that isn’t avoidable by using generics), it also can be used as a custom code generator. The current software environment provides a number of ways to store your data: There are relational, graph, key-value, query-based, document-based, and other types of databases. Without Copygen, changing any of these things along with the business-case risks outlining your entire repository in red.

Read More

link