Skip to content
Closed
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
9 changes: 9 additions & 0 deletions Sources/LMStorage/LMCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ extension LMCodable {
}
return []
}

/// Returns an optional `LMJSONOptional` if the serialization is successful; otherwise, returns `nil`.
public func dictionaryNullable() -> LMJSONOptional? {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix code indentation

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be better to declare this function after the dictionary() one:

Screenshot 2025-06-17 at 11 09 25 AM

if let jsonData = data(),
let result = try? JSONSerialization.jsonObject(with: jsonData, options: []) as? LMJSONOptional {
return result
}
return nil
}
}

extension Encodable {
Expand Down
15 changes: 15 additions & 0 deletions Tests/LMStorageTests/LMCodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,19 @@ final class LMCodableTests: XCTestCase {
XCTAssertEqual(movies.first?.title, "Black Panther")
XCTAssertEqual(movies.last?.title, "Iron Man")
}

func testParseNullable() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This project uses 2 for tab and indent, so it needs to adjust the code indentation accordingly.

Screenshot 2025-06-17 at 11 11 25 AM

let contet = ContentId(id: "Movie-ID", seriesTmdb: nil, source: "Amazon Prime")
let dict = contet.dictionaryNullable()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Paulohos I think this PR won't be required, you can use the double optional for the property you want to be encoded, even if it's null as described here.

Example:

Screenshot 2025-06-17 at 12 44 40 PM Screenshot 2025-06-17 at 12 44 23 PM

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please let me know if it works for you

Copy link
Owner

Choose a reason for hiding this comment

The 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)
}
}
27 changes: 27 additions & 0 deletions Tests/LMStorageTests/Mock/ContentId.swift
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 {
Copy link
Owner

Choose a reason for hiding this comment

The 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)
}
}