mirror of
https://github.com/MacPass/MacPass.git
synced 2025-12-13 22:52:26 +00:00
Added dynamic methods for custom attribute lookup
This commit is contained in:
@@ -138,6 +138,7 @@
|
||||
4C61EA0316D2FD0800AC519E /* MPOutlineViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C61EA0216D2FD0800AC519E /* MPOutlineViewController.m */; };
|
||||
4C61EA0516D2FFE200AC519E /* OutlineView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4C61EA0416D2FFE200AC519E /* OutlineView.xib */; };
|
||||
4C63B8FB17A3154D0091BD72 /* MPContextButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C63B8FA17A3154D0091BD72 /* MPContextButton.m */; };
|
||||
4C642AE92195E3B700689431 /* MPTestCustomAttributeGetter.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C642AE82195E3B700689431 /* MPTestCustomAttributeGetter.m */; };
|
||||
4C65C79C16DD283900E32CFF /* MPToolbarButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C65C79B16DD283900E32CFF /* MPToolbarButton.m */; };
|
||||
4C65FAE916D16DDB006E0577 /* MPPasswordInputController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C65FAE716D16DDB006E0577 /* MPPasswordInputController.m */; };
|
||||
4C663D411D6D91A900CB6237 /* MPNumberFormatter.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C663D401D6D91A900CB6237 /* MPNumberFormatter.m */; };
|
||||
@@ -552,6 +553,7 @@
|
||||
4C61EA0416D2FFE200AC519E /* OutlineView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = OutlineView.xib; sourceTree = "<group>"; };
|
||||
4C63B8F917A3154D0091BD72 /* MPContextButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPContextButton.h; sourceTree = "<group>"; };
|
||||
4C63B8FA17A3154D0091BD72 /* MPContextButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPContextButton.m; sourceTree = "<group>"; };
|
||||
4C642AE82195E3B700689431 /* MPTestCustomAttributeGetter.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MPTestCustomAttributeGetter.m; sourceTree = "<group>"; };
|
||||
4C65C79A16DD283900E32CFF /* MPToolbarButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPToolbarButton.h; sourceTree = "<group>"; };
|
||||
4C65C79B16DD283900E32CFF /* MPToolbarButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPToolbarButton.m; sourceTree = "<group>"; };
|
||||
4C65FAE616D16DDB006E0577 /* MPPasswordInputController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPPasswordInputController.h; sourceTree = "<group>"; };
|
||||
@@ -1219,8 +1221,9 @@
|
||||
4C8030491E2FBAA300133E4C /* MPTestKeyMapper.m */,
|
||||
4C8F0C721FCF1B7A00BE157F /* MPTestPickcharsParser.m */,
|
||||
4C71BCB32167B75900B4CBDA /* MPTestPluginVersionComparator.m */,
|
||||
4C45FB1F178E09ED0010007D /* Supporting Files */,
|
||||
4C58A4A72192EEBE00B13370 /* MPTestIndexPathAdditions.m */,
|
||||
4C642AE82195E3B700689431 /* MPTestCustomAttributeGetter.m */,
|
||||
4C45FB1F178E09ED0010007D /* Supporting Files */,
|
||||
);
|
||||
path = MacPassTests;
|
||||
sourceTree = "<group>";
|
||||
@@ -1952,6 +1955,7 @@
|
||||
4C8DEAA21C314D2C00D24C32 /* MPTestAutotypeDelay.m in Sources */,
|
||||
4C71BCB42167B75900B4CBDA /* MPTestPluginVersionComparator.m in Sources */,
|
||||
4C8F0C731FCF1B7A00BE157F /* MPTestPickcharsParser.m in Sources */,
|
||||
4C642AE92195E3B700689431 /* MPTestCustomAttributeGetter.m in Sources */,
|
||||
4C45FB30178E0CE20010007D /* MPTestDocument.m in Sources */,
|
||||
4C6BC6601A36717E00BDDF3D /* MPDatabaseSearch.m in Sources */,
|
||||
4C10207F1B750E2F00BFCD59 /* MPTestAutotype.m in Sources */,
|
||||
|
||||
@@ -22,6 +22,8 @@
|
||||
|
||||
#import <KeePassKit/KeePassKit.h>
|
||||
|
||||
FOUNDATION_EXTERN NSString *const MPCustomAttributePropertyPrefix;
|
||||
|
||||
@interface KPKEntry (MPCustomAttributeProperties)
|
||||
|
||||
@end
|
||||
|
||||
@@ -21,30 +21,25 @@
|
||||
//
|
||||
|
||||
#import "KPKEntry+MPCustomAttributeProperties.h"
|
||||
#import <objc/runtime.h>
|
||||
|
||||
NSString *const MPCustomAttributePropertyPrefix = @"valueForCustomAttribute";
|
||||
|
||||
@implementation KPKEntry (MPCustomAttributeProperties)
|
||||
|
||||
/*- (void)forwardInvocation:(NSInvocation *)anInvocation {
|
||||
NSString *selector = NSStringFromSelector(anInvocation.selector);
|
||||
if([selector hasPrefix:MPCustomAttributePropertyPrefix]) {
|
||||
NSString *key = [selector substringFromIndex:MPCustomAttributePropertyPrefix.length];
|
||||
KPKAttribute *attribute = [self attributeWithKey:key];
|
||||
if(attribute) {
|
||||
anInvocation.selector = @selector(value);
|
||||
[anInvocation invokeWithTarget:attribute];
|
||||
// generic getter
|
||||
static id propertyIMP(id self, SEL _cmd) {
|
||||
NSString *propertyKey = [NSStringFromSelector(_cmd) substringFromIndex:MPCustomAttributePropertyPrefix.length];
|
||||
return [self valueForAttributeWithKey:propertyKey];
|
||||
}
|
||||
else {
|
||||
anInvocation.selector = @selector(_unkownCustomAttributeValue);
|
||||
[anInvocation invokeWithTarget:self];
|
||||
}
|
||||
}
|
||||
else [super forwardInvocation:anInvocation];
|
||||
}*/
|
||||
|
||||
- (NSString *)_unkownCustomAttributeValue {
|
||||
return @"";
|
||||
|
||||
+ (BOOL)resolveInstanceMethod:(SEL)aSEL {
|
||||
if ([NSStringFromSelector(aSEL) hasPrefix:MPCustomAttributePropertyPrefix]) {
|
||||
class_addMethod([self class], aSEL,(IMP)propertyIMP, "@@:");
|
||||
return YES;
|
||||
}
|
||||
return [super resolveInstanceMethod:aSEL];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
53
MacPassTests/MPTestCustomAttributeGetter.m
Normal file
53
MacPassTests/MPTestCustomAttributeGetter.m
Normal file
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// MPTestCustomAttributeGetter.m
|
||||
// MacPassTests
|
||||
//
|
||||
// Created by Michael Starke on 09.11.18.
|
||||
// Copyright © 2018 HicknHack Software GmbH. All rights reserved.
|
||||
//
|
||||
|
||||
#import <XCTest/XCTest.h>
|
||||
#import <KeePassKit/KeePassKit.h>
|
||||
|
||||
#import "KPKEntry+MPCustomAttributeProperties.h"
|
||||
|
||||
@interface MPTestCustomAttributeGetter : XCTestCase
|
||||
@end
|
||||
|
||||
@implementation MPTestCustomAttributeGetter
|
||||
|
||||
- (void)setUp {
|
||||
}
|
||||
|
||||
- (void)testValidCustomAttribute {
|
||||
KPKEntry *entry = [[KPKEntry alloc] init];
|
||||
KPKAttribute *attribute1 = [[KPKAttribute alloc] initWithKey:@"custom1" value:@"value1"];
|
||||
KPKAttribute *attribute2 = [[KPKAttribute alloc] initWithKey:@"custom2" value:@"value2"];
|
||||
[entry addCustomAttribute:attribute1];
|
||||
[entry addCustomAttribute:attribute2];
|
||||
|
||||
SEL selector1 = NSSelectorFromString([MPCustomAttributePropertyPrefix stringByAppendingString:attribute1.key]);
|
||||
IMP imp1 = [entry methodForSelector:selector1];
|
||||
NSString *(*func1)(id, SEL, NSString*) = (void *)imp1;
|
||||
NSString *value1 = func1(entry, selector1 , attribute1.key);
|
||||
XCTAssertEqualObjects(value1, attribute1.value);
|
||||
|
||||
SEL selector2 = NSSelectorFromString([MPCustomAttributePropertyPrefix stringByAppendingString:attribute2.key]);
|
||||
IMP imp2 = [entry methodForSelector:selector2];
|
||||
NSString *(*func2)(id, SEL, NSString*) = (void *)imp2;
|
||||
NSString *value2 = func2(entry, selector2, attribute2.key);
|
||||
XCTAssertEqualObjects(value2, attribute2.value);
|
||||
|
||||
SEL selector3 = NSSelectorFromString([MPCustomAttributePropertyPrefix stringByAppendingString:@"novalidkey"]);
|
||||
IMP imp3 = [entry methodForSelector:selector3];
|
||||
NSString *(*func3)(id, SEL, NSString*) = (void *)imp3;
|
||||
NSString *value3 = func3(entry, selector3, @"novalidkey");
|
||||
XCTAssertNil(value3);
|
||||
}
|
||||
|
||||
- (void)testInvalidCustomAttribute {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user