From 5c72c96493ad61179b1d0ac01bc0e463df0a1d33 Mon Sep 17 00:00:00 2001 From: Jakob Mygind Date: Tue, 10 Mar 2020 13:47:00 +0100 Subject: [PATCH] Add support for Bool inference --- .../Classes/Helpers/CodableGenerator.swift | 67 ++++++++++--------- ModelBoilerTests/ModelBoilerTests.swift | 6 ++ 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/Model Boiler/Classes/Helpers/CodableGenerator.swift b/Model Boiler/Classes/Helpers/CodableGenerator.swift index 9d9ffcd..3670c9b 100644 --- a/Model Boiler/Classes/Helpers/CodableGenerator.swift +++ b/Model Boiler/Classes/Helpers/CodableGenerator.swift @@ -45,37 +45,40 @@ class Generator { codingKeys.append(" case \(name) = \"\(name)\"") } - func generate() throws -> String { - var collector = DeclarationCollector() - let tree = try SyntaxParser.parse(source: source) - tree.walk(&collector) - - - for v in collector.variables { - let typeString: String - - switch (v.typeAnnotation, v.initializedValue) { - case (.some(let type), _): - typeString = type - case (_, .some(let value)) where value.contains("\""): - typeString = "String" - case (_, .some(let value)) where value.contains(".") && Double(value) != nil: - typeString = "Double" - case (_, .some(let value)) where Int(value) != nil: - typeString = "Int" - case (_, .some(let value)) where Set(value.unicodeScalars).contains(where: CharacterSet.init(charactersIn: "()").contains): - typeString = value.replacingOccurrences(of: "()", with: "") - default: throw NSError(domain: "dk.nodes.modelboiler", code: 1, userInfo: ["error": "Could not generate type for \(v)"]) - - } - addNode(name: v.name, type: typeString, isOptional: typeString.contains("?")) - } - - encode.append("}\n") - initStrings.append("}") - codingKeys.append("}\n") - - return codingKeys.joined(separator: "\n") + encode.joined(separator: "\n") + initStrings.joined(separator: "\n") - } + /// Generation is based on dumb pattern matchint + func generate() throws -> String { + var collector = DeclarationCollector() + let tree = try SyntaxParser.parse(source: source) + tree.walk(&collector) + + + for v in collector.variables { + let typeString: String + + switch (v.typeAnnotation, v.initializedValue) { + case (.some(let type), _): + typeString = type + case (_, .some(let value)) where value.contains("\""): + typeString = "String" + case (_, .some(let value)) where value.contains(".") && Double(value) != nil: + typeString = "Double" + case (_, .some(let value)) where Int(value) != nil: + typeString = "Int" + case (_, .some(let value)) where Bool(value) != nil: + typeString = "Bool" + case (_, .some(let value)) where Set(value.unicodeScalars).contains(where: CharacterSet.init(charactersIn: "()").contains): + typeString = value.replacingOccurrences(of: "()", with: "") + default: throw NSError(domain: "dk.nodes.modelboiler", code: 1, userInfo: ["error": "Could not generate type for \(v)"]) + + } + addNode(name: v.name, type: typeString, isOptional: typeString.contains("?")) + } + + encode.append("}\n\n") + initStrings.append("}") + codingKeys.append("}\n\n") + + return codingKeys.joined(separator: "\n") + encode.joined(separator: "\n") + initStrings.joined(separator: "\n") + } } diff --git a/ModelBoilerTests/ModelBoilerTests.swift b/ModelBoilerTests/ModelBoilerTests.swift index 5f25e64..f5f930d 100644 --- a/ModelBoilerTests/ModelBoilerTests.swift +++ b/ModelBoilerTests/ModelBoilerTests.swift @@ -113,6 +113,7 @@ class ModelBoilerTests: XCTestCase { var intVal = 1 var doubleVal = 2.33 var stringVal = "Hello" + var boolVal = true } """ @@ -123,7 +124,9 @@ class ModelBoilerTests: XCTestCase { case intVal = "intVal" case doubleVal = "doubleVal" case stringVal = "stringVal" + case boolVal = "boolVal" } + public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(custom, forKey: .custom) @@ -131,7 +134,9 @@ class ModelBoilerTests: XCTestCase { try container.encode(intVal, forKey: .intVal) try container.encode(doubleVal, forKey: .doubleVal) try container.encode(stringVal, forKey: .stringVal) + try container.encode(boolVal, forKey: .boolVal) } + public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) custom = try container.decode(CustomType.self, forKey: .custom) @@ -139,6 +144,7 @@ class ModelBoilerTests: XCTestCase { intVal = try container.decode(Int.self, forKey: .intVal) doubleVal = try container.decode(Double.self, forKey: .doubleVal) stringVal = try container.decode(String.self, forKey: .stringVal) + boolVal = try container.decode(Bool.self, forKey: .boolVal) } """ let res = try XCTUnwrap(try Generator(source: str).generate())