Skip to content

Commit

Permalink
fixed value json parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ypopovych committed Jun 21, 2023
1 parent e7a39c7 commit 116204f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 31 deletions.
53 changes: 22 additions & 31 deletions Sources/Substrate/Types/Value/Value+Decodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ extension Value where C == RuntimeTypeId {

public enum ValueDecodingContainer {
case decoder(Decoder)
case single(SingleValueDecodingContainer)
case unkeyed(UnkeyedDecodingContainer)
case keyed(AnyCodableCodingKey, KeyedDecodingContainer<AnyCodableCodingKey>)

Expand All @@ -60,54 +59,48 @@ public enum ValueDecodingContainer {

mutating func decode<T: Decodable>(_ type: T.Type) throws -> T {
switch self {
case .decoder(let decoder):
self = try .single(decoder.singleValueContainer())
return try self.decode(type)
case .single(let container): return try container.decode(type)
case .decoder(let decoder): return try decoder.singleValueContainer().decode(type)
case .keyed(let key, let container): return try container.decode(type, forKey: key)
case .unkeyed(var container): return try container.decode(type)
case .unkeyed(var container):
let val = try container.decode(type)
self = .unkeyed(container)
return val
}
}

mutating func decodeNil() throws -> Bool {
switch self {
case .decoder(let decoder):
self = try .single(decoder.singleValueContainer())
return try self.decodeNil()
case .single(let container): return container.decodeNil()
case .decoder(let decoder): return try decoder.singleValueContainer().decodeNil()
case .keyed(let key, let container): return try container.decodeNil(forKey: key)
case .unkeyed(var container): return try container.decodeNil()
case .unkeyed(var container):
let val = try container.decodeNil()
self = .unkeyed(container)
return val
}
}

mutating func nestedUnkeyedContainer() throws -> Self {
switch self {
case .decoder(let decoder):
let container = try decoder.unkeyedContainer()
self = .unkeyed(container)
return self
case .single(let container):
throw DecodingError.dataCorruptedError(in: container,
debugDescription: "SingleValueContainer asked for nested")
case .unkeyed(var container): return try .unkeyed(container.nestedUnkeyedContainer())
case .decoder(let decoder): return try .unkeyed(decoder.unkeyedContainer())
case .keyed(let key, let container): return try .unkeyed(container.nestedUnkeyedContainer(forKey: key))
case .unkeyed(var container):
let nested = try container.nestedUnkeyedContainer()
self = .unkeyed(container)
return .unkeyed(nested)
}
}

mutating func nestedKeyedContainer() throws -> Self {
let emptyKey = AnyCodableCodingKey(0)
switch self {
case .decoder(let decoder):
let container = try decoder.container(keyedBy: AnyCodableCodingKey.self)
self = .keyed(emptyKey, container)
return self
case .single(let container):
throw DecodingError.dataCorruptedError(in: container,
debugDescription: "SingleValueContainer asked for nested")
case .unkeyed(var container):
return try .keyed(emptyKey, container.nestedContainer(keyedBy: AnyCodableCodingKey.self))
return try .keyed(emptyKey, decoder.container(keyedBy: AnyCodableCodingKey.self))
case .keyed(let key, let container):
return try .keyed(emptyKey, container.nestedContainer(keyedBy: AnyCodableCodingKey.self, forKey: key))
case .unkeyed(var container):
let nested = try container.nestedContainer(keyedBy: AnyCodableCodingKey.self)
self = .unkeyed(container)
return .keyed(emptyKey, nested)
}
}

Expand Down Expand Up @@ -146,8 +139,6 @@ public enum ValueDecodingContainer {
case .decoder(let decoder):
let container = try decoder.singleValueContainer()
return DecodingError.dataCorruptedError(in: container, debugDescription: description)
case .single(let container):
return DecodingError.dataCorruptedError(in: container, debugDescription: description)
case .unkeyed(let container):
return DecodingError.dataCorruptedError(in: container, debugDescription: description)
case .keyed(let key, let container):
Expand All @@ -173,7 +164,7 @@ private extension Value where C == RuntimeTypeId {
var value = try from.nestedKeyedContainer()
var map: [String: Value<C>] = Dictionary(minimumCapacity: fields.count)
for field in fields {
try value.next(key: field.name!)
try value.next(key: field.name!.camelCased(with: "_"))
map[field.name!] = try Value(from: &value, as: field.type, runtime: runtime)
}
return Value(value: .map(map), context: type)
Expand Down Expand Up @@ -253,7 +244,7 @@ private extension Value where C == RuntimeTypeId {
guard let found = try variants.first(where: { try container.contains(key: $0.name) }) else {
throw try container.newError("variant not found")
}
try container.next(key: found.name)
try container.next(key: found.name.camelCased(with: "_"))
variant = found
}
let value = try Self._decodeComposite(from: &container, type: type,
Expand Down
19 changes: 19 additions & 0 deletions Sources/Substrate/Utils/String+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// String+Extensions.swift
//
//
// Created by Yehor Popovych on 21/06/2023.
//

import Foundation

public extension String {
@inlinable
func camelCased(with separator: Character = "_") -> String {
return self.lowercased()
.split(separator: separator)
.enumerated()
.map { $0.offset > 0 ? $0.element.capitalized : $0.element.lowercased() }
.joined()
}
}

0 comments on commit 116204f

Please sign in to comment.