Files
firebase-ios-sdk/FirebaseAI/Sources/TemplateInput.swift
T
4f36a1cc7b [AI] Server Prompt Templates (#15402)
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Co-authored-by: Andrew Heard <andrewheard@google.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
2025-10-31 16:48:46 -07:00

67 lines
2.0 KiB
Swift

// Copyright 2025 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
enum TemplateInput: Encodable, Sendable {
case string(String)
case int(Int)
case double(Double)
case bool(Bool)
case array([TemplateInput])
case dictionary([String: TemplateInput])
init(value: Any) throws {
switch value {
case let value as String:
self = .string(value)
case let value as Int:
self = .int(value)
case let value as Double:
self = .double(value)
case let value as Float:
self = .double(Double(value))
case let value as Bool:
self = .bool(value)
case let value as [Any]:
self = try .array(value.map { try TemplateInput(value: $0) })
case let value as [String: Any]:
self = try .dictionary(value.mapValues { try TemplateInput(value: $0) })
default:
throw EncodingError.invalidValue(
value,
EncodingError.Context(codingPath: [], debugDescription: "Invalid value")
)
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case let .string(value):
try container.encode(value)
case let .int(value):
try container.encode(value)
case let .double(value):
try container.encode(value)
case let .bool(value):
try container.encode(value)
case let .array(value):
try container.encode(value)
case let .dictionary(value):
try container.encode(value)
}
}
}