Skip to content

Commit c8fe19e

Browse files
committed
RPCJSONValue をやめる
1 parent fcfd9a4 commit c8fe19e

File tree

1 file changed

+14
-145
lines changed

1 file changed

+14
-145
lines changed

Sora/RPCTypes.swift

Lines changed: 14 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -47,123 +47,6 @@ public protocol RPCMethodProtocol {
4747
static var name: String { get }
4848
}
4949

50-
public enum RPCJSONValue: Codable {
51-
case object([String: RPCJSONValue])
52-
case array([RPCJSONValue])
53-
case string(String)
54-
case number(Double)
55-
case bool(Bool)
56-
case null
57-
58-
public init?(any value: Any) {
59-
if let dictionary = value as? [String: Any] {
60-
var mapped: [String: RPCJSONValue] = [:]
61-
for (key, item) in dictionary {
62-
guard let jsonValue = RPCJSONValue(any: item) else {
63-
return nil
64-
}
65-
mapped[key] = jsonValue
66-
}
67-
self = .object(mapped)
68-
return
69-
}
70-
if let array = value as? [Any] {
71-
let mapped = array.compactMap { RPCJSONValue(any: $0) }
72-
guard mapped.count == array.count else {
73-
return nil
74-
}
75-
self = .array(mapped)
76-
return
77-
}
78-
if let string = value as? String {
79-
self = .string(string)
80-
return
81-
}
82-
if let bool = value as? Bool {
83-
self = .bool(bool)
84-
return
85-
}
86-
if let number = value as? NSNumber {
87-
if CFGetTypeID(number) == CFBooleanGetTypeID() {
88-
self = .bool(number.boolValue)
89-
} else {
90-
self = .number(number.doubleValue)
91-
}
92-
return
93-
}
94-
if value is NSNull {
95-
self = .null
96-
return
97-
}
98-
return nil
99-
}
100-
101-
public init(from decoder: Decoder) throws {
102-
if let container = try? decoder.container(keyedBy: DynamicCodingKeys.self) {
103-
var object: [String: RPCJSONValue] = [:]
104-
for key in container.allKeys {
105-
object[key.stringValue] = try container.decode(RPCJSONValue.self, forKey: key)
106-
}
107-
self = .object(object)
108-
return
109-
}
110-
if var container = try? decoder.unkeyedContainer() {
111-
var array: [RPCJSONValue] = []
112-
while !container.isAtEnd {
113-
array.append(try container.decode(RPCJSONValue.self))
114-
}
115-
self = .array(array)
116-
return
117-
}
118-
let container = try decoder.singleValueContainer()
119-
if container.decodeNil() {
120-
self = .null
121-
return
122-
}
123-
if let bool = try? container.decode(Bool.self) {
124-
self = .bool(bool)
125-
return
126-
}
127-
if let number = try? container.decode(Double.self) {
128-
self = .number(number)
129-
return
130-
}
131-
if let string = try? container.decode(String.self) {
132-
self = .string(string)
133-
return
134-
}
135-
throw DecodingError.dataCorruptedError(
136-
in: container, debugDescription: "RPCJSONValue のデコードに失敗しました")
137-
}
138-
139-
public func encode(to encoder: Encoder) throws {
140-
switch self {
141-
case .object(let object):
142-
var container = encoder.container(keyedBy: DynamicCodingKeys.self)
143-
for (key, value) in object {
144-
try container.encode(value, forKey: DynamicCodingKeys(stringValue: key))
145-
}
146-
case .array(let array):
147-
var container = encoder.unkeyedContainer()
148-
for value in array {
149-
try container.encode(value)
150-
}
151-
case .string(let string):
152-
var container = encoder.singleValueContainer()
153-
try container.encode(string)
154-
case .number(let number):
155-
var container = encoder.singleValueContainer()
156-
try container.encode(number)
157-
case .bool(let value):
158-
var container = encoder.singleValueContainer()
159-
try container.encode(value)
160-
case .null:
161-
var container = encoder.singleValueContainer()
162-
try container.encodeNil()
163-
}
164-
}
165-
}
166-
16750
public struct RequestSimulcastRidParams: Encodable {
16851
public let rid: String
16952
public let senderConnectionId: String?
@@ -213,22 +96,22 @@ public struct ResetSpotlightRidParams: Encodable {
21396
}
21497
}
21598

216-
public struct PutSignalingNotifyMetadataParams: Encodable {
217-
public let metadata: [String: RPCJSONValue]
99+
public struct PutSignalingNotifyMetadataParams<Metadata: Encodable>: Encodable {
100+
public let metadata: Metadata
218101
public let push: Bool?
219102

220-
public init(metadata: [String: RPCJSONValue], push: Bool? = nil) {
103+
public init(metadata: Metadata, push: Bool? = nil) {
221104
self.metadata = metadata
222105
self.push = push
223106
}
224107
}
225108

226-
public struct PutSignalingNotifyMetadataItemParams: Encodable {
109+
public struct PutSignalingNotifyMetadataItemParams<Value: Encodable>: Encodable {
227110
public let key: String
228-
public let value: RPCJSONValue
111+
public let value: Value
229112
public let push: Bool?
230113

231-
public init(key: String, value: RPCJSONValue, push: Bool? = nil) {
114+
public init(key: String, value: Value, push: Bool? = nil) {
232115
self.key = key
233116
self.value = value
234117
self.push = push
@@ -301,9 +184,6 @@ public struct ResetSpotlightRidResult: Decodable {
301184
}
302185
}
303186

304-
public typealias PutSignalingNotifyMetadataResult = [String: RPCJSONValue]
305-
public typealias PutSignalingNotifyMetadataItemResult = [String: RPCJSONValue]
306-
307187
public enum RequestSimulcastRid: RPCMethodProtocol {
308188
public typealias Params = RequestSimulcastRidParams
309189
public typealias Result = RequestSimulcastRidResult
@@ -322,27 +202,16 @@ public enum ResetSpotlightRid: RPCMethodProtocol {
322202
public static let name = "2025.2.0/ResetSpotlightRid"
323203
}
324204

325-
public enum PutSignalingNotifyMetadata: RPCMethodProtocol {
326-
public typealias Params = PutSignalingNotifyMetadataParams
327-
public typealias Result = PutSignalingNotifyMetadataResult
205+
public enum PutSignalingNotifyMetadata<Metadata: Codable>: RPCMethodProtocol {
206+
public typealias Params = PutSignalingNotifyMetadataParams<Metadata>
207+
public typealias Result = Metadata
328208
public static let name = "2025.2.0/PutSignalingNotifyMetadata"
329209
}
330210

331-
public enum PutSignalingNotifyMetadataItem: RPCMethodProtocol {
332-
public typealias Params = PutSignalingNotifyMetadataItemParams
333-
public typealias Result = PutSignalingNotifyMetadataItemResult
211+
public enum PutSignalingNotifyMetadataItem<Metadata: Decodable, Value: Encodable>:
212+
RPCMethodProtocol
213+
{
214+
public typealias Params = PutSignalingNotifyMetadataItemParams<Value>
215+
public typealias Result = Metadata
334216
public static let name = "2025.2.0/PutSignalingNotifyMetadataItem"
335217
}
336-
337-
private struct DynamicCodingKeys: CodingKey {
338-
let stringValue: String
339-
let intValue: Int? = nil
340-
341-
init?(intValue: Int) {
342-
return nil
343-
}
344-
345-
init(stringValue: String) {
346-
self.stringValue = stringValue
347-
}
348-
}

0 commit comments

Comments
 (0)