Skip to content

Commit f9faf8e

Browse files
authored
refactor(swift): refine swift api (#3554)
## Why? ## What does this PR do? ## Related issues ## AI Contribution Checklist - [ ] Substantial AI assistance was used in this PR: `yes` / `no` - [ ] If `yes`, I included a completed [AI Contribution Checklist](https://github.com/apache/fory/blob/main/AI_POLICY.md#9-contributor-checklist-for-ai-assisted-prs) in this PR description and the required `AI Usage Disclosure`. - [ ] If `yes`, my PR description includes the required `ai_review` summary and screenshot evidence of the final clean AI review results from both fresh reviewers on the current PR diff or current HEAD after the latest code changes. ## Does this PR introduce any user-facing change? - [ ] Does this PR introduce any public API change? - [ ] Does this PR introduce any binary protocol compatibility change? ## Benchmark
1 parent 4777250 commit f9faf8e

7 files changed

Lines changed: 144 additions & 152 deletions

File tree

swift/Sources/Fory/AnySerializer.swift

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,15 @@ extension Optional: OptionalTypeMarker {
117117
static var noneValue: Wrapped? { nil }
118118
}
119119

120-
struct DynamicAnyValue: Serializer {
120+
struct SerializableAny: Serializer {
121121
var value: Any = ForyAnyNullValue()
122122

123123
init(_ value: Any) {
124124
self.value = value
125125
}
126126

127-
static func foryDefault() -> DynamicAnyValue {
128-
DynamicAnyValue(ForyAnyNullValue())
127+
static func foryDefault() -> SerializableAny {
128+
SerializableAny(ForyAnyNullValue())
129129
}
130130

131131
static var staticTypeId: TypeId {
@@ -144,7 +144,7 @@ struct DynamicAnyValue: Serializer {
144144
value is ForyAnyNullValue
145145
}
146146

147-
static func wrapped(_ value: Any?) -> DynamicAnyValue {
147+
static func wrapped(_ value: Any?) -> SerializableAny {
148148
guard let value else {
149149
return .foryDefault()
150150
}
@@ -154,7 +154,7 @@ struct DynamicAnyValue: Serializer {
154154
if unwrapped is NSNull {
155155
return .foryDefault()
156156
}
157-
return DynamicAnyValue(unwrapped)
157+
return SerializableAny(unwrapped)
158158
}
159159

160160
func anyValue() -> Any? {
@@ -172,19 +172,19 @@ struct DynamicAnyValue: Serializer {
172172
try writeAnyPayload(value, context: context, hasGenerics: hasGenerics)
173173
}
174174

175-
static func foryReadData(_ context: ReadContext) throws -> DynamicAnyValue {
175+
static func foryReadData(_ context: ReadContext) throws -> SerializableAny {
176176
_ = context
177177
throw ForyError.invalidData(
178178
"dynamic Any read requires type info; foryReadData should not be called directly"
179179
)
180180
}
181181

182-
static func foryReadCompatibleData(_ context: ReadContext, remoteTypeInfo: TypeInfo) throws -> DynamicAnyValue {
182+
static func foryReadCompatibleData(_ context: ReadContext, remoteTypeInfo: TypeInfo) throws -> SerializableAny {
183183
let typeInfo = remoteTypeInfo
184184
if typeInfo.typeID == .none {
185185
return .foryDefault()
186186
}
187-
return DynamicAnyValue(try context.readAnyValue(typeInfo: typeInfo))
187+
return SerializableAny(try context.readAnyValue(typeInfo: typeInfo))
188188
}
189189

190190
static func foryWriteStaticTypeInfo(_ context: WriteContext) throws {
@@ -234,7 +234,7 @@ struct DynamicAnyValue: Serializer {
234234
_ context: ReadContext,
235235
refMode: RefMode,
236236
readTypeInfo: Bool
237-
) throws -> DynamicAnyValue {
237+
) throws -> SerializableAny {
238238
@inline(__always)
239239
func requireDynamicTypeInfo() throws -> TypeInfo {
240240
if readTypeInfo {
@@ -261,13 +261,13 @@ struct DynamicAnyValue: Serializer {
261261
case .ref:
262262
let refID = try context.buffer.readVarUInt32()
263263
let referenced = try context.refReader.readRefValue(refID)
264-
if let value = referenced as? DynamicAnyValue {
264+
if let value = referenced as? SerializableAny {
265265
return value
266266
}
267267
if referenced is NSNull {
268268
return .foryDefault()
269269
}
270-
return DynamicAnyValue(referenced)
270+
return SerializableAny(referenced)
271271
case .refValue:
272272
let reservedRefID = context.trackRef ? context.refReader.reserveRefID() : nil
273273
let remoteTypeInfo = try requireDynamicTypeInfo()
@@ -343,22 +343,22 @@ private func writeAnyPayload(_ value: Any, context: WriteContext, hasGenerics: B
343343
return
344344
}
345345
if let list = value as? [Any] {
346-
try writeAnyList(list, context: context, refMode: .none, hasGenerics: hasGenerics)
346+
try writeListOfAny(list, context: context, refMode: .none, hasGenerics: hasGenerics)
347347
return
348348
}
349349
if let map = value as? [String: Any] {
350350
// Always include key type info for dynamic map payload.
351-
try writeStringAnyMap(map, context: context, refMode: .none, hasGenerics: false)
351+
try writeMapStringToAny(map, context: context, refMode: .none, hasGenerics: false)
352352
return
353353
}
354354
if let map = value as? [Int32: Any] {
355355
// Always include key type info for dynamic map payload.
356-
try writeInt32AnyMap(map, context: context, refMode: .none, hasGenerics: false)
356+
try writeMapInt32ToAny(map, context: context, refMode: .none, hasGenerics: false)
357357
return
358358
}
359359
if let map = value as? [AnyHashable: Any] {
360360
// Always include key type info for dynamic map payload.
361-
try writeAnyHashableAnyMap(map, context: context, refMode: .none, hasGenerics: false)
361+
try writeMapAnyHashableToAny(map, context: context, refMode: .none, hasGenerics: false)
362362
return
363363
}
364364
throw ForyError.invalidData("unsupported dynamic Any runtime type \(type(of: value))")
@@ -401,7 +401,7 @@ public func writeAny(
401401
writeTypeInfo: Bool = true,
402402
hasGenerics: Bool = false
403403
) throws {
404-
try DynamicAnyValue.wrapped(value).foryWrite(
404+
try SerializableAny.wrapped(value).foryWrite(
405405
context,
406406
refMode: refMode,
407407
writeTypeInfo: writeTypeInfo,
@@ -414,17 +414,17 @@ public func readAny(
414414
refMode: RefMode,
415415
readTypeInfo: Bool = true
416416
) throws -> Any? {
417-
try DynamicAnyValue.foryRead(context, refMode: refMode, readTypeInfo: readTypeInfo).anyValue()
417+
try SerializableAny.foryRead(context, refMode: refMode, readTypeInfo: readTypeInfo).anyValue()
418418
}
419419

420-
public func writeAnyList(
420+
public func writeListOfAny(
421421
_ value: [Any]?,
422422
context: WriteContext,
423423
refMode: RefMode,
424424
writeTypeInfo: Bool = false,
425425
hasGenerics: Bool = true
426426
) throws {
427-
let wrapped = value?.map { DynamicAnyValue.wrapped($0) }
427+
let wrapped = value?.map { SerializableAny.wrapped($0) }
428428
try wrapped.foryWrite(
429429
context,
430430
refMode: refMode,
@@ -433,28 +433,28 @@ public func writeAnyList(
433433
)
434434
}
435435

436-
public func readAnyList(
436+
public func readListOfAny(
437437
context: ReadContext,
438438
refMode: RefMode,
439439
readTypeInfo: Bool = false
440440
) throws -> [Any]? {
441-
let wrapped: [DynamicAnyValue]? = try [DynamicAnyValue]?.foryRead(
441+
let wrapped: [SerializableAny]? = try [SerializableAny]?.foryRead(
442442
context,
443443
refMode: refMode,
444444
readTypeInfo: readTypeInfo
445445
)
446446
return wrapped?.map { $0.anyValueForCollection() }
447447
}
448448

449-
public func writeStringAnyMap(
449+
public func writeMapStringToAny(
450450
_ value: [String: Any]?,
451451
context: WriteContext,
452452
refMode: RefMode,
453453
writeTypeInfo: Bool = false,
454454
hasGenerics: Bool = true
455455
) throws {
456-
let wrapped = value?.reduce(into: [String: DynamicAnyValue]()) { result, pair in
457-
result[pair.key] = DynamicAnyValue.wrapped(pair.value)
456+
let wrapped = value?.reduce(into: [String: SerializableAny]()) { result, pair in
457+
result[pair.key] = SerializableAny.wrapped(pair.value)
458458
}
459459
try wrapped.foryWrite(
460460
context,
@@ -464,12 +464,12 @@ public func writeStringAnyMap(
464464
)
465465
}
466466

467-
public func readStringAnyMap(
467+
public func readMapStringToAny(
468468
context: ReadContext,
469469
refMode: RefMode,
470470
readTypeInfo: Bool = false
471471
) throws -> [String: Any]? {
472-
let wrapped: [String: DynamicAnyValue]? = try [String: DynamicAnyValue]?.foryRead(
472+
let wrapped: [String: SerializableAny]? = try [String: SerializableAny]?.foryRead(
473473
context,
474474
refMode: refMode,
475475
readTypeInfo: readTypeInfo
@@ -485,15 +485,15 @@ public func readStringAnyMap(
485485
return map
486486
}
487487

488-
public func writeInt32AnyMap(
488+
public func writeMapInt32ToAny(
489489
_ value: [Int32: Any]?,
490490
context: WriteContext,
491491
refMode: RefMode,
492492
writeTypeInfo: Bool = false,
493493
hasGenerics: Bool = true
494494
) throws {
495-
let wrapped = value?.reduce(into: [Int32: DynamicAnyValue]()) { result, pair in
496-
result[pair.key] = DynamicAnyValue.wrapped(pair.value)
495+
let wrapped = value?.reduce(into: [Int32: SerializableAny]()) { result, pair in
496+
result[pair.key] = SerializableAny.wrapped(pair.value)
497497
}
498498
try wrapped.foryWrite(
499499
context,
@@ -503,12 +503,12 @@ public func writeInt32AnyMap(
503503
)
504504
}
505505

506-
public func readInt32AnyMap(
506+
public func readMapInt32ToAny(
507507
context: ReadContext,
508508
refMode: RefMode,
509509
readTypeInfo: Bool = false
510510
) throws -> [Int32: Any]? {
511-
let wrapped: [Int32: DynamicAnyValue]? = try [Int32: DynamicAnyValue]?.foryRead(
511+
let wrapped: [Int32: SerializableAny]? = try [Int32: SerializableAny]?.foryRead(
512512
context,
513513
refMode: refMode,
514514
readTypeInfo: readTypeInfo
@@ -524,15 +524,15 @@ public func readInt32AnyMap(
524524
return map
525525
}
526526

527-
public func writeAnyHashableAnyMap(
527+
public func writeMapAnyHashableToAny(
528528
_ value: [AnyHashable: Any]?,
529529
context: WriteContext,
530530
refMode: RefMode,
531531
writeTypeInfo: Bool = false,
532532
hasGenerics: Bool = true
533533
) throws {
534-
let wrapped = value?.reduce(into: [AnyHashable: DynamicAnyValue]()) { result, pair in
535-
result[pair.key] = DynamicAnyValue.wrapped(pair.value)
534+
let wrapped = value?.reduce(into: [AnyHashable: SerializableAny]()) { result, pair in
535+
result[pair.key] = SerializableAny.wrapped(pair.value)
536536
}
537537
try wrapped.foryWrite(
538538
context,
@@ -542,12 +542,12 @@ public func writeAnyHashableAnyMap(
542542
)
543543
}
544544

545-
public func readAnyHashableAnyMap(
545+
public func readMapAnyHashableToAny(
546546
context: ReadContext,
547547
refMode: RefMode,
548548
readTypeInfo: Bool = false
549549
) throws -> [AnyHashable: Any]? {
550-
let wrapped: [AnyHashable: DynamicAnyValue]? = try [AnyHashable: DynamicAnyValue]?.foryRead(
550+
let wrapped: [AnyHashable: SerializableAny]? = try [AnyHashable: SerializableAny]?.foryRead(
551551
context,
552552
refMode: refMode,
553553
readTypeInfo: readTypeInfo
@@ -564,7 +564,7 @@ public func readAnyHashableAnyMap(
564564
}
565565

566566
func readDynamicAnyMapValue(context: ReadContext) throws -> Any {
567-
let map = try readAnyHashableAnyMap(context: context, refMode: .none) ?? [:]
567+
let map = try readMapAnyHashableToAny(context: context, refMode: .none) ?? [:]
568568
if map.isEmpty {
569569
return [String: Any]()
570570
}

0 commit comments

Comments
 (0)