Skip to content

Commit

Permalink
Merge pull request #2 from aws-amplify/palpatim/wip
Browse files Browse the repository at this point in the history
Palpatim/wip
  • Loading branch information
lawmicha authored Sep 8, 2019
2 parents 5cd96ba + b73bf92 commit c4d6e3f
Show file tree
Hide file tree
Showing 51 changed files with 1,111 additions and 1,314 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class AWSS3StoragePluginIntegrationTests: XCTestCase {
let storageConfig = BasicCategoryConfiguration(
plugins: ["AWSS3StoragePlugin": awss3StoragePluginConfig]
)
let amplifyConfig = BasicAmplifyConfiguration(storage: storageConfig)
let amplifyConfig = AmplifyConfiguration(storage: storageConfig)

let plugin = AWSS3StoragePlugin()
do {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Amplify
@testable import AWSS3StoragePlugin

class AWSS3StorageGetOperationTests: XCTestCase {
var mockAmplifyConfig: BasicAmplifyConfiguration!
var mockAmplifyConfig: AmplifyConfiguration!

override func setUp() {
Amplify.reset()
Expand All @@ -19,7 +19,7 @@ class AWSS3StorageGetOperationTests: XCTestCase {
plugins: ["MockHubCategoryPlugin": true]
)

mockAmplifyConfig = BasicAmplifyConfiguration(hub: hubConfig)
mockAmplifyConfig = AmplifyConfiguration(hub: hubConfig)
}

func testGetOperation() throws {
Expand Down
178 changes: 93 additions & 85 deletions Amplify.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions Amplify.xcodeproj/xcshareddata/xcschemes/Amplify.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,25 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
codeCoverageEnabled = "YES"
onlyGenerateCoverageForSpecifiedTargets = "YES"
shouldUseLaunchSchemeArgsEnv = "YES">
<CodeCoverageTargets>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "FAC234D12279F8DA00424678"
BuildableName = "Amplify.framework"
BlueprintName = "Amplify"
ReferencedContainer = "container:Amplify.xcodeproj">
</BuildableReference>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "21265DA32307389A004CCE39"
BuildableName = "AWSS3StoragePlugin.framework"
BlueprintName = "AWSS3StoragePlugin"
ReferencedContainer = "container:Amplify.xcodeproj">
</BuildableReference>
</CodeCoverageTargets>
<Testables>
<TestableReference
skipped = "NO">
Expand Down
4 changes: 1 addition & 3 deletions Amplify/Amplify.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public class Amplify {
// Storage for the categories themselves, which will be instantiated during configuration, and cleared during reset
public static internal(set) var Analytics = AnalyticsCategory()
public static internal(set) var API = APICategory()
public static internal(set) var Auth = AuthCategory()
public static internal(set) var Hub = HubCategory()
public static internal(set) var Logging = LoggingCategory()
public static internal(set) var Storage = StorageCategory()
Expand All @@ -36,8 +35,6 @@ public class Amplify {
try Analytics.add(plugin: plugin)
} else if let plugin = plugin as? APICategoryPlugin {
try API.add(plugin: plugin)
} else if let plugin = plugin as? AuthCategoryPlugin {
try Auth.add(plugin: plugin)
} else if let plugin = plugin as? HubCategoryPlugin {
try Hub.add(plugin: plugin)
} else if let plugin = plugin as? LoggingCategoryPlugin {
Expand All @@ -50,4 +47,5 @@ public class Amplify {
"Verify that the library version is correct and supports the plugin's category.")
}
}

}
42 changes: 10 additions & 32 deletions Amplify/Categories/API/APICategory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ final public class APICategory: Category {
/// this property if no plugins are added, or if more than one plugin is added without a pluginSelectorFactory,
/// will cause a preconditionFailure.
var pluginOrSelector: PluginOrSelector {
guard isConfigured else {
preconditionFailure(
"""
\(categoryType.displayName) category is not configured. Call Amplify.configure() before using
any methods on the category.
"""
)
}

if plugins.count == 1, let plugin = plugins.first?.value {
return .plugin(plugin)
}
Expand All @@ -44,33 +53,7 @@ final public class APICategory: Category {
return .selector(selector)
}

// MARK: - Configuration

/// For each key in the category configuration's `plugins` section, retrieves the plugin added for that
/// key, then invokes `configure` on that plugin.
///
/// - Parameter configuration: The category-specific configuration
/// - Throws:
/// - PluginError.noSuchPlugin if there is no plugin added for the specified key
/// - PluginError.pluginConfigurationError: If any plugin encounters an error during configuration
public func configure(using configuration: CategoryConfiguration) throws {
for (pluginKey, pluginConfiguration) in configuration.plugins {
let plugin = try getPlugin(for: pluginKey)
try plugin.configure(using: pluginConfiguration)
}
}

/// Convenience method for configuring the category using the top-level AmplifyConfiguration
///
/// - Parameter amplifyConfiguration: The AmplifyConfiguration
/// - Throws:
/// - PluginError.pluginConfigurationError: If any plugin encounters an error during configuration
func configure(using amplifyConfiguration: AmplifyConfiguration) throws {
guard let configuration = categoryConfiguration(from: amplifyConfiguration) else {
return
}
try configure(using: configuration)
}
var isConfigured = false

// MARK: - Plugin handling

Expand Down Expand Up @@ -158,9 +141,4 @@ final public class APICategory: Category {
plugins.values.forEach { self.pluginSelectorFactory?.add(plugin: $0) }
}

/// Invokes `reset` on each added plugin
public func resetPlugins() {
plugins.values.forEach { $0.reset() }
}

}
16 changes: 16 additions & 0 deletions Amplify/Categories/API/APICategoryConfiguration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Copyright 2018-2019 Amazon.com,
// Inc. or its affiliates. All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import Foundation

public struct APICategoryConfiguration: CategoryConfiguration {
public let plugins: [String: JSONValue]

public init(plugins: [String: JSONValue] = [:]) {
self.plugins = plugins
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// Copyright 2018-2019 Amazon.com,
// Inc. or its affiliates. All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

extension APICategory: CategoryConfigurable {

func configure(using configuration: CategoryConfiguration) throws {
guard !isConfigured else {
let error = ConfigurationError.amplifyAlreadyConfigured(
"\(categoryType.displayName) has already been configured.",
"""
Either remove the duplicate call to `Amplify.configure()`, or call \
`Amplify.reset()` before issuing the second call to `configure()`
"""
)
throw error
}

for (pluginKey, pluginConfiguration) in configuration.plugins {
let plugin = try getPlugin(for: pluginKey)
try plugin.configure(using: pluginConfiguration)
}

isConfigured = true
}

func configure(using amplifyConfiguration: AmplifyConfiguration) throws {
guard let configuration = categoryConfiguration(from: amplifyConfiguration) else {
return
}
try configure(using: configuration)
}

func reset() {
plugins.values.forEach { $0.reset() }
isConfigured = false
}

}

This file was deleted.

42 changes: 10 additions & 32 deletions Amplify/Categories/Analytics/AnalyticsCategory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ final public class AnalyticsCategory: Category {
/// this property if no plugins are added, or if more than one plugin is added without a pluginSelectorFactory,
/// will cause a preconditionFailure.
var pluginOrSelector: PluginOrSelector {
guard isConfigured else {
preconditionFailure(
"""
\(categoryType.displayName) category is not configured. Call Amplify.configure() before using
any methods on the category.
"""
)
}

if plugins.count == 1, let plugin = plugins.first?.value {
return .plugin(plugin)
}
Expand All @@ -44,33 +53,7 @@ final public class AnalyticsCategory: Category {
return .selector(selector)
}

// MARK: - Configuration

/// For each key in the category configuration's `plugins` section, retrieves the plugin added for that
/// key, then invokes `configure` on that plugin.
///
/// - Parameter configuration: The category-specific configuration
/// - Throws:
/// - PluginError.noSuchPlugin if there is no plugin added for the specified key
/// - PluginError.pluginConfigurationError: If any plugin encounters an error during configuration
public func configure(using configuration: CategoryConfiguration) throws {
for (pluginKey, pluginConfiguration) in configuration.plugins {
let plugin = try getPlugin(for: pluginKey)
try plugin.configure(using: pluginConfiguration)
}
}

/// Convenience method for configuring the category using the top-level AmplifyConfiguration
///
/// - Parameter amplifyConfiguration: The AmplifyConfiguration
/// - Throws:
/// - PluginError.pluginConfigurationError: If any plugin encounters an error during configuration
func configure(using amplifyConfiguration: AmplifyConfiguration) throws {
guard let configuration = categoryConfiguration(from: amplifyConfiguration) else {
return
}
try configure(using: configuration)
}
var isConfigured = false

// MARK: - Plugin handling

Expand Down Expand Up @@ -158,9 +141,4 @@ final public class AnalyticsCategory: Category {
plugins.values.forEach { self.pluginSelectorFactory?.add(plugin: $0) }
}

/// Invokes `reset` on each added plugin
public func resetPlugins() {
plugins.values.forEach { $0.reset() }
}

}
Loading

0 comments on commit c4d6e3f

Please sign in to comment.