Skip to content

Commit

Permalink
Deeplink: Encoding deeplink has been added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Lobanov committed Jan 29, 2024
1 parent fbf9291 commit 325a6f8
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Sources/Deeplink/Deeplink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public enum Deeplink<Value>: Equatable, Hashable {
case .interpolated(let interpolation): return interpolation
}
}

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

0 comments on commit 325a6f8

Please sign in to comment.