From 25d2a49583950befd3b545bfe29125152ddd51d9 Mon Sep 17 00:00:00 2001 From: rawnly Date: Sun, 19 Nov 2023 10:06:18 +0100 Subject: [PATCH] fix: improved dx Signed-off-by: rawnly --- .../OpenAI/Public/Models/Chat/ChatQuery.swift | 4 ++-- .../Public/Utilities/CodableUtilities.swift | 22 +++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Sources/OpenAI/Public/Models/Chat/ChatQuery.swift b/Sources/OpenAI/Public/Models/Chat/ChatQuery.swift index 618db6e2..01cee052 100644 --- a/Sources/OpenAI/Public/Models/Chat/ChatQuery.swift +++ b/Sources/OpenAI/Public/Models/Chat/ChatQuery.swift @@ -53,7 +53,7 @@ public struct ChatQuery: Equatable, Codable, Streamable { /// /// `none` is default when no functions are present /// `auto` is default if functions are present - public let toolChoice: AnyOf? + public let toolChoice: EnumOrCodable? var stream: Bool = false @@ -83,7 +83,7 @@ public struct ChatQuery: Equatable, Codable, Streamable { messages: [Message], responseFormat: ResponseFormat? = nil, tools: [ChatTool]? = nil, - toolChoice: AnyOf? = nil, + toolChoice: EnumOrCodable? = nil, functions: [ChatFunctionDeclaration]? = nil, functionCall: FunctionCall? = nil, temperature: Double? = nil, diff --git a/Sources/OpenAI/Public/Utilities/CodableUtilities.swift b/Sources/OpenAI/Public/Utilities/CodableUtilities.swift index ac8a4bd5..2919b213 100644 --- a/Sources/OpenAI/Public/Utilities/CodableUtilities.swift +++ b/Sources/OpenAI/Public/Utilities/CodableUtilities.swift @@ -77,10 +77,10 @@ public enum StringOrCodable: Equatable, Codable where T: Equatable { } } -/// Same as ``StringOrCodable`` but accepts 2 codable generics instead of String as first generic argument -public enum AnyOf: Equatable, Codable where T: Equatable, U: Equatable { - case objectA(T) - case objectB(U) + +public enum EnumOrCodable: Equatable, Codable where E: Equatable, C: Equatable { + case `enum`(E) + case codable(C) enum CodingKeys: CodingKey { case objectA @@ -91,9 +91,9 @@ public enum AnyOf: Equatable, Codable where T: Equatable var container = encoder.singleValueContainer() switch self { - case .objectB(let value): + case .codable(let value): try container.encode(value) - case .objectA(let value): + case .enum(let value): try container.encode(value) } } @@ -102,15 +102,15 @@ public enum AnyOf: Equatable, Codable where T: Equatable public init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() - if let valueT = try? container.decode(T.self) { - self = .objectA(valueT) - } else if let valueU = try? container.decode(U.self) { - self = .objectB(valueU) + if let value = try? container.decode(E.self) { + self = .enum(value) + } else if let value = try? container.decode(C.self) { + self = .codable(value) } else { throw DecodingError.dataCorrupted( DecodingError.Context( codingPath: decoder.codingPath, - debugDescription: "Invalid data encountered when decoding StringOrCodable" + debugDescription: "Invalid data encountered when decoding EnumOrCodable" ) ) }