Created
June 17, 2023 16:24
-
-
Save tifoaudii/01f31d3af1920368a3a7b0a2c36f1b5b to your computer and use it in GitHub Desktop.
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
func traverse(dictionary: [String : Any], parent: String) -> String { | |
if dictionary.keys.contains("type") { | |
let type = dictionary["type"] as! String | |
let tokenType: TokenType = TokenType(value: type) | |
switch tokenType { | |
case .color: | |
let value = dictionary["value"] as! String | |
let variableValue = "UIColor.hexColor(\"\(value)\") ?? .black" | |
return createVariable(dataType: tokenType.dataType, variableName: parent.camelCased(with: "."), value: variableValue) | |
case .typography: | |
let fontName = dictionary["fontName"] as! String | |
let size = dictionary["size"] as! String | |
let value = "UIFont(name: \"\(fontName)\", size: \(size)) ?? .systemFont(ofSize: \(size))" | |
return createVariable(dataType: tokenType.dataType, variableName: parent.camelCased(with: "."), value: value) | |
case .any: | |
return "" | |
} | |
} | |
var contentArray: [String] = [] | |
for tokenDictionary in dictionary { | |
let tokenValue: [String : Any] = tokenDictionary.value as! [String : Any] | |
let childParent: String = tokenDictionary.key.appending(".") | |
let parent: String = parent.appending(childParent) | |
contentArray.append(traverse(dictionary: tokenValue, parent: parent)) | |
} | |
return contentArray.joined(separator: "\n") | |
} | |
var tokenExtensionArray: [String] = [] | |
for tokenDictionary in dictionary { | |
let tokenValue: [String : Any] = tokenDictionary.value as! [String : Any] | |
let extensionContent = traverse(dictionary: tokenValue, parent: "") | |
tokenExtensionArray.append(createExtension(from: TokenType(value: tokenDictionary.key.lowercased()), content: extensionContent)) | |
} | |
let tokenExtension = | |
""" | |
import UIKit | |
\(tokenExtensionArray.joined(separator: "\n")) | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment