Skip to content

Commit

Permalink
Merge pull request #7 from PureSwift/feature/decoder
Browse files Browse the repository at this point in the history
Add `ModelDataDecoder`
  • Loading branch information
colemancda authored Aug 19, 2023
2 parents 746def1 + 316f98a commit ecf6ebe
Show file tree
Hide file tree
Showing 9 changed files with 776 additions and 30 deletions.
4 changes: 2 additions & 2 deletions Sources/CoreDataModel/NSAttributeType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public extension NSAttributeType {

init(attributeType: AttributeType) {
switch attributeType {
case .boolean:
case .bool:
self = .booleanAttributeType
case .int16:
self = .integer16AttributeType
Expand Down Expand Up @@ -61,7 +61,7 @@ public extension AttributeType {
case .stringAttributeType:
self = .string
case .booleanAttributeType:
self = .boolean
self = .bool
case .dateAttributeType:
self = .date
case .binaryDataAttributeType:
Expand Down
2 changes: 1 addition & 1 deletion Sources/CoreModel/AttributeType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation
public enum AttributeType: String, Codable, CaseIterable, Sendable {

/// Boolean number type.
case boolean
case bool

/// 16 bit Integer number type.
case int16
Expand Down
40 changes: 34 additions & 6 deletions Sources/CoreModel/Decodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,46 @@ public extension ModelData {
}
}

// MARK: - Default Codable Implementation
// MARK: - AttributeDecodable

extension Entity where Self: Decodable, Self.ID: Decodable {
public protocol AttributeDecodable {

// TODO: Default implementation for Decodable
init?(attributeValue: AttributeValue)
}

// MARK: - AttributeDecodable
extension Optional: AttributeDecodable where Wrapped: AttributeDecodable {

public init?(attributeValue: AttributeValue) {
switch attributeValue {
case .null:
self = .none
default:
guard let value = Wrapped.init(attributeValue: attributeValue) else {
return nil
}
self = .some(value)
}
}
}

public protocol AttributeDecodable {
extension AttributeDecodable where Self: RawRepresentable, RawValue: AttributeDecodable {

init?(attributeValue: AttributeValue)
public init?(attributeValue: AttributeValue) {
guard let rawValue = RawValue.init(attributeValue: attributeValue) else {
return nil
}
self.init(rawValue: rawValue)
}
}

extension Bool: AttributeDecodable {

public init?(attributeValue: AttributeValue) {
guard case let .bool(value) = attributeValue else {
return nil
}
self = value
}
}

extension String: AttributeDecodable {
Expand Down
Loading

0 comments on commit ecf6ebe

Please sign in to comment.