Skip to content

Commit

Permalink
Update ObjectEncoder initializers
Browse files Browse the repository at this point in the history
  • Loading branch information
Alkenso committed Oct 19, 2023
1 parent aa8c618 commit b16da27
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
12 changes: 6 additions & 6 deletions Sources/SpellbookFoundation/Common/Extensions - Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,33 +51,33 @@ public struct ObjectEncoder<T> {
}

extension ObjectEncoder where T: Encodable {
public static func json(_ type: T.Type = T.self, encoder: JSONEncoder = JSONEncoder()) -> Self {
public static func json(type: T.Type = T.self, encoder: JSONEncoder = JSONEncoder()) -> Self {
.init(name: "json", encode: encoder.encode)
}

public static func json(_ type: T.Type = T.self, formatting: JSONEncoder.OutputFormatting) -> Self {
public static func json(type: T.Type = T.self, _ formatting: JSONEncoder.OutputFormatting) -> Self {
let encoder = JSONEncoder()
encoder.outputFormatting = formatting
return .json(encoder: encoder)
}

public static func plist(_ type: T.Type = T.self, encoder: PropertyListEncoder = PropertyListEncoder()) -> Self {
public static func plist(type: T.Type = T.self, encoder: PropertyListEncoder = PropertyListEncoder()) -> Self {
.init(name: "plist", encode: encoder.encode)
}

public static func plist(_ type: T.Type = T.self, format: PropertyListSerialization.PropertyListFormat) -> Self {
public static func plist(type: T.Type = T.self, _ format: PropertyListSerialization.PropertyListFormat) -> Self {
let encoder = PropertyListEncoder()
encoder.outputFormat = format
return .plist(encoder: encoder)
}
}

extension ObjectEncoder {
public static func foundationJSON(_ type: T.Type = T.self, options: JSONSerialization.WritingOptions = []) -> Self {
public static func foundationJSON(type: T.Type = T.self, _ options: JSONSerialization.WritingOptions = []) -> Self {
.init(name: "json") { try JSONSerialization.data(withJSONObject: $0, options: options) }
}

public static func foundationPlist(_ type: T.Type = T.self, format: PropertyListSerialization.PropertyListFormat) -> Self {
public static func foundationPlist(type: T.Type = T.self, _ format: PropertyListSerialization.PropertyListFormat) -> Self {
.init(name: "plist") { try PropertyListSerialization.data(fromPropertyList: $0, format: format, options: 0) }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ public struct FileStoreCoder<T: Codable> {

extension FileStoreCoder {
public static func json(_ formatting: JSONEncoder.OutputFormatting = []) -> Self {
.init(encoder: .json(formatting: formatting), decoder: .json())
.init(encoder: .json(formatting), decoder: .json())
}

public static func plist(_ format: PropertyListSerialization.PropertyListFormat = .xml) -> Self {
.init(encoder: .plist(format: format), decoder: .plist())
.init(encoder: .plist(format), decoder: .plist())
}
}
8 changes: 4 additions & 4 deletions Sources/SpellbookHTTP/HTTPRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,19 @@ extension HTTPRequest.Body {
}

public static func foundation(json obj: Any, options: JSONSerialization.WritingOptions = []) -> Self {
.init(value: obj, encoder: .foundationJSON(options: options), contentType: contentTypeJSON)
.init(value: obj, encoder: .foundationJSON(options), contentType: contentTypeJSON)
}

public static func foundation(plist obj: Any, format: PropertyListSerialization.PropertyListFormat = .xml) -> Self {
.init(value: obj, encoder: .foundationPlist(format: format), contentType: contentTypePlist)
.init(value: obj, encoder: .foundationPlist(format), contentType: contentTypePlist)
}

public static func codable<T: Encodable>(json obj: T, formatting: JSONEncoder.OutputFormatting = []) -> Self {
.init(value: obj, encoder: .json(formatting: formatting), contentType: contentTypeJSON)
.init(value: obj, encoder: .json(formatting), contentType: contentTypeJSON)
}

public static func codable<T: Encodable>(plist obj: T, format: PropertyListSerialization.PropertyListFormat = .xml) -> Self {
.init(value: obj, encoder: .plist(format: format), contentType: contentTypePlist)
.init(value: obj, encoder: .plist(format), contentType: contentTypePlist)
}

public static func data(_ data: Data, contentType: String?) -> Self {
Expand Down

0 comments on commit b16da27

Please sign in to comment.