GithubHelp home page GithubHelp logo

protoc-gen-fieldmask's Introduction

protoc-gen-fieldmask

CI Go Report Card GitHub release (latest SemVer) GitHub go.mod Go version GitHub

A protoc plugin that generates fieldmask paths as static type properties for proto messages, which elimantes the usage of error-prone strings.

For example, given the following proto messages:

syntax = "proto3";

package example;

option go_package = "example/;example";

import "google/type/date.proto";

message Foo {
  string baz = 1;
  int32 xyz = 2;
  Bar my_bar = 3;
  google.type.Date some_date = 4;
}

message Bar {
  string some_field = 1;
  bool another_field = 2;
}

fieldmasks paths can be used as follows:

  foo := &example.Foo{}

  // Prints "baz"
  fmt.Println(foo.FieldMaskPaths().Baz())
  
  // Prints "xyz"
  fmt.Println(foo.FieldMaskPaths().Xyz())

  // Prints "my_bar"
  fmt.Println(foo.FieldMaskPaths().MyBar().String())

  // Since baz is a nested message, we can print a nested path - "my_bar.some_field"
  fmt.Println(foo.FieldMaskPaths().MyBar().SomeField())

  // Thirdparty messages work the same way:
  // Prints "some_date"
  fmt.Println(foo.FieldMaskPaths().SomeDate().String())

  // Prints "some_date.year"
  fmt.Println(foo.FieldMaskPaths().SomeDate().Year())

Usage

Installation

The plugin can be downloaded from the release page, and should be ideally installed somewhere available in your $PATH.

Executing the plugin

protoc --fieldmask_out=gen protos/example.proto

# If the plugin is not in your $PATH:
protoc --fieldmask_out=out_dir protos/example.proto --plugin=protoc-gen-fieldmask=/path/to/protoc-gen-fieldmask

Parameters

The following parameters can be set by passing --fieldmask_opt to the command:

  • maxdepth: This option is relevant for a recursive message case.
    Specify the max depth for which the paths will be pregenerated. If the path depth gets over the max value, it will be generated at runtime. default value is 7.

Features

  • Currently the only supported language is go.
  • All paths are pregenerated (except for recursive messages past maxdepth).
  • Support all type of fields including repeated fields, maps, oneofs, third parties, nested messages and recursive messages.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.