Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
vvkeep committed Jun 28, 2021
2 parents be41e9d + da1e19e commit 201edf2
Show file tree
Hide file tree
Showing 103 changed files with 622 additions and 468 deletions.
820 changes: 412 additions & 408 deletions JSONConverter.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import Foundation

private let FILE_CACHE_CONFIG_KEY = "FILE_CACHE_CONFIG_KEY"

class FileConfigManager {
class FileConfigBuilder {

private lazy var fileConfigDic: [String: String]? = {
let dic = UserDefaults.standard.object(forKey: FILE_CACHE_CONFIG_KEY) as? [String: String]
return dic
}()

static let shared: FileConfigManager = {
let manager = FileConfigManager()
static let shared: FileConfigBuilder = {
let manager = FileConfigBuilder()
return manager
}()

Expand Down
112 changes: 112 additions & 0 deletions JSONConverter/Classes/Builder/JSONBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//
// JSONParseManager.swift
// Test
//
// Created by Yao on 2018/2/3.
// Copyright © 2018年 Yao. All rights reserved.
//

import Foundation

class JSONBuilder {

static let shared: JSONBuilder = {
let manager = JSONBuilder()
return manager
}()

private var file: File!

func buildWithJSONObject(_ obj: Any, file: File) -> (String, String?) {
file.contents.removeAll()
self.file = file
var content : Content?
let propertyKey = file.rootName.propertyName()

switch obj {
case let dic as [String: Any]:
content = addDictionaryWithKeyName(propertyKey, dic: dic)
case let arr as [Any]:
_ = addArraryWithKeyName(propertyKey, valueArrary: arr)
default:
assertionFailure("parse object type error")
}

if let content = content {
file.contents.insert(content, at: 0)
}

return file.toString()
}


private func addDictionaryWithKeyName(_ keyName: String, dic: [String: Any]) -> Content {
let content = file.contentWithKeyName(keyName)

dic.forEach { (item) in
let keyName = item.key
var property: Property?

switch item.value {
case _ as String:
property = file.propertyWithKeyName(keyName, type: .String)
case let num as NSNumber:
property = file.propertyWithKeyName(keyName, type: num.valueType())
case let dic as [String: Any]:
property = file.propertyWithKeyName(keyName, type: .Dictionary)
let content = addDictionaryWithKeyName(keyName, dic: dic)
file.contents.insert(content, at: 0)
case let arr as [Any]:
property = addArraryWithKeyName(keyName, valueArrary: arr)
case _ as NSNull:
property = file.propertyWithKeyName(keyName, type: .nil)
default:
assertionFailure("build JSON object type error")
}

if let propertyModel = property {
content.properties.append(propertyModel)
}
}

return content
}

private func addArraryWithKeyName(_ keyName: String, valueArrary: [Any]) -> Property? {
var item = valueArrary.first
if valueArrary.first is Dictionary<String, Any> {
var temp = [String: Any]()
valueArrary.forEach { temp.merge($0 as! [String: Any]) { $1 } }
item = temp
}

if let item = item {
var propertyModel: Property?
switch item {
case _ as String:
propertyModel = file.propertyWithKeyName(keyName, type: .ArrayString)
case let num as NSNumber:
let type = PropertyType(rawValue: num.valueType().rawValue + 6)!
propertyModel = file.propertyWithKeyName(keyName, type: type)
case let dic as [String: Any]:
propertyModel = file.propertyWithKeyName(keyName, type: .ArrayDictionary)
let content = addDictionaryWithKeyName(keyName, dic: dic)
file.contents.insert(content, at: 0)
default:
assertionFailure("build JSON object type error")
break
}

return propertyModel
}else {
return nil
}
}
}







Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

import Foundation

class JSONParseManager {
class JSONBuilder {

static let shared: JSONParseManager = {
let manager = JSONParseManager()
static let shared: JSONBuilder = {
let manager = JSONBuilder()
return manager
}()

Expand All @@ -25,9 +25,9 @@ class JSONParseManager {

switch obj {
case let dic as [String: Any]:
content = handleDictionary(propertyKey: propertyKey, dic: dic)
content = addDictionaryWithKeyName(propertyKey, dic: dic)
case let arr as [Any]:
_ = handleArrary(itemKey: propertyKey, arr: arr)
_ = addArraryWithKeyName(propertyKey, valueArrary: arr)
default:
assertionFailure("parse object type error")
}
Expand All @@ -40,26 +40,26 @@ class JSONParseManager {
}


private func handleDictionary(propertyKey: String, dic: [String: Any]) -> Content {
let content = file.content(withPropertyKey: propertyKey)
private func addDictionaryWithKeyName(_ keyName: String, dic: [String: Any]) -> Content {
let content = file.contentWithKeyName(keyName)

dic.forEach { (item) in
let itemKey = item.key
var propertyModel: Property?

switch item.value {
case _ as String:
propertyModel = file.property(withPropertykey: itemKey, type: .String)
propertyModel = file.propertyWithKeyName(itemKey, type: .String)
case let num as NSNumber:
propertyModel = file.property(withPropertykey: itemKey, type: num.valueType())
propertyModel = file.propertyWithKeyName(itemKey, type: num.valueType())
case let dic as [String: Any]:
propertyModel = file.property(withPropertykey: itemKey, type: .Dictionary)
let content = handleDictionary(propertyKey: itemKey, dic: dic)
propertyModel = file.propertyWithKeyName(itemKey, type: .Dictionary)
let content = addDictionaryWithKeyName(itemKey, dic: dic)
file.contents.insert(content, at: 0)
case let arr as [Any]:
propertyModel = handleArrary(itemKey: itemKey, arr: arr)
propertyModel = addArraryWithKeyName(itemKey, valueArrary: arr)
case _ as NSNull:
propertyModel = file.property(withPropertykey: itemKey, type: .nil)
propertyModel = file.propertyWithKeyName(itemKey, type: .nil)
default:
assertionFailure("parse object type error")
}
Expand All @@ -72,18 +72,22 @@ class JSONParseManager {
return content
}

private func handleArrary(itemKey: String, arr: [Any]) -> Property? {
if let first = arr.first {
private func addArraryWithKeyName(_ keyName: String, valueArrary: [Any]) -> Property? {
if valueArrary.count == 0 {
return nil
}

if let first = valueArrary.first {
var propertyModel: Property?
switch first {
case _ as String:
propertyModel = file.property(withPropertykey: itemKey, type: .ArrayString)
propertyModel = file.propertyWithKeyName(keyName, type: .ArrayString)
case let num as NSNumber:
let type = PropertyType(rawValue: num.valueType().rawValue + 6)!
propertyModel = file.property(withPropertykey: itemKey, type: type)
propertyModel = file.propertyWithKeyName(keyName, type: type)
case let dic as [String: Any]:
propertyModel = file.property(withPropertykey: itemKey, type: .ArrayDictionary)
let content = handleDictionary(propertyKey: itemKey, dic: dic)
propertyModel = file.propertyWithKeyName(keyName, type: .ArrayDictionary)
let content = addDictionaryWithKeyName(keyName, dic: dic)
file.contents.insert(content, at: 0)
default:
assertionFailure("parse object type error")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class MainViewController: NSViewController {
}

private func setupCacheConfig() {
let configFile = FileConfigManager.shared.currentConfigFile()
let configFile = FileConfigBuilder.shared.currentConfigFile()
languageBox.selectItem(at: configFile.langStruct.langType.rawValue)
structureBox.selectItem(at: configFile.langStruct.structType.rawValue)
if let themeIndex = highlightr.availableThemes().firstIndex(where: {configFile.theme == $0}) {
Expand Down Expand Up @@ -169,7 +169,7 @@ class MainViewController: NSViewController {
}

func exportClassesFileWithPath(_ path : String) {
let configFile = FileConfigManager.shared.currentConfigFile()
let configFile = FileConfigBuilder.shared.currentConfigFile()
let classfilePath = "\(path)/\(configFile.rootName.className(withPrefix: configFile.prefix))"
let suffix = configFile.classSuffixString()

Expand Down Expand Up @@ -216,8 +216,8 @@ class MainViewController: NSViewController {
let JSONData = try? JSONSerialization.data(withJSONObject: JSONObject, options: [.sortedKeys, .prettyPrinted]),
let JSONString = String(data: JSONData, encoding: .utf8) {

let configFile = FileConfigManager.shared.currentConfigFile()
let fileString = JSONParseManager.shared.parseJSONObject(JSONObject, file:configFile)
let configFile = FileConfigBuilder.shared.currentConfigFile()
let fileString = JSONBuilder.shared.buildWithJSONObject(JSONObject, file:configFile)
let endTime1 = CFAbsoluteTimeGetCurrent()
let offsetTime1 = Int((endTime1 - startTime) * 1000)

Expand Down Expand Up @@ -265,7 +265,7 @@ class MainViewController: NSViewController {
}

private func updateCacheConfigAndUI() {
let configFile = FileConfigManager.shared.currentConfigFile()
let configFile = FileConfigBuilder.shared.currentConfigFile()
guard let langType = LangType(rawValue: languageBox.indexOfSelectedItem),
let structType = StructType(rawValue: structureBox.indexOfSelectedItem)
else {
Expand Down Expand Up @@ -293,7 +293,7 @@ class MainViewController: NSViewController {

let theme = highlightr.availableThemes()[structureBox.indexOfSelectedItem]
configFile.theme = theme
FileConfigManager.shared.updateConfigWithFile(configFile)
FileConfigBuilder.shared.updateConfigWithFile(configFile)
generateClasses()
}

Expand All @@ -312,8 +312,8 @@ class MainViewController: NSViewController {

extension MainViewController {
@objc func applicationWillTerminateNotiAction() {
let currentConfigFile = FileConfigManager.shared.currentConfigFile()
FileConfigManager.shared.updateConfigWithFile(currentConfigFile)
let currentConfigFile = FileConfigBuilder.shared.currentConfigFile()
FileConfigBuilder.shared.updateConfigWithFile(currentConfigFile)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class SettingViewController: NSViewController {
}

private func updateCacheConfigUI() {
let configFile = FileConfigManager.shared.currentConfigFile()
let configFile = FileConfigBuilder.shared.currentConfigFile()
prefixField.stringValue = configFile.prefix ?? ""
rootClassField.stringValue = configFile.rootName
parentClassField.stringValue = configFile.parentName ?? ""
Expand All @@ -65,23 +65,23 @@ class SettingViewController: NSViewController {
}

@IBAction func saveConfigAction(_ sender: NSButton) {
let configFile = FileConfigManager.shared.currentConfigFile()
let configFile = FileConfigBuilder.shared.currentConfigFile()
configFile.prefix = prefixField.stringValue
configFile.rootName = rootClassField.stringValue
configFile.parentName = parentClassField.stringValue
configFile.header = headerField.stringValue
configFile.isCustomHeader = customHeaderSwitch.state.rawValue == 1
configFile.autoCaseUnderline = autoHumpSwitch.state.rawValue == 1
FileConfigManager.shared.updateConfigWithFile(configFile)
FileConfigBuilder.shared.updateConfigWithFile(configFile)
fileConfigChangedClosure?()
dismiss(nil)
}

@IBAction func customFileHeaderSwitch(_ sender: NSSwitch) {
let configFile = FileConfigManager.shared.currentConfigFile()
let configFile = FileConfigBuilder.shared.currentConfigFile()
configFile.isCustomHeader = customHeaderSwitch.state.rawValue == 1
configFile.autoCaseUnderline = autoHumpSwitch.state.rawValue == 1
FileConfigManager.shared.updateConfigWithFile(configFile)
FileConfigBuilder.shared.updateConfigWithFile(configFile)
updateCacheConfigUI()
}
}
34 changes: 18 additions & 16 deletions JSONConverter/Classes/Model/File.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ class File {
}
}

func content(withPropertyKey key: String) -> Content {
let content = Content(propertyKey: key, langStruct: langStruct, parentClsName: parentName, prefixStr: prefix, autoCaseUnderline: autoCaseUnderline)
func contentWithKeyName(_ keyName: String) -> Content {
let content = Content(propertyKey: keyName, langStruct: langStruct, parentClsName: parentName, prefixStr: prefix, autoCaseUnderline: autoCaseUnderline)
return content
}

func property(withPropertykey key: String, type: PropertyType) -> Property {
let property = Property(propertyKey: key, type: type, langStruct: langStruct, prefixStr: prefix, autoCaseUnderline: autoCaseUnderline)
func propertyWithKeyName(_ keyName: String, type: PropertyType) -> Property {
let property = Property(propertyKey: keyName, type: type, langStruct: langStruct, prefixStr: prefix, autoCaseUnderline: autoCaseUnderline)
return property
}

Expand All @@ -72,6 +72,20 @@ class File {
"structType": "\(langStruct.structType.rawValue)", "theme": theme]
}

func classSuffixString() -> (String, String?) {
switch langStruct.langType {
case .Swift, .HandyJSON, .SwiftyJSON, .ObjectMapper, .Codable:
return ( "swift", nil)
case .ObjC:
return ("h", "m")
case .Flutter:
return ("dart", nil)
}
}
}

extension File {

private func defaultHeaderString(suffix: String) -> String {
let headerString = """
//
Expand Down Expand Up @@ -154,16 +168,4 @@ class File {
return nil
}
}


func classSuffixString() -> (String, String?) {
switch langStruct.langType {
case .Swift, .HandyJSON, .SwiftyJSON, .ObjectMapper, .Codable:
return ( "swift", nil)
case .ObjC:
return ("h", "m")
case .Flutter:
return ("dart", nil)
}
}
}
24 changes: 24 additions & 0 deletions JSONConverter/Vendor/Highlightr/Assets/Highlighter/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Copyright (c) 2006, Ivan Sagalaev
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of highlight.js nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Loading

0 comments on commit 201edf2

Please sign in to comment.