Skip to content

Commit 4274e6f

Browse files
authored
Use generated Codable implementation for TypeDescription (#8)
1 parent 209f319 commit 4274e6f

File tree

2 files changed

+0
-557
lines changed

2 files changed

+0
-557
lines changed

Sources/SafeDICore/Models/TypeDescription.swift

Lines changed: 0 additions & 196 deletions
Original file line numberDiff line numberDiff line change
@@ -125,154 +125,6 @@ public enum TypeDescription: Codable, Hashable, Comparable, Sendable {
125125
}
126126
}
127127

128-
public init(from decoder: Decoder) throws {
129-
let values = try decoder.container(keyedBy: CodingKeys.self)
130-
let caseDescription = try values.decode(String.self, forKey: .caseDescription)
131-
switch caseDescription {
132-
case Self.simpleDescription:
133-
let text = try values.decode(String.self, forKey: .text)
134-
let typeDescriptions = try values.decode([Self].self, forKey: .typeDescriptions)
135-
self = .simple(name: text, generics: typeDescriptions)
136-
137-
case Self.unknownDescription:
138-
let text = try values.decode(String.self, forKey: .text)
139-
self = .unknown(text: text)
140-
141-
case Self.nestedDescription:
142-
let text = try values.decode(String.self, forKey: .text)
143-
let parentType = try values.decode(Self.self, forKey: .typeDescription)
144-
let typeDescriptions = try values.decode([Self].self, forKey: .typeDescriptions)
145-
self = .nested(name: text, parentType: parentType, generics: typeDescriptions)
146-
147-
case Self.optionalDescription:
148-
let typeDescription = try values.decode(Self.self, forKey: .typeDescription)
149-
self = .optional(typeDescription)
150-
151-
case Self.implicitlyUnwrappedOptionalDescription:
152-
let typeDescription = try values.decode(Self.self, forKey: .typeDescription)
153-
self = .implicitlyUnwrappedOptional(typeDescription)
154-
155-
case Self.compositionDescription:
156-
let typeDescriptions = try values.decode([Self].self, forKey: .typeDescriptions)
157-
self = .composition(typeDescriptions)
158-
159-
case Self.someDescription:
160-
let typeDescription = try values.decode(Self.self, forKey: .typeDescription)
161-
self = .some(typeDescription)
162-
163-
case Self.anyDescription:
164-
let typeDescription = try values.decode(Self.self, forKey: .typeDescription)
165-
self = .any(typeDescription)
166-
167-
case Self.metatypeDescription:
168-
let typeDescription = try values.decode(Self.self, forKey: .typeDescription)
169-
let isType = try values.decode(Bool.self, forKey: .isType)
170-
self = .metatype(typeDescription, isType: isType)
171-
172-
case Self.attributedDescription:
173-
let typeDescription = try values.decode(Self.self, forKey: .typeDescription)
174-
let specifier = try values.decodeIfPresent(String.self, forKey: .specifier)
175-
let attributes = try values.decodeIfPresent([String].self, forKey: .attributes)
176-
self = .attributed(typeDescription, specifier: specifier, attributes: attributes)
177-
178-
case Self.arrayDescription:
179-
let typeDescription = try values.decode(Self.self, forKey: .typeDescription)
180-
self = .array(element: typeDescription)
181-
182-
case Self.dictionaryDescription:
183-
let key = try values.decode(Self.self, forKey: .dictionaryKey)
184-
let value = try values.decode(Self.self, forKey: .dictionaryValue)
185-
self = .dictionary(key: key, value: value)
186-
187-
case Self.tupleDescription:
188-
let typeDescriptions = try values.decode([Self].self, forKey: .typeDescriptions)
189-
self = .tuple(typeDescriptions)
190-
191-
case Self.closureDescription:
192-
let typeDescriptions = try values.decode([Self].self, forKey: .closureArguments)
193-
let isAsync = try values.decode(Bool.self, forKey: .closureIsAsync)
194-
let doesThrow = try values.decode(Bool.self, forKey: .closureThrows)
195-
let typeDescription = try values.decode(Self.self, forKey: .closureReturn)
196-
self = .closure(arguments: typeDescriptions, isAsync: isAsync, doesThrow: doesThrow, returnType: typeDescription)
197-
198-
default:
199-
throw CodingError.unknownCase
200-
}
201-
}
202-
203-
public func encode(to encoder: Encoder) throws {
204-
var container = encoder.container(keyedBy: CodingKeys.self)
205-
try container.encode(caseDescription, forKey: .caseDescription)
206-
switch self {
207-
case let .simple(name, generics):
208-
try container.encode(name, forKey: .text)
209-
try container.encode(generics, forKey: .typeDescriptions)
210-
case let .unknown(text):
211-
try container.encode(text, forKey: .text)
212-
case let .optional(type),
213-
let .implicitlyUnwrappedOptional(type),
214-
let .array(type),
215-
let .some(type),
216-
let .any(type):
217-
try container.encode(type, forKey: .typeDescription)
218-
case let .tuple(types),
219-
let .composition(types):
220-
try container.encode(types, forKey: .typeDescriptions)
221-
case let .metatype(type, isType):
222-
try container.encode(type, forKey: .typeDescription)
223-
try container.encode(isType, forKey: .isType)
224-
case let .attributed(type, specifier: specifier, attributes: attributes):
225-
try container.encode(type, forKey: .typeDescription)
226-
try container.encodeIfPresent(specifier, forKey: .specifier)
227-
try container.encodeIfPresent(attributes, forKey: .attributes)
228-
case let .nested(name, parentType, generics):
229-
try container.encode(name, forKey: .text)
230-
try container.encode(parentType, forKey: .typeDescription)
231-
try container.encode(generics, forKey: .typeDescriptions)
232-
case let .dictionary(key, value):
233-
try container.encode(key, forKey: .dictionaryKey)
234-
try container.encode(value, forKey: .dictionaryValue)
235-
case let .closure(arguments, isAsync, doesThrow, returnType):
236-
try container.encode(arguments, forKey: .closureArguments)
237-
try container.encode(isAsync, forKey: .closureIsAsync)
238-
try container.encode(doesThrow, forKey: .closureThrows)
239-
try container.encode(returnType, forKey: .closureReturn)
240-
}
241-
}
242-
243-
enum CodingKeys: String, CodingKey {
244-
/// The value for this key is the case encoded as a String.
245-
case caseDescription
246-
/// The value for this key is an associated value of type String
247-
case text
248-
/// The value for this key is the associated value of type TypeDescription
249-
case typeDescription
250-
/// The value for this key is the associated value of type [TypeDescription]
251-
case typeDescriptions
252-
/// The value for this key represents whether a metatype is a Type (as opposed to a Protocol) and is of type Bool
253-
case isType
254-
/// The value for this key is the specifier on an attributed type of type String
255-
case specifier
256-
/// The value for this key is the attributes on an attributed type of type [String]
257-
case attributes
258-
/// The value for this key is a dictionary's key of type TypeDescription
259-
case dictionaryKey
260-
/// The value for this key is a dictionary's value of type TypeDescription
261-
case dictionaryValue
262-
/// The value for this key represents the list of types in a closure argument list and is of type [TypeDescription]
263-
case closureArguments
264-
/// The value for this key represents whether a closure is `async` and is of type Bool
265-
case closureIsAsync
266-
/// The value for this key represents whether a closure `throws` and is of type Bool
267-
case closureThrows
268-
/// The value for this key represents the return type of a closure argument list and is of type TypeDescription
269-
case closureReturn
270-
}
271-
272-
public enum CodingError: Error {
273-
case unknownCase
274-
}
275-
276128
public static func < (lhs: TypeDescription, rhs: TypeDescription) -> Bool {
277129
lhs.asSource < rhs.asSource
278130
}
@@ -343,54 +195,6 @@ public enum TypeDescription: Codable, Hashable, Comparable, Sendable {
343195
return self
344196
}
345197
}
346-
347-
private var caseDescription: String {
348-
switch self {
349-
case .composition:
350-
return Self.compositionDescription
351-
case .implicitlyUnwrappedOptional:
352-
return Self.implicitlyUnwrappedOptionalDescription
353-
case .nested:
354-
return Self.nestedDescription
355-
case .optional:
356-
return Self.optionalDescription
357-
case .simple:
358-
return Self.simpleDescription
359-
case .some:
360-
return Self.someDescription
361-
case .any:
362-
return Self.anyDescription
363-
case .metatype:
364-
return Self.metatypeDescription
365-
case .attributed:
366-
return Self.attributedDescription
367-
case .array:
368-
return Self.arrayDescription
369-
case .dictionary:
370-
return Self.dictionaryDescription
371-
case .tuple:
372-
return Self.tupleDescription
373-
case .closure:
374-
return Self.closureDescription
375-
case .unknown:
376-
return Self.unknownDescription
377-
}
378-
}
379-
380-
private static let simpleDescription = "simple"
381-
private static let nestedDescription = "nested"
382-
private static let compositionDescription = "composition"
383-
private static let optionalDescription = "optional"
384-
private static let implicitlyUnwrappedOptionalDescription = "implicitlyUnwrappedOptional"
385-
private static let someDescription = "some"
386-
private static let anyDescription = "any"
387-
private static let metatypeDescription = "metatype"
388-
private static let attributedDescription = "attributed"
389-
private static let arrayDescription = "array"
390-
private static let dictionaryDescription = "dictionary"
391-
private static let tupleDescription = "tuple"
392-
private static let closureDescription = "closure"
393-
private static let unknownDescription = "unknown"
394198
}
395199

396200
extension TypeSyntax {

0 commit comments

Comments
 (0)