Skip to content

Commit

Permalink
proto->sysl: filter out duplicate enum values (#925)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriehSchneier authored Jun 19, 2024
1 parent 423659f commit 2239b33
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 2 deletions.
Binary file modified internal/bundles/assets/import_proto_cli.arraiz
Binary file not shown.
17 changes: 17 additions & 0 deletions pkg/importer/importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,23 @@ func TestLoadProtobufFromTestFiles(t *testing.T) {
})
}

func TestLoadProtobufTestErrors(t *testing.T) {
t.Parallel()

runImportErrorTest(t, errTestConfig{
testConfig: testConfig{
name: "TestLoadProtobufTestErrors",
testDir: "proto/tests/errors",
testExtension: ".proto",
format: Protobuf.Name,
},

cases: map[string]string{
"dulicate_enum": "enum with multiple names for value 0 found",
},
})
}

func TestLoadProtobufDirFromTestDir(t *testing.T) {
runImportDirEqualityTests(t, testConfig{
name: "TestLoadProtobufDirFromTestDir",
Expand Down
13 changes: 12 additions & 1 deletion pkg/importer/proto/proto_to_sysl.arrai
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,21 @@ let type = \message \file \appName \imports \prefix
# `enum` the enum being converted.
# `file` the file the enum is located in.
let enum = \enum \file
let fields = [email protected]?:{} => (@: .@, @value: evalNumber([email protected]), deprecated: [email protected]?('deprecated')?.b?:false);
let nested = fields nest|@,deprecated|e;
(
name: syslSafeName(enum.@),
attrs: baseAttrs(enum, file),
enum: (items: [email protected]?:{} => (@: .@, @value: evalNumber([email protected])))
enum: (items: (fields where \f
(let n = ((nested where .@value = f.@value) single).e;
cond {
n count = 1: true, # only entry for value
(n where !.deprecated) count > 1: //error($`enum with multiple names for value ${f.@value} found: ${(n where !.deprecated) => .@}`),
!f.deprecated: true, # this is the only non-deprecated
(n where !.deprecated) count = 1: false, # this is deprecated and there is a non-deprecated value
(n => .@ orderby .)(0) = f.@: true, # use the first (alphabetically) deprecated value
_: false, # skip it
})) => (@: .@, @value: .@value))
);

# `endpoint` creates an endpoint out of a rpc.
Expand Down
27 changes: 26 additions & 1 deletion pkg/importer/proto/proto_to_sysl_test.arrai
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let (:import, ...) = //{./proto_parser};
let (:import, :parseFile, ...) = //{./proto_parser};
let (:module, ...) = //{./proto_to_sysl};

(
Expand All @@ -21,5 +21,30 @@ let (:module, ...) = //{./proto_to_sysl};
let actual = sysl.apps('SearchService').attrs('proto_options').a.elt(0).s;
# Strings are not escaped in memory, only on serialization.
//test.assert.equal(`php_namespace = Foo\Bar`, actual),
),

enum: (
dupicates_filter:
let model = {'f.proto': parseFile(`
syntax = "proto3";
package enums;
enum e {
E0 = 0;
E1 = 1;
E1_DEP = 1 [deprecated = true];
E2 = 2;
E2_DEP1 = 2 [deprecated = true];
E2_DEP2 = 2 [deprecated = true];
E3_DEP = 3 [deprecated = true];
E4_DEP1 = 4 [deprecated = true];
E4_DEP2 = 4 [deprecated = true];
E5_DEP2 = 5 [deprecated = true];
E5_DEP1 = 5 [deprecated = true];
}`
)};
let sysl = module((), model);
let expected = {'E0': 0, 'E1': 1, 'E2': 2, 'E3_DEP': 3, 'E4_DEP1': 4, 'E5_DEP1': 5};
let actual = sysl.apps('enums').types('e').enum.items;
//test.assert.equal(expected, actual),
)
)
15 changes: 15 additions & 0 deletions pkg/importer/proto/tests/enum.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";
package enums;
enum e {
E0 = 0;
E1 = 1;
E1_DEP = 1 [deprecated = true];
E2 = 2;
E2_DEP1 = 2 [deprecated = true];
E2_DEP2 = 2 [deprecated = true];
E3_DEP = 3 [deprecated = true];
E4_DEP1 = 4 [deprecated = true];
E4_DEP2 = 4 [deprecated = true];
E5_DEP2 = 5 [deprecated = true];
E5_DEP1 = 5 [deprecated = true];
}
12 changes: 12 additions & 0 deletions pkg/importer/proto/tests/enum.sysl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Code generated by Sysl. DO NOT EDIT.

enums:
@package = "enums"

!enum e [source_path="proto/tests/enum.proto"]:
E0 : 0
E1 : 1
E2 : 2
E3_DEP : 3
E4_DEP1 : 4
E5_DEP1 : 5
6 changes: 6 additions & 0 deletions pkg/importer/proto/tests/errors/dulicate_enum.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
syntax = "proto3";
package enums;
enum e {
E0 = 0;
E1 = 0;
}

0 comments on commit 2239b33

Please sign in to comment.