Skip to content
This repository has been archived by the owner on Feb 2, 2022. It is now read-only.

Commit

Permalink
Add support for Bool inference
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakob Mygind committed Mar 10, 2020
1 parent 9d86aed commit 5c72c96
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 32 deletions.
67 changes: 35 additions & 32 deletions Model Boiler/Classes/Helpers/CodableGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

6 changes: 6 additions & 0 deletions ModelBoilerTests/ModelBoilerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class ModelBoilerTests: XCTestCase {
var intVal = 1
var doubleVal = 2.33
var stringVal = "Hello"
var boolVal = true
}
"""

Expand All @@ -123,22 +124,27 @@ 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)
try container.encode(custom2, forKey: .custom2)
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)
custom2 = try container.decode([CustomType].self, forKey: .custom2)
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())
Expand Down

0 comments on commit 5c72c96

Please sign in to comment.