kNitroAnyMapSwiftHelper top-level constant
String
const kNitroAnyMapSwiftHelper
Swift inline encoder/decoder source, emitted by the generator into bridge .swift files.
Implementation
const String kNitroAnyMapSwiftHelper = r'''
// NitroAnyMap binary codec — generated inline by Nitrogen
private enum _AnyTag: UInt8 { case null_ = 0, bool_ = 1, int_ = 2, double_ = 3, string_ = 4, list_ = 5, object_ = 6 }
private func _writeLE32(_ v: Int32, into data: inout Data) {
var n = v.littleEndian; withUnsafeBytes(of: &n) { data.append(contentsOf: $0) }
}
private func _writeLE64(_ v: Int64, into data: inout Data) {
var n = v.littleEndian; withUnsafeBytes(of: &n) { data.append(contentsOf: $0) }
}
private func _readLE32(_ data: Data, at offset: inout Int) -> Int32 {
let v = data.withUnsafeBytes { $0.load(fromByteOffset: offset, as: Int32.self) }
offset += 4; return Int32(littleEndian: v)
}
private func _readLE64(_ data: Data, at offset: inout Int) -> Int64 {
let v = data.withUnsafeBytes { $0.load(fromByteOffset: offset, as: Int64.self) }
offset += 8; return Int64(littleEndian: v)
}
private func _writeStr(_ s: String, into data: inout Data) {
let bytes = Array(s.utf8); _writeLE32(Int32(bytes.count), into: &data); data.append(contentsOf: bytes)
}
private func _readStr(_ data: Data, at offset: inout Int) -> String {
let len = Int(_readLE32(data, at: &offset))
let s = String(bytes: data[offset..<(offset + len)], encoding: .utf8) ?? ""
offset += len; return s
}
private func _writeAnyValue(_ v: Any?, into data: inout Data) {
switch v {
case nil: data.append(_AnyTag.null_.rawValue)
case let b as Bool: data.append(_AnyTag.bool_.rawValue); data.append(b ? 1 : 0)
case let i as Int64: data.append(_AnyTag.int_.rawValue); _writeLE64(i, into: &data)
case let i as Int: data.append(_AnyTag.int_.rawValue); _writeLE64(Int64(i), into: &data)
case let d as Double: data.append(_AnyTag.double_.rawValue); _writeLE64(Int64(bitPattern: d.bitPattern), into: &data)
case let s as String: data.append(_AnyTag.string_.rawValue); _writeStr(s, into: &data)
case let arr as [Any?]: data.append(_AnyTag.list_.rawValue); _writeLE32(Int32(arr.count), into: &data); arr.forEach { _writeAnyValue($0, into: &data) }
case let obj as [String: Any?]: data.append(_AnyTag.object_.rawValue); _writeLE32(Int32(obj.count), into: &data); obj.forEach { k, vv in _writeStr(k, into: &data); _writeAnyValue(vv, into: &data) }
default: fatalError("Cannot encode \(String(describing: v)) as NitroAnyValue")
}
}
private func _readAnyValue(_ data: Data, at offset: inout Int) -> Any? {
let tag = _AnyTag(rawValue: data[offset])!; offset += 1
switch tag {
case .null_: return nil
case .bool_: let b = data[offset] != 0; offset += 1; return b
case .int_: return _readLE64(data, at: &offset)
case .double_: return Double(bitPattern: UInt64(bitPattern: _readLE64(data, at: &offset)))
case .string_: return _readStr(data, at: &offset)
case .list_: let n = Int(_readLE32(data, at: &offset)); return (0..<n).map { _ in _readAnyValue(data, at: &offset) }
case .object_: let n = Int(_readLE32(data, at: &offset)); var d = [String: Any?](); (0..<n).forEach { _ in d[_readStr(data, at: &offset)] = _readAnyValue(data, at: &offset) }; return d
}
}
private func _encodeAnyMap(_ map: [String: Any?]) -> Data {
var payload = Data()
_writeLE32(Int32(map.count), into: &payload)
map.forEach { k, v in _writeStr(k, into: &payload); _writeAnyValue(v, into: &payload) }
var result = Data(capacity: 4 + payload.count)
_writeLE32(Int32(payload.count), into: &result)
result.append(payload)
return result
}
private func _decodeAnyMap(_ data: Data) -> [String: Any?] {
// skip the outer 4-byte length prefix
var offset = 4
let count = Int(_readLE32(data, at: &offset))
var map = [String: Any?](minimumCapacity: count)
for _ in 0..<count { map[_readStr(data, at: &offset)] = _readAnyValue(data, at: &offset) }
return map
}
''';