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

Commit fb6570f

Browse files
authored
Support enum values with prefix (#121)
* feat: support enum trim prefix from value * style: format code
1 parent 1e10cf8 commit fb6570f

File tree

9 files changed

+123
-43
lines changed

9 files changed

+123
-43
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ These apply to specifically marked enums, giving you more finely-grained control
9595

9696
- [enums_as_constants](internal/converter/testdata/proto/ImportedEnum.proto): Encode ENUMs (and their annotations) as CONST
9797
- [enums_as_strings_only](internal/converter/testdata/proto/OptionEnumsAsStringsOnly.proto): ENUM values are only strings (not the numeric counterparts)
98+
- [enums_trim_prefix](internal/converter/testdata/proto/OptionEnumsTrimPrefix.proto): ENUM values have enum name prefix removed
9899

99100
### Field Options
100101

internal/converter/converter.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import (
1010
"strings"
1111

1212
"github.com/alecthomas/jsonschema"
13-
"github.com/chrusty/protoc-gen-jsonschema/internal/protos"
13+
"github.com/iancoleman/strcase"
1414
"github.com/sirupsen/logrus"
1515
"github.com/xeipuuv/gojsonschema"
1616
gengo "google.golang.org/protobuf/cmd/protoc-gen-go/internal_gengo"
1717
"google.golang.org/protobuf/proto"
1818
descriptor "google.golang.org/protobuf/types/descriptorpb"
1919
plugin "google.golang.org/protobuf/types/pluginpb"
20+
21+
"github.com/chrusty/protoc-gen-jsonschema/internal/protos"
2022
)
2123

2224
const (
@@ -52,6 +54,7 @@ type ConverterFlags struct {
5254
EnforceOneOf bool
5355
EnumsAsConstants bool
5456
EnumsAsStringsOnly bool
57+
EnumsTrimPrefix bool
5558
KeepNewLinesInDescription bool
5659
PrefixSchemaFilesWithPackage bool
5760
UseJSONFieldnamesOnly bool
@@ -106,6 +109,8 @@ func (c *Converter) parseGeneratorParameters(parameters string) {
106109
c.Flags.EnforceOneOf = true
107110
case "enums_as_strings_only":
108111
c.Flags.EnumsAsStringsOnly = true
112+
case "enums_trim_prefix":
113+
c.Flags.EnumsTrimPrefix = true
109114
case "json_fieldnames":
110115
c.Flags.UseJSONFieldnamesOnly = true
111116
case "prefix_schema_files_with_package":
@@ -151,6 +156,11 @@ func (c *Converter) convertEnumType(enum *descriptor.EnumDescriptorProto, conver
151156
if enumOptions.GetEnumsAsStringsOnly() {
152157
converterFlags.EnumsAsStringsOnly = true
153158
}
159+
160+
// ENUM values trim enum name prefix:
161+
if enumOptions.GetEnumsTrimPrefix() {
162+
converterFlags.EnumsTrimPrefix = true
163+
}
154164
}
155165
}
156166
}
@@ -182,6 +192,9 @@ func (c *Converter) convertEnumType(enum *descriptor.EnumDescriptorProto, conver
182192
jsonSchemaType.OneOf = nil
183193
}
184194

195+
// If we need to trim prefix from enum value
196+
enumNamePrefix := fmt.Sprintf("%s_", strcase.ToScreamingSnake(*enum.Name))
197+
185198
// We have found an enum, append its values:
186199
for _, value := range enum.Value {
187200

@@ -191,17 +204,24 @@ func (c *Converter) convertEnumType(enum *descriptor.EnumDescriptorProto, conver
191204
_, valueDescription = c.formatTitleAndDescription(nil, src)
192205
}
193206

207+
valueName := value.GetName()
208+
209+
// If enum name prefix should be removed from enum value name:
210+
if converterFlags.EnumsTrimPrefix {
211+
valueName = strings.TrimPrefix(valueName, enumNamePrefix)
212+
}
213+
194214
// If we're using constants for ENUMs then add these here, along with their title:
195215
if converterFlags.EnumsAsConstants {
196216
c.schemaVersion = versionDraft06 // Const requires draft-06
197-
jsonSchemaType.OneOf = append(jsonSchemaType.OneOf, &jsonschema.Type{Extras: map[string]interface{}{"const": value.GetName()}, Description: valueDescription})
217+
jsonSchemaType.OneOf = append(jsonSchemaType.OneOf, &jsonschema.Type{Extras: map[string]interface{}{"const": valueName}, Description: valueDescription})
198218
if !converterFlags.EnumsAsStringsOnly {
199219
jsonSchemaType.OneOf = append(jsonSchemaType.OneOf, &jsonschema.Type{Extras: map[string]interface{}{"const": value.GetNumber()}, Description: valueDescription})
200220
}
201221
}
202222

203223
// Add the values to the ENUM:
204-
jsonSchemaType.Enum = append(jsonSchemaType.Enum, value.Name)
224+
jsonSchemaType.Enum = append(jsonSchemaType.Enum, valueName)
205225
if !converterFlags.EnumsAsStringsOnly {
206226
jsonSchemaType.Enum = append(jsonSchemaType.Enum, value.Number)
207227
}

internal/converter/converter_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,16 @@ func configureSampleProtos() map[string]sampleProto {
307307
ObjectsToValidateFail: []string{testdata.OptionEnumsAsStringsOnlyFail},
308308
ObjectsToValidatePass: []string{testdata.OptionEnumsAsStringsOnlyPass},
309309
},
310+
"OptionEnumsTrimPrefix": {
311+
Flags: ConverterFlags{
312+
EnumsTrimPrefix: true,
313+
},
314+
ExpectedJSONSchema: []string{testdata.OptionEnumsTrimPrefix},
315+
FilesToGenerate: []string{"OptionEnumsTrimPrefix.proto"},
316+
ProtoFileName: "OptionEnumsTrimPrefix.proto",
317+
ObjectsToValidateFail: []string{testdata.OptionEnumsTrimPrefixFail},
318+
ObjectsToValidatePass: []string{testdata.OptionEnumsTrimPrefixPass},
319+
},
310320
"OptionFileExtension": {
311321
ExpectedJSONSchema: []string{testdata.OptionFileExtension},
312322
ExpectedFileNames: []string{"OptionFileExtension.jsonschema"},
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package testdata
2+
3+
const OptionEnumsTrimPrefix = `{
4+
"$schema": "http://json-schema.org/draft-04/schema#",
5+
"enum": [
6+
"UNSPECIFIED",
7+
"HTTP",
8+
"HTTPS"
9+
],
10+
"type": "string",
11+
"title": "Scheme"
12+
}`
13+
14+
const OptionEnumsTrimPrefixPass = `"HTTP"`
15+
16+
const OptionEnumsTrimPrefixFail = `4`
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
syntax = "proto3";
2+
package samples;
3+
import "options.proto";
4+
5+
enum Scheme {
6+
option (protoc.gen.jsonschema.enum_options).enums_trim_prefix = true;
7+
8+
// for test
9+
option (protoc.gen.jsonschema.enum_options).enums_as_strings_only = true;
10+
11+
SCHEME_UNSPECIFIED = 0;
12+
SCHEME_HTTP = 1;
13+
SCHEME_HTTPS = 2;
14+
}

internal/converter/types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import (
66
"strings"
77

88
"github.com/alecthomas/jsonschema"
9-
"github.com/chrusty/protoc-gen-jsonschema/internal/protos"
109
"github.com/iancoleman/orderedmap"
1110
"github.com/xeipuuv/gojsonschema"
1211
"google.golang.org/protobuf/proto"
1312
descriptor "google.golang.org/protobuf/types/descriptorpb"
13+
14+
"github.com/chrusty/protoc-gen-jsonschema/internal/protos"
1415
)
1516

1617
var (

internal/protos/options.pb.go

Lines changed: 50 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jsonschemas/EnumOptions.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
"enums_as_strings_only": {
1212
"type": "boolean",
1313
"description": "Enums tagged with this will only provide string values as options (not their numerical equivalents):"
14+
},
15+
"enums_trim_prefix": {
16+
"type": "boolean",
17+
"description": "Enums tagged with this will have enum name prefix removed from values:"
1418
}
1519
},
1620
"additionalProperties": true,

options.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ message EnumOptions {
6767

6868
// Enums tagged with this will only provide string values as options (not their numerical equivalents):
6969
bool enums_as_strings_only = 2;
70+
71+
// Enums tagged with this will have enum name prefix removed from values:
72+
bool enums_trim_prefix = 3;
7073
}
7174

7275

0 commit comments

Comments
 (0)