Skip to content

Commit

Permalink
adjust code
Browse files Browse the repository at this point in the history
  • Loading branch information
vvkeep committed May 8, 2022
1 parent a069359 commit 213296d
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 35 deletions.
2 changes: 1 addition & 1 deletion JSONConverter/Classes/Builder/BuilderProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protocol BuilderProtocol {
func propertySetterText(_ type: PropertyType, keyName: String, strategy: PropertyStrategy, keyTypeName: String?) -> String

func contentParentClassText(_ clsText: String?) -> String
func contentText(_ structType: StructType, clsName: String, parentClsName: String, propertiesText: inout String, propertiesInitText: inout String?, propertiesGetterSetterText: inout String?) -> String
func contentText(_ structType: StructType, clsName: String, parentClsName: String, propertiesText: String, propertiesInitText: String?, propertiesGetterSetterText: String?) -> String

func fileSuffix() -> String
func fileImplSuffix() -> String
Expand Down
6 changes: 3 additions & 3 deletions JSONConverter/Classes/Builder/CodableBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ class CodableBuilder: BuilderProtocol {
return StringUtils.isEmpty(clsText) ? ": Codable" : ": \(clsText!)"
}

func contentText(_ structType: StructType, clsName: String, parentClsName: String, propertiesText: inout String, propertiesInitText: inout String?, propertiesGetterSetterText: inout String?) -> String {
func contentText(_ structType: StructType, clsName: String, parentClsName: String, propertiesText: String, propertiesInitText: String?, propertiesGetterSetterText: String?) -> String {
if structType == .class {
return "\nclass \(clsName)\(parentClsName) {\n\(propertiesText)\n\trequired init() {}\n}\n"
} else {
propertiesText.removeLastChar()
return "\nstruct \(clsName)\(parentClsName) {\n\(propertiesText)\n}\n"
let tempPropertiesText = StringUtils.removeLastChar(propertiesText)
return "\nstruct \(clsName)\(parentClsName) {\n\(tempPropertiesText)\n}\n"
}
}

Expand Down
2 changes: 1 addition & 1 deletion JSONConverter/Classes/Builder/FlutterBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class FlutterBuilder: BuilderProtocol {
return StringUtils.isEmpty(clsText) ? " extends Object" : " extends \(clsText!)"
}

func contentText(_ structType: StructType, clsName: String, parentClsName: String, propertiesText: inout String, propertiesInitText: inout String?, propertiesGetterSetterText: inout String?) -> String {
func contentText(_ structType: StructType, clsName: String, parentClsName: String, propertiesText: String, propertiesInitText: String?, propertiesGetterSetterText: String?) -> String {
return "\n@JsonSerializable()\nclass \(clsName)\(parentClsName) {\n\(propertiesText)\n\t\(clsName)(\(propertiesInitText!));\n\n\tfactory \(clsName).fromJson(Map<String, dynamic> srcJson) => _$\(clsName)FromJson(srcJson);\n\n\tMap<String, dynamic> toJson() => _$\(clsName)ToJson(this);\n\n}\n"
}

Expand Down
6 changes: 3 additions & 3 deletions JSONConverter/Classes/Builder/GolangBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ class GolangBuilder: BuilderProtocol {
return StringUtils.isEmpty(clsText) ? "" : ": \(clsText!)"
}

func contentText(_ structType: StructType, clsName: String, parentClsName: String, propertiesText: inout String, propertiesInitText: inout String?, propertiesGetterSetterText: inout String?) -> String {
propertiesText.removeLastChar()
return "\ntype \(clsName) struct {\n\(propertiesText)\n}\n"
func contentText(_ structType: StructType, clsName: String, parentClsName: String, propertiesText: String, propertiesInitText: String?, propertiesGetterSetterText: String?) -> String {
let tempPropertiesText = StringUtils.removeLastChar(propertiesText)
return "\ntype \(clsName) struct {\n\(tempPropertiesText)\n}\n"
}

func fileSuffix() -> String {
Expand Down
6 changes: 3 additions & 3 deletions JSONConverter/Classes/Builder/HandyJSONBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ class HandyJSONBuilder: BuilderProtocol {
return StringUtils.isEmpty(clsText) ? ": HandyJSON" : ": \(clsText!)"
}

func contentText(_ structType: StructType, clsName: String, parentClsName: String, propertiesText: inout String, propertiesInitText: inout String?, propertiesGetterSetterText: inout String?) -> String {
func contentText(_ structType: StructType, clsName: String, parentClsName: String, propertiesText: String, propertiesInitText: String?, propertiesGetterSetterText: String?) -> String {
if structType == .class {
return "\nclass \(clsName)\(parentClsName) {\n\(propertiesText)\n\trequired init() {}\n}\n"
} else {
propertiesText.removeLastChar()
return "\nstruct \(clsName)\(parentClsName) {\n\(propertiesText)\n}\n"
let tempPropertiesText = StringUtils.removeLastChar(propertiesText)
return "\nstruct \(clsName)\(parentClsName) {\n\(tempPropertiesText)\n}\n"
}
}

Expand Down
13 changes: 8 additions & 5 deletions JSONConverter/Classes/Builder/JavaBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,20 @@ class JavaBuilder: BuilderProtocol {
}
}

func contentText(_ structType: StructType, clsName: String, parentClsName: String, propertiesText: inout String, propertiesInitText: inout String?, propertiesGetterSetterText: inout String?) -> String {
func contentText(_ structType: StructType, clsName: String, parentClsName: String, propertiesText: String, propertiesInitText: String?, propertiesGetterSetterText: String?) -> String {
assert(propertiesGetterSetterText != nil, "property getter setter text can't be nil")
if let getterSetterText = propertiesGetterSetterText, StringUtils.isNotEmpty(getterSetterText) {
let range = getterSetterText.index(getterSetterText.endIndex, offsetBy: -2)..<getterSetterText.endIndex
propertiesGetterSetterText?.removeSubrange(range)

var getterSetterText = propertiesGetterSetterText ?? ""
if var tempGetterSetterText = propertiesGetterSetterText, StringUtils.isNotEmpty(tempGetterSetterText) {
let range = tempGetterSetterText.index(tempGetterSetterText.endIndex, offsetBy: -2)..<tempGetterSetterText.endIndex
tempGetterSetterText.removeSubrange(range)
getterSetterText = tempGetterSetterText
}

return """
\npublic class \(clsName) \(parentClsName) {
\(propertiesText)
\(propertiesGetterSetterText!)
\(getterSetterText)
}\n
"""
}
Expand Down
6 changes: 3 additions & 3 deletions JSONConverter/Classes/Builder/KakaJSONBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ class KakaJSONBuilder: BuilderProtocol {
return StringUtils.isEmpty(clsText) ? ": Convertible" : ": \(clsText!)"
}

func contentText(_ structType: StructType, clsName: String, parentClsName: String, propertiesText: inout String, propertiesInitText: inout String?, propertiesGetterSetterText: inout String?) -> String {
func contentText(_ structType: StructType, clsName: String, parentClsName: String, propertiesText: String, propertiesInitText: String?, propertiesGetterSetterText: String?) -> String {
if structType == .class {
return "\nclass \(clsName)\(parentClsName) {\n\(propertiesText)\n\trequired init() {}\n}\n"
} else {
propertiesText.removeLastChar()
return "\nstruct \(clsName)\(parentClsName) {\n\(propertiesText)\n}\n"
let tempPropertiesText = StringUtils.removeLastChar(propertiesText)
return "\nstruct \(clsName)\(parentClsName) {\n\(tempPropertiesText)\n}\n"
}
}

Expand Down
6 changes: 3 additions & 3 deletions JSONConverter/Classes/Builder/ObjCBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ class ObjCBuilder: BuilderProtocol {
return StringUtils.isEmpty(clsText) ? ": NSObject" : ": \(clsText!)"
}

func contentText(_ structType: StructType, clsName: String, parentClsName: String, propertiesText: inout String, propertiesInitText: inout String?, propertiesGetterSetterText: inout String?) -> String {
propertiesText.removeLastChar()
return "\n@interface \(clsName)\(parentClsName)\n\(propertiesText)\n@end\n"
func contentText(_ structType: StructType, clsName: String, parentClsName: String, propertiesText: String, propertiesInitText: String?, propertiesGetterSetterText: String?) -> String {
let tempPropertiesText = StringUtils.removeLastChar(propertiesText)
return "\n@interface \(clsName)\(parentClsName)\n\(tempPropertiesText)\n@end\n"
}

func fileSuffix() -> String {
Expand Down
4 changes: 2 additions & 2 deletions JSONConverter/Classes/Builder/ObjectMapperBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ObjectMapperBuilder: BuilderProtocol {
}
}

func initPropertyText(_ type: PropertyType, keyName: String, strategy: PropertyStrategy, maxKeyNameLength: Int, typeName: String?) -> String {
func propertyInitText(_ type: PropertyType, keyName: String, strategy: PropertyStrategy, maxKeyNameLength: Int, keyTypeName: String?) -> String {
let tempKeyName = strategy.processed(keyName)
let spaceText = String.numSpace(count: maxKeyNameLength - tempKeyName.count)
return "\t\t\(tempKeyName)\(spaceText) <- map[\"\(keyName)\"]\n"
Expand All @@ -55,7 +55,7 @@ class ObjectMapperBuilder: BuilderProtocol {
return StringUtils.isEmpty(clsText) ? ": Mappable" : ": \(clsText!)"
}

func contentText(_ structType: StructType, clsName: String, parentClsName: String, propertiesText: inout String, propertiesInitText: inout String?, propertiesGetterSetterText: inout String?) -> String {
func contentText(_ structType: StructType, clsName: String, parentClsName: String, propertiesText: String, propertiesInitText: String?, propertiesGetterSetterText: String?) -> String {
if structType == .class {
return "\nclass \(clsName)\(parentClsName) {\n\(propertiesText)\n\trequired init?(map: Map) {}\n\n\tfunc mapping(map: Map) {\n\(propertiesInitText!)\t}\n}\n"
} else {
Expand Down
8 changes: 4 additions & 4 deletions JSONConverter/Classes/Builder/SwiftBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ class SwiftBuilder: BuilderProtocol {
return StringUtils.isEmpty(clsText) ? "" : ": \(clsText!)"
}

func contentText(_ structType: StructType, clsName: String, parentClsName: String, propertiesText: inout String, propertiesInitText: inout String?, propertiesGetterSetterText: inout String?) -> String {
propertiesText.removeLastChar()
func contentText(_ structType: StructType, clsName: String, parentClsName: String, propertiesText: String, propertiesInitText: String?, propertiesGetterSetterText: String?) -> String {
let tempPropertiesText = StringUtils.removeLastChar(propertiesText)
if structType == .class {
return "\nclass \(clsName)\(parentClsName) {\n\(propertiesText)\n}\n"
return "\nclass \(clsName)\(parentClsName) {\n\(tempPropertiesText)\n}\n"
} else {
return "\nstruct \(clsName)\(parentClsName) {\n\(propertiesText)\n}\n"
return "\nstruct \(clsName)\(parentClsName) {\n\(tempPropertiesText)\n}\n"
}
}

Expand Down
2 changes: 1 addition & 1 deletion JSONConverter/Classes/Builder/SwiftyJSONBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class SwiftyJSONBuilder: BuilderProtocol {
return StringUtils.isEmpty(clsText) ? "" : ": \(clsText!)"
}

func contentText(_ structType: StructType, clsName: String, parentClsName: String, propertiesText: inout String, propertiesInitText: inout String?, propertiesGetterSetterText: inout String?) -> String {
func contentText(_ structType: StructType, clsName: String, parentClsName: String, propertiesText: String, propertiesInitText: String?, propertiesGetterSetterText: String?) -> String {
if structType == .class {
return "\nclass \(clsName)\(parentClsName) {\n\(propertiesText)\n\tinit(json: JSON) {\n\(propertiesInitText!)\t}\n}\n"
} else {
Expand Down
14 changes: 12 additions & 2 deletions JSONConverter/Classes/Common/Utils/StringUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

class StringUtils {
class func isEmpty(_ string: String?) -> Bool {
static func isEmpty(_ string: String?) -> Bool {
if let string = string,
string.trimmingCharacters(in: .whitespacesAndNewlines).count > 0 {
return false
Expand All @@ -18,7 +18,17 @@ class StringUtils {
}
}

class func isNotEmpty(_ string: String?) -> Bool {
static func isNotEmpty(_ string: String?) -> Bool {
return !isEmpty(string)
}

static func removeLastChar(_ string: String) -> String {
var temp = string
if isNotEmpty(temp) {
let range = temp.index(temp.endIndex, offsetBy: -1)..<temp.endIndex
temp.removeSubrange(range)
}

return temp
}
}
8 changes: 4 additions & 4 deletions JSONConverter/Classes/Model/Content.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ class Content {

func toString() -> String {
properties.sort { $0 < $1 }
var propertiesText = allPropertyString()
var propertiesInitText: String? = allPropertyInitString()
let propertiesText = allPropertyString()
let propertiesInitText: String? = allPropertyInitString()
let parentClsName = builder.contentParentClassText(self.parentClsName)
var propertiesGetterSetterText: String? = allGetterSetterString()
let propertiesGetterSetterText: String? = allGetterSetterString()

let contentText = builder.contentText(langStruct.structType, clsName: className, parentClsName: parentClsName, propertiesText: &propertiesText, propertiesInitText: &propertiesInitText, propertiesGetterSetterText: &propertiesGetterSetterText)
let contentText = builder.contentText(langStruct.structType, clsName: className, parentClsName: parentClsName, propertiesText: propertiesText, propertiesInitText: propertiesInitText, propertiesGetterSetterText: propertiesGetterSetterText)
return contentText
}
}
Expand Down

0 comments on commit 213296d

Please sign in to comment.