Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

希望能添加一个Codable默认值的转模型 #38

Open
mrkison opened this issue Jul 16, 2022 · 2 comments
Open

希望能添加一个Codable默认值的转模型 #38

mrkison opened this issue Jul 16, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@mrkison
Copy link

mrkison commented Jul 16, 2022

`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设置默认值

@vvkeep
Copy link
Owner

vvkeep commented Jul 20, 2022

看了你写的文章,非常👍🏻,建议可以吧 @default 和 .defaultValue 在设置页面写成可配置项,欢迎提交一个pr!

@vvkeep vvkeep added the enhancement New feature or request label Jul 22, 2022
@zonggexu
Copy link

这个默认值功能添加了嘛大佬 ^V^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants