Git Product home page Git Product logo

protoc-gen-stringer's Introduction

protoc-gen-stringer

About

A proto plugin to generate string values for go as defined by proto options.

This proto plugin was created because protobufs use c++ style naming scopes: github.com/protocolbuffers/protobuf/issues/5425. This means that it's forbidden to have the following in ptotobuf:

enum Foo {
    Foo1 = 1;
    Foo2 = 2;
    Unknown = 3;
}

enum Bar {
    Bar1 = 1;
    Bar2 = 2;
    Unknown = 3; // 'Unknown' is already defined in scope 'example'
}

The solution to this is to prefix the enum with a stringval, but this means that a custom stringer logic needs to be implemented, which is annoying and unmaintainable.

enum Foo {
    Foo1 = 1;
    Foo2 = 2;
    Foo_Unknown = 3;
}

enum Bar {
    Bar1 = 1;
    Bar2 = 2;
    Bar_Unknown = 3; // No error but now custom stringer logic needs to be implemented
}

Getting Started

Installing

go install github.com/joshcarp/protoc-gen-stringer

Usage

enum Foo {
    Foo1 = 0 [(string_val) = "Foo1"];
    Foo2 = 1 [(string_val) = "Foo2"];
    Foo_Unknown = 2 [(string_val) = "Unknown"];
}

enum Bar {
    Bar1 = 0 [(string_val) = "Bar1"];
    Bar2 = 1 [(string_val) = "Bar2"];
    Bar_Unknown = 2 [(string_val) = "Unknown"]; // No error but now custom stringer logic needs to be implemented
}
protoc -I example/ example/example.proto --go_out=paths=source_relative:example --stringer_out=source_relative:example

This will generate a StringVal() method for each enum, and a func StringToEnum(s string) Enum function:

fmt.Println(example.Foo_Unknown.StringVal())
// Unknown
fmt.Println(example.Bar_Unknown.StringVal())
// Unknown
a := example.StringToFoo("Unknown") // gives example.Foo_Foo_Unknown

protoc-gen-stringer's People

Contributors

joshcarp avatar

Watchers

James Cloos avatar  avatar

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.