dart_frog icon indicating copy to clipboard operation
dart_frog copied to clipboard

feat: Protocol Buffers Support (protobuf)

Open nosmirck opened this issue 3 years ago • 2 comments

Description

Protocol Buffers or "protobuf" is a tool (developed by Google) to provide mechanisms to serialize structured data in a very efficient way making the data transferred very small and the serialization blazing fast!.

Proposal

It would be great if dart_frog supported protobuf or at least leave a clear way to be handled by a plugin or brick.

Requirements

  • [ ] Response Models for the endpoints to be automatically generated based on .proto files.
  • [ ] Generated Models can be bundled in a shared package between the server and the client applications.

Additional Context

Dart is supported!

nosmirck avatar Jun 19 '22 22:06 nosmirck

Hey @nosmirck :wave:! I was interested in using protobufs as well so I played around with it and it looks totally supported by the current package with existing tools.

Basically, you can using the protoc CLI tool to generate the Dart objects for your protobuf messages, and it generates all your Dart classes.

You can then use those objects with dart_frog like this

import 'package:dart_frog/dart_frog.dart';
import 'package:protobuf_play/model.dart';

Response onRequest(RequestContext context) {
  final person = Person(
    id: 3,
    email: '[email protected]',
    name: 'Tester McTesterson',
    phones: [
      Person_PhoneNumber(
        number: '+14068675309',
        type: Person_PhoneType.HOME,
      )
    ],
  );

  return Response.bytes(
    body: person.writeToBuffer(),
  );
}

All your generated classes also have a factory constructor called fromBuffer which makes them super easy to convert from the Uint8List of bytes back into a useful object on the frontend side of things.

I created a little example repo showing how to use this: https://github.com/mtwichel/protobuf_play. @felangel , let me know if you want me to make a PR adding it as an example in this repo - I'd be happy to clean it up and share 💙

mtwichel avatar Jun 22 '22 18:06 mtwichel

@felangel Now that we have expanded examples, would it be okay if I merged in my protobuf example and made it a tutorial? I would also make sure it has test coverage and all that jazz.

mtwichel avatar Aug 10 '22 23:08 mtwichel

As discussed, closing this issue since the answer provided by @mtwichel is quite thorough and illustrates the steps needed to use protobuf in the context of a Dart Frog application.

felangel avatar Aug 15 '22 04:08 felangel