Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add encoding capabilities to the library. #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Sources/FatturaElettronica/CustomDecoder/PABool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ public struct PABool: Codable {

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self.wrappedValue)
/// https://www.fatturapa.gov.it/it/norme-e-regole/documentazione-fattura-elettronica/formato-fatturapa/
/// As of documentation this value if true must be encoded as "SI".
/// and in case of false the value is not encoded
if let bool = self.wrappedValue,
bool {
try container.encode("SI")
}
}
}

Expand All @@ -38,7 +44,7 @@ extension KeyedDecodingContainer {

extension KeyedEncodingContainer {
public mutating func encode(_ value: PABool, forKey key: Self.Key) throws {
if value.wrappedValue != nil {
if let bool = value.wrappedValue, bool {
try value.encode(to: self.superEncoder(forKey: key))
}
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/FatturaElettronica/FileHandler/FileHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ extension XMLHandler{
public func handle(_ xml: String) -> EventLoopFuture<FatturaElettronica> {
return self.xmlToInvoice(xml)
}

public func encode(_ invoice: FatturaElettronica) throws -> Data {
try self.invoiceToXML(invoice)
}

private func handleXML(_ data: Data) -> EventLoopFuture<FatturaElettronica> {
self.eventLoop.submit{
Expand Down
19 changes: 18 additions & 1 deletion Sources/FatturaElettronica/FileHandler/XMLHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ extension Date{
public class XMLHandler{
let eventLoop: EventLoop
let decoder = XMLDecoder()
let encoder = XMLEncoder()

private let defaultFormatter: DateFormatter = DateFormatter()

public convenience init() {
let loop = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount).next()
Expand All @@ -48,7 +51,8 @@ public class XMLHandler{
public init(on loop: EventLoop){
self.eventLoop = loop
self.decoder.shouldProcessNamespaces = true

self.defaultFormatter.dateFormat = "yyyy-MM-dd"

self.decoder.dateDecodingStrategy = .custom {
let container = try $0.singleValueContainer()
let string = try container.decode(String.self)
Expand All @@ -72,8 +76,21 @@ public class XMLHandler{
let loweCasedKey = last.stringValue.lowercased()
return XMLKey(stringValue: loweCasedKey)
}
self.decoder.dataDecodingStrategy = .base64
self.encoder.dateEncodingStrategy = .custom { date, encoder in
var container = encoder.singleValueContainer()
try container.encode(self.defaultFormatter.string(from: date))
}
self.encoder.dataEncodingStrategy = .base64
self.encoder.outputFormatting = .prettyPrinted
self.encoder.prettyPrintIndentation = .spaces(4)
self.encoder.stringEncodingStrategy = .deferredToString
}

func invoiceToXML(_ invoice: FatturaElettronica) throws -> Data {
try self.encoder.encode(invoice)
}

func xmlToInvoice(_ xml: String) -> EventLoopFuture<FatturaElettronica>{
let loop = self.eventLoop.next()

Expand Down
Loading