|
| 1 | +//===------------------------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftTencentSCFRuntime open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2021 stevapple and the SwiftTencentSCFRuntime project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftTencentSCFRuntime project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===------------------------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import Gzip |
| 16 | +import struct Foundation.Data |
| 17 | +import struct Foundation.Date |
| 18 | + |
| 19 | +// https://intl.cloud.tencent.com/document/product/583/38845 |
| 20 | + |
| 21 | +import class Foundation.JSONDecoder |
| 22 | + |
| 23 | +public enum CLS { |
| 24 | + public struct Logs: Decodable { |
| 25 | + public let topicId: String |
| 26 | + public let topicName: String |
| 27 | + public let records: [CLS.Record] |
| 28 | + |
| 29 | + public init(from decoder: Decoder) throws { |
| 30 | + let rawContainer = try decoder.singleValueContainer() |
| 31 | + let data = try rawContainer.decode(CLS.Raw.self).data.gunzipped() |
| 32 | + |
| 33 | + let _self = try CLS.jsonDecoder.decode(CLS._Logs.self, from: data) |
| 34 | + self.topicId = _self.topicId |
| 35 | + self.topicName = _self.topicName |
| 36 | + self.records = _self.records |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public struct Record: Decodable { |
| 41 | + public let timestamp: UInt |
| 42 | + public let content: String |
| 43 | + |
| 44 | + public var date: Date { |
| 45 | + Date(timeIntervalSince1970: Double(timestamp) / 1000) |
| 46 | + } |
| 47 | + |
| 48 | + enum CodingKeys: String, CodingKey { |
| 49 | + case timestamp |
| 50 | + case content |
| 51 | + } |
| 52 | + |
| 53 | + public init(from decoder: Decoder) throws { |
| 54 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 55 | + let timestring = try container.decode(String.self, forKey: .timestamp) |
| 56 | + guard let timestamp = UInt(timestring) else { |
| 57 | + throw DecodingError.dataCorruptedError(forKey: .timestamp, in: container, debugDescription: "Expected timestamp to be unsigned numbers") |
| 58 | + } |
| 59 | + self.timestamp = timestamp |
| 60 | + self.content = try container.decode(String.self, forKey: .content) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + internal static let jsonDecoder = JSONDecoder() |
| 65 | +} |
0 commit comments