From 180706401d1177a1cfaf669c12153bf79d671e42 Mon Sep 17 00:00:00 2001 From: Anton Doudarev Date: Mon, 22 Feb 2016 09:46:20 -0800 Subject: [PATCH 1/3] Fixed model generation script, now all the import statements should be correctly generated --- Source/ModelScript/modelgen-swift.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/ModelScript/modelgen-swift.py b/Source/ModelScript/modelgen-swift.py index e74270c..ceda904 100644 --- a/Source/ModelScript/modelgen-swift.py +++ b/Source/ModelScript/modelgen-swift.py @@ -27,7 +27,7 @@ MAPPING_KEY_TRANSFORMER = "transformer" MAPPING_KEY_COLLECTION_SUBTYPE = "collection_subtype" -STRING_IMPORT_FOUNDATION = "import Foundation\n" +STRING_IMPORT_FOUNDATION = "import Foundation\nimport US2MapperKit\n" STRING_REQUIRED_INIT_START = "\n\trequired init(" STRING_MAP_VALUES_DICT_START = "\n\n\tprivate func setValues(" STRING_MAP_DICT_START = '\n\n\tfunc updateWithDictionary(dictionary: Dictionary) {\n\n\t\tlet dynamicTypeString = "\(self.dynamicType)"\n\t\tlet className = dynamicTypeString.componentsSeparatedByString(".").last\n\n\t\tif let valuesDict = US2Mapper.mapValues(from: dictionary, forType: className!, employing: US2Instantiator.sharedInstance, defaultsEnabled : false) {' From 1031d8757a7769a52fdf8bcf2a1bb7bd5835eeac Mon Sep 17 00:00:00 2001 From: Anton Doudarev Date: Wed, 24 Feb 2016 02:41:38 -0800 Subject: [PATCH 2/3] Fixed script model generation error. It appears that since all the files were part of the test target, importing the Framework into the test was causing an error. I updated the script to account for a testing target but adding the -t for test parameter to the argument list it accepts. This is only used in the test project and should not affect the any projects --- Source/ModelScript/modelgen-swift.py | 100 +++++++++++++----- .../Classes/Internal/US2Instantiator.swift | 1 + .../Classes/Internal/_TestObjectEight.swift | 4 +- .../Classes/Internal/_TestObjectEleven.swift | 8 +- .../Classes/Internal/_TestObjectFive.swift | 4 +- .../Classes/Internal/_TestObjectFour.swift | 8 +- .../Classes/Internal/_TestObjectNine.swift | 7 +- .../Classes/Internal/_TestObjectSeven.swift | 4 +- .../Classes/Internal/_TestObjectSix.swift | 4 +- .../Classes/Internal/_TestObjectTen.swift | 7 +- .../Internal/_TestObjectThirteen.swift | 4 +- .../Classes/Internal/_TestObjectThree.swift | 8 +- .../Classes/Internal/_TestObjectTwelve.swift | 8 +- .../Classes/Internal/_TestObjectTwo.swift | 4 +- .../US2ExampleClosureTransformer.swift | 2 +- .../Internal/_TestObjectEight.swift | 50 +++++++++ .../US2MapperKit.xcodeproj/project.pbxproj | 4 +- .../xcschemes/US2MapperKit - iOS.xcscheme | 11 +- 18 files changed, 148 insertions(+), 90 deletions(-) create mode 100644 US2MapperKit/US2MapperKit/Internal/_TestObjectEight.swift diff --git a/Source/ModelScript/modelgen-swift.py b/Source/ModelScript/modelgen-swift.py index ceda904..da2e601 100644 --- a/Source/ModelScript/modelgen-swift.py +++ b/Source/ModelScript/modelgen-swift.py @@ -27,7 +27,9 @@ MAPPING_KEY_TRANSFORMER = "transformer" MAPPING_KEY_COLLECTION_SUBTYPE = "collection_subtype" -STRING_IMPORT_FOUNDATION = "import Foundation\nimport US2MapperKit\n" +STRING_IMPORT_FOUNDATION = "import Foundation\nimport US2MapperKit\n" +STRING_IMPORT_FOUNDATION_TEST = "import Foundation\n" + STRING_REQUIRED_INIT_START = "\n\trequired init(" STRING_MAP_VALUES_DICT_START = "\n\n\tprivate func setValues(" STRING_MAP_DICT_START = '\n\n\tfunc updateWithDictionary(dictionary: Dictionary) {\n\n\t\tlet dynamicTypeString = "\(self.dynamicType)"\n\t\tlet className = dynamicTypeString.componentsSeparatedByString(".").last\n\n\t\tif let valuesDict = US2Mapper.mapValues(from: dictionary, forType: className!, employing: US2Instantiator.sharedInstance, defaultsEnabled : false) {' @@ -42,8 +44,8 @@ STRING_USMAPPER_IMPORT = "\n" STRING_USMAPPER_INHERITENCE = "\nclass US2Instantiator : US2InstantiatorProtocol {\n\n" -def generate_model(mappinglist, output_directory, version): - +def generate_model(mappinglist, output_directory, version, testEnabled): + for mapping in mappinglist: filename = mapping[mapping.rindex('/',0,-1)+1:-1] if mapping.endswith('/') else mapping[mapping.rindex('/')+1:] classname = filename.split('.', 1 )[0] @@ -52,16 +54,16 @@ def generate_model(mappinglist, output_directory, version): validate_class_mapping_configuration(classname, mappingPlist) - generate_internal_file(mappingPlist, classname, output_directory) - generate_external_file_if_needed(classname, output_directory) + generate_internal_file(mappingPlist, classname, output_directory, testEnabled) + generate_external_file_if_needed(classname, output_directory, testEnabled) - generate_internal_instantiator_file(mappinglist, output_directory) + generate_internal_instantiator_file(mappinglist, output_directory, testEnabled) ''' External Model File Generation ''' -def generate_external_file_if_needed(classname, class_directory): +def generate_external_file_if_needed(classname, class_directory, testEnabled): filename = class_directory + classname + '.swift' @@ -72,15 +74,21 @@ def generate_external_file_if_needed(classname, class_directory): os.makedirs(os.path.dirname(filename)) outputfile = open(filename, "wba") - - outputfile.write(STRING_IMPORT_FOUNDATION + '\nclass ' + classname + ' : _' + classname + ' {\n\n}') + + if testEnabled == 1: + print 'TEST ENABLED' + outputfile.write(STRING_IMPORT_FOUNDATION_TEST + '\nclass ' + classname + ' : _' + classname + ' {\n\n}') + else: + print 'TEST DISABLED' + outputfile.write(STRING_IMPORT_FOUNDATION + '\nclass ' + classname + ' : _' + classname + ' {\n\n}') + outputfile.close(); ''' Internal Model File Generation ''' -def generate_internal_file(mappingPlist, classname, class_directory): +def generate_internal_file(mappingPlist, classname, class_directory, testEnabled): filename = class_directory + 'Internal/_'+ classname + '.swift' @@ -89,7 +97,13 @@ def generate_internal_file(mappingPlist, classname, class_directory): outputfile = open(filename, "wba") - outputfile.write(STRING_IMPORT_FOUNDATION + STRING_USMAPPER_IMPORT + '\nclass _' + classname + ' {\n') + if testEnabled is '1': + print 'TEST ENABLED' + outputfile.write(STRING_IMPORT_FOUNDATION_TEST + STRING_USMAPPER_IMPORT + '\nclass _' + classname + ' {\n') + else: + print 'TEST DISABLED' + outputfile.write(STRING_IMPORT_FOUNDATION + STRING_USMAPPER_IMPORT + '\nclass _' + classname + ' {\n') + append_optional_property_definitions(outputfile, mappingPlist) append_non_optional_property_definitions(outputfile, mappingPlist) @@ -317,7 +331,7 @@ def append_dictionary_failed_initialiser_property(classfile, propertyname, datat ''' Create External US2Mapper Inherited File ''' -def generate_internal_instantiator_file(mappingPlist, output_directory): +def generate_internal_instantiator_file(mappingPlist, output_directory, testEnabled): filename = output_directory + 'Internal/US2Instantiator.swift' if not os.path.exists(os.path.dirname(filename)): @@ -325,7 +339,12 @@ def generate_internal_instantiator_file(mappingPlist, output_directory): outputfile = open(filename, "wba") - outputfile.write(STRING_FILE_INTRO + STRING_IMPORT_FOUNDATION + STRING_USMAPPER_IMPORT) + if testEnabled == 1: + outputfile.write(STRING_FILE_INTRO + STRING_IMPORT_FOUNDATION_TEST + STRING_USMAPPER_IMPORT) + else: + outputfile.write(STRING_FILE_INTRO + STRING_IMPORT_FOUNDATION + STRING_USMAPPER_IMPORT) + + classnames = [] @@ -461,28 +480,59 @@ def xcode_version(): return 7.0 else: return 6.0 - - def main(argv): + inputfile = '' + outputfile = '' + testEnabled = 0 try: - opts, args = getopt.getopt(argv,"hv:i:o:",["version=", "mapdir=", "classdir="]) + opts, args = getopt.getopt(argv,"hv:i:o:t:",["version=","mapdir=","classname=","testing="]) except getopt.GetoptError: - print 'test.py -v -i -o ' + print 'test.py -i -o ' sys.exit(2) for opt, arg in opts: if opt == '-h': - print 'test.py -v -i -o ' + print 'test.py -i -o ' sys.exit() - elif opt in ("-v", "--version"): - version = arg - elif opt in ("-i", "--mapdir"): + elif opt in ("-v", "--ifile"): + currentVersion = arg + elif opt in ("-i", "--ofile"): mapdir = arg - elif opt in ("-o", "--classdir"): + elif opt in ("-o", "--ifile"): classdir = arg - + elif opt in ("-t", "--ofile"): + testEnabled = arg mappinglist = glob.glob(mapdir + "*.plist") - generate_model(mappinglist, classdir, version) + + generate_model(mappinglist, classdir, currentVersion, testEnabled) + + print 'Input file is "', inputfile + print 'Output file is "', outputfile if __name__ == "__main__": - main(sys.argv[1:]) \ No newline at end of file + main(sys.argv[1:]) + +#def main(argv): +# try: +# opts, args = getopt.getopt(argv,"hv:i:o:t:", ["version=", "testing=", "mapdir=", "classdir="]) +# except getopt.GetoptError: +# print 'modelgen-swift.py -v -i -o -t ' +# sys.exit(2) +# for opt, arg in opts: +# if opt == '-h': +# print 'modelgen-swift.py -v -i -o -t ' +# sys.exit(2) +# elif opt in ("-v", "--version"): +# version = arg +# elif opt in ("-i", "--mapdir"): +# mapdir = arg +# elif opt in ("-o", "--classdir"): +# classdir = arg +# elif opt in ("-t", "--testing"): +# testing = arg +# +# mappinglist = glob.glob(mapdir + "*.plist") +# generate_model(mappinglist, classdir, version, testing) +# +#if __name__ == "__main__": +# main(sys.argv[1:])# \ No newline at end of file diff --git a/US2MapperKit/Shared Test Cases/Classes/Internal/US2Instantiator.swift b/US2MapperKit/Shared Test Cases/Classes/Internal/US2Instantiator.swift index 2f99f3e..0754175 100644 --- a/US2MapperKit/Shared Test Cases/Classes/Internal/US2Instantiator.swift +++ b/US2MapperKit/Shared Test Cases/Classes/Internal/US2Instantiator.swift @@ -2,6 +2,7 @@ // UPDATE LISCENSE HERE import Foundation +import US2MapperKit enum US2MapperClassEnum: String { case _TestObjectEight = "TestObjectEight" diff --git a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectEight.swift b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectEight.swift index 9b08cbb..af00249 100644 --- a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectEight.swift +++ b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectEight.swift @@ -27,9 +27,7 @@ class _TestObjectEight { optionalDictionaryType = typeCast(unwrapped_optionalDictionaryType) } - } else { - self.init(_non_optionalDictionaryType : Dictionary()) - + } else { return nil } } diff --git a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectEleven.swift b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectEleven.swift index ece38bd..c0a5ca7 100644 --- a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectEleven.swift +++ b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectEleven.swift @@ -67,13 +67,7 @@ class _TestObjectEleven { optionalFloat = typeCast(unwrapped_optionalFloat) } - } else { - self.init(_non_optionalDouble : Double(), - _non_optionalFloat : Float(), - _non_optionalInt : Int(), - _non_optionalBool : Bool(), - _non_optionalString : String()) - + } else { return nil } } diff --git a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectFive.swift b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectFive.swift index ae0389f..7ba5644 100644 --- a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectFive.swift +++ b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectFive.swift @@ -27,9 +27,7 @@ class _TestObjectFive { optionalSubType = typeCast(unwrapped_optionalSubType) } - } else { - self.init(_non_optionalSubType : TestObjectThree(Dictionary())!) - + } else { return nil } } diff --git a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectFour.swift b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectFour.swift index 4cdfee2..dd63070 100644 --- a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectFour.swift +++ b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectFour.swift @@ -67,13 +67,7 @@ class _TestObjectFour { optionalFloat = typeCast(unwrapped_optionalFloat) } - } else { - self.init(_non_optionalDouble : Double(), - _non_optionalFloat : Float(), - _non_optionalInt : Int(), - _non_optionalBool : Bool(), - _non_optionalString : String()) - + } else { return nil } } diff --git a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectNine.swift b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectNine.swift index 51e5980..ba53d3c 100644 --- a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectNine.swift +++ b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectNine.swift @@ -57,12 +57,7 @@ class _TestObjectNine { optionalArrayFloatType = typeCast(unwrapped_optionalArrayFloatType) } - } else { - self.init(_non_optionalArrayFloatType : [Float](), - _non_optionalArrayDoubleType : [Double](), - _non_optionalArrayIntType : [Int](), - _non_optionalArrayStringType : [String]()) - + } else { return nil } } diff --git a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectSeven.swift b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectSeven.swift index bb695e4..707ec9c 100644 --- a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectSeven.swift +++ b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectSeven.swift @@ -27,9 +27,7 @@ class _TestObjectSeven { optionalArrayType = typeCast(unwrapped_optionalArrayType) } - } else { - self.init(_non_optionalArrayType : [TestObjectFour]()) - + } else { return nil } } diff --git a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectSix.swift b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectSix.swift index b977774..d000cbb 100644 --- a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectSix.swift +++ b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectSix.swift @@ -27,9 +27,7 @@ class _TestObjectSix { optionalCompoundString = typeCast(unwrapped_optionalCompoundString) } - } else { - self.init(_non_optionalCompoundString : String()) - + } else { return nil } } diff --git a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectTen.swift b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectTen.swift index 96df440..e3c6f7c 100644 --- a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectTen.swift +++ b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectTen.swift @@ -57,12 +57,7 @@ class _TestObjectTen { optionalDictionaryDoubleType = typeCast(unwrapped_optionalDictionaryDoubleType) } - } else { - self.init(_non_optionalDictionaryDoubleType : Dictionary(), - _non_optionalDictionaryIntType : Dictionary(), - _non_optionalDictionaryFloatType : Dictionary(), - _non_optionalDictionaryStringType : Dictionary()) - + } else { return nil } } diff --git a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectThirteen.swift b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectThirteen.swift index fbc3f25..9df23a7 100644 --- a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectThirteen.swift +++ b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectThirteen.swift @@ -44,9 +44,7 @@ class _TestObjectThirteen { optionalEnum = typeCast(unwrapped_optionalEnum) } - } else { - self.init() - + } else { return nil } } diff --git a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectThree.swift b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectThree.swift index f3f4a5e..2f8df7e 100644 --- a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectThree.swift +++ b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectThree.swift @@ -67,13 +67,7 @@ class _TestObjectThree { optionalFloat = typeCast(unwrapped_optionalFloat) } - } else { - self.init(_non_optionalDouble : Double(), - _non_optionalInt : Int(), - _non_optionalFloat : Float(), - _non_optionalBool : Bool(), - _non_optionalString : String()) - + } else { return nil } } diff --git a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectTwelve.swift b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectTwelve.swift index 4924ebe..8e54b82 100644 --- a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectTwelve.swift +++ b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectTwelve.swift @@ -67,13 +67,7 @@ class _TestObjectTwelve { optionalFloat = typeCast(unwrapped_optionalFloat) } - } else { - self.init(_non_optionalDouble : Double(), - _non_optionalFloat : Float(), - _non_optionalInt : Int(), - _non_optionalBool : Bool(), - _non_optionalString : String()) - + } else { return nil } } diff --git a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectTwo.swift b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectTwo.swift index b2be168..1b12d79 100644 --- a/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectTwo.swift +++ b/US2MapperKit/Shared Test Cases/Classes/Internal/_TestObjectTwo.swift @@ -19,9 +19,7 @@ class _TestObjectTwo { self.init() - } else { - self.init() - + } else { return nil } } diff --git a/US2MapperKit/Shared Test Cases/Test Case Objects/US2ExampleClosureTransformer.swift b/US2MapperKit/Shared Test Cases/Test Case Objects/US2ExampleClosureTransformer.swift index 8875c8b..434b209 100644 --- a/US2MapperKit/Shared Test Cases/Test Case Objects/US2ExampleClosureTransformer.swift +++ b/US2MapperKit/Shared Test Cases/Test Case Objects/US2ExampleClosureTransformer.swift @@ -7,7 +7,7 @@ // import Foundation -import UIKit + public class US2ExampleClosureTransformer : US2TransformerProtocol { public func transformValues(inputValues : Dictionary?) -> Any? { diff --git a/US2MapperKit/US2MapperKit/Internal/_TestObjectEight.swift b/US2MapperKit/US2MapperKit/Internal/_TestObjectEight.swift new file mode 100644 index 0000000..af00249 --- /dev/null +++ b/US2MapperKit/US2MapperKit/Internal/_TestObjectEight.swift @@ -0,0 +1,50 @@ +import Foundation + + +class _TestObjectEight { + + var optionalDictionaryType : Dictionary? + + var non_optionalDictionaryType : Dictionary + + required init(_non_optionalDictionaryType : Dictionary) { + + non_optionalDictionaryType = _non_optionalDictionaryType + } + + convenience init?(_ dictionary: Dictionary) { + + let dynamicTypeString = "\(self.dynamicType)" + let className = dynamicTypeString.componentsSeparatedByString(".").last + + if let valuesDict = US2Mapper.mapValues(from: dictionary, forType: className!, employing: US2Instantiator.sharedInstance, defaultsEnabled : true) { + + let temp_non_optionalDictionaryType : Dictionary = typeCast(valuesDict["non_optionalDictionaryType"])! + + self.init(_non_optionalDictionaryType : temp_non_optionalDictionaryType) + + if let unwrapped_optionalDictionaryType : Any = valuesDict["optionalDictionaryType"] { + optionalDictionaryType = typeCast(unwrapped_optionalDictionaryType) + } + + } else { + return nil + } + } + + func updateWithDictionary(dictionary: Dictionary) { + + let dynamicTypeString = "\(self.dynamicType)" + let className = dynamicTypeString.componentsSeparatedByString(".").last + + if let valuesDict = US2Mapper.mapValues(from: dictionary, forType: className!, employing: US2Instantiator.sharedInstance, defaultsEnabled : false) { + if let unwrapped_non_optionalDictionaryType : Any = valuesDict["non_optionalDictionaryType"] { + non_optionalDictionaryType = typeCast(unwrapped_non_optionalDictionaryType)! + } + + if let unwrapped_optionalDictionaryType : Any = valuesDict["optionalDictionaryType"] { + optionalDictionaryType = typeCast(unwrapped_optionalDictionaryType) + } + } + } +} \ No newline at end of file diff --git a/US2MapperKit/US2MapperKit/US2MapperKit.xcodeproj/project.pbxproj b/US2MapperKit/US2MapperKit/US2MapperKit.xcodeproj/project.pbxproj index 14751b5..1ae9dee 100644 --- a/US2MapperKit/US2MapperKit/US2MapperKit.xcodeproj/project.pbxproj +++ b/US2MapperKit/US2MapperKit/US2MapperKit.xcodeproj/project.pbxproj @@ -713,7 +713,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "python $PROJECT_DIR/../../Source/ModelScript/modelgen-swift.py -v 0.1 -i $PROJECT_DIR/../Shared\\ Test\\ Cases/Mapping/ -o $PROJECT_DIR/../Shared\\ Test\\ Cases/Classes/"; + shellScript = "python $PROJECT_DIR/../../Source/ModelScript/modelgen-swift.py -v 0.1 -i $PROJECT_DIR/../Shared\\ Test\\ Cases/Mapping/ -o $PROJECT_DIR/../Shared\\ Test\\ Cases/Classes/ -t 1"; }; 3AC7C3B31B5055650087B0BA /* Rebuild Model */ = { isa = PBXShellScriptBuildPhase; @@ -727,7 +727,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "python $PROJECT_DIR/../../Source/ModelScript/modelgen-swift.py -v 0.1 -i $PROJECT_DIR/../Shared\\ Test\\ Cases/Mapping/ -o $PROJECT_DIR/../Shared\\ Test\\ Cases/Classes/"; + shellScript = "python $PROJECT_DIR/../../Source/ModelScript/modelgen-swift.py -v 0.1 -i $PROJECT_DIR/../Shared\\ Test\\ Cases/Mapping/ -o $PROJECT_DIR/../Shared\\ Test\\ Cases/Classes/ -t 1"; }; /* End PBXShellScriptBuildPhase section */ diff --git a/US2MapperKit/US2MapperKit/US2MapperKit.xcodeproj/xcshareddata/xcschemes/US2MapperKit - iOS.xcscheme b/US2MapperKit/US2MapperKit/US2MapperKit.xcodeproj/xcshareddata/xcschemes/US2MapperKit - iOS.xcscheme index 672929a..3844ff0 100644 --- a/US2MapperKit/US2MapperKit/US2MapperKit.xcodeproj/xcshareddata/xcschemes/US2MapperKit - iOS.xcscheme +++ b/US2MapperKit/US2MapperKit/US2MapperKit.xcodeproj/xcshareddata/xcschemes/US2MapperKit - iOS.xcscheme @@ -37,10 +37,10 @@ + shouldUseLaunchSchemeArgsEnv = "YES"> @@ -62,15 +62,18 @@ ReferencedContainer = "container:US2MapperKit.xcodeproj"> + + Date: Fri, 1 Jul 2016 10:41:15 -0700 Subject: [PATCH 3/3] Fixed up the resource bundle location, and the script to ensure both environments work --- Source/ModelScript/modelgen-swift.py | 90 +++++++++++-------- Source/US2MapperKit/US2Mapper.swift | 33 +++++-- .../Classes/Internal/US2Instantiator.swift | 76 ++++++++++++++-- .../xcschemes/US2MapperKit - OSX.xcscheme | 17 +++- .../xcschemes/US2MapperKit - iOS.xcscheme | 8 +- documentation/custom_transforms_tuples.md | 2 +- 6 files changed, 169 insertions(+), 57 deletions(-) diff --git a/Source/ModelScript/modelgen-swift.py b/Source/ModelScript/modelgen-swift.py index da2e601..eda6bc7 100644 --- a/Source/ModelScript/modelgen-swift.py +++ b/Source/ModelScript/modelgen-swift.py @@ -24,7 +24,7 @@ MAPPING_KEY_DEFAULT = "default" MAPPING_KEY_KEY = "key" MAPPING_KEY_NONOPTIONAL = "nonoptional" -MAPPING_KEY_TRANSFORMER = "transformer" +MAPPING_KEY_TRANSFORMER = "transformer" MAPPING_KEY_COLLECTION_SUBTYPE = "collection_subtype" STRING_IMPORT_FOUNDATION = "import Foundation\nimport US2MapperKit\n" @@ -62,6 +62,7 @@ def generate_model(mappinglist, output_directory, version, testEnabled): ''' External Model File Generation + ''' def generate_external_file_if_needed(classname, class_directory, testEnabled): @@ -117,6 +118,7 @@ def generate_internal_file(mappingPlist, classname, class_directory, testEnabled ''' Appeand Properties + ''' def append_optional_property_definitions(classfile, mappingPlist): classfile.write('\n') @@ -166,15 +168,18 @@ def append_non_optional_property_definitions(classfile, mappingPlist): def append_instance_property(classfile, propertyname, datatype, optional): classfile.write(STRING_PROPERTY_VAR + ' ' + propertyname + ' : ' + datatype + '{}\n'.format('?' if optional else '')) + def append_array_instance_property(classfile, propertyname, collectionSubtype, optional): classfile.write(STRING_PROPERTY_VAR + ' ' + propertyname + ' : [' + collectionSubtype + ']{}\n'.format('?' if optional else '')) + def append_dictionary_instance_property(classfile, propertyname, collectionSubtype, optional): classfile.write(STRING_PROPERTY_VAR + ' ' + propertyname + ' : Dictionary{}\n'.format('?' if optional else '')) ''' Append Required Initializer + ''' def append_required_initializer(classFile, mappingPlist): classFile.write(STRING_REQUIRED_INIT_START) @@ -213,6 +218,7 @@ def append_required_initializer_non_optional_property(classFile, mappingPlist, p ''' Append Failable Initializer + ''' def append_failable_initializer(classFile, mappingPlist): @@ -240,7 +246,6 @@ def append_failable_initializer(classFile, mappingPlist): else: append_swift_1_2_failable_reinitialization(classFile, mappingPlist) - def append_swift_1_2_failable_reinitialization(classFile, mappingPlist): classFile.write(' \n } else {\n self.init(') @@ -315,21 +320,25 @@ def append_failable_initializer_typecasting(classFile, mappingPlist): def append_failable_typecast_unwrap_statement(classFile, propertyName): classFile.write('\n\t\t\tif let unwrapped_' + propertyName + ' : Any = valuesDict["' + propertyName + '"] {\n\t\t\t\t' + propertyName + ' = typeCast(unwrapped_' + propertyName + ')\n\t\t\t}\n') + def append_failed_initialiser_property(classfile, propertyname, datatype, optional, isFirstLine): if datatype not in NATIVE_PROPERTY_TYPES: classfile.write('{}_'.format('' if isFirstLine else ',\n\t\t\t\t ') + propertyname + ' : ' + datatype + '(Dictionary())!{}'.format('?' if optional else '')) else: classfile.write('{}_'.format('' if isFirstLine else ',\n\t\t\t\t ') + propertyname + ' : ' + datatype + '(){}'.format('?' if optional else '')) + def append_array_failed_initialiser_property(classfile, propertyname, datatype, collectionSubtype, optional, isFirstLine): classfile.write('{}_'.format('' if isFirstLine else ',\n\t\t\t\t ') + propertyname + ' : [' + collectionSubtype + '](){}'.format('?' if optional else '')) + def append_dictionary_failed_initialiser_property(classfile, propertyname, datatype, collectionSubtype, optional, isFirstLine): classfile.write( '{}_'.format('' if isFirstLine else ',\n\t\t\t\t ') + propertyname + ' : Dictionary(){}'.format('?' if optional else '')) ''' Create External US2Mapper Inherited File + ''' def generate_internal_instantiator_file(mappingPlist, output_directory, testEnabled): filename = output_directory + 'Internal/US2Instantiator.swift' @@ -344,8 +353,6 @@ def generate_internal_instantiator_file(mappingPlist, output_directory, testEnab else: outputfile.write(STRING_FILE_INTRO + STRING_IMPORT_FOUNDATION + STRING_USMAPPER_IMPORT) - - classnames = [] for mapping in mappingPlist: @@ -353,6 +360,15 @@ def generate_internal_instantiator_file(mappingPlist, output_directory, testEnab classname = filename.split('.', 1 )[0] classnames.append(classname) + append_mapper_class_enum(classnames, outputfile) + append_mapper_method_definitions(outputfile, mappingPlist) + append_mapping_dict_enum(classnames, outputfile) + append_instantiator_protocol(outputfile) + + outputfile.close(); + + +def append_mapper_class_enum(classnames, outputfile): outputfile.write('enum US2MapperClassEnum: String {') for classname in classnames: @@ -365,15 +381,34 @@ def generate_internal_instantiator_file(mappingPlist, output_directory, testEnab outputfile.write('\n\t\tcase ._' + classname + ':\n\t\t\treturn '+ classname + '(data)' ) outputfile.write('\n\t\tcase ._None:\n\t\t\treturn nil' ) - outputfile.write('\n\t\t}\n\t}\n}\n\n') - append_mapper_method_definitions(outputfile, mappingPlist) - outputfile.write('\n\nclass US2Instantiator : US2InstantiatorProtocol {\n\n\tstatic let sharedInstance : US2Instantiator = US2Instantiator()\n\n\tfunc newInstance(ofType classname : String, withValue data : Dictionary) -> AnyObject? {\n\t\treturn US2MapperClassEnum(rawValue: classname)?.createObject(data)\n\t}\n\n' ) +def append_mapping_dict_enum(classnames, outputfile): + outputfile.write('\n\nenum US2MappingEnum : String {') + - outputfile.write('\tfunc transformerFromString(classString: String) -> US2TransformerProtocol? {\n\t\treturn US2TransformerEnum(rawValue: classString)!.transformer()\n\t}\n}') - outputfile.close(); + for classname in classnames: + outputfile.write('\n\tcase _' + classname + ' \t= "'+ classname + '"' ) + + outputfile.write('\n\tcase _None\t\t\t\t= "None"') + outputfile.write('\n\n\tfunc mapping() -> Dictionary>? {\n\t\tvar mappingDict = Dictionary> ()\n\n\t\tswitch self {') + #outputfile.write('\t\t\t\tvar mappingDict = Dictionary> ()' ) + + + for classname in classnames: + #outputfile.write('\n\t\tcase ._' + classname + ':\n\t\t\treturn '+ classname + '(data)' ) + outputfile.write('\n\t\t\tcase ._' + classname + ':\n') + outputfile.write('\n\t\t\treturn mappingDict' ) + + #var mappingDict = Dictionary> () + # mappingDict["optionalSubType"] = ["key" : "optional_subtype", "type" : "TestObjectThree"] + # mappingDict["non_optionalSubType"] = ["key" : "non_optional_subtype", "type" : "TestObjectThree", "nonoptional" : true] + # return mappingDict + #outputfile.write('\n\t\tcase ._' + classname + ':\n\t\t\treturn nil' ) + + outputfile.write('\n\t\tcase ._None:\n\t\t\treturn nil' ) + outputfile.write('\n\t\t}\n\t}\n}\n\n') def append_mapper_method_definitions(classfile, mappinglist): @@ -397,12 +432,17 @@ def append_mapper_method_definitions(classfile, mappinglist): classfile.write('\n\n\tfunc transformer() -> US2TransformerProtocol? {\n\t\tswitch self {') for mapperClass in distinctMapperClassDefinitions: - classfile.write('\n\t\tcase ._' + mapperClass + ':\n\t\t\treturn ' + mapperClass + '()\n' ) + classfile.write('\n\t\tcase ._' + mapperClass + ':\n\t\t\treturn ' + mapperClass + '()' ) classfile.write('\n\t\tcase ._None:\n\t\t\treturn nil' ) + classfile.write('\n\t\t}\n\t} \n}') - classfile.write('\t\t}\n\t} \n}') +def append_instantiator_protocol(outputfile): + outputfile.write('\n\nclass US2Instantiator : US2InstantiatorProtocol {\n\n\tstatic let sharedInstance : US2Instantiator = US2Instantiator()\n\n\tfunc newInstance(ofType classname : String, withValue data : Dictionary) -> AnyObject? {\n\t\treturn US2MapperClassEnum(rawValue: classname)?.createObject(data)\n\t}\n\n' ) + outputfile.write('\tfunc transformerFromString(classString: String) -> US2TransformerProtocol? {\n\t\treturn US2TransformerEnum(rawValue: classString)!.transformer()\n\t}\n\n') + outputfile.write('\tfunc mappingForClass(classString: String) -> Dictionary>? {\n\t\treturn US2MappingEnum(rawValue: classString)!.mapping()\n\t}\n\n}') + ''' Create Mapping for Instantiated Class @@ -473,7 +513,6 @@ def throw_missing_json_key_error(classname, propertykey, mapping): print "The mapping configuration for the " + propertykey + " property is missing the key configuration.\nAll properties must specify a 'key' value to map against value in a dictionary.\n\n" raise Exception('Invalid Configuration') - def xcode_version(): status, xcodeVersionString = commands.getstatusoutput("xcodebuild -version") if xcodeVersionString.find("Xcode 7.") != -1: @@ -510,29 +549,4 @@ def main(argv): print 'Output file is "', outputfile if __name__ == "__main__": - main(sys.argv[1:]) - -#def main(argv): -# try: -# opts, args = getopt.getopt(argv,"hv:i:o:t:", ["version=", "testing=", "mapdir=", "classdir="]) -# except getopt.GetoptError: -# print 'modelgen-swift.py -v -i -o -t ' -# sys.exit(2) -# for opt, arg in opts: -# if opt == '-h': -# print 'modelgen-swift.py -v -i -o -t ' -# sys.exit(2) -# elif opt in ("-v", "--version"): -# version = arg -# elif opt in ("-i", "--mapdir"): -# mapdir = arg -# elif opt in ("-o", "--classdir"): -# classdir = arg -# elif opt in ("-t", "--testing"): -# testing = arg -# -# mappinglist = glob.glob(mapdir + "*.plist") -# generate_model(mappinglist, classdir, version, testing) -# -#if __name__ == "__main__": -# main(sys.argv[1:])# \ No newline at end of file + main(sys.argv[1:]) \ No newline at end of file diff --git a/Source/US2MapperKit/US2Mapper.swift b/Source/US2MapperKit/US2Mapper.swift index 8937922..1db12a4 100644 --- a/Source/US2MapperKit/US2Mapper.swift +++ b/Source/US2MapperKit/US2Mapper.swift @@ -11,6 +11,7 @@ import Foundation public protocol US2InstantiatorProtocol { func newInstance(ofType classname : String, withValue data : Dictionary) -> AnyObject? func transformerFromString(classString: String) -> US2TransformerProtocol? + func mappingForClass(classString: String) -> Dictionary>? } public protocol US2TransformerProtocol { @@ -54,7 +55,7 @@ final public class US2Mapper { public class func mapValues(from dictionary : Dictionary, forType classType : String , employing instantiator : US2InstantiatorProtocol, defaultsEnabled : Bool) -> Dictionary? { - if let mappingConfiguration = retrieveMappingConfiguration(classType) { + if let mappingConfiguration = retrieveMappingConfiguration(classType, employing: instantiator) { // Dictionary to store parsed values to be returned var retrievedValueDictionary = Dictionary() @@ -75,20 +76,38 @@ final public class US2Mapper { return nil } - class func retrieveMappingConfiguration(className : String) -> Dictionary>? { + class func retrieveMappingConfiguration(className : String, employing instantiator : US2InstantiatorProtocol) -> Dictionary>? { + if let mappingconfiguration = propertyMappings[className] { return mappingconfiguration } else { - if let mappingPath = NSBundle(forClass: self).pathForResource(className, ofType: "plist") { - let tempMapping = NSDictionary(contentsOfFile: mappingPath) as? Dictionary> - - if tempMapping!.isEmpty { return nil } + + NSProcessInfo.processInfo().arguments.containsValue("TEST") + if NSProcessInfo.processInfo().arguments.containsValue("TEST") { + if let mappingPath = NSBundle(forClass: self).pathForResource(className, ofType: "plist"), + let tempMapping = NSDictionary(contentsOfFile: mappingPath) as? Dictionary> { + if tempMapping.keys.count > 0 { + propertyMappings[className] = tempMapping + return tempMapping + } + + return nil + + } else { + return nil + } + } else { + if let mappingPath = NSBundle.mainBundle().pathForResource(className, ofType: "plist"), + + let tempMapping = NSDictionary(contentsOfFile: mappingPath) as? Dictionary> { propertyMappings[className] = tempMapping - return tempMapping! + return tempMapping } else { return nil } + + } } } } diff --git a/US2MapperKit/Shared Test Cases/Classes/Internal/US2Instantiator.swift b/US2MapperKit/Shared Test Cases/Classes/Internal/US2Instantiator.swift index 0754175..66be947 100644 --- a/US2MapperKit/Shared Test Cases/Classes/Internal/US2Instantiator.swift +++ b/US2MapperKit/Shared Test Cases/Classes/Internal/US2Instantiator.swift @@ -63,24 +63,83 @@ enum US2TransformerEnum: String { switch self { case ._US2CompoundValueTransformer: return US2CompoundValueTransformer() - case ._US2ExampleStructTransformer: return US2ExampleStructTransformer() - case ._US2ExampleTupleTransformer: return US2ExampleTupleTransformer() - case ._US2ExampleClosureTransformer: return US2ExampleClosureTransformer() - case ._US2ExampleEnumTransformer: return US2ExampleEnumTransformer() - case ._None: - return nil } + return nil + } } } +enum US2MappingEnum : String { + case _TestObjectEight = "TestObjectEight" + case _TestObjectEleven = "TestObjectEleven" + case _TestObjectFive = "TestObjectFive" + case _TestObjectFour = "TestObjectFour" + case _TestObjectNine = "TestObjectNine" + case _TestObjectSeven = "TestObjectSeven" + case _TestObjectSix = "TestObjectSix" + case _TestObjectTen = "TestObjectTen" + case _TestObjectThirteen = "TestObjectThirteen" + case _TestObjectThree = "TestObjectThree" + case _TestObjectTwelve = "TestObjectTwelve" + case _TestObjectTwo = "TestObjectTwo" + case _None = "None" + + func mapping() -> Dictionary>? { + var mappingDict = Dictionary> () + + switch self { + case ._TestObjectEight: + + return mappingDict + case ._TestObjectEleven: + + return mappingDict + case ._TestObjectFive: + + return mappingDict + case ._TestObjectFour: + + return mappingDict + case ._TestObjectNine: + + return mappingDict + case ._TestObjectSeven: + + return mappingDict + case ._TestObjectSix: + + return mappingDict + case ._TestObjectTen: + + return mappingDict + case ._TestObjectThirteen: + + return mappingDict + case ._TestObjectThree: + + return mappingDict + case ._TestObjectTwelve: + + return mappingDict + case ._TestObjectTwo: + + return mappingDict + case ._None: + return nil + } + } +} + + + class US2Instantiator : US2InstantiatorProtocol { static let sharedInstance : US2Instantiator = US2Instantiator() @@ -92,4 +151,9 @@ class US2Instantiator : US2InstantiatorProtocol { func transformerFromString(classString: String) -> US2TransformerProtocol? { return US2TransformerEnum(rawValue: classString)!.transformer() } + + func mappingForClass(classString: String) -> Dictionary>? { + return US2MappingEnum(rawValue: classString)!.mapping() + } + } \ No newline at end of file diff --git a/US2MapperKit/US2MapperKit/US2MapperKit.xcodeproj/xcshareddata/xcschemes/US2MapperKit - OSX.xcscheme b/US2MapperKit/US2MapperKit/US2MapperKit.xcodeproj/xcshareddata/xcschemes/US2MapperKit - OSX.xcscheme index a961895..b735c69 100644 --- a/US2MapperKit/US2MapperKit/US2MapperKit.xcodeproj/xcshareddata/xcschemes/US2MapperKit - OSX.xcscheme +++ b/US2MapperKit/US2MapperKit/US2MapperKit.xcodeproj/xcshareddata/xcschemes/US2MapperKit - OSX.xcscheme @@ -37,10 +37,10 @@ + shouldUseLaunchSchemeArgsEnv = "NO"> @@ -62,15 +62,24 @@ ReferencedContainer = "container:US2MapperKit.xcodeproj"> + + + + + + + shouldUseLaunchSchemeArgsEnv = "NO"> @@ -62,6 +62,12 @@ ReferencedContainer = "container:US2MapperKit.xcodeproj"> + + + + diff --git a/documentation/custom_transforms_tuples.md b/documentation/custom_transforms_tuples.md index 165a772..cc50156 100644 --- a/documentation/custom_transforms_tuples.md +++ b/documentation/custom_transforms_tuples.md @@ -42,7 +42,7 @@ Now that we have created a transformer, let's create mapping for our business Ob **Business.plist**
-![alt tag](/documentation/readme_assets/tuple_mapping_example.png?raw=true) +![alt tag](/documentation/readme_assets/tuple_mapping_example.png?raw=true width="172" height="61")
After the creation of the mapping, perform a build **(⌘-B)**. The changes should be reflected accordingly in the internal `_Business.swift` class.