Skip to content

Commit ce87a7e

Browse files
committed
Update traversal logic for proto2
1 parent a285f80 commit ce87a7e

File tree

2 files changed

+6
-120
lines changed

2 files changed

+6
-120
lines changed

Sources/protoc-gen-swift/MessageFieldGenerator.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class MessageFieldGenerator: FieldGeneratorBase, FieldGenerator {
5050
}
5151

5252
var usesAlwaysVisitPrimitivesFlagForTraversal: Bool {
53-
!hasFieldPresence || (!isGroupOrMessage && fieldDescriptor.file.syntax == .proto2)
53+
!hasFieldPresence
5454
}
5555

5656
init(descriptor: FieldDescriptor,
@@ -206,13 +206,7 @@ class MessageFieldGenerator: FieldGeneratorBase, FieldGenerator {
206206
conditional = "!\(storedProperty).isEmpty || alwaysVisitPrimitiveFields"
207207
} else if hasFieldPresence {
208208
usesLocals = true
209-
if !isGroupOrMessage, fieldDescriptor.file.syntax == .proto2 {
210-
// Under proto2 all primitives have presence but
211-
// we want include primitives if alwaysVisitPrimitiveFields is true
212-
conditional = "let v = \(storedProperty) ?? (alwaysVisitPrimitiveFields ? \(isEnum ? swiftType : "")\(swiftDefaultValue) : nil)"
213-
} else {
214-
conditional = "let v = \(storedProperty)"
215-
}
209+
conditional = "let v = \(storedProperty)"
216210
} else {
217211
// At this point, the fields would be a primative type, and should only
218212
// be visted if it is the non default value.

Tests/SwiftProtobufTests/Test_JSON.swift

Lines changed: 4 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,26 +1177,6 @@ final class Test_JSONDefaultValues: XCTestCase {
11771177

11781178
func testProto2() {
11791179
let expected = "{"
1180-
+ #""optionalInt32":0,"#
1181-
+ #""optionalInt64":"0","#
1182-
+ #""optionalUint32":0,"#
1183-
+ #""optionalUint64":"0","#
1184-
+ #""optionalSint32":0,"#
1185-
+ #""optionalSint64":"0","#
1186-
+ #""optionalFixed32":0,"#
1187-
+ #""optionalFixed64":"0","#
1188-
+ #""optionalSfixed32":0,"#
1189-
+ #""optionalSfixed64":"0","#
1190-
+ #""optionalFloat":0.0,"#
1191-
+ #""optionalDouble":0.0,"#
1192-
+ #""optionalBool":false,"#
1193-
+ #""optionalString":"","#
1194-
+ #""optionalBytes":"","#
1195-
+ #""optionalNestedEnum":"FOO","#
1196-
+ #""optionalForeignEnum":"FOREIGN_FOO","#
1197-
+ #""optionalImportEnum":"IMPORT_FOO","#
1198-
+ #""optionalStringPiece":"","#
1199-
+ #""optionalCord":"","#
12001180
+ #""repeatedInt32":[],"#
12011181
+ #""repeatedInt64":[],"#
12021182
+ #""repeatedUint32":[],"#
@@ -1220,27 +1200,7 @@ final class Test_JSONDefaultValues: XCTestCase {
12201200
+ #""repeatedImportEnum":[],"#
12211201
+ #""repeatedStringPiece":[],"#
12221202
+ #""repeatedCord":[],"#
1223-
+ #""repeatedLazyMessage":[],"#
1224-
+ #""defaultInt32":41,"#
1225-
+ #""defaultInt64":"42","#
1226-
+ #""defaultUint32":43,"#
1227-
+ #""defaultUint64":"44","#
1228-
+ #""defaultSint32":-45,"#
1229-
+ #""defaultSint64":"46","#
1230-
+ #""defaultFixed32":47,"#
1231-
+ #""defaultFixed64":"48","#
1232-
+ #""defaultSfixed32":49,"#
1233-
+ #""defaultSfixed64":"-50","#
1234-
+ #""defaultFloat":51.5,"#
1235-
+ #""defaultDouble":52000.0,"#
1236-
+ #""defaultBool":true,"#
1237-
+ #""defaultString":"hello","#
1238-
+ #""defaultBytes":"d29ybGQ=","#
1239-
+ #""defaultNestedEnum":"BAR","#
1240-
+ #""defaultForeignEnum":"FOREIGN_BAR","#
1241-
+ #""defaultImportEnum":"IMPORT_BAR","#
1242-
+ #""defaultStringPiece":"abc","#
1243-
+ #""defaultCord":"123""#
1203+
+ #""repeatedLazyMessage":[]"#
12441204
+ "}"
12451205

12461206
var encodingOptions = JSONEncodingOptions()
@@ -1251,27 +1211,9 @@ final class Test_JSONDefaultValues: XCTestCase {
12511211

12521212
func testProto2Configured() {
12531213
let expected = "{"
1254-
+ #""optionalInt32":0,"#
1255-
+ #""optionalInt64":"0","#
1256-
+ #""optionalUint32":0,"#
1257-
+ #""optionalUint64":"0","#
1258-
+ #""optionalSint32":0,"#
1259-
+ #""optionalSint64":"0","#
1260-
+ #""optionalFixed32":0,"#
1261-
+ #""optionalFixed64":"0","#
1262-
+ #""optionalSfixed32":0,"#
1263-
+ #""optionalSfixed64":"0","#
1264-
+ #""optionalFloat":0.0,"#
1265-
+ #""optionalDouble":0.0,"#
1266-
+ #""optionalBool":false,"#
12671214
+ #""optionalString":"i am a test string value","#
12681215
+ #""optionalBytes":"aSBhbSBhIHRlc3Qgc3RyaW5nIHZhbHVl","#
1269-
+ #""optionalNestedMessage":{"bb":0},"#
1270-
+ #""optionalNestedEnum":"FOO","#
1271-
+ #""optionalForeignEnum":"FOREIGN_FOO","#
1272-
+ #""optionalImportEnum":"IMPORT_FOO","#
1273-
+ #""optionalStringPiece":"","#
1274-
+ #""optionalCord":"","#
1216+
+ #""optionalNestedMessage":{},"#
12751217
+ #""repeatedInt32":[],"#
12761218
+ #""repeatedInt64":[],"#
12771219
+ #""repeatedUint32":[],"#
@@ -1295,27 +1237,7 @@ final class Test_JSONDefaultValues: XCTestCase {
12951237
+ #""repeatedImportEnum":[],"#
12961238
+ #""repeatedStringPiece":[],"#
12971239
+ #""repeatedCord":[],"#
1298-
+ #""repeatedLazyMessage":[],"#
1299-
+ #""defaultInt32":41,"#
1300-
+ #""defaultInt64":"42","#
1301-
+ #""defaultUint32":43,"#
1302-
+ #""defaultUint64":"44","#
1303-
+ #""defaultSint32":-45,"#
1304-
+ #""defaultSint64":"46","#
1305-
+ #""defaultFixed32":47,"#
1306-
+ #""defaultFixed64":"48","#
1307-
+ #""defaultSfixed32":49,"#
1308-
+ #""defaultSfixed64":"-50","#
1309-
+ #""defaultFloat":51.5,"#
1310-
+ #""defaultDouble":52000.0,"#
1311-
+ #""defaultBool":true,"#
1312-
+ #""defaultString":"hello","#
1313-
+ #""defaultBytes":"d29ybGQ=","#
1314-
+ #""defaultNestedEnum":"BAR","#
1315-
+ #""defaultForeignEnum":"FOREIGN_BAR","#
1316-
+ #""defaultImportEnum":"IMPORT_BAR","#
1317-
+ #""defaultStringPiece":"abc","#
1318-
+ #""defaultCord":"123""#
1240+
+ #""repeatedLazyMessage":[]"#
13191241
+ "}"
13201242

13211243
var encodingOptions = JSONEncodingOptions()
@@ -1329,37 +1251,7 @@ final class Test_JSONDefaultValues: XCTestCase {
13291251
}
13301252

13311253
func testExtremeDefaultValues() {
1332-
let expected = "{"
1333-
+ #""escapedBytes":"AAEHCAwKDQkLXCci/g==","#
1334-
+ #""largeUint32":4294967295,"#
1335-
+ #""largeUint64":"18446744073709551615","#
1336-
+ #""smallInt32":-2147483647,"#
1337-
+ #""smallInt64":"-9223372036854775807","#
1338-
+ #""utf8String":"ሴ","#
1339-
+ #""zeroFloat":0.0,"#
1340-
+ #""oneFloat":1.0,"#
1341-
+ #""smallFloat":1.5,"#
1342-
+ #""negativeOneFloat":-1.0,"#
1343-
+ #""negativeFloat":-1.5,"#
1344-
+ #""largeFloat":2e+08,"#
1345-
+ #""smallNegativeFloat":-8e-28,"#
1346-
+ #""infDouble":"Infinity","#
1347-
+ #""negInfDouble":"-Infinity","#
1348-
// TODO: NaN and Inifinity should fall back to 0 like they do in C++
1349-
+ #""nanDouble":"NaN","#
1350-
+ #""infFloat":"Infinity","#
1351-
+ #""negInfFloat":"-Infinity","#
1352-
+ #""nanFloat":"NaN","#
1353-
+ #""cppTrigraph":"? ? ?? ?? ??? ??/ ??-","#
1354-
+ #""reallySmallInt32":-2147483648,"#
1355-
+ #""reallySmallInt64":"-9223372036854775808","#
1356-
+ #""stringWithZero":"hel\u0000lo","#
1357-
+ #""bytesWithZero":"d29yAGxk","#
1358-
+ #""stringPieceWithZero":"ab\u0000c","#
1359-
+ #""cordWithZero":"12\u00003","#
1360-
+ #""replacementString":"${unknown}""#
1361-
+ "}"
1362-
1254+
let expected = "{}"
13631255
var encodingOptions = JSONEncodingOptions()
13641256
encodingOptions.alwaysPrintPrimitiveFields = true
13651257
let msg = SwiftProtoTesting_TestExtremeDefaultValues()

0 commit comments

Comments
 (0)