-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
753 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Foundation | ||
|
||
protocol DefaultValueGenerator { | ||
func makeDefaultValue(for varDeclaration: VarDeclaration) -> String | ||
} | ||
|
||
final class DefaultValueGeneratorImpl: DefaultValueGenerator { | ||
private let unknownDefaultType: String | ||
private let customDefaultTypes: [String: String] | ||
|
||
init(unknownDefaultType: String, customDefaultTypes: [String: String]) { | ||
self.unknownDefaultType = unknownDefaultType | ||
self.customDefaultTypes = customDefaultTypes | ||
} | ||
|
||
func makeDefaultValue(for varDeclaration: VarDeclaration) -> String { | ||
if let customValue = customDefaultTypes[varDeclaration.type.name] { | ||
return customValue | ||
} | ||
|
||
guard !varDeclaration.type.isOptional else { | ||
return "nil" | ||
} | ||
|
||
switch varDeclaration.type.name { | ||
case "String": | ||
return "\"\(varDeclaration.identifier)\"" | ||
case "Int": | ||
return "0" | ||
case "Bool": | ||
return "false" | ||
default: | ||
return unknownDefaultType | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
protocol ExtensionGenerator { | ||
func make(name: String, content: [String]) -> [String] | ||
} | ||
|
||
final class ExtensionGeneratorImpl: ExtensionGenerator { | ||
private let accessLevelGenerator: AccessLevelGenerator | ||
private let indentationGenerator: IndentationGenerator | ||
|
||
init( | ||
accessLevelGenerator: AccessLevelGenerator, | ||
indentationGenerator: IndentationGenerator | ||
) { | ||
self.accessLevelGenerator = accessLevelGenerator | ||
self.indentationGenerator = indentationGenerator | ||
} | ||
|
||
func make(name: String, content: [String]) -> [String] { | ||
let content = content.map(indentationGenerator.indenting) | ||
return [ | ||
"\(accessLevelGenerator.makeClassAccessLevel())extension \(name) {" | ||
] + content + [ | ||
"}", | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
protocol StructStubGenerator { | ||
func generate(from structType: StructDeclaration, functionName: String) -> String | ||
} | ||
|
||
final class StructStubGeneratorImpl: StructStubGenerator { | ||
private let extensionGenerator: ExtensionGenerator | ||
private let functionGenerator: FunctionGenerator | ||
private let indentationGenerator: IndentationGenerator | ||
private let defaultValueGenerator: DefaultValueGenerator | ||
|
||
init( | ||
extensionGenerator: ExtensionGenerator, | ||
functionGenerator: FunctionGenerator, | ||
indentationGenerator: IndentationGenerator, | ||
defaultValueGenerator: DefaultValueGenerator | ||
) { | ||
self.extensionGenerator = extensionGenerator | ||
self.functionGenerator = functionGenerator | ||
self.indentationGenerator = indentationGenerator | ||
self.defaultValueGenerator = defaultValueGenerator | ||
} | ||
|
||
func generate(from structType: StructDeclaration, functionName: String) -> String { | ||
let content = generateBody(from: structType, functionName: functionName) | ||
return extensionGenerator.make( | ||
name: structType.name, | ||
content: content | ||
).joined(separator: "\n") + "\n" | ||
} | ||
|
||
private func generateBody(from structType: StructDeclaration, functionName: String) -> [String] { | ||
let content = makeContent(from: structType) | ||
let functionDeclaration = FunctionDeclaration( | ||
name: functionName, | ||
arguments: structType.variables.map(makeArgument), | ||
isThrowing: false, | ||
isAsync: false, | ||
isStatic: true, | ||
returnType: TypeDeclaration(name: structType.name, isOptional: false, prefix: []) | ||
) | ||
return functionGenerator.makeCode( | ||
from: functionDeclaration, | ||
content: content, | ||
isEachArgumentOnNewLineEnabled: true | ||
) | ||
} | ||
|
||
private func makeContent(from structType: StructDeclaration) -> [String] { | ||
let variables = structType.variables.enumerated().map{ makeAssigment(of: $1, isLast: $0 == structType.variables.endIndex - 1) } | ||
|
||
guard !variables.isEmpty else { | ||
return ["return \(structType.name)()"] | ||
} | ||
|
||
return [ | ||
"return \(structType.name)(" | ||
] + variables.map(indentationGenerator.indenting) + [ | ||
")" | ||
] | ||
} | ||
|
||
private func makeAssigment(of variable: VarDeclaration, isLast: Bool) -> String { | ||
return "\(variable.identifier): \(variable.identifier)\(isLast ? "" : ",")" | ||
} | ||
|
||
private func makeArgument(from varDeclaration: VarDeclaration) -> ArgumentDeclaration { | ||
ArgumentDeclaration( | ||
name: varDeclaration.identifier, | ||
type: varDeclaration.type, | ||
defaultValue: defaultValueGenerator.makeDefaultValue(for: varDeclaration) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.