-
Notifications
You must be signed in to change notification settings - Fork 578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[swift] Provide a default value for sub fields and common types as per proto spec #2650
Conversation
wire-swift-generator/src/main/java/com/squareup/wire/swift/SwiftGenerator.kt
Outdated
Show resolved
Hide resolved
b27b725
to
eac52e6
Compare
35a6331
to
b1b41ec
Compare
4f6ba0c
to
8e7fb83
Compare
wire-swift-generator/src/main/java/com/squareup/wire/swift/SwiftGenerator.kt
Outdated
Show resolved
Hide resolved
6db6816
to
a4af441
Compare
f009d6e
to
a7bdfd8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is effectively doubling the storage requirements for everything...
It's now making me a bit nervous...
Maybe we can do something more like BetterCodable and use statics for String, Number, etc...
The empty structs could also be solved by conforming to a protocol like EmptyInitializable
@propertyWrapper
public struct DefaultedZero<Value: Numeric> {
public var wrappedValue: Value?
public var projectedValue: Value {
wrappedValue ?? .zero
}
}
@propertyWrapper
public struct DefaultedEmpty<Value: EmptyInitializable> {
public var wrappedValue: Value?
public var projectedValue: Value {
wrappedValue ?? Value.init()
}
}
@@ -8,13 +8,17 @@ public struct Dinosaur { | |||
/** | |||
* Common name of this dinosaur, like "Stegosaurus". | |||
*/ | |||
@Defaulted(defaultValue: "") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Should probably update the README to include info about @defaulted and this updated model
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeap, thanks for reminding
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
} | ||
if (isEnum) { | ||
val enumType = schema.getType(type!!) as EnumType | ||
return if (enumType.constants.getOrNull(0) != null) CodeBlock.of("%T.%L", typeName.makeNonOptional(), enumType.constants[0].name) else null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is enumType.constants.getOrNull(0)
correct? That just looks like the first (mapped) constant, not the 0-th value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the proto3 code, I think we want enum.constants.filter { it.tag == 0 }.firstOrNull()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes this is probably incorrect. There is also enum.constant(0)
but I think this didnt work, will be updating and looking again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
DOUBLE -> return CodeBlock.of("%L", 0) | ||
STRING -> return CodeBlock.of("%S", "") | ||
DATA -> return CodeBlock.of( | ||
"%T(base64Encoded: %S)!", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you not just do Data()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah it was an initial copy paste from below will be updating
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
Discussed offline: we need Defaulted to have conditional conformance to |
f2772d1
to
e6a251e
Compare
@@ -44,16 +44,20 @@ extension Redactable { | |||
guard let label = label else { | |||
return "\(value)" | |||
} | |||
if RedactedKeys(rawValue: label) != nil { | |||
var strippedLabel = label |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is an existing bug today. If a custom default value was specified due to applying @Defaulted
the internal property is renamed with a _
in front. This fails this check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test caught this now because we emit @Defaulted
identity types for many properties which now as an empty string exacerbated this issue which is now fixed and the test passes.
e6a251e
to
e1c4eaa
Compare
7a8f922
to
dd5aafe
Compare
dd5aafe
to
db6b654
Compare
before this falls out of my memory: the conditional conformance did not appear necessary to address the test failures occurring with the nested, partially redacted types. the reason being that the |
This implements the proto spec: