Skip to content

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

Description

@mrkison

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions