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

Enhance robustness of AliyunpanFile JSON decoding #60

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion AliyunpanSDK.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = "AliyunpanSDK"
spec.version = "0.3.3"
spec.version = "0.3.4"
spec.summary = "Aliyunpan OpenSDK-iOS"

spec.description = <<-DESC
Expand Down
2 changes: 1 addition & 1 deletion Sources/AliyunpanSDK/AliyunpanSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ public class Aliyunpan {
}
}

let version = "0.3.3"
let version = "0.3.4"
2 changes: 1 addition & 1 deletion Sources/AliyunpanSDK/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.3.3</string>
<string>0.3.4</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
47 changes: 45 additions & 2 deletions Sources/AliyunpanSDK/Model/AliyunpanFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public struct AliyunpanFile: Codable {
public let file_extension: String?
/// 文件 hash
public let content_hash: String?
public var category: FileCategory?
public let category: FileCategory?
public let type: FileType?
public let mime_type: String?
/// 缩略图
Expand All @@ -34,7 +34,50 @@ public struct AliyunpanFile: Codable {
/// 视频信息
public let video_media_metadata: MediaMetadata?
/// 视频预览信息
public var video_preview_metadata: AudioMetaData?
public let video_preview_metadata: AudioMetaData?

init(drive_id: String, file_id: String, parent_file_id: String, name: String, size: Int64?, file_extension: String?, content_hash: String?, category: FileCategory? = nil, type: FileType?, mime_type: String?, thumbnail: URL?, url: URL?, created_at: Date?, updated_at: Date?, play_cursor: String?, image_media_metadata: MediaMetadata?, video_media_metadata: MediaMetadata?, video_preview_metadata: AudioMetaData? = nil) {
self.drive_id = drive_id
self.file_id = file_id
self.parent_file_id = parent_file_id
self.name = name
self.size = size
self.file_extension = file_extension
self.content_hash = content_hash
self.category = category
self.type = type
self.mime_type = mime_type
self.thumbnail = thumbnail
self.url = url
self.created_at = created_at
self.updated_at = updated_at
self.play_cursor = play_cursor
self.image_media_metadata = image_media_metadata
self.video_media_metadata = video_media_metadata
self.video_preview_metadata = video_preview_metadata
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.drive_id = try container.decode(String.self, forKey: .drive_id)
self.file_id = try container.decode(String.self, forKey: .file_id)
self.parent_file_id = try container.decode(String.self, forKey: .parent_file_id)
self.name = try container.decode(String.self, forKey: .name)
self.size = try? container.decodeIfPresent(Int64.self, forKey: .size)
self.file_extension = try? container.decodeIfPresent(String.self, forKey: .file_extension)
self.content_hash = try? container.decodeIfPresent(String.self, forKey: .content_hash)
self.category = try? container.decodeIfPresent(AliyunpanFile.FileCategory.self, forKey: .category)
self.type = try? container.decodeIfPresent(AliyunpanFile.FileType.self, forKey: .type)
self.mime_type = try? container.decodeIfPresent(String.self, forKey: .mime_type)
self.thumbnail = try? container.decodeIfPresent(URL.self, forKey: .thumbnail)
self.url = try? container.decodeIfPresent(URL.self, forKey: .url)
self.created_at = try? container.decodeIfPresent(Date.self, forKey: .created_at)
self.updated_at = try? container.decodeIfPresent(Date.self, forKey: .updated_at)
self.play_cursor = try container.decodeIfPresent(String.self, forKey: .play_cursor)
self.image_media_metadata = try? container.decodeIfPresent(AliyunpanFile.MediaMetadata.self, forKey: .image_media_metadata)
self.video_media_metadata = try? container.decodeIfPresent(AliyunpanFile.MediaMetadata.self, forKey: .video_media_metadata)
self.video_preview_metadata = try? container.decodeIfPresent(AliyunpanFile.AudioMetaData.self, forKey: .video_preview_metadata)
}
}

extension AliyunpanFile: CustomStringConvertible {
Expand Down
50 changes: 50 additions & 0 deletions Tests/AliyunpanSDKTests/JSONTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,54 @@ class JSONTests: XCTestCase {
ISO8601DateFormatter().date(from: json["updated_at"]!),
ISO8601DateFormatter().date(from: "2023-06-28T20:00:20.022+08:00"))
}

func testNilArg() throws {
let json = """
{
"trashed": null,
"name": "music.mp3",
"thumbnail": null,
"type": "filexx",
"category": "audioxx",
"hidden": false,
"status": "available",
"description": null,
"meta": null,
"url": "https://stg111-enet.cn-shanghai.data.alicloudccp.com",
"size": 561701,
"starred": false,
"location": null,
"deleted": null,
"channel": null,
"user_tags": null,
"mime_type": "audio/mpeg",
"parent_file_id": "root",
"drive_id": "drive_id1",
"file_id": "file_id1",
"file_extension": "mp3",
"revision_id": null,
"content_hash": "content_hash1",
"content_hash_name": "sha1",
"encrypt_mode": "none",
"domain_id": "stg111",
"user_meta": null,
"content_type": null,
"created_at": "2023-06-28",
"updated_at": "2023-06-28T12:00:20.022Z",
"local_created_at": null,
"local_modified_at": null,
"trashed_at": null,
"punish_flag": 0,
"video_media_metadata": null,
"image_media_metadata": null,
"play_cursor": null,
"video_preview_metadata": null,
"streams_info": null
}
"""
let file = try JSONParameterDecoder().decode(AliyunpanFile.self, from: json.data(using: .utf8)!)
XCTAssertNil(file.created_at)
XCTAssertNil(file.category)
XCTAssertNil(file.type)
}
}