From 213296d6b8b2ac700b007f7d504b0eb23a0b03d4 Mon Sep 17 00:00:00 2001 From: Yao <15919688564@163.com> Date: Sun, 8 May 2022 22:48:34 +0800 Subject: [PATCH] adjust code --- .../Classes/Builder/BuilderProtocol.swift | 2 +- JSONConverter/Classes/Builder/CodableBuilder.swift | 6 +++--- JSONConverter/Classes/Builder/FlutterBuilder.swift | 2 +- JSONConverter/Classes/Builder/GolangBuilder.swift | 6 +++--- .../Classes/Builder/HandyJSONBuilder.swift | 6 +++--- JSONConverter/Classes/Builder/JavaBuilder.swift | 13 ++++++++----- .../Classes/Builder/KakaJSONBuilder.swift | 6 +++--- JSONConverter/Classes/Builder/ObjCBuilder.swift | 6 +++--- .../Classes/Builder/ObjectMapperBuilder.swift | 4 ++-- JSONConverter/Classes/Builder/SwiftBuilder.swift | 8 ++++---- .../Classes/Builder/SwiftyJSONBuilder.swift | 2 +- .../Classes/Common/Utils/StringUtils.swift | 14 ++++++++++++-- JSONConverter/Classes/Model/Content.swift | 8 ++++---- 13 files changed, 48 insertions(+), 35 deletions(-) diff --git a/JSONConverter/Classes/Builder/BuilderProtocol.swift b/JSONConverter/Classes/Builder/BuilderProtocol.swift index 9447afb..0723aa1 100644 --- a/JSONConverter/Classes/Builder/BuilderProtocol.swift +++ b/JSONConverter/Classes/Builder/BuilderProtocol.swift @@ -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 diff --git a/JSONConverter/Classes/Builder/CodableBuilder.swift b/JSONConverter/Classes/Builder/CodableBuilder.swift index a474fed..1b739c4 100644 --- a/JSONConverter/Classes/Builder/CodableBuilder.swift +++ b/JSONConverter/Classes/Builder/CodableBuilder.swift @@ -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" } } diff --git a/JSONConverter/Classes/Builder/FlutterBuilder.swift b/JSONConverter/Classes/Builder/FlutterBuilder.swift index d8c6590..b533537 100644 --- a/JSONConverter/Classes/Builder/FlutterBuilder.swift +++ b/JSONConverter/Classes/Builder/FlutterBuilder.swift @@ -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 srcJson) => _$\(clsName)FromJson(srcJson);\n\n\tMap toJson() => _$\(clsName)ToJson(this);\n\n}\n" } diff --git a/JSONConverter/Classes/Builder/GolangBuilder.swift b/JSONConverter/Classes/Builder/GolangBuilder.swift index d9dddeb..312f6d6 100644 --- a/JSONConverter/Classes/Builder/GolangBuilder.swift +++ b/JSONConverter/Classes/Builder/GolangBuilder.swift @@ -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 { diff --git a/JSONConverter/Classes/Builder/HandyJSONBuilder.swift b/JSONConverter/Classes/Builder/HandyJSONBuilder.swift index 9643bab..136866a 100644 --- a/JSONConverter/Classes/Builder/HandyJSONBuilder.swift +++ b/JSONConverter/Classes/Builder/HandyJSONBuilder.swift @@ -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" } } diff --git a/JSONConverter/Classes/Builder/JavaBuilder.swift b/JSONConverter/Classes/Builder/JavaBuilder.swift index 0982c94..76a64d1 100644 --- a/JSONConverter/Classes/Builder/JavaBuilder.swift +++ b/JSONConverter/Classes/Builder/JavaBuilder.swift @@ -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).. 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" } } diff --git a/JSONConverter/Classes/Builder/ObjCBuilder.swift b/JSONConverter/Classes/Builder/ObjCBuilder.swift index 6dcc4ab..46cd5c4 100644 --- a/JSONConverter/Classes/Builder/ObjCBuilder.swift +++ b/JSONConverter/Classes/Builder/ObjCBuilder.swift @@ -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 { diff --git a/JSONConverter/Classes/Builder/ObjectMapperBuilder.swift b/JSONConverter/Classes/Builder/ObjectMapperBuilder.swift index d42ca14..b701f9e 100644 --- a/JSONConverter/Classes/Builder/ObjectMapperBuilder.swift +++ b/JSONConverter/Classes/Builder/ObjectMapperBuilder.swift @@ -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" @@ -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 { diff --git a/JSONConverter/Classes/Builder/SwiftBuilder.swift b/JSONConverter/Classes/Builder/SwiftBuilder.swift index c5c6847..fe7686f 100644 --- a/JSONConverter/Classes/Builder/SwiftBuilder.swift +++ b/JSONConverter/Classes/Builder/SwiftBuilder.swift @@ -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" } } diff --git a/JSONConverter/Classes/Builder/SwiftyJSONBuilder.swift b/JSONConverter/Classes/Builder/SwiftyJSONBuilder.swift index ce4bfca..4068868 100644 --- a/JSONConverter/Classes/Builder/SwiftyJSONBuilder.swift +++ b/JSONConverter/Classes/Builder/SwiftyJSONBuilder.swift @@ -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 { diff --git a/JSONConverter/Classes/Common/Utils/StringUtils.swift b/JSONConverter/Classes/Common/Utils/StringUtils.swift index e974f54..30f4bdd 100644 --- a/JSONConverter/Classes/Common/Utils/StringUtils.swift +++ b/JSONConverter/Classes/Common/Utils/StringUtils.swift @@ -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 @@ -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).. 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 } }