Skip to content

Commit d9c67bf

Browse files
Added TypedIdentifier for other identifier types
1 parent fec5ba2 commit d9c67bf

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// TypedIdentifier.swift
3+
// CodableDatastore
4+
//
5+
// Created by Dimitri Bouniol on 2023-06-10.
6+
// Copyright © 2023 Mochi Development, Inc. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
struct TypedIdentifier<T>: TypedIdentifierProtocol {
12+
var rawValue: String
13+
14+
init(rawValue: String) {
15+
self.rawValue = rawValue
16+
}
17+
}
18+
19+
protocol TypedIdentifierProtocol: RawRepresentable, Codable, Equatable, Hashable, CustomStringConvertible {
20+
var rawValue: String { get }
21+
init(rawValue: String)
22+
}
23+
24+
extension TypedIdentifierProtocol {
25+
init(from decoder: Decoder) throws {
26+
let rawValue = try decoder.singleValueContainer().decode(String.self)
27+
self.init(rawValue: rawValue)
28+
}
29+
30+
func encode(to encoder: Encoder) throws {
31+
var encoder = encoder.singleValueContainer()
32+
try encoder.encode(rawValue)
33+
}
34+
35+
var description: String { rawValue }
36+
}

0 commit comments

Comments
 (0)