diff --git a/FirebaseVertexAI/CHANGELOG.md b/FirebaseVertexAI/CHANGELOG.md index 3a194a4a8b4..3aa08503f17 100644 --- a/FirebaseVertexAI/CHANGELOG.md +++ b/FirebaseVertexAI/CHANGELOG.md @@ -11,9 +11,9 @@ renamed to `inlineData`; no functionality changes. (#13700) - [changed] **Breaking Change**: The property `citationSources` of `CitationMetadata` has been renamed to `citations`. (#13702) -- [changed] **Breaking Change**: The constructor for `Schema` is now deprecated; +- [changed] **Breaking Change**: The constructor for `Schema` is now internal; use the new static methods `Schema.string(...)`, `Schema.object(...)`, etc., - instead. (#13616) + instead. (#13852) - [changed] **Breaking Change**: The constructor for `FunctionDeclaration` now accepts an array of *optional* parameters instead of a list of *required* parameters; if a parameter is not listed as optional it is assumed to be diff --git a/FirebaseVertexAI/Sources/Types/Internal/DataType.swift b/FirebaseVertexAI/Sources/Types/Internal/DataType.swift new file mode 100644 index 00000000000..f995eacddf2 --- /dev/null +++ b/FirebaseVertexAI/Sources/Types/Internal/DataType.swift @@ -0,0 +1,40 @@ +// 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. + +/// A data type. +/// +/// Contains the set of OpenAPI [data types](https://spec.openapis.org/oas/v3.0.3#data-types). +enum DataType: String { + /// A `String` type. + case string = "STRING" + + /// A floating-point number type. + case number = "NUMBER" + + /// An integer type. + case integer = "INTEGER" + + /// A boolean type. + case boolean = "BOOLEAN" + + /// An array type. + case array = "ARRAY" + + /// An object type. + case object = "OBJECT" +} + +// MARK: - Codable Conformance + +extension DataType: Encodable {} diff --git a/FirebaseVertexAI/Sources/Schema.swift b/FirebaseVertexAI/Sources/Types/Public/Schema.swift similarity index 72% rename from FirebaseVertexAI/Sources/Schema.swift rename to FirebaseVertexAI/Sources/Types/Public/Schema.swift index dc435626b05..0ff5b32ba47 100644 --- a/FirebaseVertexAI/Sources/Schema.swift +++ b/FirebaseVertexAI/Sources/Types/Public/Schema.swift @@ -35,8 +35,10 @@ public class Schema { case custom(String) } + let dataType: DataType + /// The data type. - public let type: DataType + public var type: String { dataType.rawValue } /// The format of the data. public let format: String? @@ -47,58 +49,22 @@ public class Schema { /// Indicates if the value may be null. public let nullable: Bool? - /// Possible values of the element of type ``DataType/string`` with "enum" format. + /// Possible values of the element of type "STRING" with "enum" format. public let enumValues: [String]? - /// Schema of the elements of type ``DataType/array``. + /// Schema of the elements of type `"ARRAY"`. public let items: Schema? - /// Properties of type ``DataType/object``. + /// Properties of type `"OBJECT"`. public let properties: [String: Schema]? - /// Required properties of type ``DataType/object``. + /// Required properties of type `"OBJECT"`. public let requiredProperties: [String]? - /// Constructs a new `Schema`. - /// - /// - Parameters: - /// - type: The data type. - /// - format: The format of the data; used only for primitive datatypes. - /// Supported formats: - /// - ``DataType/integer``: int32, int64 - /// - ``DataType/number``: float, double - /// - ``DataType/string``: enum - /// - description: A brief description of the parameter; may be formatted as Markdown. - /// - nullable: Indicates if the value may be null. - /// - enumValues: Possible values of the element of type ``DataType/string`` with "enum" format. - /// For example, an enum `Direction` may be defined as `["EAST", NORTH", "SOUTH", "WEST"]`. - /// - items: Schema of the elements of type ``DataType/array``. - /// - properties: Properties of type ``DataType/object``. - /// - requiredProperties: Required properties of type ``DataType/object``. - @available(*, deprecated, message: """ - Use static methods `string(description:format:nullable:)`, `number(description:format:nullable:)`, - etc., instead. - """) - public convenience init(type: DataType, format: String? = nil, description: String? = nil, - nullable: Bool? = nil, enumValues: [String]? = nil, items: Schema? = nil, - properties: [String: Schema]? = nil, - requiredProperties: [String]? = nil) { - self.init( - type: type, - format: format, - description: description, - nullable: nullable ?? false, - enumValues: enumValues, - items: items, - properties: properties, - requiredProperties: requiredProperties - ) - } - required init(type: DataType, format: String? = nil, description: String? = nil, nullable: Bool = false, enumValues: [String]? = nil, items: Schema? = nil, properties: [String: Schema]? = nil, requiredProperties: [String]? = nil) { - self.type = type + dataType = type self.format = format self.description = description self.nullable = nullable @@ -110,8 +76,8 @@ public class Schema { /// Returns a `Schema` representing a string value. /// - /// This schema instructs the model to produce data of type ``DataType/string``, which is suitable - /// for decoding into a Swift `String` (or `String?`, if `nullable` is set to `true`). + /// This schema instructs the model to produce data of type `"STRING"`, which is suitable for + /// decoding into a Swift `String` (or `String?`, if `nullable` is set to `true`). /// /// > Tip: If a specific set of string values should be generated by the model (for example, /// > "north", "south", "east", or "west"), use ``enumeration(values:description:nullable:)`` @@ -139,9 +105,9 @@ public class Schema { /// Returns a `Schema` representing an enumeration of string values. /// - /// This schema instructs the model to produce data of type ``DataType/string`` with the - /// `format` `"enum"`. This data is suitable for decoding into a Swift `String` (or `String?`, - /// if `nullable` is set to `true`), or an `enum` with strings as raw values. + /// This schema instructs the model to produce data of type `"STRING"` with the `format` `"enum"`. + /// This data is suitable for decoding into a Swift `String` (or `String?`, if `nullable` is set + /// to `true`), or an `enum` with strings as raw values. /// /// **Example:** /// The values `["north", "south", "east", "west"]` for an enumeration of directions. @@ -171,9 +137,9 @@ public class Schema { /// Returns a `Schema` representing a single-precision floating-point number. /// - /// This schema instructs the model to produce data of type ``DataType/number`` with the - /// `format` `"float"`, which is suitable for decoding into a Swift `Float` (or `Float?`, if - /// `nullable` is set to `true`). + /// This schema instructs the model to produce data of type `"NUMBER"` with the `format` + /// `"float"`, which is suitable for decoding into a Swift `Float` (or `Float?`, if `nullable` is + /// set to `true`). /// /// > Important: This `Schema` provides a hint to the model that it should generate a /// > single-precision floating-point number, a `float`, but only guarantees that the value will @@ -195,9 +161,9 @@ public class Schema { /// Returns a `Schema` representing a double-precision floating-point number. /// - /// This schema instructs the model to produce data of type ``DataType/number`` with the - /// `format` `"double"`, which is suitable for decoding into a Swift `Double` (or `Double?`, if - /// `nullable` is set to `true`). + /// This schema instructs the model to produce data of type `"NUMBER"` with the `format` + /// `"double"`, which is suitable for decoding into a Swift `Double` (or `Double?`, if `nullable` + /// is set to `true`). /// /// > Important: This `Schema` provides a hint to the model that it should generate a /// > double-precision floating-point number, a `double`, but only guarantees that the value will @@ -219,9 +185,9 @@ public class Schema { /// Returns a `Schema` representing an integer value. /// - /// This schema instructs the model to produce data of type ``DataType/integer``, which is - /// suitable for decoding into a Swift `Int` (or `Int?`, if `nullable` is set to `true`) or other - /// integer types (such as `Int32`) based on the expected size of values being generated. + /// This schema instructs the model to produce data of type `"INTEGER"`, which is suitable for + /// decoding into a Swift `Int` (or `Int?`, if `nullable` is set to `true`) or other integer types + /// (such as `Int32`) based on the expected size of values being generated. /// /// > Important: If a `format` of ``IntegerFormat/int32`` or ``IntegerFormat/int64`` is /// > specified, this provides a hint to the model that it should generate 32-bit or 64-bit @@ -249,8 +215,8 @@ public class Schema { /// Returns a `Schema` representing a boolean value. /// - /// This schema instructs the model to produce data of type ``DataType/boolean``, which is - /// suitable for decoding into a Swift `Bool` (or `Bool?`, if `nullable` is set to `true`). + /// This schema instructs the model to produce data of type `"BOOLEAN"`, which is suitable for + /// decoding into a Swift `Bool` (or `Bool?`, if `nullable` is set to `true`). /// /// - Parameters: /// - description: An optional description of what the boolean should contain or represent; may @@ -263,10 +229,10 @@ public class Schema { /// Returns a `Schema` representing an array. /// - /// This schema instructs the model to produce data of type ``DataType/array``, which has elements - /// of any other ``DataType`` (including nested ``DataType/array``s). This data is suitable for - /// decoding into many Swift collection types, including `Array`, holding elements of types - /// suitable for decoding from the respective `items` type. + /// This schema instructs the model to produce data of type `"ARRAY"`, which has elements of any + /// other data type (including nested `"ARRAY"`s). This data is suitable for decoding into many + /// Swift collection types, including `Array`, holding elements of types suitable for decoding + /// from the respective `items` type. /// /// - Parameters: /// - items: The `Schema` of the elements that the array will hold. @@ -281,10 +247,10 @@ public class Schema { /// Returns a `Schema` representing an object. /// - /// This schema instructs the model to produce data of type ``DataType/object``, which has keys - /// of type ``DataType/string`` and values of any other ``DataType`` (including nested - /// ``DataType/object``s). This data is suitable for decoding into Swift keyed collection types, - /// including `Dictionary`, or other custom `struct` or `class` types. + /// This schema instructs the model to produce data of type `"OBJECT"`, which has keys of type + /// `"STRING"` and values of any other data type (including nested `"OBJECT"`s). This data is + /// suitable for decoding into Swift keyed collection types, including `Dictionary`, or other + /// custom `struct` or `class` types. /// /// **Example:** A `City` could be represented with the following object `Schema`. /// ``` @@ -331,34 +297,11 @@ public class Schema { } } -/// A data type. -/// -/// Contains the set of OpenAPI [data types](https://spec.openapis.org/oas/v3.0.3#data-types). -public enum DataType: String { - /// A `String` type. - case string = "STRING" - - /// A floating-point number type. - case number = "NUMBER" - - /// An integer type. - case integer = "INTEGER" - - /// A boolean type. - case boolean = "BOOLEAN" - - /// An array type. - case array = "ARRAY" - - /// An object type. - case object = "OBJECT" -} - // MARK: - Codable Conformance extension Schema: Encodable { enum CodingKeys: String, CodingKey { - case type + case dataType = "type" case format case description case nullable @@ -369,8 +312,6 @@ extension Schema: Encodable { } } -extension DataType: Encodable {} - // MARK: - RawRepresentable Conformance extension Schema.IntegerFormat: RawRepresentable {