MimeCraft, JavaWriter, and ProtoParser

Small and focused libraries doing one task well.

Square Engineering
May 8, 2013 · 3 min read

Heads up, we’ve moved! If you’d like to continue keeping up with the latest technical content from Square please visit us at our new home https://developer.squareup.com/blog

In any complex project you’ll need a variety of tools to solve a variety of problems. The general rule that we use when developing libraries is “perform one task and perform it well”. Here are three purpose-built libraries which do just that:

MimeCraft

Making requests with OkHttp or HttpUrlConnection requires that you encode the body of requests yourself. If you are using JSON or protocol buffers then there are multiple libraries which will take care of this for you. Other common request formats like form-encoding and multi-part are often left for the developer or through heavyweight HTTP abstractions.

MimeCraft aims to eliminate that by providing a terse, fluent API to construct both of these formats.

FormEncoding fe = new FormEncoding.Builder()
.add("name", "Jake Wharton")
.add("occupation", "Byte code chef")
.build();
Multipart m = new Multipart.Builder()
.addPart(new Part.Builder()
.contentType("image/gif")
.body(new File("/Users/jake/images/bees.gif"))
.build())
.addPart(new Part.Builder()
.contentType("text/plain")
.body("The quick brown fox jumps over the lazy dog.")
.build())
.build();

The resulting FormEncoding and Multipart objects can write their content directly to an OutputStream in addition to providing header information such as MIME type and content length.

Check out the project page on GitHub for more information.

ProtoParser

Protocol buffers are an efficient wire format for sending data and RPCs by using generated code based on a schema. While the generated code has reflection APIs that describe the underlying format, it requires generating the most verbose version of the code. ProtoParser quickly parses proto files into an object representation without the need of the protoc tool.

Having the proto represented in an object format is useful for a number of reasons:

  • API documentation can be generated in alternate formats that are easier to understand. For example, visualizing dependency graphs and RPC call breakdowns in HTML and GraphViz.
  • Validation tools that perform deep analysis which are easier to write. A Maven plugin or Ant task can fail the build if a complex constraint is not met.
  • Code that indirectly supports the proto format can be generated. Mechanical code that deals with the generated protocol buffer can be generated rather than relying on generics and abstract implementations.
  • The proto spec can be used to dictate other wire formats. At Square we’re using protocol buffers as a schema language for JSON and generating POJOs.

Check out the project page on GitHub for more information.

JavaWriter

Code that generates other code is an immensely powerful concept that saves developer time. JavaWriter is a simple class which assists in producing Java code. This library was developed in parallel with Dagger and is responsible for all of its generated code.

Creating Java constructs becomes a much simpler and more declarative task:

JavaWriter writer = new JavaWriter(fileWriter);
writer.beginType("Hello", "class", PUBLIC)
.beginMethod("void", "main", PUBLIC | STATIC, "String...", "args")
.emitStatement("System.out.println(\"Hello, World!\");")
.endMethod()
.endType();

With this we will have generated your standard “Hello, World” example.

public class Hello {
public static void main(String... args) {
System.out.println("Hello, World!");
}
}

JavaWriter intentionally lacks a lot of logic, delegating to the calling code for structure. This also makes it very powerful since no assumptions are made about how the resulting code will be organized.

Check out the project page on GitHub for more information.

Tiny Tools

Tools like the ones listed above are sprinkled throughout Square’s infrastructure in every language that we use. These projects are intentionally tiny in scope and purpose-built for a specific problem.


Square Corner Blog

Buying and selling sound like simple things - and they…

Square Engineering

Written by

The official account for @Square Engineering.

Square Corner Blog

Buying and selling sound like simple things - and they should be. Somewhere along the way, they got complicated. At Square, we're working hard to make commerce easy for everyone.

Square Engineering

Written by

The official account for @Square Engineering.

Square Corner Blog

Buying and selling sound like simple things - and they should be. Somewhere along the way, they got complicated. At Square, we're working hard to make commerce easy for everyone.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store