Skip to content

Commit

Permalink
[swift] Better handling for edge case field names
Browse files Browse the repository at this point in the history
  • Loading branch information
dnkoutso committed Oct 18, 2023
1 parent 0767120 commit f20751b
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 8 deletions.
1 change: 1 addition & 0 deletions gen-tests.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ val generateSwiftProto2Tests by tasks.creating(JavaExec::class) {
// 'redacted_test.proto',
"recursive_map.proto",
"same_name_enum.proto",
"swift_edge_cases.proto",
// 'simple_message.proto',
"to_string.proto",
"unknown_fields.proto"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -712,13 +712,14 @@ class SwiftGenerator private constructor(
}
}

val fieldName = if (hasPropertyWrapper) { "_${field.name}" } else { field.name }
addStatement(
if (hasPropertyWrapper) {
"self._%N.wrappedValue = %L"
"self.%N.wrappedValue = %L"
} else {
"self.%N = %L"
},
field.name,
fieldName,
initializer,
)
}
Expand Down Expand Up @@ -899,10 +900,11 @@ class SwiftGenerator private constructor(
.map { CodeBlock.of("%S", it) }
.joinToCode()

val prefix = if (hasPropertyWrapper) { "self._%1N.wrappedValue" } else { "self.%1N" }
val fieldName = if (hasPropertyWrapper) { "_${field.name}" } else { field.name }
val prefix = if (hasPropertyWrapper) { "self.%1N.wrappedValue" } else { "self.%1N" }
addStatement(
"$prefix = try container.$decode($typeArg%2T.self, $forKeys: $keys)",
field.name,
fieldName,
typeName,
)
}
Expand Down Expand Up @@ -1214,12 +1216,14 @@ class SwiftGenerator private constructor(
.apply {
type.fields.filter { it.isRequiredParameter }.forEach { field ->
val hasPropertyWrapper = !isIndirect(type, field) && (field.defaultedValue != null || field.isProtoDefaulted)
val fieldName = if (hasPropertyWrapper) { "_${field.name}" } else { field.name }
addStatement(
if (hasPropertyWrapper) {
"self._%1N.wrappedValue = %1N"
"self.%1N.wrappedValue = %2N"
} else {
"self.%1N = %1N"
"self.%1N = %2N"
},
fieldName,
field.name,
)
}
Expand All @@ -1245,12 +1249,14 @@ class SwiftGenerator private constructor(
.apply {
type.fields.forEach { field ->
val hasPropertyWrapper = !isIndirect(type, field) && (field.defaultedValue != null || field.isProtoDefaulted)
val fieldName = if (hasPropertyWrapper) { "_${field.name}" } else { field.name }
addStatement(
if (hasPropertyWrapper) {
"self._%1N.wrappedValue = %1N"
"self.%1N.wrappedValue = %2N"
} else {
"self.%1N = %1N"
"self.%1N = %2N"
},
fieldName,
field.name,
)
}
Expand Down
99 changes: 99 additions & 0 deletions wire-tests-swift/no-manifest/src/main/swift/SwiftEdgeCases.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Code generated by Wire protocol buffer compiler, do not edit.
// Source: squareup.protos.kotlin.swift_modules.SwiftEdgeCases in swift_edge_cases.proto
import Foundation
import Wire

public struct SwiftEdgeCases {

@ProtoDefaulted
public var `return`: String?
public var unknownFields: Foundation.Data = .init()

public init(configure: (inout Self) -> Swift.Void = { _ in }) {
configure(&self)
}

}

#if WIRE_INCLUDE_MEMBERWISE_INITIALIZER
extension SwiftEdgeCases {

@_disfavoredOverload
@available(*, deprecated)
public init(`return`: Swift.String? = nil) {
self._return.wrappedValue = `return`
}

}
#endif

#if !WIRE_REMOVE_EQUATABLE
extension SwiftEdgeCases : Equatable {
}
#endif

#if !WIRE_REMOVE_HASHABLE
extension SwiftEdgeCases : Hashable {
}
#endif

#if swift(>=5.5)
extension SwiftEdgeCases : Sendable {
}
#endif

extension SwiftEdgeCases : ProtoDefaultedValue {

public static var defaultedValue: SwiftEdgeCases {
SwiftEdgeCases()
}
}

extension SwiftEdgeCases : ProtoMessage {

public static func protoMessageTypeURL() -> Swift.String {
return "type.googleapis.com/squareup.protos.kotlin.swift_modules.SwiftEdgeCases"
}

}

extension SwiftEdgeCases : Proto2Codable {

public init(from protoReader: Wire.ProtoReader) throws {
var `return`: Swift.String? = nil

let token = try protoReader.beginMessage()
while let tag = try protoReader.nextTag(token: token) {
switch tag {
case 1: `return` = try protoReader.decode(Swift.String.self)
default: try protoReader.readUnknownField(tag: tag)
}
}
self.unknownFields = try protoReader.endMessage(token: token)

self._return.wrappedValue = `return`
}

public func encode(to protoWriter: Wire.ProtoWriter) throws {
try protoWriter.encode(tag: 1, value: self.`return`)
try protoWriter.writeUnknownFields(unknownFields)
}

}

#if !WIRE_REMOVE_CODABLE
extension SwiftEdgeCases : Codable {

public init(from decoder: Swift.Decoder) throws {
let container = try decoder.container(keyedBy: Wire.StringLiteralCodingKeys.self)
self._return.wrappedValue = try container.decodeIfPresent(Swift.String.self, forKey: "return")
}

public func encode(to encoder: Swift.Encoder) throws {
var container = encoder.container(keyedBy: Wire.StringLiteralCodingKeys.self)

try container.encodeIfPresent(self.`return`, forKey: "return")
}

}
#endif
22 changes: 22 additions & 0 deletions wire-tests/src/commonTest/proto/kotlin/swift_edge_cases.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2023 Square Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
syntax = "proto2";

package squareup.protos.kotlin.swift_modules;

message SwiftEdgeCases {
optional string return = 1;
}

0 comments on commit f20751b

Please sign in to comment.