-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBridgeAddress.swift
More file actions
57 lines (50 loc) · 1.55 KB
/
BridgeAddress.swift
File metadata and controls
57 lines (50 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// BridgeAddress.swift
// Bridge
//
// Created by Alexey Pichukov on 27/09/2019.
//
import Foundation
/**
* Contains the unique address representation for the Node in Bridge system
*
* - id: the unique UUID (by default) String or nil
* NOTE: use nil for Message that should be delivered to all the Nodes of the same Type (BridgeNodeTypable)
*
* - type: the Type of the Node (BridgeNodeTypable)
*/
infix operator |=
public struct BridgeNodeAddress<Type: BridgeNodeTypable> {
private(set) var id: String?
private(set) var type: Type
/**
Create the address with Type and custom ID
*/
public init(withType type: Type, andId id: String) {
self.type = type
self.id = id
}
/**
Create the address with Type and autogenerated UUID as ID
*/
public init(withAutogeneratedIdAndType type: Type) {
let id = UUID().uuidString
self.init(withType: type, andId: id)
}
/**
Create the address with only Type. The ID will be nil in that case
NOTE: Use this constructor for creating Type delivery oriented messages
*/
public init(withOnlyType type: Type) {
self.type = type
self.id = nil
}
/// Check that our address fits the destination address
/// - lhs - our node address
/// - rhs - destination address
static func |= (lhs: BridgeNodeAddress, rhs: BridgeNodeAddress) -> Bool {
guard lhs.type == rhs.type else { return false }
guard let id = rhs.id else { return true }
return lhs.id == id
}
}