Skip to content

Commit 3251bd4

Browse files
committed
Extract duplicated code
1 parent 8a01fa9 commit 3251bd4

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

FirebaseAI/Tests/TestApp/Packages/FirebaseAILogicExtended/Sources/FirebaseAILogicMacros/FirebaseGenerableMacro.swift

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -105,43 +105,41 @@ public struct FirebaseGenerableMacro: ExtensionMacro {
105105
}
106106
}
107107

108-
let isStringBacked = enumDecl.inheritanceClause?.inheritedTypes.contains {
109-
$0.type.trimmed.description == "String"
110-
} ?? false
111-
112-
let initBody: String
113-
if isStringBacked {
114-
initBody = """
115-
let rawValue = try content.value(String.self)
108+
let unexpectedValue: CodeBlockItemSyntax = """
109+
throw FirebaseAILogic.GenerativeModel.GenerationError.decodingFailure(
110+
FirebaseAILogic.GenerativeModel.GenerationError.Context(
111+
debugDescription: "Unexpected value \\"\\(rawValue)\\" for \\(Self.self)"
112+
)
113+
)
114+
"""
115+
let coreInitBody: CodeBlockItemSyntax
116+
if isStringBacked(enumDecl: enumDecl) {
117+
coreInitBody = """
116118
if let value = Self(rawValue: rawValue) {
117119
self = value
118120
} else {
119-
throw FirebaseAILogic.GenerativeModel.GenerationError.decodingFailure(
120-
FirebaseAILogic.GenerativeModel.GenerationError.Context(
121-
debugDescription: "Unexpected value \\"\\(rawValue)\\" for \\(Self.self)"
122-
)
123-
)
121+
\(unexpectedValue)
124122
}
125123
"""
126124
} else {
127125
let caseChecks = caseNames.map { caseName in
128126
"case \"\(caseName)\":\nself = .\(caseName)"
129127
}.joined(separator: "\n")
130128

131-
initBody = """
132-
let rawValue = try content.value(String.self)
129+
coreInitBody = """
133130
switch rawValue {
134-
\(caseChecks)
131+
\(raw: caseChecks)
135132
default:
136-
throw FirebaseAILogic.GenerativeModel.GenerationError.decodingFailure(
137-
FirebaseAILogic.GenerativeModel.GenerationError.Context(
138-
debugDescription: "Unexpected value \\"\\(rawValue)\\" for \\(Self.self)"
139-
)
140-
)
133+
\(unexpectedValue)
141134
}
142135
"""
143136
}
144137

138+
let initBody = """
139+
let rawValue = try content.value(String.self)
140+
\(coreInitBody)
141+
"""
142+
145143
var declarations = [ExtensionDeclSyntax]()
146144
let declSyntaxString = """
147145
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
@@ -161,6 +159,12 @@ public struct FirebaseGenerableMacro: ExtensionMacro {
161159

162160
return declarations
163161
}
162+
163+
private static func isStringBacked(enumDecl: EnumDeclSyntax) -> Bool {
164+
return enumDecl.inheritanceClause?.inheritedTypes.contains {
165+
$0.type.trimmed.description == "String"
166+
} ?? false
167+
}
164168
}
165169

166170
struct PropertyInfo {
@@ -274,16 +278,12 @@ extension FirebaseGenerableMacro: MemberMacro {
274278
}
275279
}
276280

277-
let isStringBacked = enumDecl.inheritanceClause?.inheritedTypes.contains {
278-
$0.type.trimmed.description == "String"
279-
} ?? false
280-
281281
// Find the description for the enum itself from the @FirebaseGenerable macro.
282282
let enumDescription = try getDescriptionFromGenerableMacro(node)
283283

284284
// Generate `static var jsonSchema: ...` computed property.
285285
let anyOfList: String
286-
if isStringBacked {
286+
if isStringBacked(enumDecl: enumDecl) {
287287
anyOfList = caseNames.map { "\($0).rawValue" }.joined(separator: ", ")
288288
} else {
289289
anyOfList = rawValues.map { "\"\($0)\"" }.joined(separator: ", ")
@@ -304,7 +304,7 @@ extension FirebaseGenerableMacro: MemberMacro {
304304

305305
// Generate `var modelOutput: ...` computed property.
306306
let modelOutputCode: String
307-
if isStringBacked {
307+
if isStringBacked(enumDecl: enumDecl) {
308308
modelOutputCode = """
309309
nonisolated var modelOutput: FirebaseAILogic.ModelOutput {
310310
rawValue.modelOutput

0 commit comments

Comments
 (0)