question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A more user-friendly API

See original GitHub issue

Is anyone here opposed to maybe adding a more user-friendly API to this library.

Right now it takes a fair amount of boilerplate to do what is essentially JSON.parse(encoded) or JSON.stringify(obj).

Plus, there doesn’t seem to be a way to get the same objects out on the decode end as were put into the encoding step. This means even more boilerplate code to translate certain fields from numbers into enum strings. Considering this is probably the most common use case it seems worth it to make this a simple one-step call like buffer = encode(schema, obj) and obj = decode(schema, buffer). And preferably, the input to encode and the output of decode should have deep equality.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
kjvalencikcommented, Jan 10, 2018

@download13 Thanks for sharing your thoughts! This library has a focus on performance and makes sacrifices in other areas such as usability. However, I agree that there is definitely room for improvement in ergonomics! I’ll try to add my thoughts one at a time.

it seems worth it to make this a simple one-step call like buffer = encode(schema, obj)

One of the things this library does to make things fast is generate code for serialization instead of using a generic provider. This allows the JS engine to perform optimizations such as inlining and limits the amount of branching.

Unfortunately, compiling is quite expensive and shouldn’t be performed on every call. This makes your version of encode and decode difficult to achieve.

However, there is probably some improvement that could be made to simplify write calls. Currently, pbf objects are passed in to allow object pooling of the buffers. If we added a default value and returned pbf from write calls (allowing chaining) encoding could be much more simple.

Type.write = function (obj, pbf = new Pbf()) {
	/* normal generated code */

	return pbf;
};

const encoded = Type.encode(obj).finish();

This means even more boilerplate code to translate certain fields from numbers into enum strings

This is definitely one of the sources of the most boilerplate and it would be great to translate these on the way in / out. We would probably want this optional to prevent the branching and string allocations for use cases that do not need it.

And preferably, the input to encode and the output of decode should have deep equality.

The primary reason for this is defaults as defined by the protobuf standard. This is also mentioned in https://github.com/mapbox/pbf/issues/80. If compiling had a flag to opt out of default values, this could be achieved.

Lastly, if you are looking to simplify the compilation process, take a look at pbf-loader for webpack.

0reactions
kjvalencikcommented, Jan 10, 2018

Yep, so you can write something like:

switch (msg.type) {
    case Tile.GeomType.POINT:
    case Tile.GeomType.LINESTRING:
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Developer-Friendly APIs - Mutually Human
Choosing to invest time into the design and development of your API can make it more appealing and “friendly” for developers to use,...
Read more >
Best Practices for Building a User-Friendly API - Mike Gingerich
A user-friendly API is considered to be robust. Such an API will enable its users to strengthen and integrate software without facing any...
Read more >
API design guidelines|Best practices for User-Friendly API
Do you want to design a robust API? Then follow these core API design guidelines, applicable for all types, and build User-friendly, ...
Read more >
User friendly API - Gleb Bahmutov
How to design a simple to use and powerful library API. ... then you need to carefully think about the most user-friendly argument...
Read more >
Building a Stable and User Friendly API for your Model
Constructing a stable and user friendly API is key for getting users / product teams to start consuming your services and to keep...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found