Skip to content

Commit 20b59fa

Browse files
committed
refactor: Refactored the initialization syntax of the existing LinkItem.
Resolved an issue where decoding failed when passing items as a String without base64 encoding.
1 parent 00d0338 commit 20b59fa

1 file changed

Lines changed: 36 additions & 63 deletions

File tree

Sources/LinkNavigator/Core/BaseComponent/LinkItem.swift

Lines changed: 36 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,38 @@ import URLEncodedForm
66
/// Represents a link item that contains paths and associated items.
77
/// It is used to manage the links and state values that are injected into a page.
88
public struct LinkItem {
9-
109
// MARK: Lifecycle
11-
1210
/// Initializes a LinkItem instance with a given path list and an items parameter.
13-
///
11+
12+
/// - Parameters:
13+
/// - path: A string representing the path.
14+
/// - itemsString: The objects to be injected into pathList are in string format (e.g., queryString, base64EncodedString, etc.).
15+
/// - isBase64EncodedItemsString: The 'itemsString' indicates whether it is base64 encoded or not.
16+
public init(path: String, itemsString: String = "", isBase64EncodedItemsString: Bool = false) {
17+
pathList = [path]
18+
encodedItemString = isBase64EncodedItemsString ? itemsString : itemsString.encodedBase64()
19+
}
20+
1421
/// - Parameters:
1522
/// - pathList: An array of strings representing the path list.
16-
/// - items: The items associated with the pathList.
17-
public init(pathList: [String], items: String = "") {
23+
/// - itemsString: The objects to be injected into pathList are in string format (e.g., queryString, base64EncodedString, etc.).
24+
/// - isBase64EncodedItemsString: The 'itemsString' indicates whether it is base64 encoded or not.
25+
public init(pathList: [String], itemsString: String = "", isBase64EncodedItemsString: Bool = false) {
1826
self.pathList = pathList
19-
encodedItemString = items
27+
encodedItemString = isBase64EncodedItemsString ? itemsString : itemsString.encodedBase64()
2028
}
2129

22-
/// Initializes a LinkItem instance with a given path and an items parameter.
23-
///
2430
/// - Parameters:
2531
/// - path: A string representing the path.
26-
/// - items: The items associated with the path.
27-
public init(path: String, items: String = "") {
28-
pathList = [path]
29-
encodedItemString = items
30-
}
31-
32+
/// - items: The object to be injected into the RouteBuilder corresponding to each path .
3233
public init(path: String, items: Codable?) {
3334
pathList = [path]
3435
encodedItemString = items?.encoded() ?? ""
3536
}
3637

38+
/// - Parameters:
39+
/// - pathList: An array of strings representing the path list.
40+
/// - items: The object to be injected into the RouteBuilder corresponding to each path .
3741
public init(pathList: [String], items: Codable?) {
3842
self.pathList = pathList
3943
encodedItemString = items?.encoded() ?? ""
@@ -46,7 +50,20 @@ public struct LinkItem {
4650

4751
/// A parameter containing the items associated with the path or path list.
4852
let encodedItemString: String
53+
}
54+
55+
extension LinkItem {
56+
@available(*, deprecated, message: "Please use init(pathList:itemsString:isConvertBase64:) instead.")
57+
public init(pathList: [String], items: String) {
58+
self.pathList = pathList
59+
encodedItemString = items
60+
}
4961

62+
@available(*, deprecated, message: "Please use init(path:itemsString:isConvertBase64:) instead.")
63+
public init(path: String, items: String) {
64+
pathList = [path]
65+
encodedItemString = items
66+
}
5067
}
5168

5269
// MARK: Equatable
@@ -58,62 +75,18 @@ extension LinkItem: Equatable {
5875
}
5976
}
6077

61-
// extension LinkItem where ItemType == String {
62-
// /// Initializes a LinkItem instance with a given path list and an optional items parameter.
63-
// ///
64-
// /// - Parameters:
65-
// /// - pathList: An array of strings representing the path list.
66-
// /// - items: Encoded URLEncodedQuery items. Defaults to an empty string.
67-
// public init(pathList: [String], items: ItemType = "") {
68-
// self.pathList = pathList
69-
// self.items = items
70-
// }
71-
//
72-
// /// Initializes a LinkItem instance with a given path and an optional items parameter.
73-
// ///
74-
// /// - Parameters:
75-
// /// - path: A string representing the path.
76-
// /// - items: Encoded URLEncodedQuery items. Defaults to an empty string.
77-
// public init(path: String, items: ItemType = "") {
78-
// pathList = [path]
79-
// self.items = items
80-
// }
81-
// }
82-
83-
// extension LinkItem where ItemType == [String: String] {
84-
// /// Initializes a LinkItem instance with a given path list and an optional items dictionary.
85-
// ///
86-
// /// - Parameters:
87-
// /// - pathList: An array of strings representing the path list.
88-
// /// - items: A dictionary containing key-value pairs representing the items. Defaults to an empty dictionary.
89-
// public init(pathList: [String], items: ItemType = [:]) {
90-
// self.pathList = pathList
91-
// self.items = items
92-
// }
93-
//
94-
// /// Initializes a LinkItem instance with a given path and an optional items dictionary.
95-
// ///
96-
// /// - Parameters:
97-
// /// - path: A string representing the path.
98-
// /// - items: A dictionary containing key-value pairs representing the items. Defaults to an empty dictionary.
99-
// public init(path: String, items: ItemType = [:]) {
100-
// pathList = [path]
101-
// self.items = items
102-
// }
103-
// }
104-
10578
extension String {
10679
public func decoded<T: Decodable>() -> T? {
10780
if let decodedValue = self as? T {
10881
return decodedValue
10982
}
11083

111-
guard
112-
let data = Data(base64Encoded: self),
113-
let decodedModel = try? JSONDecoder().decode(T.self, from: data)
114-
else { return .none }
84+
guard let data = Data(base64Encoded: self) else { return .none }
85+
return (try? JSONDecoder().decode(T.self, from: data)) ?? (try? URLEncodedFormDecoder().decode(T.self, from: data))
86+
}
11587

116-
return decodedModel
88+
fileprivate func encodedBase64() -> Self {
89+
data(using: .utf8)?.base64EncodedString() ?? self
11790
}
11891
}
11992

0 commit comments

Comments
 (0)