-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from hyperledger/feature/serialization-factory
Serialization factory
- Loading branch information
Showing
12 changed files
with
297 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
IrohaCommunication/Classes/Public/Serializers/IRSerializationFactory.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "IRTransaction.h" | ||
#import "IRQueryRequest.h" | ||
#import "IRQueryResponse.h" | ||
|
||
@protocol IRSerializationFactoryProtocol <NSObject> | ||
|
||
+ (nullable NSData*)serializeTransaction:(nonnull id<IRTransaction>)transaction error:(NSError**)error; | ||
+ (nullable NSData*)serializeQueryRequest:(nonnull id<IRQueryRequest>)queryRequest error:(NSError**)error; | ||
|
||
+ (nullable id<IRTransaction>)deserializeTransactionFromData:(nonnull NSData*)data error:(NSError**)error; | ||
+ (nullable id<IRQueryResponse>)deserializeQueryResponseFromData:(nonnull NSData*)data error:(NSError**)error; | ||
|
||
@end | ||
|
||
@interface IRSerializationFactory : NSObject<IRSerializationFactoryProtocol> | ||
|
||
@end |
81 changes: 81 additions & 0 deletions
81
IrohaCommunication/Classes/Public/Serializers/IRSerializationFactory.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#import "IRSerializationFactory.h" | ||
#import "IRTransactionImpl.h" | ||
#import "IRQueryRequestImpl.h" | ||
#import "IRTransactionImpl+Proto.h" | ||
#import "IRQueryResponse+Proto.h" | ||
#import "Transaction.pbobjc.h" | ||
#import "Queries.pbobjc.h" | ||
#import "QryResponses.pbobjc.h" | ||
|
||
@implementation IRSerializationFactory | ||
|
||
+ (nullable NSData*)serializeTransaction:(nonnull id<IRTransaction>)transaction error:(NSError**)error { | ||
if (![transaction conformsToProtocol:@protocol(IRProtobufTransformable)]) { | ||
|
||
if (error) { | ||
NSString *message = @"Unsupported transaction implementation"; | ||
*error = [NSError errorWithDomain:NSStringFromClass([IRSerializationFactory class]) | ||
code:IRTransactionErrorSerialization | ||
userInfo:@{NSLocalizedDescriptionKey: message}]; | ||
} | ||
|
||
return nil; | ||
} | ||
|
||
Transaction *pbTransaction = [(id<IRProtobufTransformable>)transaction transform:error]; | ||
|
||
if (!pbTransaction) { | ||
return nil; | ||
} | ||
|
||
return [pbTransaction data]; | ||
} | ||
|
||
+ (nullable NSData*)serializeQueryRequest:(nonnull id<IRQueryRequest>)queryRequest error:(NSError**)error { | ||
if (![queryRequest conformsToProtocol:@protocol(IRProtobufTransformable)]) { | ||
|
||
if (error) { | ||
NSString *message = @"Unsupported query request implementation"; | ||
*error = [NSError errorWithDomain:NSStringFromClass([IRSerializationFactory class]) | ||
code:IRQueryRequestErrorSerialization | ||
userInfo:@{NSLocalizedDescriptionKey: message}]; | ||
} | ||
|
||
return nil; | ||
} | ||
|
||
Query *protobufQuery = [(id<IRProtobufTransformable>)queryRequest transform:error]; | ||
|
||
if (!protobufQuery) { | ||
return nil; | ||
} | ||
|
||
return [protobufQuery data]; | ||
} | ||
|
||
+ (nullable id<IRTransaction>)deserializeTransactionFromData:(nonnull NSData*)data error:(NSError**)error { | ||
Transaction *transaction = [[Transaction alloc] initWithData:data error:error]; | ||
|
||
if (!transaction) { | ||
return nil; | ||
} | ||
|
||
return [IRTransaction transactionFromPbTransaction:transaction error:error]; | ||
} | ||
|
||
+ (nullable id<IRQueryResponse>)deserializeQueryResponseFromData:(nonnull NSData*)data error:(NSError**)error { | ||
QueryResponse *response = [[QueryResponse alloc] initWithData:data error:error]; | ||
|
||
if (!response) { | ||
return nil; | ||
} | ||
|
||
return [IRQueryResponseProtoFactory responseFromProtobuf:response error:error]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
@import XCTest; | ||
@import IrohaCommunication; | ||
|
||
|
||
@interface IRPromiseTests : XCTestCase | ||
|
||
@end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
@import XCTest; | ||
@import IrohaCommunication; | ||
|
||
@interface IRInvalidSerializationTests : XCTestCase | ||
|
||
@end | ||
|
||
@implementation IRInvalidSerializationTests | ||
|
||
- (void)testInvalidTransactionDeserialization { | ||
NSError *error = nil; | ||
NSData *invalidTransactionData = [[NSData alloc] init]; | ||
id<IRTransaction> invalidTransaction = [IRSerializationFactory deserializeTransactionFromData:invalidTransactionData | ||
error:&error]; | ||
|
||
XCTAssertNil(invalidTransaction); | ||
XCTAssertNotNil(error); | ||
} | ||
|
||
- (void)testInvalidQueryResponseDeserialization { | ||
NSError *error = nil; | ||
NSData *invalidQueryResponseData = [[NSData alloc] init]; | ||
id<IRQueryResponse> invalidQueryResponse = [IRSerializationFactory deserializeQueryResponseFromData:invalidQueryResponseData | ||
error:&error]; | ||
|
||
XCTAssertNil(invalidQueryResponse); | ||
XCTAssertNotNil(error); | ||
} | ||
|
||
@end |
Oops, something went wrong.