Skip to content

Commit a219b40

Browse files
committed
Coalesce type conversion failures
1 parent a6e8e4e commit a219b40

File tree

1 file changed

+5
-15
lines changed

1 file changed

+5
-15
lines changed

FirebaseAI/Sources/Types/Public/Generable/Generable.swift

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,9 @@ extension Int: Generable {
100100
}
101101

102102
public init(_ content: ModelOutput) throws {
103-
guard case let .number(value) = content.kind else {
103+
guard case let .number(value) = content.kind, let integer = Int(exactly: value) else {
104104
throw Self.decodingFailure(content)
105105
}
106-
// TODO: Determine the correct error to throw.
107-
guard let integer = Int(exactly: value) else {
108-
fatalError("Expected an integer but found \(value)")
109-
}
110106
self = integer
111107
}
112108

@@ -122,13 +118,10 @@ extension Float: Generable {
122118
}
123119

124120
public init(_ content: ModelOutput) throws {
125-
guard case let .number(value) = content.kind else {
121+
// TODO: Determine if we need to use `exactly: ` or be more lenient.
122+
guard case let .number(value) = content.kind, let float = Float(exactly: value) else {
126123
throw Self.decodingFailure(content)
127124
}
128-
// TODO: Determine the correct error to throw.
129-
guard let float = Float(exactly: value) else {
130-
fatalError("Expected a float but found \(value)")
131-
}
132125
self = float
133126
}
134127

@@ -144,13 +137,10 @@ extension Double: Generable {
144137
}
145138

146139
public init(_ content: ModelOutput) throws {
147-
guard case let .number(value) = content.kind else {
140+
// TODO: Determine if we need to use `exactly: ` or be more lenient.
141+
guard case let .number(value) = content.kind, let double = Double(exactly: value) else {
148142
throw Self.decodingFailure(content)
149143
}
150-
// TODO: Determine the correct error to throw.
151-
guard let double = Double(exactly: value) else {
152-
fatalError("Expected a double but found \(value)")
153-
}
154144
self = double
155145
}
156146

0 commit comments

Comments
 (0)