Skip to content

Deeplink encoding #4

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

Merged
merged 2 commits into from
Feb 1, 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
26 changes: 26 additions & 0 deletions Sources/Deeplink/Deeplink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,32 @@ public enum Deeplink<Value>: Equatable, Hashable {
case .interpolated(let interpolation): return interpolation
}
}

/// Encodes object into string representation of deeplink
///
/// Example
///
/// ```swift
/// struct Ticket {
/// var ticketId: String?
/// }
///
/// let deeplink = try! "/sell/\(\.ticketId)" as Deeplink<Ticket>
///
/// var ticket = Ticket(ticketId: "123")
/// let encoded = deeplink.encode(ticket)
///
/// print(encoded) // "/sell/123"
/// ```
///
/// Opposite to parse
/// - Parameter value: object to encode
/// - Returns: string representation of deeplink with specified object
public func encode(
_ value: Value
) -> String {
interpolation.encode(value)
}
}

// MARK: - ExpressibleByStringLiteral
Expand Down
19 changes: 19 additions & 0 deletions Sources/Deeplink/DeeplinkInterpolation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ public struct DeeplinkInterpolation<Value>: Equatable, Hashable, StringInterpola
case .argumentList: return "{{ argument list }}"
}
}

func encode(value: Value) -> String {
switch self {
case let .literal(v):
return v
case let .argument(v):
return value[keyPath: v] ?? ""
case let .argumentList(v, separator: character):
return value[keyPath: v]?.joined(separator: String(character)) ?? ""
}
}
}

// MARK: - Properties
Expand Down Expand Up @@ -206,6 +217,14 @@ public struct DeeplinkInterpolation<Value>: Equatable, Hashable, StringInterpola
// Use the url data to pattern match the deeplink components.
try data.match(components: self.components, into: &instance)
}

func encode(
_ value: Value
) -> String {
self.components.map({
$0.encode(value: value)
}).joined()
}
}

// MARK: - CustomStringConvertible
Expand Down
107 changes: 107 additions & 0 deletions Tests/DeeplinkTests/EncodingDeeplinkTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//
// EncodingDeeplinkTests.swift
//
//
// Created by Dmitry Lobanov on 28.01.2024.
//

import Foundation
import XCTest
@testable import Deeplink

@MainActor
class EncodingDeeplinkTests: XCTestCase {

struct Artist: Equatable {
var id: String?
var slug: String?
}

struct Event: Equatable {
var id: String?
var slug: String?
}

struct EventType: Equatable {
var id: String?
var slug: String?
var event = Event()
}

struct Search: Equatable {
var query: String?
}

struct CustomData: Equatable {
var testData: Int?

var testDataString: String? {
get { testData.map { "\($0)" } }
set { testData = newValue.flatMap(Int.init) }
}
}

struct Restaurants: Equatable {
var ids: [String]?
}

func testEncoding_Artist() {
let deeplink = try! "/artist/\(\.slug)/\(\.id)" as Deeplink<Artist>

let url = "/artist/123/1"

var object = Artist()
object.slug = "123"
object.id = "1"
XCTAssertEqual(url, deeplink.encode(object))
}

func testEncoding_EventType() {
let deeplink = try! "/event/\(\.event.slug)/\(\.slug)/\(\.event.id)/\(\.id)" as Deeplink<EventType>

let url = "/event/food/restaurants/123/1"

var object = EventType()
var event = Event()

event.slug = "food"
object.slug = "restaurants"
event.id = "123"
object.id = "1"
object.event = event

XCTAssertEqual(url, deeplink.encode(object))
}

func testEncoding_Search() {
let deeplink = try! "/search?query=\(\.query)" as Deeplink<Search>

let url = "/search?query=food"

var object = Search()
object.query = "food"
XCTAssertEqual(url, deeplink.encode(object))
}

func testEncoding_CustomData() {
let deeplink = try! "/test/\(\.testDataString)" as Deeplink<CustomData>

let url = "/test/123"

var object = CustomData()
object.testData = 123

XCTAssertEqual(url, deeplink.encode(object))
}

func testEncoding_Restaurants() {
let deeplink = try! "/restaurants/ids=\(\.ids, separator: ",")" as Deeplink<Restaurants>

let url = "/restaurants/ids=123,456,789"

var object = Restaurants()
object.ids = ["123", "456", "789"]

XCTAssertEqual(url, deeplink.encode(object))
}
}