-
Notifications
You must be signed in to change notification settings - Fork 5
Added new parse to dictionary nullable #17
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -137,6 +137,15 @@ extension LMCodable { | |
| } | ||
| return [] | ||
| } | ||
|
|
||
| /// Returns an optional `LMJSONOptional` if the serialization is successful; otherwise, returns `nil`. | ||
| public func dictionaryNullable() -> LMJSONOptional? { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| if let jsonData = data(), | ||
| let result = try? JSONSerialization.jsonObject(with: jsonData, options: []) as? LMJSONOptional { | ||
| return result | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| extension Encodable { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,4 +87,19 @@ final class LMCodableTests: XCTestCase { | |
| XCTAssertEqual(movies.first?.title, "Black Panther") | ||
| XCTAssertEqual(movies.last?.title, "Iron Man") | ||
| } | ||
|
|
||
| func testParseNullable() { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| let contet = ContentId(id: "Movie-ID", seriesTmdb: nil, source: "Amazon Prime") | ||
| let dict = contet.dictionaryNullable() | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please let me know if it works for you
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it works, we can still keep the PR to add the unit test to cover this scenario |
||
| let dictionaryItens = 3 | ||
| let keys: [String] = ["id", "seriesTmdb", "source"] | ||
|
|
||
| var itemsCount = 0 | ||
| dict?.forEach { key, _ in | ||
| itemsCount += 1 | ||
| XCTAssertTrue(keys.contains { $0 == key }) | ||
| } | ||
|
|
||
| XCTAssertEqual(dictionaryItens, itemsCount) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // | ||
| // ContentId.swift | ||
| // LMStorage | ||
| // | ||
| // Created by Paulo Henrique Oliveira Souza on 17/06/25. | ||
| // | ||
|
|
||
| @testable import LMStorage | ||
|
|
||
| struct ContentId: LMCodable { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be better to simplify this model this way: struct Media: LMCodable {
var id: String?
var source: String?
} |
||
| var id: String? | ||
| var seriesTmdb: String? | ||
| var source: String? | ||
|
|
||
| enum CodingKeys: String, CodingKey { | ||
| case id = "id" | ||
| case seriesTmdb = "seriesTmdb" | ||
| case source = "source" | ||
| } | ||
|
|
||
| func encode(to encoder: Encoder) throws { | ||
| var container = encoder.container(keyedBy: CodingKeys.self) | ||
| try container.encode(id, forKey: .id) | ||
| try container.encode(seriesTmdb, forKey: .seriesTmdb) | ||
| try container.encode(source, forKey: .source) | ||
| } | ||
| } | ||




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.
Please fix code indentation