Skip to content

Commit

Permalink
chore(analytics): resolve swiftformat errors and warnings (#3844)
Browse files Browse the repository at this point in the history
* chore(analytics): resolve swiftformat errors and warnings

* chore(analytics): resolve swiftformat errors and warnings

* resolve review comments

* fix build errors
  • Loading branch information
phantumcode authored Sep 10, 2024
1 parent 4e0fb7c commit 47601cd
Show file tree
Hide file tree
Showing 78 changed files with 1,281 additions and 897 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import AWSPinpoint
import Foundation
@_spi(InternalAWSPinpoint) import InternalAWSPinpoint

extension AWSPinpointAnalyticsPlugin {
public func identifyUser(userId: String, userProfile: AnalyticsUserProfile?) {
public extension AWSPinpointAnalyticsPlugin {
func identifyUser(userId: String, userProfile: AnalyticsUserProfile?) {
if !isEnabled {
log.warn("Cannot identify user. Analytics is disabled. Call Amplify.Analytics.enable() to enable")
return
Expand All @@ -20,20 +20,22 @@ extension AWSPinpointAnalyticsPlugin {
Task {
var currentEndpointProfile = await pinpoint.currentEndpointProfile()
currentEndpointProfile.addUserId(userId)
if let userProfile = userProfile {
if let userProfile {
currentEndpointProfile.addUserProfile(userProfile)
}
do {
try await pinpoint.updateEndpoint(with: currentEndpointProfile,
source: .analytics)
try await pinpoint.updateEndpoint(
with: currentEndpointProfile,
source: .analytics
)
Amplify.Hub.dispatchIdentifyUser(userId, userProfile: userProfile)
} catch {
Amplify.Hub.dispatchIdentifyUser(AnalyticsErrorHelper.getDefaultError(error))
}
}
}

public func record(event: AnalyticsEvent) {
func record(event: AnalyticsEvent) {
if !isEnabled {
log.warn("Cannot record events. Analytics is disabled. Call Amplify.Analytics.enable() to enable")
return
Expand All @@ -55,12 +57,12 @@ extension AWSPinpointAnalyticsPlugin {
}
}

public func record(eventWithName eventName: String) {
func record(eventWithName eventName: String) {
let event = BasicAnalyticsEvent(name: eventName)
record(event: event)
}

public func registerGlobalProperties(_ properties: [String: AnalyticsPropertyValue]) {
func registerGlobalProperties(_ properties: [String: AnalyticsPropertyValue]) {
// TODO: check if there is a limit on total number of properties
properties.forEach { key, _ in
guard key.count >= 1, key.count <= 50 else {
Expand All @@ -75,13 +77,13 @@ extension AWSPinpointAnalyticsPlugin {
}
}

public func unregisterGlobalProperties(_ keys: Set<String>?) {
func unregisterGlobalProperties(_ keys: Set<String>?) {
Task {
await unregisterGlobalProperties(keys)
}
}

public func flushEvents() {
func flushEvents() {
if !isEnabled {
log.warn("Cannot flushEvents. Analytics is disabled. Call Amplify.Analytics.enable() to enable")
return
Expand All @@ -105,18 +107,18 @@ extension AWSPinpointAnalyticsPlugin {
}
}

public func enable() {
func enable() {
isEnabled = true
}

public func disable() {
func disable() {
isEnabled = false
}

/// Retrieve the escape hatch to perform actions directly on PinpointClient.
///
/// - Returns: PinpointClientProtocol instance
public func getEscapeHatch() -> PinpointClientProtocol {
func getEscapeHatch() -> PinpointClientProtocol {
pinpoint.pinpointClient
}

Expand All @@ -128,7 +130,7 @@ extension AWSPinpointAnalyticsPlugin {
}

private func unregisterGlobalProperties(_ keys: Set<String>?) async {
guard let keys = keys else {
guard let keys else {
for (key, value) in globalProperties {
await pinpoint.removeGlobalProperty(value, forKey: key)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import Foundation
@_spi(InternalAWSPinpoint) import InternalAWSPinpoint
import Network

extension AWSPinpointAnalyticsPlugin {
public extension AWSPinpointAnalyticsPlugin {
/// Configures AWSPinpointAnalyticsPlugin with the specified configuration.
///
/// This method will be invoked as part of the Amplify configuration flow.
///
/// - Parameter configuration: The configuration specified for this plugin
/// - Throws:
/// - PluginError.pluginConfigurationError: If one of the configuration values is invalid or empty
public func configure(using configuration: Any?) throws {
func configure(using configuration: Any?) throws {
let pluginConfiguration: AWSPinpointAnalyticsPluginConfiguration
if let config = configuration as? AmplifyOutputsData {
print(config)
Expand All @@ -45,7 +45,7 @@ extension AWSPinpointAnalyticsPlugin {
}

/// Configure AWSPinpointAnalyticsPlugin programatically using AWSPinpointAnalyticsPluginConfiguration
public func configure(using configuration: AWSPinpointAnalyticsPluginConfiguration) throws {
func configure(using configuration: AWSPinpointAnalyticsPluginConfiguration) throws {
let pinpoint = try AWSPinpointFactory.sharedPinpoint(
appId: configuration.appId,
region: configuration.region
Expand Down Expand Up @@ -82,10 +82,12 @@ extension AWSPinpointAnalyticsPlugin {
// MARK: Internal

/// Internal configure method to set the properties of the plugin
func configure(pinpoint: AWSPinpointBehavior,
networkMonitor: NetworkMonitor,
globalProperties: AtomicDictionary<String, AnalyticsPropertyValue> = [:],
isEnabled: Bool = true) {
internal func configure(
pinpoint: AWSPinpointBehavior,
networkMonitor: NetworkMonitor,
globalProperties: AtomicDictionary<String, AnalyticsPropertyValue> = [:],
isEnabled: Bool = true
) {
self.pinpoint = pinpoint
self.networkMonitor = networkMonitor
self.globalProperties = globalProperties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@

import Foundation

extension AWSPinpointAnalyticsPlugin {
public struct Options {
public extension AWSPinpointAnalyticsPlugin {
struct Options {
static let defaultAutoFlushEventsInterval: TimeInterval = 60
static let defaultTrackAppSession = true

public let autoFlushEventsInterval: TimeInterval
public let trackAppSessions: Bool

#if os(macOS)
public init(autoFlushEventsInterval: TimeInterval = 60,
trackAppSessions: Bool = true) {
public init(
autoFlushEventsInterval: TimeInterval = 60,
trackAppSessions: Bool = true
) {
self.autoFlushEventsInterval = autoFlushEventsInterval
self.trackAppSessions = trackAppSessions
}
#else
public init(autoFlushEventsInterval: TimeInterval = 60,
trackAppSessions: Bool = true) {
public init(
autoFlushEventsInterval: TimeInterval = 60,
trackAppSessions: Bool = true
) {
self.autoFlushEventsInterval = autoFlushEventsInterval
self.trackAppSessions = trackAppSessions
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import Amplify
import Foundation
@_spi(InternalAWSPinpoint) import InternalAWSPinpoint

extension AWSPinpointAnalyticsPlugin {
public extension AWSPinpointAnalyticsPlugin {
/// Resets the state of the plugin
public func reset() async {
func reset() async {
if pinpoint != nil {
pinpoint = nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
//

@_spi(InternalAmplifyConfiguration) import Amplify
import AWSPinpoint
import AWSClientRuntime
import AWSPinpoint
import Foundation
@_spi(InternalAWSPinpoint) import InternalAWSPinpoint

Expand Down Expand Up @@ -58,9 +58,11 @@ public struct AWSPinpointAnalyticsPluginConfiguration {
if let options {
configOptions = options
} else {
configOptions = .init(
autoFlushEventsInterval: try Self.getAutoFlushEventsInterval(configObject),
trackAppSessions: try Self.getTrackAppSessions(configObject))
configOptions = try .init(
autoFlushEventsInterval: Self.getAutoFlushEventsInterval(configObject),
trackAppSessions: Self.getTrackAppSessions(configObject)
)

}
let autoSessionTrackingInterval = try Self.getAutoSessionTrackingInterval(configObject)

Expand All @@ -71,14 +73,18 @@ public struct AWSPinpointAnalyticsPluginConfiguration {
Self.logger.warn("Having different regions for Analytics and Targeting operations is not supported. The Analytics region will be used.")
}

self.init(appId: pluginConfiguration.appId,
region: pluginConfiguration.region,
autoSessionTrackingInterval: autoSessionTrackingInterval,
options: configOptions)
self.init(
appId: pluginConfiguration.appId,
region: pluginConfiguration.region,
autoSessionTrackingInterval: autoSessionTrackingInterval,
options: configOptions
)
}

init(_ configuration: AmplifyOutputsData,
options: AWSPinpointAnalyticsPlugin.Options) throws {
init(
_ configuration: AmplifyOutputsData,
options: AWSPinpointAnalyticsPlugin.Options
) throws {
guard let analyticsConfig = configuration.analytics else {
throw PluginError.pluginConfigurationError(
AnalyticsPluginErrorConstant.missingAnalyticsCategoryConfiguration.errorDescription,
Expand All @@ -93,16 +99,20 @@ public struct AWSPinpointAnalyticsPluginConfiguration {
)
}

self.init(appId: pinpointAnalyticsConfig.appId,
region: pinpointAnalyticsConfig.awsRegion,
autoSessionTrackingInterval: Self.defaultAutoSessionTrackingInterval,
options: options)
self.init(
appId: pinpointAnalyticsConfig.appId,
region: pinpointAnalyticsConfig.awsRegion,
autoSessionTrackingInterval: Self.defaultAutoSessionTrackingInterval,
options: options
)
}

init(appId: String,
region: String,
autoSessionTrackingInterval: TimeInterval,
options: AWSPinpointAnalyticsPlugin.Options) {
init(
appId: String,
region: String,
autoSessionTrackingInterval: TimeInterval,
options: AWSPinpointAnalyticsPlugin.Options
) {
self.appId = appId
self.region = region
self.autoSessionTrackingInterval = autoSessionTrackingInterval
Expand Down Expand Up @@ -148,7 +158,7 @@ public struct AWSPinpointAnalyticsPluginConfiguration {

private static func getAutoSessionTrackingInterval(_ configuration: [String: JSONValue]) throws -> TimeInterval {
guard let autoSessionTrackingInterval = configuration[autoSessionTrackingIntervalKey] else {
return Self.defaultAutoSessionTrackingInterval
return defaultAutoSessionTrackingInterval
}

guard case let .number(autoSessionTrackingIntervalValue) = autoSessionTrackingInterval else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation

typealias AnalyticsPluginErrorString = (errorDescription: ErrorDescription, recoverySuggestion: RecoverySuggestion)

struct AnalyticsPluginErrorConstant {
enum AnalyticsPluginErrorConstant {
static let decodeConfigurationError: AnalyticsPluginErrorString = (
"Unable to decode configuration",
"Make sure the plugin configuration is JSONValue"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import Foundation

extension HubCategory {
func dispatchIdentifyUser(_ identityId: String, userProfile: AnalyticsUserProfile?) {
let payload = HubPayload(eventName: HubPayload.EventName.Analytics.identifyUser,
data: (identityId, userProfile))
let payload = HubPayload(
eventName: HubPayload.EventName.Analytics.identifyUser,
data: (identityId, userProfile)
)
dispatch(to: .analytics, payload: payload)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import Amplify
import Foundation
@_spi(InternalAWSPinpoint) import InternalAWSPinpoint

extension PinpointEvent {
public func asAnalyticsEvent() -> AnalyticsEvent {
public extension PinpointEvent {
func asAnalyticsEvent() -> AnalyticsEvent {
var properties: AnalyticsProperties = [:]

for attribute in attributes {
Expand All @@ -21,12 +21,14 @@ extension PinpointEvent {
properties[metric.key] = metric.value
}

return BasicAnalyticsEvent(name: eventType,
properties: properties)
return BasicAnalyticsEvent(
name: eventType,
properties: properties
)
}
}

extension Array where Element == PinpointEvent {
extension [PinpointEvent] {
func asAnalyticsEventArray() -> [AnalyticsEvent] {
map { $0.asAnalyticsEvent() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
// SPDX-License-Identifier: Apache-2.0
//

import Foundation
import Amplify
import AWSPinpoint
import ClientRuntime
import Foundation

extension AWSPinpoint.BadRequestException: AnalyticsErrorConvertible {
var analyticsError: AnalyticsError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// SPDX-License-Identifier: Apache-2.0
//

import Foundation
import Amplify
import Foundation

protocol AnalyticsErrorConvertible {
var analyticsError: AnalyticsError { get }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
// SPDX-License-Identifier: Apache-2.0
//

import Foundation
import Amplify
import AwsCommonRuntimeKit
import Foundation

enum AnalyticsErrorHelper {
static func getDefaultError(_ error: Error) -> AnalyticsError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
// SPDX-License-Identifier: Apache-2.0
//

import Foundation
import Amplify
@_spi(InternalAWSPinpoint) import InternalAWSPinpoint
import AwsCommonRuntimeKit
import Foundation
@_spi(InternalAWSPinpoint) import InternalAWSPinpoint

extension CommonRunTimeError: AnalyticsErrorConvertible {
var analyticsError: AnalyticsError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// SPDX-License-Identifier: Apache-2.0
//

import XCTest
import AWSPinpointAnalyticsPlugin
import XCTest

// swiftlint:disable:next type_name
class AWSPinpointAnalyticsPluginAmplifyVersionableTests: XCTestCase {
Expand Down
Loading

0 comments on commit 47601cd

Please sign in to comment.