krzysztofzablocki / Sourcery

Meta-programming for Swift, stop writing boilerplate code.
http://merowing.info
MIT License
7.68k stars 618 forks source link

AccessLevel does not (and cannot) conform to NSCoding, causing issues with Swift templates #113

Closed Obbut closed 7 years ago

Obbut commented 7 years ago

I've been playing with Sourcery a bit together with @Joannis and we have been following your progress quite close. Trying to play with the Swift templates in 0.5.2, we couldn't get them to work due to the following exception:

2017-01-10 09:25:13.513 Sourcery[44543:41067027] -[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x7fb84f532e10
2017-01-10 09:25:13.513 Sourcery[44543:41067027] -[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x7fb84f532e10
2017-01-10 09:25:13.515 Sourcery[44543:41067027] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x7fb84f532e10'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fffcb75de7b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fffe0348cad objc_exception_throw + 48
    2   CoreFoundation                      0x00007fffcb7dfcb4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x00007fffcb6cffb5 ___forwarding___ + 1061
    4   CoreFoundation                      0x00007fffcb6cfb08 _CF_forwarding_prep_0 + 120
    5   Foundation                          0x00007fffcd140a2a _encodeObject + 1241
    6   Sourcery                            0x00000001039bc4cc _TTSf4g_n___TFC8Sourcery6Method6encodefT4withCSo7NSCoder_T_ + 508
    7   Sourcery                            0x00000001039bac29 _TToFCC8Sourcery6Method9Parameter6encodefT4withCSo7NSCoder_T__merged + 41
    8   Foundation                          0x00007fffcd140a2a _encodeObject + 1241
    9   Foundation                          0x00007fffcd141fdc -[NSKeyedArchiver _encodeArrayOfObjects:forKey:] + 460
    10  Foundation                          0x00007fffcd140a2a _encodeObject + 1241
    11  Sourcery                            0x00000001039e63f2 _TTSf4g_n___TFC8Sourcery4Type6encodefT4withCSo7NSCoder_T_ + 770
    12  Sourcery                            0x00000001039c3b75 _TToFC8Sourcery5Class6encodefT4withCSo7NSCoder_T_ + 37
    13  Foundation                          0x00007fffcd140a2a _encodeObject + 1241
    14  Foundation                          0x00007fffcd141fdc -[NSKeyedArchiver _encodeArrayOfObjects:forKey:] + 460
    15  Foundation                          0x00007fffcd140a2a _encodeObject + 1241
    16  Sourcery                            0x00000001039ca9f8 _TTSf4g_n___TFC8Sourcery17GenerationContext6encodefT4withCSo7NSCoder_T_ + 120
    17  Sourcery                            0x00000001039c7f35 _TToFC8Sourcery17GenerationContext6encodefT4withCSo7NSCoder_T_ + 37
    18  Foundation                          0x00007fffcd140a2a _encodeObject + 1241
    19  Foundation                          0x00007fffcd17c562 +[NSKeyedArchiver archivedDataWithRootObject:] + 156
    20  Sourcery                            0x00000001039ed67d _TTSf4g_g_n___TFC8Sourcery13SwiftTemplate6renderfzT5typesGSaCS_4Type_9argumentsGVs10DictionarySSCSo8NSObject__SS + 1821
    21  Sourcery                            0x00000001039e9183 _TTWC8Sourcery13SwiftTemplateS_8TemplateS_FS1_6renderfzT5typesGSaCS_4Type_9argumentsGVs10DictionarySSCSo8NSObject__SS + 51
    22  Sourcery                            0x00000001039d1485 _TTSf4gs_gs_n_n___TFC8Sourcery8SourceryP33_DB5C4364BFA94633EE953DD412A08C308generatefzT12templatePathV7PathKit4Path6outputS2_5typesGSaCS_4Type__T_ + 2677
    23  Sourcery                            0x00000001039d1b0b _TTSf4gs_gs_s_n_n___TFC8Sourcery8Sourcery12processFilesfzTV7PathKit4Path14usingTemplatesS2_6outputS2_14watcherEnabledSb_GSqGSaCOS_13FolderWatcher5Local__ + 347
    24  Sourcery                            0x000000010398cb4a _TTSf4n_n_gs_gs_gs_g___TFF8Sourcery6runCLIFT_T_U_FTSbSbV7PathKit4PathS1_S1_VS_15CustomArguments_T_ + 426
    25  Sourcery                            0x0000000103988f1f _TFF8Sourcery6runCLIFT_T_U_FTSbSbV7PathKit4PathS1_S1_VS_15CustomArguments_T_ + 79
    26  Sourcery                            0x0000000103990c7d _TPA__TTRXFo_dSbdSboV7PathKit4PathoS0_oS0_oV8Sourcery15CustomArguments_zoPs5Error__XFo_iSbiSbiS0_iS0_iS0_iS2__zoPS3___ + 173
    27  Commander                           0x0000000103b334de _TFF9Commander7commandu4_RxS_18ArgumentDescriptor_S0_0_S0_1_S0_2_S0_3_S0_rFTxq_q0_q1_q2_q3_FzTwx9ValueTypew_S1_w0_S1_w1_S1_w2_S1_w3_S1__T__PS_11CommandType_U_FzCS_14ArgumentParserT_ + 2830
    28  Commander                           0x0000000103b33c93 _TPA__TFF9Commander7commandu4_RxS_18ArgumentDescriptor_S0_0_S0_1_S0_2_S0_3_S0_rFTxq_q0_q1_q2_q3_FzTwx9ValueTypew_S1_w0_S1_w1_S1_w2_S1_w3_S1__T__PS_11CommandType_U_FzCS_14ArgumentParserT_ + 515
    29  Commander                           0x0000000103b2511f _TFE9CommanderPS_11CommandType3runfGSqSS_Os5Never + 287
    30  Sourcery                            0x0000000103988ec8 _TF8Sourcery6runCLIFT_T_ + 1624
    31  Sourcery                            0x00000001039883d1 main + 241
    32  libdyld.dylib                       0x00007fffe0c2c255 start + 1
    33  ???                                 0x0000000000000005 0x0 + 5
)
libc++abi.dylib: terminating with uncaught exception of type NSException
/Users/robbert/Documents/Tools/sourcery: line 4: 44543 Abort trap: 6           /Applications/Sourcery.app/Contents/MacOS/Sourcery "$@"

This actually happens because of this line in Method.swift, which tries to encode the AccessLevel enum using NSCoder. However, AccessLevel does not conform to NSCoding, which is a class protocol.

I don't really know how you guys would like to fix this, so that's why I'm opening an issue instead of submitting a pull request.

krzysztofzablocki commented 7 years ago

Turning access level into regular NSObject subclass as every other model we have would be fine, I'll need to be able to persist that regardless of a templating engine we use as I want to cache artifacts.

If you want to submit PR for that, it would be awesome :) You'll need to add markup as we do for other nscoding items and run sourcery on itself (using latest release binary) and then find Sourcery -name \"*.swift\" | xargs ./Scripts/update-placeholders ./Sourcery/CodeGenerated/Coding.Generated.swift

PS. My general recommendation is still to use Stencil rather than Swift templates, they are very much an early prototype in situations where there is no way to express something in Stencil (really rare).

Let me know if you want to try implementing that fix

ilyapuchka commented 7 years ago

Maybe we can just encode/decode rawValue?

krzysztofzablocki commented 7 years ago

We could but then we'd need to add support for that to the coding template, NSObject would be more consistent with general architecture

krzysztofzablocki commented 7 years ago

I'll adjust this, thanks for report @Obbut

krzysztofzablocki commented 7 years ago

Actually much simpler, I forgot we don't really use AccessLevel outside of Method (which I missed), we only store rawValue of it anyway, so just updating method should fix the issue, we can optimize it for swift templates in the future but for now just to make it consistent in both templating engines I'll update to String as its everywhere else /cc @ilyapuchka