Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Added a proto enum option to only encode values as strings (not numbe…
Browse files Browse the repository at this point in the history
…rs) (#106)

* Added a proto enum option to only encode values as strings (not numbers)

* Bringing the converterFlag logic into the convertEnumType method

* Using the proto option for the testdata proto

Co-authored-by: Chrusty <>
  • Loading branch information
chrusty authored Dec 20, 2021
1 parent 3cbbf5c commit f5fcc60
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 31 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ If you don't want to use the configuration parameters (admittedly quite a nasty
These apply to specifically marked enums, giving you more finely-grained control than with the CLI flags.

- [enums_as_constants](internal/converter/testdata/proto/ImportedEnum.proto): Encode ENUMs (and their annotations) as CONST
- [enums_as_strings_only](internal/converter/testdata/proto/OptionEnumsAsStringsOnly.proto): ENUM values are only strings (not the numeric counterparts)

### Field Options

Expand Down
10 changes: 9 additions & 1 deletion internal/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ func (c *Converter) parseGeneratorParameters(parameters string) {
// Converts a proto "ENUM" into a JSON-Schema:
func (c *Converter) convertEnumType(enum *descriptor.EnumDescriptorProto, converterFlags ConverterFlags) (jsonschema.Type, error) {

// Inherit the CLI converterFlags:
converterFlags.EnumsAsStringsOnly = c.Flags.EnumsAsStringsOnly

// Set some per-enum flags from config and options:
if opts := enum.GetOptions(); opts != nil && proto.HasExtension(opts, protos.E_EnumOptions) {
if opt := proto.GetExtension(opts, protos.E_EnumOptions); opt != nil {
Expand All @@ -138,6 +141,11 @@ func (c *Converter) convertEnumType(enum *descriptor.EnumDescriptorProto, conver
if enumOptions.GetEnumsAsConstants() {
converterFlags.EnumsAsConstants = true
}

// ENUM values as strings only:
if enumOptions.GetEnumsAsStringsOnly() {
converterFlags.EnumsAsStringsOnly = true
}
}
}
}
Expand Down Expand Up @@ -225,7 +233,7 @@ func (c *Converter) convertFile(file *descriptor.FileDescriptorProto, fileExtens
c.logger.WithField("proto_filename", protoFileName).WithField("enum_name", enum.GetName()).WithField("jsonschema_filename", jsonSchemaFileName).Info("Generating JSON-schema for stand-alone ENUM")

// Convert the ENUM:
enumJSONSchema, err := c.convertEnumType(enum, ConverterFlags{EnumsAsStringsOnly: c.Flags.EnumsAsStringsOnly})
enumJSONSchema, err := c.convertEnumType(enum, ConverterFlags{})
if err != nil {
c.logger.WithError(err).WithField("proto_filename", protoFileName).Error("Failed to convert")
return nil, err
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
syntax = "proto3";
package samples;
import "options.proto";

enum Currency {
option (protoc.gen.jsonschema.enum_options).enums_as_strings_only = true;
NOT_SPECIFIED = 0;
USD = 1;
GBP = 2;
Expand Down
72 changes: 42 additions & 30 deletions internal/protos/options.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions jsonschemas/EnumOptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"enums_as_constants": {
"type": "boolean",
"description": "Enums tagged with this will have be encoded to use constants instead of simple types (supports value annotations):"
},
"enums_as_strings_only": {
"type": "boolean",
"description": "Enums tagged with this will only provide string values as options (not their numerical equivalents):"
}
},
"additionalProperties": true,
Expand Down
3 changes: 3 additions & 0 deletions options.proto
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ message EnumOptions {

// Enums tagged with this will have be encoded to use constants instead of simple types (supports value annotations):
bool enums_as_constants = 1;

// Enums tagged with this will only provide string values as options (not their numerical equivalents):
bool enums_as_strings_only = 2;
}


Expand Down

0 comments on commit f5fcc60

Please sign in to comment.