Skip to content

Commit ebc9475

Browse files
committed
Merge branch 'release/1.0.3'
2 parents 753b760 + bc71141 commit ebc9475

File tree

4 files changed

+248
-0
lines changed

4 files changed

+248
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//
2+
// Endpoint.swift
3+
// PolymorphCore
4+
//
5+
// Created by Benoit BRIATTE on 23/10/2017.
6+
//
7+
8+
import Foundation
9+
10+
public class Endpoint: Member, Documentable, Codable {
11+
12+
public enum Method: String, Codable {
13+
case get
14+
case post
15+
case put
16+
case delete
17+
case options
18+
case trace
19+
case connect
20+
}
21+
22+
// MARK: Codable
23+
24+
enum CodingKeys: String, CodingKey {
25+
case name
26+
case path
27+
case method
28+
case inputType
29+
case inputGenericTypes
30+
case outputType
31+
case outputGenericTypes
32+
case documentation
33+
}
34+
35+
// MARK: Properties
36+
37+
public var name: String
38+
39+
public var path: String
40+
41+
public var method: Method
42+
43+
public var inputType: UUID
44+
45+
public var inputGenericTypes: [UUID]?
46+
47+
public var outputType: UUID
48+
49+
public var outputGenericTypes: [UUID]?
50+
51+
public var documentation: String?
52+
53+
public internal(set) weak var project: Project? = nil
54+
55+
// MARK: Initializers
56+
57+
public init(name: String, path: String, method: Method, inputType: UUID, inputGenericTypes: [UUID]? = nil, outputType: UUID, outputGenericTypes: [UUID]? = nil) {
58+
self.name = name
59+
self.path = path
60+
self.method = method
61+
self.inputType = inputType
62+
self.inputGenericTypes = inputGenericTypes
63+
self.outputType = outputType
64+
self.outputGenericTypes = outputGenericTypes
65+
}
66+
67+
// MARK: Linked
68+
69+
public func isLinked(to uuid: UUID) -> Bool {
70+
if self.inputType == uuid || self.outputType == uuid {
71+
return true
72+
}
73+
if let gts = self.inputGenericTypes {
74+
if gts.contains(uuid) {
75+
return true
76+
}
77+
}
78+
if let gts = self.outputGenericTypes {
79+
return gts.contains(uuid)
80+
}
81+
return false
82+
}
83+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//
2+
// Network.swift
3+
// PolymorphCore
4+
//
5+
// Created by Benoit BRIATTE on 23/10/2017.
6+
//
7+
8+
import Foundation
9+
10+
public class Network: Documentable, Codable {
11+
12+
// MARK: Codable
13+
14+
enum CodingKeys: String, CodingKey {
15+
case documentation
16+
case services
17+
}
18+
19+
// MARK: Properties
20+
21+
public var documentation: String?
22+
23+
public private(set) var services: [UUID: Service]
24+
25+
public internal(set) weak var project: Project? = nil {
26+
didSet {
27+
self.services.values.forEach { $0.project = self.project }
28+
}
29+
}
30+
31+
// MARK: Initializers
32+
33+
public init() {
34+
self.services = [:]
35+
}
36+
37+
// MARK: Objects
38+
39+
public func findObject(uuid: UUID) -> Object? {
40+
if let cl = self.services[uuid] {
41+
return cl
42+
}
43+
return nil
44+
}
45+
46+
public func findObject(name: String) -> Object? {
47+
if let c = self.findService(name: name) {
48+
return c
49+
}
50+
return nil
51+
}
52+
53+
public func searchObjects(matching: String) -> [Object] {
54+
var objects: [Object] = []
55+
objects += self.searchServices(matching: matching) as [Object]
56+
return objects
57+
}
58+
59+
// MARK: Classes
60+
61+
@discardableResult
62+
public func addService(_ service: Service) -> Bool {
63+
guard self.services[service.id] == nil else {
64+
return false
65+
}
66+
self.services[service.id] = service
67+
return true
68+
}
69+
70+
@discardableResult
71+
public func removeService(uuid: UUID) -> Bool {
72+
guard self.services[uuid] != nil else {
73+
return false
74+
}
75+
self.services[uuid] = nil
76+
return true
77+
}
78+
79+
public func findService(name: String) -> Service? {
80+
return self.services.first { $0.value.name == name }?.value
81+
}
82+
83+
public func searchServices(matching: String) -> [Service] {
84+
return self.services.values.filter { $0.name.range(of: matching, options: .caseInsensitive) != nil }
85+
}
86+
87+
public func searchServices(linkedTo uuid: UUID) -> [Service] {
88+
return self.services.values.filter { $0.isLinked(to: uuid) }
89+
}
90+
}

Sources/PolymorphCore/Project.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class Project: Packageable, Documentable {
1919
case author
2020
case copyright
2121
case models
22+
case network
2223
}
2324

2425
// MARK: Properties
@@ -37,6 +38,8 @@ public class Project: Packageable, Documentable {
3738

3839
public var models: Models
3940

41+
public var network: Network
42+
4043
public lazy var natives: [UUID: Native] = {
4144
return NativeFactory.create(project: self)
4245
}()
@@ -52,8 +55,10 @@ public class Project: Packageable, Documentable {
5255
self.package = package
5356
self.version = Polymorph.version
5457
self.models = Models()
58+
self.network = Network()
5559
defer {
5660
self.models.project = self
61+
self.network.project = self
5762
}
5863
}
5964

@@ -74,8 +79,10 @@ public class Project: Packageable, Documentable {
7479
self.author = try container.decodeIfPresent(String.self, forKey: .author)
7580
self.copyright = try container.decodeIfPresent(String.self, forKey: .copyright)
7681
self.models = try container.decode(Models.self, forKey: .models)
82+
self.network = try container.decode(Network.self, forKey: .network)
7783
defer {
7884
self.models.project = self
85+
self.network.project = self
7986
}
8087
}
8188

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//
2+
// Service.swift
3+
// PolymorphCore
4+
//
5+
// Created by Benoit BRIATTE on 23/10/2017.
6+
//
7+
8+
import Foundation
9+
10+
public class Service: Object, Documentable, Packageable {
11+
12+
public enum Transformer: String, Codable {
13+
case raw
14+
case string
15+
case json
16+
case xml
17+
case yaml
18+
}
19+
20+
// MARK: Codable
21+
22+
enum CodingKeys: String, CodingKey {
23+
case id
24+
case name
25+
case package
26+
case documentation
27+
case serializer
28+
case parser
29+
case endpoints
30+
}
31+
32+
// MARK: Properties
33+
34+
public var id: UUID
35+
36+
public var name: String
37+
38+
public var package: Package
39+
40+
public var documentation: String?
41+
42+
public var serializer: Transformer
43+
44+
public var parser: Transformer
45+
46+
public var endpoints: [Endpoint]
47+
48+
public internal(set) weak var project: Project? = nil {
49+
didSet {
50+
self.endpoints.forEach { $0.project = self.project }
51+
}
52+
}
53+
54+
public init(name: String, package: Package, serializer: Transformer = .json, parser: Transformer = .json) {
55+
self.id = UUID()
56+
self.name = name
57+
self.package = package
58+
self.serializer = serializer
59+
self.parser = parser
60+
self.endpoints = []
61+
}
62+
63+
// MARK: Linked
64+
65+
public func isLinked(to uuid: UUID) -> Bool {
66+
return self.endpoints.first { $0.isLinked(to: uuid) } != nil
67+
}
68+
}

0 commit comments

Comments
 (0)