Unify encoding protocols into JSONEncodable

https://github.com/yonaskolb/XcodeGen/pull/545#discussion_r271690085
This commit is contained in:
ryohey
2019-04-20 12:12:26 +09:00
parent 0889882aac
commit b27f71eac1
14 changed files with 97 additions and 105 deletions
+5 -5
View File
@@ -62,11 +62,11 @@ extension AggregateTarget: NamedJSONDictionaryConvertible {
}
}
extension AggregateTarget: JSONDictionaryEncodable {
public func toJSONDictionary() -> JSONDictionary {
extension AggregateTarget: JSONEncodable {
public func toJSONValue() -> Any {
var dict: JSONDictionary = [:]
let settingsDict = settings.toJSONDictionary()
let settingsDict = settings.toJSONValue()
if settingsDict.count > 0 {
dict["settings"] = settingsDict
}
@@ -81,10 +81,10 @@ extension AggregateTarget: JSONDictionaryEncodable {
dict["attributes"] = attributes
}
if buildScripts.count > 0 {
dict["buildScripts"] = buildScripts.map { $0.toJSONDictionary() }
dict["buildScripts"] = buildScripts.map { $0.toJSONValue() }
}
if let scheme = scheme {
dict["scheme"] = scheme.toJSONDictionary()
dict["scheme"] = scheme.toJSONValue()
}
return dict
+2 -2
View File
@@ -81,8 +81,8 @@ extension BuildRule: JSONObjectConvertible {
}
}
extension BuildRule: JSONDictionaryEncodable {
public func toJSONDictionary() -> JSONDictionary {
extension BuildRule: JSONEncodable {
public func toJSONValue() -> Any {
var dict: [String: Any] = [
"outputFiles": outputFiles,
"outputFilesCompilerFlags": outputFilesCompilerFlags,
+2 -2
View File
@@ -61,8 +61,8 @@ extension BuildScript: JSONObjectConvertible {
showEnvVars = jsonDictionary.json(atKeyPath: "showEnvVars") ?? true
}
}
extension BuildScript: JSONDictionaryEncodable {
public func toJSONDictionary() -> JSONDictionary {
extension BuildScript: JSONEncodable {
public func toJSONValue() -> Any {
var dict: JSONDictionary = [:]
if runOnlyWhenInstalling {
+2 -2
View File
@@ -81,8 +81,8 @@ extension Dependency: JSONObjectConvertible {
}
}
extension Dependency: JSONDictionaryEncodable {
public func toJSONDictionary() -> JSONDictionary {
extension Dependency: JSONEncodable {
public func toJSONValue() -> Any {
var dict: JSONDictionary = [:]
if !removeHeaders {
+2 -2
View File
@@ -79,8 +79,8 @@ extension DeploymentTarget: JSONObjectConvertible {
}
}
extension DeploymentTarget: JSONDictionaryEncodable {
public func toJSONDictionary() -> JSONDictionary {
extension DeploymentTarget: JSONEncodable {
public func toJSONValue() -> Any {
var dict: JSONDictionary = [:]
if let iOS = iOS {
+1 -13
View File
@@ -1,19 +1,7 @@
import Foundation
import JSONUtilities
public protocol JSONDictionaryEncodable {
func toJSONDictionary() -> JSONDictionary
}
public protocol JSONArrayEncodable {
func toJSONArray() -> JSONArray
}
public protocol JSONPrimitiveEncodable {
func toJSONValue() -> JSONRawType
}
public protocol JSONDynamicEncodable {
public protocol JSONEncodable {
// returns JSONDictionary or JSONArray or JSONRawType
func toJSONValue() -> Any
}
+2 -2
View File
@@ -25,8 +25,8 @@ extension Plist: JSONObjectConvertible {
}
}
extension Plist: JSONDictionaryEncodable {
public func toJSONDictionary() -> JSONDictionary {
extension Plist: JSONEncodable {
public func toJSONValue() -> Any {
return [
"path": path,
"properties": properties
+11 -7
View File
@@ -231,14 +231,18 @@ extension BuildSettingsContainer {
}
}
extension Project: JSONDictionaryEncodable {
extension Project: JSONEncodable {
public func toJSONValue() -> Any {
return toJSONDictionary()
}
public func toJSONDictionary() -> JSONDictionary {
var dict: JSONDictionary = [
"name": name,
"options": options.toJSONDictionary(),
"options": options.toJSONValue(),
]
let settingsDict = settings.toJSONDictionary()
let settingsDict = settings.toJSONValue()
if settingsDict.count > 0 {
dict["settings"] = settingsDict
}
@@ -256,7 +260,7 @@ extension Project: JSONDictionaryEncodable {
dict["attributes"] = attributes
}
let targetPairs = targets.map { ($0.name, $0.toJSONDictionary()) }
let targetPairs = targets.map { ($0.name, $0.toJSONValue()) }
if targetPairs.count > 0 {
dict["targets"] = Dictionary(uniqueKeysWithValues: targetPairs)
}
@@ -266,18 +270,18 @@ extension Project: JSONDictionaryEncodable {
dict["configs"] = Dictionary(uniqueKeysWithValues: configsPairs)
}
let aggregateTargetsPairs = aggregateTargets.map { ($0.name, $0.toJSONDictionary()) }
let aggregateTargetsPairs = aggregateTargets.map { ($0.name, $0.toJSONValue()) }
if aggregateTargetsPairs.count > 0 {
dict["aggregateTargets"] = Dictionary(uniqueKeysWithValues: aggregateTargetsPairs)
}
let schemesPairs = schemes.map { ($0.name, $0.toJSONDictionary()) }
let schemesPairs = schemes.map { ($0.name, $0.toJSONValue()) }
if schemesPairs.count > 0 {
dict["schemes"] = Dictionary(uniqueKeysWithValues: schemesPairs)
}
if settingGroups.count > 0 {
dict["settingGroups"] = settingGroups.mapValues { $0.toJSONDictionary() }
dict["settingGroups"] = settingGroups.mapValues { $0.toJSONValue() }
}
return dict
+40 -40
View File
@@ -218,8 +218,8 @@ extension Scheme.ExecutionAction: JSONObjectConvertible {
}
}
extension Scheme.ExecutionAction: JSONDictionaryEncodable {
public func toJSONDictionary() -> JSONDictionary {
extension Scheme.ExecutionAction: JSONEncodable {
public func toJSONValue() -> Any {
var dict = [
"script": script,
"name": name,
@@ -244,20 +244,20 @@ extension Scheme.Run: JSONObjectConvertible {
}
}
extension Scheme.Run: JSONDictionaryEncodable {
public func toJSONDictionary() -> JSONDictionary {
extension Scheme.Run: JSONEncodable {
public func toJSONValue() -> Any {
var dict: JSONDictionary = [
"commandLineArguments": commandLineArguments,
]
if preActions.count > 0 {
dict["preActions"] = preActions.map { $0.toJSONDictionary() }
dict["preActions"] = preActions.map { $0.toJSONValue() }
}
if postActions.count > 0 {
dict["postActions"] = postActions.map { $0.toJSONDictionary() }
dict["postActions"] = postActions.map { $0.toJSONValue() }
}
if environmentVariables.count > 0 {
dict["environmentVariables"] = environmentVariables.map { $0.toJSONDictionary() }
dict["environmentVariables"] = environmentVariables.map { $0.toJSONValue() }
}
if let config = config {
dict["config"] = config
@@ -292,8 +292,8 @@ extension Scheme.Test: JSONObjectConvertible {
}
}
extension Scheme.Test: JSONDictionaryEncodable {
public func toJSONDictionary() -> JSONDictionary {
extension Scheme.Test: JSONEncodable {
public func toJSONValue() -> Any {
var dict: JSONDictionary = [
"gatherCoverageData": gatherCoverageData,
]
@@ -305,13 +305,13 @@ extension Scheme.Test: JSONDictionaryEncodable {
dict["targets"] = targets.map { $0.toJSONValue() }
}
if preActions.count > 0 {
dict["preActions"] = preActions.map { $0.toJSONDictionary() }
dict["preActions"] = preActions.map { $0.toJSONValue() }
}
if postActions.count > 0 {
dict["postActions"] = postActions.map { $0.toJSONDictionary() }
dict["postActions"] = postActions.map { $0.toJSONValue() }
}
if environmentVariables.count > 0 {
dict["environmentVariables"] = environmentVariables.map { $0.toJSONDictionary() }
dict["environmentVariables"] = environmentVariables.map { $0.toJSONValue() }
}
if let config = config {
dict["config"] = config
@@ -330,7 +330,7 @@ extension Scheme.Test.TestTarget: JSONObjectConvertible {
}
}
extension Scheme.Test.TestTarget: JSONDynamicEncodable {
extension Scheme.Test.TestTarget: JSONEncodable {
public func toJSONValue() -> Any {
if !randomExecutionOrder && !parallelizable {
return name
@@ -362,20 +362,20 @@ extension Scheme.Profile: JSONObjectConvertible {
}
}
extension Scheme.Profile: JSONDictionaryEncodable {
public func toJSONDictionary() -> JSONDictionary {
extension Scheme.Profile: JSONEncodable {
public func toJSONValue() -> Any {
var dict: JSONDictionary = [
"commandLineArguments": commandLineArguments,
]
if preActions.count > 0 {
dict["preActions"] = preActions.map { $0.toJSONDictionary() }
dict["preActions"] = preActions.map { $0.toJSONValue() }
}
if postActions.count > 0 {
dict["postActions"] = postActions.map { $0.toJSONDictionary() }
dict["postActions"] = postActions.map { $0.toJSONValue() }
}
if environmentVariables.count > 0 {
dict["environmentVariables"] = environmentVariables.map { $0.toJSONDictionary() }
dict["environmentVariables"] = environmentVariables.map { $0.toJSONValue() }
}
if let config = config {
dict["config"] = config
@@ -392,8 +392,8 @@ extension Scheme.Analyze: JSONObjectConvertible {
}
}
extension Scheme.Analyze: JSONDictionaryEncodable {
public func toJSONDictionary() -> JSONDictionary {
extension Scheme.Analyze: JSONEncodable {
public func toJSONValue() -> Any {
var dict: JSONDictionary = [:]
if let config = config {
@@ -415,18 +415,18 @@ extension Scheme.Archive: JSONObjectConvertible {
}
}
extension Scheme.Archive: JSONDictionaryEncodable {
public func toJSONDictionary() -> JSONDictionary {
extension Scheme.Archive: JSONEncodable {
public func toJSONValue() -> Any {
var dict: JSONDictionary = [:]
if !revealArchiveInOrganizer {
dict["revealArchiveInOrganizer"] = revealArchiveInOrganizer
}
if preActions.count > 0 {
dict["preActions"] = preActions.map { $0.toJSONDictionary() }
dict["preActions"] = preActions.map { $0.toJSONValue() }
}
if postActions.count > 0 {
dict["postActions"] = postActions.map { $0.toJSONDictionary() }
dict["postActions"] = postActions.map { $0.toJSONValue() }
}
if let config = config {
dict["config"] = config
@@ -452,26 +452,26 @@ extension Scheme: NamedJSONDictionaryConvertible {
}
}
extension Scheme: JSONDictionaryEncodable {
public func toJSONDictionary() -> JSONDictionary {
extension Scheme: JSONEncodable {
public func toJSONValue() -> Any {
var dict = [
"build": build.toJSONDictionary()
"build": build.toJSONValue()
]
if let run = run {
dict["run"] = run.toJSONDictionary()
dict["run"] = run.toJSONValue()
}
if let test = test {
dict["test"] = test.toJSONDictionary()
dict["test"] = test.toJSONValue()
}
if let analyze = analyze {
dict["analyze"] = analyze.toJSONDictionary()
dict["analyze"] = analyze.toJSONValue()
}
if let profile = profile {
dict["profile"] = profile.toJSONDictionary()
dict["profile"] = profile.toJSONValue()
}
if let archive = archive {
dict["archive"] = archive.toJSONDictionary()
dict["archive"] = archive.toJSONValue()
}
return dict
@@ -510,8 +510,8 @@ extension Scheme.Build: JSONObjectConvertible {
}
}
extension Scheme.Build: JSONDictionaryEncodable {
public func toJSONDictionary() -> JSONDictionary {
extension Scheme.Build: JSONEncodable {
public func toJSONValue() -> Any {
let targetPairs = targets.map { ($0.target, $0.buildTypes.map { $0.toJSONValue() }) }
var dict: JSONDictionary = [
@@ -525,10 +525,10 @@ extension Scheme.Build: JSONDictionaryEncodable {
dict["buildImplicitDependencies"] = buildImplicitDependencies
}
if preActions.count > 0 {
dict["preActions"] = preActions.map { $0.toJSONDictionary() }
dict["preActions"] = preActions.map { $0.toJSONValue() }
}
if postActions.count > 0 {
dict["postActions"] = postActions.map { $0.toJSONDictionary() }
dict["postActions"] = postActions.map { $0.toJSONValue() }
}
return dict
@@ -555,8 +555,8 @@ extension BuildType: JSONPrimitiveConvertible {
}
}
extension BuildType: JSONPrimitiveEncodable {
public func toJSONValue() -> JSONRawType {
extension BuildType: JSONEncodable {
public func toJSONValue() -> Any {
switch self {
case .testing: return "testing"
case .profiling: return "profiling"
@@ -604,8 +604,8 @@ extension XCScheme.EnvironmentVariable: JSONObjectConvertible {
}
}
extension XCScheme.EnvironmentVariable: JSONDictionaryEncodable {
public func toJSONDictionary() -> JSONDictionary {
extension XCScheme.EnvironmentVariable: JSONEncodable {
public func toJSONValue() -> Any {
return [
"variable": variable,
"value": value,
+3 -3
View File
@@ -106,8 +106,8 @@ public func += (lhs: inout BuildSettings, rhs: BuildSettings?) {
lhs.merge(rhs)
}
extension Settings: JSONDictionaryEncodable {
public func toJSONDictionary() -> JSONDictionary {
extension Settings: JSONEncodable {
public func toJSONValue() -> Any {
if groups.count > 0 || configSettings.count > 0 {
var dict: JSONDictionary = [:]
@@ -118,7 +118,7 @@ extension Settings: JSONDictionaryEncodable {
dict["groups"] = groups
}
if configSettings.count > 0 {
dict["configs"] = configSettings.mapValues { $0.toJSONDictionary() }
dict["configs"] = configSettings.mapValues { $0.toJSONValue() }
}
return dict
+3 -3
View File
@@ -126,10 +126,10 @@ extension SpecOptions: JSONObjectConvertible {
}
}
extension SpecOptions: JSONDictionaryEncodable {
public func toJSONDictionary() -> JSONDictionary {
extension SpecOptions: JSONEncodable {
public func toJSONValue() -> Any {
var dict: JSONDictionary = [
"deploymentTarget": deploymentTarget.toJSONDictionary(),
"deploymentTarget": deploymentTarget.toJSONValue(),
"transitivelyLinkDependencies": transitivelyLinkDependencies,
"groupSortPosition": groupSortPosition.rawValue,
]
+14 -14
View File
@@ -272,8 +272,8 @@ extension LegacyTarget: JSONObjectConvertible {
}
}
extension LegacyTarget: JSONDictionaryEncodable {
public func toJSONDictionary() -> JSONDictionary {
extension LegacyTarget: JSONEncodable {
public func toJSONValue() -> Any {
var dict: JSONDictionary = [
"toolPath": toolPath,
"passSettings": passSettings,
@@ -362,14 +362,14 @@ extension Target: NamedJSONDictionaryConvertible {
}
extension Target: JSONDictionaryEncodable {
public func toJSONDictionary() -> JSONDictionary {
extension Target: JSONEncodable {
public func toJSONValue() -> Any {
var dict: JSONDictionary = [
"type": type.name,
"platform": platform.rawValue,
]
let settingsDict = settings.toJSONDictionary()
let settingsDict = settings.toJSONValue()
if settingsDict.count > 0 {
dict["settings"] = settingsDict
}
@@ -387,28 +387,28 @@ extension Target: JSONDictionaryEncodable {
dict["sources"] = sources.map { $0.toJSONValue() }
}
if dependencies.count > 0 {
dict["dependencies"] = dependencies.map { $0.toJSONDictionary() }
dict["dependencies"] = dependencies.map { $0.toJSONValue() }
}
if postCompileScripts.count > 0 {
dict["postCompileScripts"] = postCompileScripts.map{ $0.toJSONDictionary() }
dict["postCompileScripts"] = postCompileScripts.map{ $0.toJSONValue() }
}
if preBuildScripts.count > 0 {
dict["prebuildScripts"] = preBuildScripts.map{ $0.toJSONDictionary() }
dict["prebuildScripts"] = preBuildScripts.map{ $0.toJSONValue() }
}
if postBuildScripts.count > 0 {
dict["postbuildScripts"] = postBuildScripts.map{ $0.toJSONDictionary() }
dict["postbuildScripts"] = postBuildScripts.map{ $0.toJSONValue() }
}
if buildRules.count > 0 {
dict["buildRules"] = buildRules.map{ $0.toJSONDictionary() }
dict["buildRules"] = buildRules.map{ $0.toJSONValue() }
}
if let deploymentTarget = deploymentTarget {
dict["deploymentTarget"] = deploymentTarget.deploymentTarget
}
if let info = info {
dict["info"] = info.toJSONDictionary()
dict["info"] = info.toJSONValue()
}
if let entitlements = entitlements {
dict["entitlements"] = entitlements.toJSONDictionary()
dict["entitlements"] = entitlements.toJSONValue()
}
if let transitivelyLinkDependencies = transitivelyLinkDependencies {
dict["transitivelyLinkDependencies"] = transitivelyLinkDependencies
@@ -420,10 +420,10 @@ extension Target: JSONDictionaryEncodable {
dict["requiresObjCLinking"] = requiresObjCLinking
}
if let scheme = scheme {
dict["scheme"] = scheme.toJSONDictionary()
dict["scheme"] = scheme.toJSONValue()
}
if let legacy = legacy {
dict["legacy"] = legacy.toJSONDictionary()
dict["legacy"] = legacy.toJSONValue()
}
return dict
+5 -5
View File
@@ -55,8 +55,8 @@ extension TargetScheme: JSONObjectConvertible {
}
}
extension TargetScheme: JSONDictionaryEncodable {
public func toJSONDictionary() -> JSONDictionary {
extension TargetScheme: JSONEncodable {
public func toJSONValue() -> Any {
var dict: JSONDictionary = [
"gatherCoverageData": gatherCoverageData,
]
@@ -71,13 +71,13 @@ extension TargetScheme: JSONDictionaryEncodable {
dict["testTargets"] = testTargets.map { $0.toJSONValue() }
}
if environmentVariables.count > 0 {
dict["environmentVariables"] = environmentVariables.map { $0.toJSONDictionary() }
dict["environmentVariables"] = environmentVariables.map { $0.toJSONValue() }
}
if preActions.count > 0 {
dict["preActions"] = preActions.map { $0.toJSONDictionary() }
dict["preActions"] = preActions.map { $0.toJSONValue() }
}
if postActions.count > 0 {
dict["postActions"] = postActions.map { $0.toJSONDictionary() }
dict["postActions"] = postActions.map { $0.toJSONValue() }
}
return dict
+5 -5
View File
@@ -181,7 +181,7 @@ extension TargetSource: JSONObjectConvertible {
}
}
extension TargetSource: JSONDynamicEncodable {
extension TargetSource: JSONEncodable {
public func toJSONValue() -> Any {
var dict: JSONDictionary = [:]
@@ -243,13 +243,13 @@ extension TargetSource.BuildPhase: JSONObjectConvertible {
}
}
extension TargetSource.BuildPhase: JSONDynamicEncodable {
extension TargetSource.BuildPhase: JSONEncodable {
public func toJSONValue() -> Any {
switch self {
case .sources: return "sources"
case .headers: return "headers"
case .resources: return "resources"
case .copyFiles(let files): return ["copyFiles": files.toJSONDictionary()]
case .copyFiles(let files): return ["copyFiles": files.toJSONValue()]
case .none: return "none"
case .frameworks: fatalError("invalid build phase")
case .runScript: fatalError("invalid build phase")
@@ -267,8 +267,8 @@ extension TargetSource.BuildPhase.CopyFilesSettings: JSONObjectConvertible {
}
}
extension TargetSource.BuildPhase.CopyFilesSettings: JSONDictionaryEncodable {
public func toJSONDictionary() -> JSONDictionary {
extension TargetSource.BuildPhase.CopyFilesSettings: JSONEncodable {
public func toJSONValue() -> Any {
return [
"destination": destination.rawValue,
"subpath": subpath