Skip to content
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

fix: some bugs in swift protobuf #276

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions Services/SwiftProtobuf/SwiftProtobuf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public class SwiftProtobufWrapper: NSObject {
dto.referralPage = jsonObject["referralPage"] as? String ?? ""
dto.protocolType = jsonObject["protocolType"] as? String ?? ""
dto.eventName = jsonObject["eventName"] as? String ?? ""
dto.timezoneOffset = (jsonObject["timezoneOffset"] as? NSNumber)?.int32Value ?? 0
dto.timezoneOffset = jsonObject["timezoneOffset"] as? String ?? ""

return SwiftProtobufWrapper(dto)
}
Expand All @@ -97,7 +97,15 @@ public class SwiftProtobufWrapper: NSObject {
public func toJsonObject() -> [String: AnyObject]? {
do {
let data = try unbox.jsonUTF8Data()
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: AnyObject]
guard var json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: AnyObject] else {
return nil
}
// Int64 类型在 proto3 JSON Mapping 默认为 String 类型,apple/swift-protobuf 有将 Int64 转为 Number 的提案,但
// 截止 SwiftProtobuf 1.23.0,该提案尚未合并到 1_x_release_branch 分支上,仅合并入 main 分支
// 见:https://github.com/growingio/growingio-sdk-ios-autotracker/pull/248#issuecomment-1515913736
if let timestamp = json["timestamp"] as? String, let tm = Int64(timestamp) {
json["timestamp"] = tm as AnyObject
}
return json
} catch {
return nil
Expand Down Expand Up @@ -161,10 +169,16 @@ public extension SwiftProtobufWrapper {
var array = [[String: AnyObject]]()
for dto in list.values {
let jsonData = try dto.jsonUTF8Data()
let dic = try JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) as? [String: AnyObject]
if let dic = dic {
array.append(dic)
guard var json = try JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) as? [String: AnyObject] else {
continue
}
// Int64 类型在 proto3 JSON Mapping 默认为 String 类型,apple/swift-protobuf 有将 Int64 转为 Number 的提案,但
// 截止 SwiftProtobuf 1.23.0,该提案尚未合并到 1_x_release_branch 分支上,仅合并入 main 分支
// 见:https://github.com/growingio/growingio-sdk-ios-autotracker/pull/248#issuecomment-1515913736
if let timestamp = json["timestamp"] as? String, let tm = Int64(timestamp) {
json["timestamp"] = tm as AnyObject
}
array.append(json)
}
return array
} catch {
Expand Down Expand Up @@ -201,7 +215,7 @@ public extension GrowingBaseEvent {
dto.longitude = longitude
dto.sdkVersion = sdkVersion
dto.userKey = userKey ?? ""
dto.timezoneOffset = Int32(timezoneOffset)
dto.timezoneOffset = timezoneOffset

dto.eventType = SwiftProtobufWrapper.eventType(eventType)
dto.idfa = idfa()
Expand Down