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

Commit

Permalink
Breaking repository and datasource in CRUD based
Browse files Browse the repository at this point in the history
  • Loading branch information
Joan Martin committed Jun 27, 2018
1 parent d1e5ff7 commit 6c88ff1
Show file tree
Hide file tree
Showing 18 changed files with 223 additions and 99 deletions.
8 changes: 4 additions & 4 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

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

4 changes: 2 additions & 2 deletions MJSwiftCore/Classes/Common/Repository/AnyDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ extension DataSource {
/// This is an abstract class. Do not use it.
/// DataSource base class defining a generic type T (which is unrelated to the associated type of the DataSource protocol)
///
fileprivate class DataSourceBoxBase <T>: DataSource {
internal class DataSourceBoxBase <T>: DataSource {

func get(_ query: Query) -> Future<T> {
fatalError("This method is abstract.")
Expand Down Expand Up @@ -98,7 +98,7 @@ fileprivate class DataSourceBoxBase <T>: DataSource {
///
/// A data source box, which has as generic type a DataSource and links the DataSourceBoxBase type T as the Base.T type.
///
fileprivate class DataSourceBox <Base> : DataSourceBoxBase <Base.T> where Base : DataSource {
internal class DataSourceBox <Base: DataSource> : DataSourceBoxBase <Base.T> {

private let base: Base

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ extension DeleteDataSource {
/// This is an abstract class. Do not use it.
/// DeleteDataSource base class defining a generic type T (which is unrelated to the associated type of the DeleteDataSource protocol)
///
fileprivate class DeleteDataSourceBoxBase <T>: DeleteDataSource {
internal class DeleteDataSourceBoxBase <T>: DeleteDataSource {

func delete(_ query: Query) -> Future<Void> {
fatalError("This method is abstract.")
Expand All @@ -66,7 +66,7 @@ fileprivate class DeleteDataSourceBoxBase <T>: DeleteDataSource {
///
/// A data source box, which has as generic type a DeleteDataSource and links the DeleteDataSourceBoxBase type T as the Base.T type.
///
fileprivate class DeleteDataSourceBox <Base> : DeleteDataSourceBoxBase <Base.T> where Base : DeleteDataSource {
internal class DeleteDataSourceBox <Base: DeleteDataSource> : DeleteDataSourceBoxBase <Base.T> {

private let base: Base

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ extension DeleteRepository {
/// This is an abstract class. Do not use it.
/// DeleteRepository base class defining a generic type T (which is unrelated to the associated type of the DeleteRepository protocol)
///
fileprivate class DeleteRepositoryBoxBase <T>: DeleteRepository {
internal class DeleteRepositoryBoxBase <T>: DeleteRepository {

func delete(_ query: Query, operation: Operation) -> Future<Void> {
fatalError("This method is abstract.")
Expand All @@ -70,7 +70,7 @@ fileprivate class DeleteRepositoryBoxBase <T>: DeleteRepository {
///
/// A repository box, which has as generic type a DeleteRepository and links the DeleteRepositoryBoxBase type T as the Base.T type.
///
fileprivate class DeleteRepositoryBox <Base> : DeleteRepositoryBoxBase <Base.T> where Base : DeleteRepository {
internal class DeleteRepositoryBox <Base: DeleteRepository> : DeleteRepositoryBoxBase <Base.T> {

private let base: Base

Expand Down
4 changes: 2 additions & 2 deletions MJSwiftCore/Classes/Common/Repository/AnyGetDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ extension GetDataSource {
/// This is an abstract class. Do not use it.
/// GetDataSource base class defining a generic type T (which is unrelated to the associated type of the GetDataSource protocol)
///
fileprivate class GetDataSourceBoxBase <T>: GetDataSource {
internal class GetDataSourceBoxBase <T>: GetDataSource {

func get(_ query: Query) -> Future<T> {
fatalError("This method is abstract.")
Expand All @@ -66,7 +66,7 @@ fileprivate class GetDataSourceBoxBase <T>: GetDataSource {
///
/// A data source box, which has as generic type a GetDataSource and links the GetDataSourceBoxBase type T as the Base.T type.
///
fileprivate class GetDataSourceBox <Base> : GetDataSourceBoxBase <Base.T> where Base : GetDataSource {
internal class GetDataSourceBox <Base: GetDataSource> : GetDataSourceBoxBase <Base.T> {

private let base: Base

Expand Down
4 changes: 2 additions & 2 deletions MJSwiftCore/Classes/Common/Repository/AnyGetRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ extension GetRepository {
/// This is an abstract class. Do not use it.
/// GetRepository base class defining a generic type T (which is unrelated to the associated type of the GetRepository protocol)
///
fileprivate class GetRepositoryBoxBase <T>: GetRepository {
internal class GetRepositoryBoxBase <T>: GetRepository {

func get(_ query: Query, operation: Operation) -> Future<T> {
fatalError("This method is abstract.")
Expand All @@ -70,7 +70,7 @@ fileprivate class GetRepositoryBoxBase <T>: GetRepository {
///
/// A repository box, which has as generic type a GetRepository and links the GetRepositoryBoxBase type T as the Base.T type.
///
fileprivate class GetRepositoryBox <Base> : GetRepositoryBoxBase <Base.T> where Base : GetRepository {
internal class GetRepositoryBox <Base: GetRepository> : GetRepositoryBoxBase <Base.T> {

private let base: Base

Expand Down
4 changes: 2 additions & 2 deletions MJSwiftCore/Classes/Common/Repository/AnyPutDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ extension PutDataSource {
/// This is an abstract class. Do not use it.
/// PutDataSource base class defining a generic type T (which is unrelated to the associated type of the PutDataSource protocol)
///
fileprivate class PutDataSourceBoxBase <T>: PutDataSource {
internal class PutDataSourceBoxBase <T>: PutDataSource {

func put(_ value: T?, in query: Query) -> Future<T> {
fatalError("This method is abstract.")
Expand All @@ -66,7 +66,7 @@ fileprivate class PutDataSourceBoxBase <T>: PutDataSource {
///
/// A data source box, which has as generic type a PutDataSource and links the PutDataSourceBoxBase type T as the Base.T type.
///
fileprivate class PutDataSourceBox <Base> : PutDataSourceBoxBase <Base.T> where Base : PutDataSource {
internal class PutDataSourceBox <Base: PutDataSource> : PutDataSourceBoxBase <Base.T> {

private let base: Base

Expand Down
4 changes: 2 additions & 2 deletions MJSwiftCore/Classes/Common/Repository/AnyPutRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ extension PutRepository {
/// This is an abstract class. Do not use it.
/// PutRepository base class defining a generic type T (which is unrelated to the associated type of the PutRepository protocol)
///
fileprivate class PutRepositoryBoxBase <T>: PutRepository {
internal class PutRepositoryBoxBase <T>: PutRepository {

func put(_ value: T?, in query: Query, operation: Operation) -> Future<T> {
fatalError("This method is abstract.")
Expand All @@ -70,7 +70,7 @@ fileprivate class PutRepositoryBoxBase <T>: PutRepository {
///
/// A repository box, which has as generic type a PutRepository and links the PutRepositoryBoxBase type T as the Base.T type.
///
fileprivate class PutRepositoryBox <Base> : PutRepositoryBoxBase <Base.T> where Base : PutRepository {
internal class PutRepositoryBox <Base: PutRepository> : PutRepositoryBoxBase <Base.T> {

private let base: Base

Expand Down
4 changes: 2 additions & 2 deletions MJSwiftCore/Classes/Common/Repository/AnyRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ extension Repository {
/// This is an abstract class. Do not use it.
/// Repository base class defining a generic type T (which is unrelated to the associated type of the Repository protocol)
///
fileprivate class RepositoryBoxBase <T>: Repository {
internal class RepositoryBoxBase <T>: Repository {

func get(_ query: Query, operation: Operation) -> Future<T> {
fatalError("This method is abstract.")
Expand Down Expand Up @@ -102,7 +102,7 @@ fileprivate class RepositoryBoxBase <T>: Repository {
///
/// A repository box, which has as generic type a Repository and links the RepositoryBoxBase type T as the Base.T type.
///
fileprivate class RepositoryBox <Base> : RepositoryBoxBase <Base.T> where Base : Repository {
internal class RepositoryBox <Base: Repository> : RepositoryBoxBase <Base.T> {

private let base: Base

Expand Down
2 changes: 1 addition & 1 deletion MJSwiftCore/Classes/Common/Repository/DataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public enum DataSourceCRUD : CustomStringConvertible {
public protocol Query { }

extension Query {
public func fatalError<D>(_ method: DataSourceCRUD, _ origin: D) -> Never where D : DataSource {
public func fatalError<D>(_ method: DataSourceCRUD, _ origin: D) -> Never where D : TypedDataSource {
Swift.fatalError("Undefined query \(String(describing: self)) for method \(method) on \(String(describing: type(of: origin)))")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Foundation
///
/// This data source uses mappers to map objects and redirects them to the contained data source, acting as a simple "translator".
///
public class DataSourceMapper <D,From,To> : DataSource where D : DataSource, D.T == To {
public class DataSourceMapper <D: DataSource,From,To> : DataSource where D.T == To {

public typealias T = From

Expand Down
196 changes: 196 additions & 0 deletions MJSwiftCore/Classes/Common/Repository/DataSourceRepository.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
//
// 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

///
/// Single data source repository.
/// All repository methods are directly forwarded to a single data source.
/// Operation parameter is not used in any case.
///
public class GetDataSourceRepository<D: GetDataSource,T> : GetRepository where D.T == T {

private let dataSource : D

/// Default initializer
///
/// - Parameters:
/// - dataSource: The contained data source
public init(_ dataSource: D) {
self.dataSource = dataSource
}

public func get(_ query: Query, operation: Operation = BlankOperation()) -> Future<T> {
return dataSource.get(query)
}

public func getAll(_ query: Query, operation: Operation = BlankOperation()) -> Future<[T]> {
return dataSource.getAll(query)
}
}

extension GetDataSource {
/// Creates a single data source repository from a data source
///
/// - Returns: A SingleGetDataSourceRepository repository
public func toGetRepository() -> AnyGetRepository<T> {
return GetDataSourceRepository(self).asAnyGetRepository()
}
}

///
/// Single data source repository.
/// All repository methods are directly forwarded to a single data source.
/// Operation parameter is not used in any case.
///
public class PutDataSourceRepository<D: PutDataSource,T> : PutRepository where D.T == T {

private let dataSource : D

/// Default initializer
///
/// - Parameters:
/// - dataSource: The contained data source
public init(_ dataSource: D) {
self.dataSource = dataSource
}

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

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

extension PutDataSource {
/// Creates a single data source repository from a data source
///
/// - Returns: A SinglePutDataSourceRepository repository
public func toPutRepository() -> AnyPutRepository<T> {
return PutDataSourceRepository(self).asAnyPutRepository()
}
}

///
/// Single data source repository.
/// All repository methods are directly forwarded to a single data source.
/// Operation parameter is not used in any case.
///
public class DeleteDataSourceRepository<D: DeleteDataSource,T> : DeleteRepository where D.T == T {

private let dataSource : D

/// Default initializer
///
/// - Parameters:
/// - dataSource: The contained data source
public init(_ dataSource: D) {
self.dataSource = dataSource
}

@discardableResult
public func delete(_ query: Query, operation: Operation = BlankOperation()) -> Future<Void> {
return dataSource.delete(query)
}

@discardableResult
public func deleteAll(_ query: Query, operation: Operation = BlankOperation()) -> Future<Void> {
return dataSource.deleteAll(query)
}
}

extension DeleteDataSource {
/// Creates a single data source repository from a data source
///
/// - Returns: A SingleDeleteDataSourceRepository repository
public func toDeleteRepository() -> AnyDeleteRepository<T> {
return DeleteDataSourceRepository(self).asAnyDeleteRepository()
}
}

///
/// Single data source repository.
/// All repository methods are directly forwarded to a single data source.
/// Operation parameter is not used in any case.
///
public class DataSourceRepository<Get: GetDataSource,Put: PutDataSource,Delete: DeleteDataSource,T> : Repository 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, operation: Operation = BlankOperation()) -> Future<T> {
return getDataSource.get(query)
}

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

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

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

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

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

extension DataSourceRepository 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 DataSource {
/// Creates a single data source repository from a data source
///
/// - Returns: A SingleDataSourceRepository repository
public func toRepository() -> AnyRepository<T> {
return DataSourceRepository(self).asAnyRepository()
}
}
Loading

0 comments on commit 6c88ff1

Please sign in to comment.