Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 41 additions & 19 deletions Bender.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
5BCE65501C7F9BF6000603ED /* array_skip_test.json in Resources */ = {isa = PBXBuildFile; fileRef = 5BCE654F1C7F9BF6000603ED /* array_skip_test.json */; };
5BCE65551C8326EB000603ED /* path_test.json in Resources */ = {isa = PBXBuildFile; fileRef = 5BCE65541C8326EB000603ED /* path_test.json */; };
764766151D7076EC00C60F0B /* paths_through_arrays.json in Resources */ = {isa = PBXBuildFile; fileRef = 764766141D7076EC00C60F0B /* paths_through_arrays.json */; };
BE78F46621580E2400089E50 /* MapRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = BE78F46521580E2400089E50 /* MapRule.swift */; };
BE78F46821580E2400089E50 /* MapRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = BE78F46521580E2400089E50 /* MapRule.swift */; };
BE78F4702158103E00089E50 /* map_test.json in Resources */ = {isa = PBXBuildFile; fileRef = BE78F46F2158103E00089E50 /* map_test.json */; };
BE78F4712158103E00089E50 /* map_test.json in Resources */ = {isa = PBXBuildFile; fileRef = BE78F46F2158103E00089E50 /* map_test.json */; };
BE78F4722158103E00089E50 /* map_test.json in Resources */ = {isa = PBXBuildFile; fileRef = BE78F46F2158103E00089E50 /* map_test.json */; };
BE78F4732158103E00089E50 /* map_test.json in Resources */ = {isa = PBXBuildFile; fileRef = BE78F46F2158103E00089E50 /* map_test.json */; };
C141AD7F1DF17F90005698F2 /* Bender.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5B54EF001C3A90AE0072B2A8 /* Bender.framework */; };
C147F5371DF8329300899BF7 /* ArrayRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = C147F5361DF8329300899BF7 /* ArrayRule.swift */; };
C147F53A1DF834C900899BF7 /* EnumRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = C147F5391DF834C900899BF7 /* EnumRule.swift */; };
Expand Down Expand Up @@ -99,6 +105,8 @@
5BCE654F1C7F9BF6000603ED /* array_skip_test.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = array_skip_test.json; sourceTree = "<group>"; };
5BCE65541C8326EB000603ED /* path_test.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = path_test.json; sourceTree = "<group>"; };
764766141D7076EC00C60F0B /* paths_through_arrays.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = paths_through_arrays.json; sourceTree = "<group>"; };
BE78F46521580E2400089E50 /* MapRule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapRule.swift; sourceTree = "<group>"; };
BE78F46F2158103E00089E50 /* map_test.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = map_test.json; sourceTree = "<group>"; };
C147F5361DF8329300899BF7 /* ArrayRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArrayRule.swift; sourceTree = "<group>"; };
C147F5391DF834C900899BF7 /* EnumRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EnumRule.swift; sourceTree = "<group>"; };
C147F53C1DF837F200899BF7 /* JSONPath.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONPath.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -198,6 +206,7 @@
C147F5421DF83A6F00899BF7 /* RuleError.swift */,
C147F5451DF83B8A00899BF7 /* StringifiedJSONRule.swift */,
C147F5481DF83C1A00899BF7 /* TypeRule.swift */,
BE78F46521580E2400089E50 /* MapRule.swift */,
);
path = Bender;
sourceTree = "<group>";
Expand All @@ -221,6 +230,7 @@
5B3C4B3D1C7A6A070032BA13 /* defaults_test.json */,
5BCE65541C8326EB000603ED /* path_test.json */,
C1650E971DFFEED400081BFB /* five_megs.json */,
BE78F46F2158103E00089E50 /* map_test.json */,
);
path = BenderTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -326,7 +336,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0920;
LastUpgradeCheck = 0810;
LastUpgradeCheck = 1000;
ORGANIZATIONNAME = "Evgenii Kamyshanov";
TargetAttributes = {
5B54EEFF1C3A90AE0072B2A8 = {
Expand All @@ -336,11 +346,11 @@
5B54EF091C3A90AE0072B2A8 = {
CreatedOnToolsVersion = 7.2;
LastSwiftMigration = 0900;
ProvisioningStyle = Manual;
};
C1492AA81FE53ABB008ECAF8 = {
CreatedOnToolsVersion = 9.2;
DevelopmentTeam = JU6Q9E22B5;
ProvisioningStyle = Automatic;
ProvisioningStyle = Manual;
};
C1F6A6231FE5168000FE716B = {
LastSwiftMigration = 0920;
Expand Down Expand Up @@ -372,6 +382,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
BE78F4702158103E00089E50 /* map_test.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -385,6 +396,7 @@
764766151D7076EC00C60F0B /* paths_through_arrays.json in Resources */,
5BCE65501C7F9BF6000603ED /* array_skip_test.json in Resources */,
5B54EF1F1C3D39DB0072B2A8 /* array_test.json in Resources */,
BE78F4712158103E00089E50 /* map_test.json in Resources */,
5BAEB5121C47D0480044C0A7 /* recursive_test.json in Resources */,
5B54EF231C3D4AD50072B2A8 /* enum_test.json in Resources */,
5B54EF211C3D46ED0072B2A8 /* natural_array_test.json in Resources */,
Expand All @@ -404,6 +416,7 @@
C1492ABC1FE53BCA008ECAF8 /* array_skip_test.json in Resources */,
C1492ABD1FE53BCA008ECAF8 /* natural_array_test.json in Resources */,
C1492ABE1FE53BCA008ECAF8 /* enum_test.json in Resources */,
BE78F4732158103E00089E50 /* map_test.json in Resources */,
C1492ABF1FE53BCA008ECAF8 /* stringified_test.json in Resources */,
C1492AC01FE53BCA008ECAF8 /* stringified_negative_test.json in Resources */,
C1492AC11FE53BCA008ECAF8 /* recursive_test.json in Resources */,
Expand All @@ -417,6 +430,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
BE78F4722158103E00089E50 /* map_test.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -462,6 +476,7 @@
buildActionMask = 2147483647;
files = (
C147F53D1DF837F200899BF7 /* JSONPath.swift in Sources */,
BE78F46621580E2400089E50 /* MapRule.swift in Sources */,
C147F53A1DF834C900899BF7 /* EnumRule.swift in Sources */,
C147F5461DF83B8A00899BF7 /* StringifiedJSONRule.swift in Sources */,
C147F5491DF83C1A00899BF7 /* TypeRule.swift in Sources */,
Expand Down Expand Up @@ -497,6 +512,7 @@
buildActionMask = 2147483647;
files = (
C1F6A6251FE5168000FE716B /* JSONPath.swift in Sources */,
BE78F46821580E2400089E50 /* MapRule.swift in Sources */,
C1F6A6261FE5168000FE716B /* EnumRule.swift in Sources */,
C1F6A6271FE5168000FE716B /* StringifiedJSONRule.swift in Sources */,
C1F6A6281FE5168000FE716B /* TypeRule.swift in Sources */,
Expand Down Expand Up @@ -654,12 +670,12 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.kamyshanov.Bender;
PRODUCT_NAME = Bender;
SDKROOT = iphoneos11.2;
SDKROOT = iphoneos;
SKIP_INSTALL = YES;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_SWIFT3_OBJC_INFERENCE = Off;
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.2;
};
name = Debug;
};
Expand All @@ -682,17 +698,19 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.kamyshanov.Bender;
PRODUCT_NAME = Bender;
SDKROOT = iphoneos11.2;
SDKROOT = iphoneos;
SKIP_INSTALL = YES;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.2;
};
name = Release;
};
5B54EF181C3A90AE0072B2A8 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = "";
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Carthage/Build/iOS",
Expand All @@ -702,14 +720,16 @@
PRODUCT_BUNDLE_IDENTIFIER = com.kamyshanov.BenderTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos;
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.2;
};
name = Debug;
};
5B54EF191C3A90AE0072B2A8 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = "";
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Carthage/Build/iOS",
Expand All @@ -719,7 +739,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.kamyshanov.BenderTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos;
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.2;
};
name = Release;
};
Expand All @@ -738,9 +758,9 @@
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_IDENTITY = "Mac Developer";
CODE_SIGN_STYLE = Automatic;
CODE_SIGN_STYLE = Manual;
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = JU6Q9E22B5;
DEVELOPMENT_TEAM = "";
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Carthage/Build/Mac",
Expand All @@ -751,9 +771,10 @@
MACOSX_DEPLOYMENT_TARGET = 10.13;
PRODUCT_BUNDLE_IDENTIFIER = com.kamyshanov.BenderTests;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SDKROOT = macosx;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 4.2;
};
name = Debug;
};
Expand All @@ -772,9 +793,9 @@
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_IDENTITY = "Mac Developer";
CODE_SIGN_STYLE = Automatic;
CODE_SIGN_STYLE = Manual;
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = JU6Q9E22B5;
DEVELOPMENT_TEAM = "";
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Carthage/Build/Mac",
Expand All @@ -785,8 +806,9 @@
MACOSX_DEPLOYMENT_TARGET = 10.13;
PRODUCT_BUNDLE_IDENTIFIER = com.kamyshanov.BenderTests;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SDKROOT = macosx;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 4.2;
};
name = Release;
};
Expand All @@ -810,12 +832,12 @@
MACOSX_DEPLOYMENT_TARGET = 10.9;
PRODUCT_BUNDLE_IDENTIFIER = com.kamyshanov.Bender;
PRODUCT_NAME = Bender;
SDKROOT = macosx10.13;
SDKROOT = macosx;
SKIP_INSTALL = YES;
SUPPORTED_PLATFORMS = macosx;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 4.2;
VALID_ARCHS = "x86_64 i386";
};
name = Debug;
Expand All @@ -840,11 +862,11 @@
MACOSX_DEPLOYMENT_TARGET = 10.9;
PRODUCT_BUNDLE_IDENTIFIER = com.kamyshanov.Bender;
PRODUCT_NAME = Bender;
SDKROOT = macosx10.13;
SDKROOT = macosx;
SKIP_INSTALL = YES;
SUPPORTED_PLATFORMS = macosx;
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 4.2;
VALID_ARCHS = "x86_64 i386";
};
name = Release;
Expand Down
8 changes: 3 additions & 5 deletions Bender.xcodeproj/xcshareddata/xcschemes/Bender iOS.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0900"
LastUpgradeVersion = "1000"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,9 +26,8 @@
buildConfiguration = "Release"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
codeCoverageEnabled = "YES"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand Down Expand Up @@ -57,7 +56,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0920"
LastUpgradeVersion = "1000"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,7 +26,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand Down Expand Up @@ -56,7 +55,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
76 changes: 76 additions & 0 deletions Bender/MapRule.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// MapRule.swift
// Bender
//
// Created by Anton Davydov on 23.09.18.
// Original work Copyright © 2018 Evgenii Kamyshanov
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import Foundation

/**
Validator for dictionaries with unknown keys like { "<id1>": value1, "<id2>": value2 }
Output structure is array of objects. Item's order is not guarantee.
This rule provides a way to bind keys to items via 'validateKey' and 'dumpKey' closures.
*/
public class MapRule<R: Rule>: Rule {
public typealias V = [R.V]
public typealias ValidateKeyType = (inout R.V, String) throws ->Void
public typealias DumpKeyType = (R.V) throws ->String
private let itemRule: R
private let validateKey: ValidateKeyType
private let dumpKey: DumpKeyType

public init(itemRule: R, validateKey: @escaping ValidateKeyType, dumpKey: @escaping DumpKeyType) {
self.itemRule = itemRule
self.validateKey = validateKey
self.dumpKey = dumpKey
}

public func validate(_ jsonValue: AnyObject) throws -> V {
guard let dictionary = jsonValue as? [String: AnyObject] else {
throw RuleError.invalidJSONType("Cannot validate \(jsonValue): expected dictionary of items", nil)
}

var result = [R.V]()
for (key, value) in dictionary {
var item = try itemRule.validate(value)
try validateKey(&item, key)
result.append(item)
}

return result
}

public func dump(_ value: V) throws -> AnyObject {
var result = [String: AnyObject]()

for item in value {
let key = try dumpKey(item)
let value = try itemRule.dump(item)
result[key] = value
}

return result as AnyObject
}
}
Loading