Skip to content

Commit 7c75767

Browse files
authored
Improve enum alias handling. (apple#357)
* Generate static-lets for enum aliases. Decode all names. * Add new test for encoding/decoding enum aliases. * Update other test protos and reference protos.
1 parent fdd0531 commit 7c75767

27 files changed

+513
-148
lines changed

Protos/unittest_swift_enum.proto

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,14 @@ message SwiftEnumTest {
5050
ENUM_TEST_RESERVED_WORD_NOT_RESERVED = 2;
5151
}
5252
}
53+
54+
message SwiftEnumWithAliasTest {
55+
enum EnumWithAlias {
56+
option allow_alias = true;
57+
FOO1 = 1;
58+
FOO2 = 1;
59+
BAR1 = 2;
60+
BAR2 = 2;
61+
}
62+
repeated EnumWithAlias values = 1 [packed=true];
63+
}

Reference/google/protobuf/test_messages_proto3.pb.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,10 +1380,10 @@ struct ProtobufTestMessages_Proto3_TestAllTypes: SwiftProtobuf.Message, SwiftPro
13801380
case UNRECOGNIZED(Int)
13811381

13821382
static let _protobuf_nameMap: SwiftProtobuf._NameMap = [
1383+
-1: .same(proto: "NEG"),
13831384
0: .same(proto: "FOO"),
13841385
1: .same(proto: "BAR"),
13851386
2: .same(proto: "BAZ"),
1386-
-1: .same(proto: "NEG"),
13871387
]
13881388

13891389
init() {
@@ -1392,20 +1392,20 @@ struct ProtobufTestMessages_Proto3_TestAllTypes: SwiftProtobuf.Message, SwiftPro
13921392

13931393
init?(rawValue: Int) {
13941394
switch rawValue {
1395+
case -1: self = .neg
13951396
case 0: self = .foo
13961397
case 1: self = .bar
13971398
case 2: self = .baz
1398-
case -1: self = .neg
13991399
default: self = .UNRECOGNIZED(rawValue)
14001400
}
14011401
}
14021402

14031403
var rawValue: Int {
14041404
switch self {
1405+
case .neg: return -1
14051406
case .foo: return 0
14061407
case .bar: return 1
14071408
case .baz: return 2
1408-
case .neg: return -1
14091409
case .UNRECOGNIZED(let i): return i
14101410
}
14111411
}

Reference/google/protobuf/unittest.pb.swift

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,13 @@ enum ProtobufUnittest_TestEnumWithDupValue: SwiftProtobuf.Enum, SwiftProtobuf._P
9696
case foo1 // = 1
9797
case bar1 // = 2
9898
case baz // = 3
99-
case foo2 // = 1
100-
case bar2 // = 2
99+
static let foo2 = foo1
100+
static let bar2 = bar1
101101

102102
static let _protobuf_nameMap: SwiftProtobuf._NameMap = [
103-
1: .same(proto: "FOO1"),
104-
2: .same(proto: "BAR1"),
103+
1: .aliased(proto: "FOO1", aliases: ["FOO2"]),
104+
2: .aliased(proto: "BAR1", aliases: ["BAR2"]),
105105
3: .same(proto: "BAZ"),
106-
1: .same(proto: "FOO2"),
107-
2: .same(proto: "BAR2"),
108106
]
109107

110108
init() {
@@ -125,8 +123,6 @@ enum ProtobufUnittest_TestEnumWithDupValue: SwiftProtobuf.Enum, SwiftProtobuf._P
125123
case .foo1: return 1
126124
case .bar1: return 2
127125
case .baz: return 3
128-
case .foo2: return 1
129-
case .bar2: return 2
130126
}
131127
}
132128

@@ -144,13 +140,13 @@ enum ProtobufUnittest_TestSparseEnum: SwiftProtobuf.Enum, SwiftProtobuf._ProtoNa
144140
case sparseG // = 2
145141

146142
static let _protobuf_nameMap: SwiftProtobuf._NameMap = [
147-
123: .same(proto: "SPARSE_A"),
148-
62374: .same(proto: "SPARSE_B"),
149-
12589234: .same(proto: "SPARSE_C"),
150-
-15: .same(proto: "SPARSE_D"),
151143
-53452: .same(proto: "SPARSE_E"),
144+
-15: .same(proto: "SPARSE_D"),
152145
0: .same(proto: "SPARSE_F"),
153146
2: .same(proto: "SPARSE_G"),
147+
123: .same(proto: "SPARSE_A"),
148+
62374: .same(proto: "SPARSE_B"),
149+
12589234: .same(proto: "SPARSE_C"),
154150
]
155151

156152
init() {
@@ -159,26 +155,26 @@ enum ProtobufUnittest_TestSparseEnum: SwiftProtobuf.Enum, SwiftProtobuf._ProtoNa
159155

160156
init?(rawValue: Int) {
161157
switch rawValue {
162-
case 123: self = .sparseA
163-
case 62374: self = .sparseB
164-
case 12589234: self = .sparseC
165-
case -15: self = .sparseD
166158
case -53452: self = .sparseE
159+
case -15: self = .sparseD
167160
case 0: self = .sparseF
168161
case 2: self = .sparseG
162+
case 123: self = .sparseA
163+
case 62374: self = .sparseB
164+
case 12589234: self = .sparseC
169165
default: return nil
170166
}
171167
}
172168

173169
var rawValue: Int {
174170
switch self {
175-
case .sparseA: return 123
176-
case .sparseB: return 62374
177-
case .sparseC: return 12589234
178-
case .sparseD: return -15
179171
case .sparseE: return -53452
172+
case .sparseD: return -15
180173
case .sparseF: return 0
181174
case .sparseG: return 2
175+
case .sparseA: return 123
176+
case .sparseB: return 62374
177+
case .sparseC: return 12589234
182178
}
183179
}
184180

@@ -1206,10 +1202,10 @@ struct ProtobufUnittest_TestAllTypes: SwiftProtobuf.Message, SwiftProtobuf._Mess
12061202
case neg // = -1
12071203

12081204
static let _protobuf_nameMap: SwiftProtobuf._NameMap = [
1205+
-1: .same(proto: "NEG"),
12091206
1: .same(proto: "FOO"),
12101207
2: .same(proto: "BAR"),
12111208
3: .same(proto: "BAZ"),
1212-
-1: .same(proto: "NEG"),
12131209
]
12141210

12151211
init() {
@@ -1218,20 +1214,20 @@ struct ProtobufUnittest_TestAllTypes: SwiftProtobuf.Message, SwiftProtobuf._Mess
12181214

12191215
init?(rawValue: Int) {
12201216
switch rawValue {
1217+
case -1: self = .neg
12211218
case 1: self = .foo
12221219
case 2: self = .bar
12231220
case 3: self = .baz
1224-
case -1: self = .neg
12251221
default: return nil
12261222
}
12271223
}
12281224

12291225
var rawValue: Int {
12301226
switch self {
1227+
case .neg: return -1
12311228
case .foo: return 1
12321229
case .bar: return 2
12331230
case .baz: return 3
1234-
case .neg: return -1
12351231
}
12361232
}
12371233

Reference/google/protobuf/unittest_custom_options.pb.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ struct ProtobufUnittest_DummyMessageContainingEnum: SwiftProtobuf.Message, Swift
381381
case testOptionEnumType2 // = -23
382382

383383
static let _protobuf_nameMap: SwiftProtobuf._NameMap = [
384-
22: .same(proto: "TEST_OPTION_ENUM_TYPE1"),
385384
-23: .same(proto: "TEST_OPTION_ENUM_TYPE2"),
385+
22: .same(proto: "TEST_OPTION_ENUM_TYPE1"),
386386
]
387387

388388
init() {
@@ -391,16 +391,16 @@ struct ProtobufUnittest_DummyMessageContainingEnum: SwiftProtobuf.Message, Swift
391391

392392
init?(rawValue: Int) {
393393
switch rawValue {
394-
case 22: self = .testOptionEnumType1
395394
case -23: self = .testOptionEnumType2
395+
case 22: self = .testOptionEnumType1
396396
default: return nil
397397
}
398398
}
399399

400400
var rawValue: Int {
401401
switch self {
402-
case .testOptionEnumType1: return 22
403402
case .testOptionEnumType2: return -23
403+
case .testOptionEnumType1: return 22
404404
}
405405
}
406406

Reference/google/protobuf/unittest_no_arena.pb.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,10 +1140,10 @@ struct ProtobufUnittestNoArena_TestAllTypes: SwiftProtobuf.Message, SwiftProtobu
11401140
case neg // = -1
11411141

11421142
static let _protobuf_nameMap: SwiftProtobuf._NameMap = [
1143+
-1: .same(proto: "NEG"),
11431144
1: .same(proto: "FOO"),
11441145
2: .same(proto: "BAR"),
11451146
3: .same(proto: "BAZ"),
1146-
-1: .same(proto: "NEG"),
11471147
]
11481148

11491149
init() {
@@ -1152,20 +1152,20 @@ struct ProtobufUnittestNoArena_TestAllTypes: SwiftProtobuf.Message, SwiftProtobu
11521152

11531153
init?(rawValue: Int) {
11541154
switch rawValue {
1155+
case -1: self = .neg
11551156
case 1: self = .foo
11561157
case 2: self = .bar
11571158
case 3: self = .baz
1158-
case -1: self = .neg
11591159
default: return nil
11601160
}
11611161
}
11621162

11631163
var rawValue: Int {
11641164
switch self {
1165+
case .neg: return -1
11651166
case .foo: return 1
11661167
case .bar: return 2
11671168
case .baz: return 3
1168-
case .neg: return -1
11691169
}
11701170
}
11711171

Reference/google/protobuf/unittest_proto3.pb.swift

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,15 @@ enum Proto3TestEnumWithDupValue: SwiftProtobuf.Enum, SwiftProtobuf._ProtoNamePro
103103
case foo1 // = 1
104104
case bar1 // = 2
105105
case baz // = 3
106-
case foo2 // = 1
107-
case bar2 // = 2
106+
static let foo2 = foo1
107+
static let bar2 = bar1
108108
case UNRECOGNIZED(Int)
109109

110110
static let _protobuf_nameMap: SwiftProtobuf._NameMap = [
111111
0: .same(proto: "TEST_ENUM_WITH_DUP_VALUE_UNSPECIFIED"),
112-
1: .same(proto: "FOO1"),
113-
2: .same(proto: "BAR1"),
112+
1: .aliased(proto: "FOO1", aliases: ["FOO2"]),
113+
2: .aliased(proto: "BAR1", aliases: ["BAR2"]),
114114
3: .same(proto: "BAZ"),
115-
1: .same(proto: "FOO2"),
116-
2: .same(proto: "BAR2"),
117115
]
118116

119117
init() {
@@ -136,8 +134,6 @@ enum Proto3TestEnumWithDupValue: SwiftProtobuf.Enum, SwiftProtobuf._ProtoNamePro
136134
case .foo1: return 1
137135
case .bar1: return 2
138136
case .baz: return 3
139-
case .foo2: return 1
140-
case .bar2: return 2
141137
case .UNRECOGNIZED(let i): return i
142138
}
143139
}
@@ -160,13 +156,13 @@ enum Proto3TestSparseEnum: SwiftProtobuf.Enum, SwiftProtobuf._ProtoNameProviding
160156
case UNRECOGNIZED(Int)
161157

162158
static let _protobuf_nameMap: SwiftProtobuf._NameMap = [
159+
-53452: .same(proto: "SPARSE_E"),
160+
-15: .same(proto: "SPARSE_D"),
163161
0: .same(proto: "TEST_SPARSE_ENUM_UNSPECIFIED"),
162+
2: .same(proto: "SPARSE_G"),
164163
123: .same(proto: "SPARSE_A"),
165164
62374: .same(proto: "SPARSE_B"),
166165
12589234: .same(proto: "SPARSE_C"),
167-
-15: .same(proto: "SPARSE_D"),
168-
-53452: .same(proto: "SPARSE_E"),
169-
2: .same(proto: "SPARSE_G"),
170166
]
171167

172168
init() {
@@ -175,26 +171,26 @@ enum Proto3TestSparseEnum: SwiftProtobuf.Enum, SwiftProtobuf._ProtoNameProviding
175171

176172
init?(rawValue: Int) {
177173
switch rawValue {
174+
case -53452: self = .sparseE
175+
case -15: self = .sparseD
178176
case 0: self = .testSparseEnumUnspecified
177+
case 2: self = .sparseG
179178
case 123: self = .sparseA
180179
case 62374: self = .sparseB
181180
case 12589234: self = .sparseC
182-
case -15: self = .sparseD
183-
case -53452: self = .sparseE
184-
case 2: self = .sparseG
185181
default: self = .UNRECOGNIZED(rawValue)
186182
}
187183
}
188184

189185
var rawValue: Int {
190186
switch self {
187+
case .sparseE: return -53452
188+
case .sparseD: return -15
191189
case .testSparseEnumUnspecified: return 0
190+
case .sparseG: return 2
192191
case .sparseA: return 123
193192
case .sparseB: return 62374
194193
case .sparseC: return 12589234
195-
case .sparseD: return -15
196-
case .sparseE: return -53452
197-
case .sparseG: return 2
198194
case .UNRECOGNIZED(let i): return i
199195
}
200196
}
@@ -751,11 +747,11 @@ struct Proto3TestAllTypes: SwiftProtobuf.Message, SwiftProtobuf._MessageImplemen
751747
case UNRECOGNIZED(Int)
752748

753749
static let _protobuf_nameMap: SwiftProtobuf._NameMap = [
750+
-1: .same(proto: "NEG"),
754751
0: .same(proto: "NESTED_ENUM_UNSPECIFIED"),
755752
1: .same(proto: "FOO"),
756753
2: .same(proto: "BAR"),
757754
3: .same(proto: "BAZ"),
758-
-1: .same(proto: "NEG"),
759755
]
760756

761757
init() {
@@ -764,22 +760,22 @@ struct Proto3TestAllTypes: SwiftProtobuf.Message, SwiftProtobuf._MessageImplemen
764760

765761
init?(rawValue: Int) {
766762
switch rawValue {
763+
case -1: self = .neg
767764
case 0: self = .nestedEnumUnspecified
768765
case 1: self = .foo
769766
case 2: self = .bar
770767
case 3: self = .baz
771-
case -1: self = .neg
772768
default: self = .UNRECOGNIZED(rawValue)
773769
}
774770
}
775771

776772
var rawValue: Int {
777773
switch self {
774+
case .neg: return -1
778775
case .nestedEnumUnspecified: return 0
779776
case .foo: return 1
780777
case .bar: return 2
781778
case .baz: return 3
782-
case .neg: return -1
783779
case .UNRECOGNIZED(let i): return i
784780
}
785781
}

Reference/google/protobuf/unittest_proto3_arena.pb.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -689,11 +689,11 @@ struct Proto3ArenaUnittest_TestAllTypes: SwiftProtobuf.Message, SwiftProtobuf._M
689689
case UNRECOGNIZED(Int)
690690

691691
static let _protobuf_nameMap: SwiftProtobuf._NameMap = [
692+
-1: .same(proto: "NEG"),
692693
0: .same(proto: "ZERO"),
693694
1: .same(proto: "FOO"),
694695
2: .same(proto: "BAR"),
695696
3: .same(proto: "BAZ"),
696-
-1: .same(proto: "NEG"),
697697
]
698698

699699
init() {
@@ -702,22 +702,22 @@ struct Proto3ArenaUnittest_TestAllTypes: SwiftProtobuf.Message, SwiftProtobuf._M
702702

703703
init?(rawValue: Int) {
704704
switch rawValue {
705+
case -1: self = .neg
705706
case 0: self = .zero
706707
case 1: self = .foo
707708
case 2: self = .bar
708709
case 3: self = .baz
709-
case -1: self = .neg
710710
default: self = .UNRECOGNIZED(rawValue)
711711
}
712712
}
713713

714714
var rawValue: Int {
715715
switch self {
716+
case .neg: return -1
716717
case .zero: return 0
717718
case .foo: return 1
718719
case .bar: return 2
719720
case .baz: return 3
720-
case .neg: return -1
721721
case .UNRECOGNIZED(let i): return i
722722
}
723723
}

0 commit comments

Comments
 (0)