|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import Foundation |
| 16 | + |
| 17 | +/// Represents a whole or partial calendar date, such as a birthday. |
| 18 | +/// |
| 19 | +/// The time of day and time zone are either specified elsewhere or are insignificant. The date is |
| 20 | +/// relative to the Gregorian Calendar. This can represent one of the following: |
| 21 | +/// - A full date, with non-zero year, month, and day values |
| 22 | +/// - A month and day value, with a zero year, such as an anniversary |
| 23 | +/// - A year on its own, with zero month and day values |
| 24 | +/// - A year and month value, with a zero day, such as a credit card expiration date |
| 25 | +/// |
| 26 | +/// This represents a |
| 27 | +/// [`google.type.Date`](https://cloud.google.com/vertex-ai/docs/reference/rest/Shared.Types/Date). |
| 28 | +struct ProtoDate { |
| 29 | + /// Year of the date. |
| 30 | + /// |
| 31 | + /// Must be from 1 to 9999, or 0 to specify a date without a year. |
| 32 | + let year: Int? |
| 33 | + |
| 34 | + /// Month of a year. |
| 35 | + /// |
| 36 | + /// Must be from 1 to 12, or 0 to specify a year without a month and day. |
| 37 | + let month: Int? |
| 38 | + |
| 39 | + /// Day of a month. |
| 40 | + /// |
| 41 | + /// Must be from 1 to 31 and valid for the year and month, or 0 to specify a year by itself or a |
| 42 | + /// year and month where the day isn't significant. |
| 43 | + let day: Int? |
| 44 | + |
| 45 | + /// Returns the a `DateComponents` representation of the `ProtoDate`. |
| 46 | + /// |
| 47 | + /// > Note: This uses the Gregorian `Calendar` to match the `google.type.Date` definition. |
| 48 | + var dateComponents: DateComponents { |
| 49 | + DateComponents( |
| 50 | + calendar: Calendar(identifier: .gregorian), |
| 51 | + year: year, |
| 52 | + month: month, |
| 53 | + day: day |
| 54 | + ) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// MARK: - Codable Conformance |
| 59 | + |
| 60 | +extension ProtoDate: Decodable { |
| 61 | + enum CodingKeys: CodingKey { |
| 62 | + case year |
| 63 | + case month |
| 64 | + case day |
| 65 | + } |
| 66 | + |
| 67 | + init(from decoder: any Decoder) throws { |
| 68 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 69 | + if let year = try container.decodeIfPresent(Int.self, forKey: .year), year != 0 { |
| 70 | + if year < 0 || year > 9999 { |
| 71 | + VertexLog.warning( |
| 72 | + code: .decodedInvalidProtoDateYear, |
| 73 | + """ |
| 74 | + Invalid year: \(year); must be from 1 to 9999, or 0 for a date without a specified year. |
| 75 | + """ |
| 76 | + ) |
| 77 | + } |
| 78 | + self.year = year |
| 79 | + } else { |
| 80 | + year = nil |
| 81 | + } |
| 82 | + |
| 83 | + if let month = try container.decodeIfPresent(Int.self, forKey: .month), month != 0 { |
| 84 | + if month < 0 || month > 12 { |
| 85 | + VertexLog.warning( |
| 86 | + code: .decodedInvalidProtoDateMonth, |
| 87 | + """ |
| 88 | + Invalid month: \(month); must be from 1 to 12, or 0 for a year date without a specified \ |
| 89 | + month and day. |
| 90 | + """ |
| 91 | + ) |
| 92 | + } |
| 93 | + self.month = month |
| 94 | + } else { |
| 95 | + month = nil |
| 96 | + } |
| 97 | + |
| 98 | + if let day = try container.decodeIfPresent(Int.self, forKey: .day), day != 0 { |
| 99 | + if day < 0 || day > 31 { |
| 100 | + VertexLog.warning( |
| 101 | + code: .decodedInvalidProtoDateDay, |
| 102 | + "Invalid day: \(day); must be from 1 to 31, or 0 for a date without a specified day." |
| 103 | + ) |
| 104 | + } |
| 105 | + self.day = day |
| 106 | + } else { |
| 107 | + day = nil |
| 108 | + } |
| 109 | + |
| 110 | + guard year != nil || month != nil || day != nil else { |
| 111 | + throw DecodingError.dataCorrupted(.init( |
| 112 | + codingPath: [CodingKeys.year, CodingKeys.month, CodingKeys.day], |
| 113 | + debugDescription: "Invalid date: missing year, month and day" |
| 114 | + )) |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments