JSONConverter
JSONConverter copied to clipboard
希望能添加一个Codable默认值的转模型
`import Foundation
class DefaultCodableBuilder: BuilderProtocol { func isMatchLang(_ lang: LangType) -> Bool { return lang == .DefualtCodable }
func propertyText(_ type: PropertyType, keyName: String, strategy: PropertyStrategy, maxKeyNameLength: Int, typeName: String?) -> String {
assert(!((type == .Dictionary || type == .ArrayDictionary) && typeName == nil), " Dictionary type the typeName can not be nil")
let tempKeyName = strategy.processed(keyName)
var str = ""
switch type {
case .String, .Null:
str = "\t<String> var \(tempKeyName): String\n"
case .Int:
str = "\t<Int> var \(tempKeyName): Int\n"
case .Float:
str = "\t<Float> var \(tempKeyName): Float\n"
case .Double:
str = "\t<Double> var \(tempKeyName): Double\n"
case .Bool:
str = "\t<Bool> var \(tempKeyName): Bool\n"
case .Dictionary:
str = "\t<\(typeName!)> var \(tempKeyName): \(typeName!)\n"
case .ArrayString, .ArrayNull:
str = "\t<Array> var \(tempKeyName): [String]\n"
case .ArrayInt:
str = "\t<Array> var \(tempKeyName): [Int]\n"
case .ArrayFloat:
str = "\t<Array> var \(tempKeyName): [Float]\n"
case .ArrayDouble:
str = "\t<Array> var \(tempKeyName): [Double]\n"
case .ArrayBool:
str = "\t<Array> var \(tempKeyName): [Bool]\n"
case .ArrayDictionary:
str = "\t<Array> var \(tempKeyName): [\(typeName!)]\n"
}
str.insert(contentsOf: "@Default", at: str.index(str.startIndex, offsetBy: 1))
str.insert(contentsOf: " = .defaultValue", at: str.index(str.endIndex, offsetBy: -1))
return str
}
func contentParentClassText(_ clsText: String?) -> String {
return StringUtils.isEmpty(clsText) ? ": Codable" : ": \(clsText!)"
}
func contentText(_ structType: StructType, clsName: String, parentClsName: String, propertiesText: inout String, propertiesInitText: inout String?, propertiesGetterSetterText: inout String?) -> String {
if structType == .class {
return "\nclass \(clsName)\(parentClsName), DefaultValue {\n\(propertiesText)\n\tstatic let defaultValue = \(clsName)()\n\trequired init() {}\n}\n"
} else {
propertiesText.removeLastChar()
return "\nstruct \(clsName)\(parentClsName), DefaultValue {\n\(propertiesText)\n\tstatic let defaultValue = \(clsName)(\(propertiesInitText!.substring(from: 2)))\n}\n"
}
}
func fileExtension() -> String {
return "swift"
}
func fileImportText(_ rootName: String, contents: [Content], strategy: PropertyStrategy, prefix: String?) -> String {
return"\nimport Foundation\n"
}
}` Codable设置默认值
看了你写的文章,非常👍🏻,建议可以吧 @Default 和 .defaultValue 在设置页面写成可配置项,欢迎提交一个pr!
这个默认值功能添加了嘛大佬 ^V^