1+ import Runtime
2+
13public func map( from value: Any ? ) throws -> Map {
24 guard let value = value else {
35 return . null
@@ -10,13 +12,14 @@ public func map(from value: Any?) throws -> Map {
1012 if let mapFallibleRepresentable = value as? MapFallibleRepresentable {
1113 return try mapFallibleRepresentable. asMap ( )
1214 }
13-
14- let props = try properties ( value)
15+
16+ let info = try typeInfo ( of: type ( of: value) )
17+ let props = info. properties
1518
1619 var dictionary = [ String: Map] ( minimumCapacity: props. count)
1720
1821 for property in props {
19- dictionary [ property. key ] = try map ( from: property. value)
22+ dictionary [ property. name ] = try map ( from: property. get ( from : value) )
2023 }
2124
2225 return . dictionary( dictionary)
@@ -31,20 +34,22 @@ public func assertMappable(_ type: Any.Type) throws {
3134 return
3235 }
3336
34- for property in try properties ( type) {
37+ let info = try typeInfo ( of: type)
38+ for property in info. properties {
3539 try assertMappable ( property. type)
3640 }
3741}
3842
3943extension MapFallibleRepresentable {
4044 public func asMap( ) throws -> Map {
41- let props = try properties ( self )
45+ let info = try typeInfo ( of: type ( of: self ) )
46+ let props = info. properties
4247 var dictionary = [ String: Map] ( minimumCapacity: props. count)
4348 for property in props {
44- guard let representable = property. value as? MapFallibleRepresentable else {
45- throw MapError . notMapRepresentable ( type ( of : property. value ) )
49+ guard let representable = try property. get ( from : self ) as? MapFallibleRepresentable else {
50+ throw MapError . notMapRepresentable ( property. type )
4651 }
47- dictionary [ property. key ] = try representable. asMap ( )
52+ dictionary [ property. name ] = try representable. asMap ( )
4853 }
4954 return . dictionary( dictionary)
5055 }
0 commit comments