-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[Vertex AI] Add Citation.publicationDate
#13893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+401
−2
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8f3df6b
[Vertex AI] Add `Citation.publicationDate`
andrewheard 431e794
Add date conversion error handling and more tests
andrewheard 686f56b
Add more ProtoData checks and tests
andrewheard 7816cd2
Update `GenerativeModelTests` to check `publicationDate`
andrewheard 6b9969d
Add changelog entry
andrewheard a388d9f
Add documentation and switch to Gregorian calendar
andrewheard 29a1d74
Add expected ranges to year, month, day decoding errors
andrewheard 5dc2cb3
Refactor to use `DateComponents` in public API instead of `Date`
andrewheard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
FirebaseVertexAI/Sources/Types/Internal/ProtoDate.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import Foundation | ||
|
||
/// Represents a whole or partial calendar date, such as a birthday. | ||
/// | ||
/// The time of day and time zone are either specified elsewhere or are insignificant. The date is | ||
/// relative to the Gregorian Calendar. This can represent one of the following: | ||
/// - A full date, with non-zero year, month, and day values | ||
/// - A month and day value, with a zero year, such as an anniversary | ||
/// - A year on its own, with zero month and day values | ||
/// - A year and month value, with a zero day, such as a credit card expiration date | ||
/// | ||
/// This represents a | ||
/// [`google.type.Date`](https://cloud.google.com/vertex-ai/docs/reference/rest/Shared.Types/Date). | ||
struct ProtoDate { | ||
/// Year of the date. | ||
/// | ||
/// Must be from 1 to 9999, or 0 to specify a date without a year. | ||
let year: Int? | ||
|
||
/// Month of a year. | ||
/// | ||
/// Must be from 1 to 12, or 0 to specify a year without a month and day. | ||
let month: Int? | ||
|
||
/// Day of a month. | ||
/// | ||
/// Must be from 1 to 31 and valid for the year and month, or 0 to specify a year by itself or a | ||
/// year and month where the day isn't significant. | ||
let day: Int? | ||
|
||
/// Returns the a `DateComponents` representation of the `ProtoDate`. | ||
/// | ||
/// > Note: This uses the Gregorian `Calendar` to match the `google.type.Date` definition. | ||
var dateComponents: DateComponents { | ||
DateComponents( | ||
calendar: Calendar(identifier: .gregorian), | ||
year: year, | ||
month: month, | ||
day: day | ||
) | ||
} | ||
|
||
/// Returns a `Date` representation of the `ProtoDate`. | ||
/// | ||
/// - Throws: An error of type `DateConversionError` if the `ProtoDate` cannot be represented as | ||
/// a `Date`. | ||
func asDate() throws -> Date { | ||
guard year != nil else { | ||
throw DateConversionError(message: "Missing a year: \(self)") | ||
} | ||
guard month != nil else { | ||
throw DateConversionError(message: "Missing a month: \(self)") | ||
} | ||
guard day != nil else { | ||
throw DateConversionError(message: "Missing a day: \(self)") | ||
} | ||
guard dateComponents.isValidDate, let date = dateComponents.date else { | ||
throw DateConversionError(message: "Invalid date: \(self)") | ||
} | ||
return date | ||
} | ||
|
||
struct DateConversionError: Error { | ||
let localizedDescription: String | ||
|
||
init(message: String) { | ||
localizedDescription = message | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Codable Conformance | ||
|
||
extension ProtoDate: Decodable { | ||
enum CodingKeys: CodingKey { | ||
case year | ||
case month | ||
case day | ||
} | ||
|
||
init(from decoder: any Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
if let year = try container.decodeIfPresent(Int.self, forKey: .year), year != 0 { | ||
guard year >= 1 && year <= 9999 else { | ||
throw DecodingError.dataCorrupted( | ||
.init(codingPath: [CodingKeys.year], debugDescription: "Invalid year: \(year)") | ||
) | ||
} | ||
self.year = year | ||
} else { | ||
year = nil | ||
} | ||
|
||
if let month = try container.decodeIfPresent(Int.self, forKey: .month), month != 0 { | ||
guard month >= 1 && month <= 12 else { | ||
throw DecodingError.dataCorrupted( | ||
.init(codingPath: [CodingKeys.month], debugDescription: "Invalid month: \(month)") | ||
) | ||
} | ||
self.month = month | ||
} else { | ||
month = nil | ||
} | ||
|
||
if let day = try container.decodeIfPresent(Int.self, forKey: .day), day != 0 { | ||
guard day >= 1 && day <= 31 else { | ||
andrewheard marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
throw DecodingError.dataCorrupted( | ||
.init(codingPath: [CodingKeys.day], debugDescription: "Invalid day: \(day)") | ||
) | ||
} | ||
self.day = day | ||
} else { | ||
day = nil | ||
} | ||
|
||
guard year != nil || month != nil || day != nil else { | ||
throw DecodingError.dataCorrupted(.init( | ||
codingPath: [CodingKeys.year, CodingKeys.month, CodingKeys.day], | ||
debugDescription: "Invalid date: missing year, month and day" | ||
)) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.