Skip to content

Commit

Permalink
Use generated Codable implementation for TypeDescription (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfed authored Jan 12, 2024
1 parent 209f319 commit 4274e6f
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 557 deletions.
196 changes: 0 additions & 196 deletions Sources/SafeDICore/Models/TypeDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,154 +125,6 @@ public enum TypeDescription: Codable, Hashable, Comparable, Sendable {
}
}

public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
let caseDescription = try values.decode(String.self, forKey: .caseDescription)
switch caseDescription {
case Self.simpleDescription:
let text = try values.decode(String.self, forKey: .text)
let typeDescriptions = try values.decode([Self].self, forKey: .typeDescriptions)
self = .simple(name: text, generics: typeDescriptions)

case Self.unknownDescription:
let text = try values.decode(String.self, forKey: .text)
self = .unknown(text: text)

case Self.nestedDescription:
let text = try values.decode(String.self, forKey: .text)
let parentType = try values.decode(Self.self, forKey: .typeDescription)
let typeDescriptions = try values.decode([Self].self, forKey: .typeDescriptions)
self = .nested(name: text, parentType: parentType, generics: typeDescriptions)

case Self.optionalDescription:
let typeDescription = try values.decode(Self.self, forKey: .typeDescription)
self = .optional(typeDescription)

case Self.implicitlyUnwrappedOptionalDescription:
let typeDescription = try values.decode(Self.self, forKey: .typeDescription)
self = .implicitlyUnwrappedOptional(typeDescription)

case Self.compositionDescription:
let typeDescriptions = try values.decode([Self].self, forKey: .typeDescriptions)
self = .composition(typeDescriptions)

case Self.someDescription:
let typeDescription = try values.decode(Self.self, forKey: .typeDescription)
self = .some(typeDescription)

case Self.anyDescription:
let typeDescription = try values.decode(Self.self, forKey: .typeDescription)
self = .any(typeDescription)

case Self.metatypeDescription:
let typeDescription = try values.decode(Self.self, forKey: .typeDescription)
let isType = try values.decode(Bool.self, forKey: .isType)
self = .metatype(typeDescription, isType: isType)

case Self.attributedDescription:
let typeDescription = try values.decode(Self.self, forKey: .typeDescription)
let specifier = try values.decodeIfPresent(String.self, forKey: .specifier)
let attributes = try values.decodeIfPresent([String].self, forKey: .attributes)
self = .attributed(typeDescription, specifier: specifier, attributes: attributes)

case Self.arrayDescription:
let typeDescription = try values.decode(Self.self, forKey: .typeDescription)
self = .array(element: typeDescription)

case Self.dictionaryDescription:
let key = try values.decode(Self.self, forKey: .dictionaryKey)
let value = try values.decode(Self.self, forKey: .dictionaryValue)
self = .dictionary(key: key, value: value)

case Self.tupleDescription:
let typeDescriptions = try values.decode([Self].self, forKey: .typeDescriptions)
self = .tuple(typeDescriptions)

case Self.closureDescription:
let typeDescriptions = try values.decode([Self].self, forKey: .closureArguments)
let isAsync = try values.decode(Bool.self, forKey: .closureIsAsync)
let doesThrow = try values.decode(Bool.self, forKey: .closureThrows)
let typeDescription = try values.decode(Self.self, forKey: .closureReturn)
self = .closure(arguments: typeDescriptions, isAsync: isAsync, doesThrow: doesThrow, returnType: typeDescription)

default:
throw CodingError.unknownCase
}
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(caseDescription, forKey: .caseDescription)
switch self {
case let .simple(name, generics):
try container.encode(name, forKey: .text)
try container.encode(generics, forKey: .typeDescriptions)
case let .unknown(text):
try container.encode(text, forKey: .text)
case let .optional(type),
let .implicitlyUnwrappedOptional(type),
let .array(type),
let .some(type),
let .any(type):
try container.encode(type, forKey: .typeDescription)
case let .tuple(types),
let .composition(types):
try container.encode(types, forKey: .typeDescriptions)
case let .metatype(type, isType):
try container.encode(type, forKey: .typeDescription)
try container.encode(isType, forKey: .isType)
case let .attributed(type, specifier: specifier, attributes: attributes):
try container.encode(type, forKey: .typeDescription)
try container.encodeIfPresent(specifier, forKey: .specifier)
try container.encodeIfPresent(attributes, forKey: .attributes)
case let .nested(name, parentType, generics):
try container.encode(name, forKey: .text)
try container.encode(parentType, forKey: .typeDescription)
try container.encode(generics, forKey: .typeDescriptions)
case let .dictionary(key, value):
try container.encode(key, forKey: .dictionaryKey)
try container.encode(value, forKey: .dictionaryValue)
case let .closure(arguments, isAsync, doesThrow, returnType):
try container.encode(arguments, forKey: .closureArguments)
try container.encode(isAsync, forKey: .closureIsAsync)
try container.encode(doesThrow, forKey: .closureThrows)
try container.encode(returnType, forKey: .closureReturn)
}
}

enum CodingKeys: String, CodingKey {
/// The value for this key is the case encoded as a String.
case caseDescription
/// The value for this key is an associated value of type String
case text
/// The value for this key is the associated value of type TypeDescription
case typeDescription
/// The value for this key is the associated value of type [TypeDescription]
case typeDescriptions
/// The value for this key represents whether a metatype is a Type (as opposed to a Protocol) and is of type Bool
case isType
/// The value for this key is the specifier on an attributed type of type String
case specifier
/// The value for this key is the attributes on an attributed type of type [String]
case attributes
/// The value for this key is a dictionary's key of type TypeDescription
case dictionaryKey
/// The value for this key is a dictionary's value of type TypeDescription
case dictionaryValue
/// The value for this key represents the list of types in a closure argument list and is of type [TypeDescription]
case closureArguments
/// The value for this key represents whether a closure is `async` and is of type Bool
case closureIsAsync
/// The value for this key represents whether a closure `throws` and is of type Bool
case closureThrows
/// The value for this key represents the return type of a closure argument list and is of type TypeDescription
case closureReturn
}

public enum CodingError: Error {
case unknownCase
}

public static func < (lhs: TypeDescription, rhs: TypeDescription) -> Bool {
lhs.asSource < rhs.asSource
}
Expand Down Expand Up @@ -343,54 +195,6 @@ public enum TypeDescription: Codable, Hashable, Comparable, Sendable {
return self
}
}

private var caseDescription: String {
switch self {
case .composition:
return Self.compositionDescription
case .implicitlyUnwrappedOptional:
return Self.implicitlyUnwrappedOptionalDescription
case .nested:
return Self.nestedDescription
case .optional:
return Self.optionalDescription
case .simple:
return Self.simpleDescription
case .some:
return Self.someDescription
case .any:
return Self.anyDescription
case .metatype:
return Self.metatypeDescription
case .attributed:
return Self.attributedDescription
case .array:
return Self.arrayDescription
case .dictionary:
return Self.dictionaryDescription
case .tuple:
return Self.tupleDescription
case .closure:
return Self.closureDescription
case .unknown:
return Self.unknownDescription
}
}

private static let simpleDescription = "simple"
private static let nestedDescription = "nested"
private static let compositionDescription = "composition"
private static let optionalDescription = "optional"
private static let implicitlyUnwrappedOptionalDescription = "implicitlyUnwrappedOptional"
private static let someDescription = "some"
private static let anyDescription = "any"
private static let metatypeDescription = "metatype"
private static let attributedDescription = "attributed"
private static let arrayDescription = "array"
private static let dictionaryDescription = "dictionary"
private static let tupleDescription = "tuple"
private static let closureDescription = "closure"
private static let unknownDescription = "unknown"
}

extension TypeSyntax {
Expand Down
Loading

0 comments on commit 4274e6f

Please sign in to comment.