-
Notifications
You must be signed in to change notification settings - Fork 134
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
Replace CodableValue
with AnyCodable
#1081
Conversation
153e0e8
to
ee76468
Compare
01d7afc
to
59bd2c9
Compare
Datadog ReportBranch report: ✅ |
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.
@maxep could you provide more context on "Why?" I get the idea, but don't fully understand why we need this new vs existing code.
@ncreated Indeed, it was missing! I've updated the description. |
// The list of NSNumber encoding value can be found in the following links: | ||
// - https://developer.apple.com/documentation/foundation/nsnumber | ||
// - https://github.com/gnustep/libs-base/blob/master/Source/NSNumber.m | ||
switch Character(Unicode.Scalar(UInt8(nsnumber.objCType.pointee))) { |
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 works best than CFNumberGetType(nsnumber)
when using Swift value:
let nsnumber = UInt64.max as NSNumber
CFNumberGetType(nsnumber) == .sInt64Type // should be .longLongType
Character(Unicode.Scalar(UInt8(nsnumber.objCType.pointee))) == "Q" // correct
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.
And this relates to your other mention @maxep, right?
The reason being that the webview bridge returns a JSON as objc dictionary with
NSNumber
, in Swift aNSNumber
with value1
or0
can be cast to Bool which would be encoded as true or false and lead to a type mismatch
If so, could we annotate this nsnumber.objCType.pointee
code with relevant comment? It seems more important than it looks.
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, so an unsigned int does not get encoded as signed! I can improve the comment 👍
This is ready for review, it works well for our different use-cases 👍 We can now encode dictionary coming from WebView tracking directly. The models have been updated to use the interface shared in Some changes in let value: Any = NSNumber(value: 1)
switch value {
case let bool as Bool:
encode(bool) // will encode 'true'
case let nsnumber as NSNumber:
encode(nsnumber) // won't reach :(
default:
break
} |
907fda8
to
618ec72
Compare
Co-Authored-By: Maciek Grzybowski <maciek.grzybowski@datadoghq.com>
618ec72
to
cf8c6ae
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.
All looks good with only minor suggestions. Yes, it makes total sense to have more versatile support for any en-, de-, and codable 👍. Well done 💪.
internal let swiftModel: Foo.Bar | ||
internal var swiftModel: Foo.Bar |
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.
Looks unrelated - does it mean rum-models-generator
tests were red 😥?
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.. It's not being tested in CI, which is ok imo, or maybe we could have a conditional workflow!
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.
We have the run_tools_tests
lane which looks perfect candidate for it and can be easily conditioned. How about creating separate task to tackle it?
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.
// The list of NSNumber encoding value can be found in the following links: | ||
// - https://developer.apple.com/documentation/foundation/nsnumber | ||
// - https://github.com/gnustep/libs-base/blob/master/Source/NSNumber.m | ||
switch Character(Unicode.Scalar(UInt8(nsnumber.objCType.pointee))) { |
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.
And this relates to your other mention @maxep, right?
The reason being that the webview bridge returns a JSON as objc dictionary with
NSNumber
, in Swift aNSNumber
with value1
or0
can be cast to Bool which would be encoded as true or false and lead to a type mismatch
If so, could we annotate this nsnumber.objCType.pointee
code with relevant comment? It seems more important than it looks.
a561d21
to
d81bc1b
Compare
Datadog ReportBranch report: ✅ |
What and why?
Share
AnyCodable
logic inDatadogInternal
.While migrating the WebView tracking to v2, I realise that we were doing such heavy transformation:
The Flight-School/AnyCodable, which was already inspiring our
DatadogObjc.AnyEncodable
, is capable of serialisingAny
and so we could use that implementation to write[String: Any]
directly.Moreover,
CodableValue
is a similar type-erasure than Flight-School/AnyCodable that can be use to decode JSON into[String: Encodable]
.For those 2 reasons, I would like to consider the
AnyCodable
as a solution for[String: Any]
serialisation and deserialisation.How?
Replace
CodableValue
andDatadogObjc.AnyEncodable
with implementation from Flight-School/AnyCodable.Extensions for
ExpressibleBy*Literal
andHashable
have been stripped out, but theEquatable
extensions remains as it can be useful for data comparison on the message bus.Model Generator
The generator now uses the shared
AnyCodable
andDynamicCodingKey
.Review checklist
Custom CI job configuration (optional)