Skip to content

Commit

Permalink
fix: improved dx
Browse files Browse the repository at this point in the history
Signed-off-by: rawnly <[email protected]>
  • Loading branch information
rawnly committed Nov 19, 2023
1 parent 098aeb7 commit 25d2a49
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Sources/OpenAI/Public/Models/Chat/ChatQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<ToolChoice, ChatTool.ToolValue>?
public let toolChoice: EnumOrCodable<ToolChoice, ChatTool.ToolValue>?

var stream: Bool = false

Expand Down Expand Up @@ -83,7 +83,7 @@ public struct ChatQuery: Equatable, Codable, Streamable {
messages: [Message],
responseFormat: ResponseFormat? = nil,
tools: [ChatTool]? = nil,
toolChoice: AnyOf<ToolChoice, ChatTool.ToolValue>? = nil,
toolChoice: EnumOrCodable<ToolChoice, ChatTool.ToolValue>? = nil,
functions: [ChatFunctionDeclaration]? = nil,
functionCall: FunctionCall? = nil,
temperature: Double? = nil,
Expand Down
22 changes: 11 additions & 11 deletions Sources/OpenAI/Public/Utilities/CodableUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ public enum StringOrCodable<T: Codable>: Equatable, Codable where T: Equatable {
}
}

/// Same as ``StringOrCodable`` but accepts 2 codable generics instead of String as first generic argument
public enum AnyOf<T: Codable, U: Codable>: Equatable, Codable where T: Equatable, U: Equatable {
case objectA(T)
case objectB(U)

public enum EnumOrCodable<E: Codable, C: Codable>: Equatable, Codable where E: Equatable, C: Equatable {
case `enum`(E)
case codable(C)

enum CodingKeys: CodingKey {
case objectA
Expand All @@ -91,9 +91,9 @@ public enum AnyOf<T: Codable, U: Codable>: 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)
}
}
Expand All @@ -102,15 +102,15 @@ public enum AnyOf<T: Codable, U: Codable>: 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<T>"
debugDescription: "Invalid data encountered when decoding EnumOrCodable<E, C>"
)
)
}
Expand Down

0 comments on commit 25d2a49

Please sign in to comment.