Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
creating data source ensamblers
Browse files Browse the repository at this point in the history
  • Loading branch information
Joan Martin committed Jun 27, 2018
1 parent 6c88ff1 commit 8ebe047
Show file tree
Hide file tree
Showing 11 changed files with 421 additions and 39 deletions.
1 change: 1 addition & 0 deletions Example/MJSwiftCore/Core/DI/InteractorAssembly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import MJSwiftCore
class InteractorAssembly: Assembly {

func assemble(container: Container) {

// Executor
container.register(Executor.self, name: "GetItems") { r in DispatchQueueExecutor() }.inObjectScope(.container)

Expand Down
2 changes: 1 addition & 1 deletion Example/MJSwiftCore/Core/DI/NetworkAssembly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class NetworkAssembly: Assembly {
}

// Network Clients
container.register(AnyDataSource<ItemEntity>.self, name: Names.networkRepository) { r in ItemNetworkDataSource(r.resolve(SessionManager.self)!).asAnyDataSource()
container.register(AnyGetDataSource<ItemEntity>.self, name: Names.networkRepository) { r in ItemNetworkDataSource(r.resolve(SessionManager.self)!).asAnyGetDataSource()
}
}
}
4 changes: 2 additions & 2 deletions Example/MJSwiftCore/Core/DI/RepositoryAssembly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class RepositoryAssembly: Assembly {
let storageDataSource = r.resolve(AnyDataSource<ItemEntity>.self, name: StorageAssembly.Names.storageRepository)!
let storageValidationDataSource = DataSourceValidator(dataSource: storageDataSource,
validator: r.resolve(ObjectValidation.self, name: Names.storageValidation)!)
let networkDataSource = r.resolve(AnyDataSource<ItemEntity>.self, name: NetworkAssembly.Names.networkRepository)!
let networkStorageRepo = NetworkStorageRepository(network: networkDataSource,
let networkDataSource = r.resolve(AnyGetDataSource<ItemEntity>.self, name: NetworkAssembly.Names.networkRepository)!
let networkStorageRepo = NetworkStorageRepository(network: DataSourceEnsambler(get: networkDataSource),
storage: storageValidationDataSource)

return RepositoryMapper(repository: networkStorageRepo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import Foundation

import Alamofire
import MJSwiftCore
import MJCocoaCore

extension ItemEntity {
fileprivate static var fromNetworkMap : [String : String] {
Expand All @@ -19,7 +18,7 @@ extension ItemEntity {
}
}

class ItemNetworkDataSource : AlamofireDataSource {
class ItemNetworkDataSource : GetDataSource {

typealias T = ItemEntity

Expand Down Expand Up @@ -48,26 +47,6 @@ class ItemNetworkDataSource : AlamofireDataSource {
query.fatalError(.getAll, self)
}
}

func putAll(_ array: [ItemEntity], in query: Query) -> Future<[ItemEntity]> {
// Protocol-refactoring: NEEDS-IMPLEMENTATION OR ONLY CONFORMING TO GetDataSource
query.fatalError(.putAll, self)
}

func put(_ value: ItemEntity?, in query: Query) -> Future<ItemEntity> {
// Protocol-refactoring: NEEDS-IMPLEMENTATION OR ONLY CONFORMING TO GetDataSource
query.fatalError(.put, self)
}

func deleteAll(_ query: Query) -> Future<Void> {
// Protocol-refactoring: NEEDS-IMPLEMENTATION OR ONLY CONFORMING TO GetDataSource
query.fatalError(.deleteAll, self)
}

func delete(_ query: Query) -> Future<Void> {
// Protocol-refactoring: NEEDS-IMPLEMENTATION OR ONLY CONFORMING TO GetDataSource
query.fatalError(.delete, self)
}
}

private extension ItemNetworkDataSource {
Expand Down
8 changes: 8 additions & 0 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions MJSwiftCore/Classes/Common/Repository/CoreError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,24 @@ public struct CoreError {
}
}

/// Illegal argument error
public class NotImplemented : ClassError {
public init(_ description: String = "Not Implemented") {
super.init(domain:CoreError.domain, code: 3, description: description)
}
}

/// Not valid error
public class NotValid : ClassError {
public init(_ description: String = "Object is not valid") {
super.init(domain:CoreError.domain, code: 3, description: description)
super.init(domain:CoreError.domain, code: 4, description: description)
}
}

/// Failed error
public class Failed : ClassError {
public init(_ description: String = "Action failed") {
super.init(domain:CoreError.domain, code: 4, description: description)
super.init(domain:CoreError.domain, code: 5, description: description)
}
}
}
Expand Down
52 changes: 51 additions & 1 deletion MJSwiftCore/Classes/Common/Repository/DataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,60 @@ extension DeleteDataSource {
return deleteAll(IdQuery(id))
}
}


///
/// Abstract definition of a DataSource
///
public protocol DataSource : GetDataSource, PutDataSource, DeleteDataSource { }

///
/// Blank data source superclass implementation
///
public class BlankDataSourceBase<T> : TypedDataSource {
private let crash : Bool
public init(crash : Bool = true) { self.crash = crash }

internal func crashOrError(_ query: Query) -> Error {
if crash {
query.fatalError(.get, self)
} else {
return CoreError.NotImplemented()
}
}
}

///
/// Blank get data source implementation
///
public class BlankGetDataSource<T> : BlankDataSourceBase<T>, GetDataSource {
public func get(_ query: Query) -> Future<T> { return Future(crashOrError(query)) }
public func getAll(_ query: Query) -> Future<[T]> { return Future(crashOrError(query)) }
}

///
/// Blank put data source implementation
///
public class BlankPutDataSource<T> : BlankDataSourceBase<T>, PutDataSource {
public func put(_ value: T?, in query: Query) -> Future<T> { return Future(crashOrError(query)) }
public func putAll(_ array: [T], in query: Query) -> Future<[T]> { return Future(crashOrError(query)) }
}

///
/// Blank delete data source implementation
///
public class BlankDeleteDataSource<T> : BlankDataSourceBase<T>, DeleteDataSource {
public func delete(_ query: Query) -> Future<Void> { return Future(crashOrError(query)) }
public func deleteAll(_ query: Query) -> Future<Void> { return Future(crashOrError(query)) }
}

///
/// Blank data source implementation
///
public class BlankDataSource<T> : BlankDataSourceBase<T>, DataSource {
public func get(_ query: Query) -> Future<T> { return Future(crashOrError(query)) }
public func getAll(_ query: Query) -> Future<[T]> { return Future(crashOrError(query)) }
public func put(_ value: T?, in query: Query) -> Future<T> { return Future(crashOrError(query)) }
public func putAll(_ array: [T], in query: Query) -> Future<[T]> { return Future(crashOrError(query)) }
public func delete(_ query: Query) -> Future<Void> { return Future(crashOrError(query)) }
public func deleteAll(_ query: Query) -> Future<Void> { return Future(crashOrError(query)) }
}
133 changes: 133 additions & 0 deletions MJSwiftCore/Classes/Common/Repository/DataSourceEnsambler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
//
// Copyright 2018 Mobile Jazz SL
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation

///
/// Ensambles a CRUD data source into a single data source object
///
public class DataSourceEnsambler <Get : GetDataSource, Put: PutDataSource, Delete: DeleteDataSource, T> : DataSource where Get.T == T, Put.T == T, Delete.T == T {

private let getDataSource : Get
private let putDataSource : Put
private let deleteDataSource : Delete

/// Main initializer
///
/// - Parameters:
/// - getDataSource: The get data source
/// - putDataSource: The put data source
/// - deleteDataSource: The delete data source
public init(get getDataSource: Get, put putDataSource: Put, delete deleteDataSource: Delete) {
self.getDataSource = getDataSource
self.putDataSource = putDataSource
self.deleteDataSource = deleteDataSource
}

public func get(_ query: Query) -> Future<T> {
return getDataSource.get(query)
}

public func getAll(_ query: Query) -> Future<[T]> {
return getDataSource.getAll(query)
}

@discardableResult
public func put(_ value: T?, in query: Query) -> Future<T> {
return putDataSource.put(value, in: query)
}

@discardableResult
public func putAll(_ array: [T], in query: Query) -> Future<[T]> {
return putDataSource.putAll(array, in: query)
}

@discardableResult
public func delete(_ query: Query) -> Future<Void> {
return deleteDataSource.delete(query)
}

@discardableResult
public func deleteAll(_ query: Query) -> Future<Void> {
return deleteDataSource.deleteAll(query)
}
}

extension DataSourceEnsambler where Get == Put, Get == Delete {
/// Initializer for a single DataSource
///
/// - Parameter dataSource: The data source
public convenience init(_ dataSource: Get) {
self.init(get: dataSource, put: dataSource, delete: dataSource)
}
}

extension DataSourceEnsambler where Put == BlankPutDataSource<T>, Delete == BlankDeleteDataSource<T> {
/// Initializer for a single DataSource
///
/// - Parameter getDataSource: The data source
public convenience init(get getDataSource: Get) {
self.init(get: getDataSource, put: BlankPutDataSource(), delete: BlankDeleteDataSource())
}
}

extension DataSourceEnsambler where Get == BlankGetDataSource<T>, Delete == BlankDeleteDataSource<T> {
/// Initializer for a single DataSource
///
/// - Parameter putDataSource: The data source
public convenience init(put putDataSource: Put) {
self.init(get: BlankGetDataSource(), put: putDataSource, delete: BlankDeleteDataSource())
}
}

extension DataSourceEnsambler where Get == BlankGetDataSource<T>, Put == BlankPutDataSource<T> {
/// Initializer for a single DataSource
///
/// - Parameter deleteDataSource: The data source
public convenience init(delete deleteDataSource: Delete) {
self.init(get: BlankGetDataSource(), put: BlankPutDataSource(), delete: deleteDataSource)
}
}

extension DataSourceEnsambler where Get == BlankGetDataSource<T> {
/// Initializer for a single DataSource
///
/// - Parameter putDataSource: The data source
/// - Parameter deleteDataSource: The data source
public convenience init(put putDataSource: Put, delete deleteDataSource: Delete) {
self.init(get: BlankGetDataSource(), put: putDataSource, delete: deleteDataSource)
}
}

extension DataSourceEnsambler where Put == BlankPutDataSource<T> {
/// Initializer for a single DataSource
///
/// - Parameter getDataSource: The data source
/// - Parameter deleteDataSource: The data source
public convenience init(get getDataSource: Get, delete deleteDataSource: Delete) {
self.init(get: getDataSource, put: BlankPutDataSource(), delete: deleteDataSource)
}
}

extension DataSourceEnsambler where Delete == BlankDeleteDataSource<T> {
/// Initializer for a single DataSource
///
/// - Parameter getDataSource: The data source
/// - Parameter putDataSource: The data source
public convenience init(get getDataSource: Get, put putDataSource: Put) {
self.init(get: getDataSource, put: putDataSource, delete: BlankDeleteDataSource())
}
}
Loading

0 comments on commit 8ebe047

Please sign in to comment.