@@ -60,7 +60,7 @@ extension DIDCore.DIDDocument {
60
60
assertionMethod: nil ,
61
61
capabilityDelegation: nil ,
62
62
keyAgreement: keyAgreementIds. map { . stringValue( $0) } ,
63
- services: services
63
+ services: services. map { $0 . toAnyCodable ( ) }
64
64
)
65
65
}
66
66
@@ -98,15 +98,16 @@ extension DIDCore.DIDDocument {
98
98
}
99
99
100
100
let services = try self . services? . map {
101
- guard
102
- let endpoint = $0. serviceEndpoint. value as? [ String : Any ] ,
101
+ let service = try DIDCore . DIDDocument. Service ( from: $0)
102
+ guard
103
+ let endpoint = service. serviceEndpoint. value as? [ String : Any ] ,
103
104
let uri = endpoint [ " uri " ] as? String
104
105
else {
105
- throw CastorError . notPossibleToResolveDID ( did: $0 . id, reason: " Invalid service " )
106
+ throw CastorError . notPossibleToResolveDID ( did: service . id, reason: " Invalid service " )
106
107
}
107
108
return Domain . DIDDocument. Service (
108
- id: $0 . id,
109
- type: [ $0 . type] ,
109
+ id: service . id,
110
+ type: [ service . type] ,
110
111
serviceEndpoint: [
111
112
. init(
112
113
uri: uri,
@@ -184,3 +185,50 @@ extension DIDCore.DIDDocument.VerificationMethod {
184
185
}
185
186
}
186
187
}
188
+
189
+ extension DIDCore . DIDDocument . Service {
190
+ init ( from: AnyCodable ) throws {
191
+ guard
192
+ let dic = from. value as? [ String : Any ] ,
193
+ let id = dic [ " id " ] as? String ,
194
+ let type = dic [ " type " ] as? String ,
195
+ let serviceEndpoint = dic [ " serviceEndpoint " ]
196
+ else { throw CommonError . invalidCoding ( message: " Could not decode service " ) }
197
+ switch serviceEndpoint {
198
+ case let value as AnyCodable :
199
+ self = . init(
200
+ id: id,
201
+ type: type,
202
+ serviceEndpoint: value
203
+ )
204
+ case let value as String :
205
+ self = . init(
206
+ id: id,
207
+ type: type,
208
+ serviceEndpoint: AnyCodable ( value)
209
+ )
210
+ case let value as [ String : Any ] :
211
+ self = . init(
212
+ id: id,
213
+ type: type,
214
+ serviceEndpoint: AnyCodable ( value)
215
+ )
216
+ case let value as [ String ] :
217
+ self = . init(
218
+ id: id,
219
+ type: type,
220
+ serviceEndpoint: AnyCodable ( value)
221
+ )
222
+ default :
223
+ throw CommonError . invalidCoding ( message: " Could not decode service " )
224
+ }
225
+ }
226
+
227
+ func toAnyCodable( ) -> AnyCodable {
228
+ AnyCodable ( dictionaryLiteral:
229
+ ( " id " , self . id) ,
230
+ ( " type " , self . type) ,
231
+ ( " serviceEndpoint " , self . serviceEndpoint. value)
232
+ )
233
+ }
234
+ }
0 commit comments