Skip to content

Commit

Permalink
Add UnkeyedDecodingContainer
Browse files Browse the repository at this point in the history
* Handle arrays in KeyedDecodingContainer and ETFDecoder
* Remove redundent doc comments
  • Loading branch information
cryptoAlgorithm committed Jun 10, 2022
1 parent 7e7aca3 commit a534dd8
Show file tree
Hide file tree
Showing 6 changed files with 374 additions and 276 deletions.
21 changes: 14 additions & 7 deletions Sources/ETFKit/Decoder/ETFDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,39 @@ open class ETFDecoder {
}

internal class _ETFDecoder: Decoder {
internal(set) public var codingPath: [CodingKey] = []
internal(set) public var codingPath: [CodingKey]

/// Contextual user-provided information for use during encoding.
public var userInfo: [CodingUserInfoKey : Any] = [:]

internal let decoded: Any?

func container<Key>(keyedBy: Key.Type) throws -> KeyedDecodingContainer<Key> {
guard let decoded = decoded as? [String : Any?] else {
throw DecodingError.typeMismatch(
[String : Any].self,
.init(codingPath: codingPath, debugDescription: "ETF data top level is not a map")
[String : Any?].self,
.init(codingPath: codingPath, debugDescription: "Top level data type is not a map")
)
}
return KeyedDecodingContainer(_ETFKeyedDecodingContainer(with: decoded, referencing: self))
}

func singleValueContainer() throws -> SingleValueDecodingContainer {
return self
self
}

func unkeyedContainer() throws -> UnkeyedDecodingContainer {
fatalError()
guard let decoded = decoded as? [Any?] else {
throw DecodingError.typeMismatch(
[Any?].self,
.init(codingPath: codingPath, debugDescription: "Top level data type is not a list")
)
}
return _ETFUnkeyedDecodingContainer(with: decoded, referencing: self)
}

init(with decoded: Any?) {
init(with decoded: Any?, at codingPath: [CodingKey] = []) {
self.decoded = decoded
self.codingPath = codingPath
}
}
Loading

0 comments on commit a534dd8

Please sign in to comment.