Skip to content

Commit

Permalink
Remove requirement for all configurations to be serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
matus-tomlein committed Dec 30, 2022
1 parent 21d0e34 commit 41ad8f6
Show file tree
Hide file tree
Showing 20 changed files with 78 additions and 207 deletions.
4 changes: 0 additions & 4 deletions Sources/Core/GDPR/GDPRConfigurationUpdate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,4 @@ class GDPRConfigurationUpdate: GDPRConfiguration {
public init() {
super.init(basis: .consent, documentId: nil, documentVersion: nil, documentDescription: nil)
}

required init?(coder: NSCoder) {
super.init(coder: coder)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import Foundation

class GlobalContextPluginConfiguration: Configuration, PluginConfigurationProtocol {
class GlobalContextPluginConfiguration: NSObject, ConfigurationProtocol, PluginConfigurationProtocol {
private(set) var identifier: String
private(set) var globalContext: GlobalContext
private(set) var afterTrackConfiguration: PluginAfterTrackConfiguration? = nil
Expand All @@ -32,20 +32,4 @@ class GlobalContextPluginConfiguration: Configuration, PluginConfigurationProtoc
self.globalContext = globalContext
self.entitiesConfiguration = PluginEntitiesConfiguration(closure: globalContext.contexts)
}

// MARK: - NSCopying

override func copy(with zone: NSZone? = nil) -> Any {
let copy = GlobalContextPluginConfiguration(
identifier: identifier,
globalContext: globalContext
)
return copy
}

// MARK: - NSCoding (No coding possible as we can't encode and decode the contextGenerators)

required convenience public init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import Foundation

class FetchedConfigurationBundle: Configuration {
class FetchedConfigurationBundle: SerializableConfiguration {
var schema: String
var configurationVersion: Int
var configurationBundle: [ConfigurationBundle] = []
Expand Down
6 changes: 3 additions & 3 deletions Sources/Core/Tracker/ServiceProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class ServiceProvider: NSObject, ServiceProviderProtocol {

// MARK: - Init

init(namespace: String, network networkConfiguration: NetworkConfiguration, configurations: [Configuration]) {
init(namespace: String, network networkConfiguration: NetworkConfiguration, configurations: [ConfigurationProtocol]) {
self.namespace = namespace
super.init()

Expand All @@ -134,7 +134,7 @@ class ServiceProvider: NSObject, ServiceProviderProtocol {
let _ = tracker // Build tracker to initialize NotificationCenter receivers
}

func reset(configurations: [Configuration]) {
func reset(configurations: [ConfigurationProtocol]) {
stopServices()
resetConfigurationUpdates()
processConfigurations(configurations)
Expand All @@ -152,7 +152,7 @@ class ServiceProvider: NSObject, ServiceProviderProtocol {

// MARK: - Private methods

func processConfigurations(_ configurations: [Configuration]) {
func processConfigurations(_ configurations: [ConfigurationProtocol]) {
for configuration in configurations {
if let configuration = configuration as? NetworkConfiguration {
networkConfigurationUpdate.sourceConfig = configuration
Expand Down
9 changes: 5 additions & 4 deletions Sources/Snowplow/Configurations/ConfigurationBundle.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// SPConfigurationBundle.swift
//
// ConfigurationBundle.swift
// Snowplow
//
// Copyright (c) 2013-2022 Snowplow Analytics Ltd. All rights reserved.
Expand All @@ -22,7 +23,7 @@ import Foundation

/// This class represents the default configuration applied in place of the remote configuration.
@objc(SPConfigurationBundle)
public class ConfigurationBundle: Configuration {
public class ConfigurationBundle: SerializableConfiguration {
@objc
private(set) public var namespace: String
@objc
Expand All @@ -35,8 +36,8 @@ public class ConfigurationBundle: Configuration {
public var sessionConfiguration: SessionConfiguration?

@objc
public var configurations: [Configuration] {
var array: [Configuration] = []
public var configurations: [ConfigurationProtocol] {
var array: [ConfigurationProtocol] = []
if let networkConfiguration = networkConfiguration {
array.append(networkConfiguration)
}
Expand Down
27 changes: 27 additions & 0 deletions Sources/Snowplow/Configurations/ConfigurationProtocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Configuration.swift
// Snowplow
//
// Copyright (c) 2013-2022 Snowplow Analytics Ltd. All rights reserved.
//
// This program is licensed to you under the Apache License Version 2.0,
// and you may not use this file except in compliance with the Apache License
// Version 2.0. You may obtain a copy of the Apache License Version 2.0 at
// http://www.apache.org/licenses/LICENSE-2.0.
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the Apache License Version 2.0 is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the Apache License Version 2.0 for the specific
// language governing permissions and limitations there under.
//
// Authors: Alex Benini
// License: Apache License Version 2.0
//

import Foundation

/// Common parent protocol for configuration classes.
@objc(SPConfigurationProtocol)
public protocol ConfigurationProtocol {
}
2 changes: 1 addition & 1 deletion Sources/Snowplow/Configurations/ConfigurationState.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// SPConfigurationState.h
// ConfigurationState.h
// Snowplow
//
// Copyright (c) 2013-2022 Snowplow Analytics Ltd. All rights reserved.
Expand Down
51 changes: 3 additions & 48 deletions Sources/Snowplow/Configurations/EmitterConfiguration.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// SPEmitterConfiguration.swift
//
// EmitterConfiguration.swift
// Snowplow
//
// Copyright (c) 2013-2022 Snowplow Analytics Ltd. All rights reserved.
Expand Down Expand Up @@ -54,7 +55,7 @@ public protocol EmitterConfigurationProtocol: AnyObject {
/// The EmitterConfiguration can be used to setup details about how the tracker should treat the events
/// to emit to the collector.
@objc(SPEmitterConfiguration)
public class EmitterConfiguration: Configuration, EmitterConfigurationProtocol {
public class EmitterConfiguration: NSObject, EmitterConfigurationProtocol, ConfigurationProtocol {
/// Sets whether the buffer should send events instantly or after the buffer
/// has reached it's limit. By default, this is set to BufferOption Default.
@objc
Expand Down Expand Up @@ -106,50 +107,4 @@ public class EmitterConfiguration: Configuration, EmitterConfigurationProtocol {
public override init() {
super.init()
}

// MARK: - NSCopying

@objc
public override func copy(with zone: NSZone? = nil) -> Any {
let copy = EmitterConfiguration()
copy.bufferOption = bufferOption
copy.emitRange = emitRange
copy.threadPoolSize = threadPoolSize
copy.byteLimitGet = byteLimitGet
copy.byteLimitPost = byteLimitPost
copy.requestCallback = requestCallback
copy.eventStore = eventStore
copy.customRetryForStatusCodes = customRetryForStatusCodes
copy.serverAnonymisation = serverAnonymisation
return copy
}

// MARK: - NSSecureCoding

@objc
public override class var supportsSecureCoding: Bool { return true }

@objc
public override func encode(with coder: NSCoder) {
coder.encode(bufferOption, forKey: "bufferOption")
coder.encode(emitRange, forKey: "emitRange")
coder.encode(threadPoolSize, forKey: "threadPoolSize")
coder.encode(byteLimitGet, forKey: "byteLimitGet")
coder.encode(byteLimitPost, forKey: "byteLimitPost")
coder.encode(customRetryForStatusCodes, forKey: "customRetryForStatusCodes")
coder.encode(serverAnonymisation, forKey: "serverAnonymisation")
}

required init?(coder: NSCoder) {
super.init()
if let bufferOption = BufferOption(rawValue: coder.decodeInteger(forKey: "bufferOption")) {
self.bufferOption = bufferOption
}
emitRange = coder.decodeInteger(forKey: "emitRange")
threadPoolSize = coder.decodeInteger(forKey: "threadPoolSize")
byteLimitGet = coder.decodeInteger(forKey: "byteLimitGet")
byteLimitPost = coder.decodeInteger(forKey: "byteLimitPost")
customRetryForStatusCodes = coder.decodeObject(forKey: "customRetryForStatusCodes") as? [Int : Bool]
serverAnonymisation = coder.decodeBool(forKey: "serverAnonymisation")
}
}
8 changes: 8 additions & 0 deletions Sources/Snowplow/Configurations/FocalMeterConfiguration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// File.swift
//
//
// Created by Matus Tomlein on 30/12/2022.
//

import Foundation
41 changes: 3 additions & 38 deletions Sources/Snowplow/Configurations/GDPRConfiguration.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// SPGDPRConfiguration.swift
//
// GDPRConfiguration.swift
// Snowplow
//
// Copyright (c) 2013-2022 Snowplow Analytics Ltd. All rights reserved.
Expand Down Expand Up @@ -38,7 +39,7 @@ public protocol GDPRConfigurationProtocol: AnyObject {

/// This class allows the GDPR configuration of the tracker.
@objc(SPGDPRConfiguration)
public class GDPRConfiguration: Configuration, GDPRConfigurationProtocol {
public class GDPRConfiguration: NSObject, GDPRConfigurationProtocol, ConfigurationProtocol {
/// Basis for processing.
@objc
public var basisForProcessing: GDPRProcessingBasis
Expand Down Expand Up @@ -71,40 +72,4 @@ public class GDPRConfiguration: Configuration, GDPRConfigurationProtocol {
self.documentDescription = documentDescription ?? ""
super.init()
}

// MARK: - NSCopying

@objc
public override func copy(with zone: NSZone? = nil) -> Any {
let copy = GDPRConfiguration(basis: basisForProcessing,
documentId: documentId,
documentVersion: documentVersion,
documentDescription: documentDescription)
return copy
}

// MARK: - NSSecureCodin

@objc
public override class var supportsSecureCoding: Bool { return true }

@objc
public override func encode(with coder: NSCoder) {
coder.encode(basisForProcessing.rawValue, forKey: "basisForProcessing")
coder.encode(documentId, forKey: "documentId")
coder.encode(documentVersion, forKey: "documentVersion")
coder.encode(documentDescription, forKey: "documentDescription")
}

required init?(coder: NSCoder) {
if let basisForProcessing = GDPRProcessingBasis(rawValue: coder.decodeInteger(forKey: "basisForProcessing")) {
self.basisForProcessing = basisForProcessing
self.documentId = coder.decodeObject(forKey: "documentId") as? String ?? ""
self.documentVersion = coder.decodeObject(forKey: "documentVersion") as? String ?? ""
self.documentDescription = coder.decodeObject(forKey: "documentDescription") as? String ?? ""
super.init()
} else {
return nil
}
}
}
16 changes: 3 additions & 13 deletions Sources/Snowplow/Configurations/GlobalContextsConfiguration.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// SPGlobalContextsConfiguration.swift
//
// GlobalContextsConfiguration.swift
// Snowplow
//
// Copyright (c) 2013-2022 Snowplow Analytics Ltd. All rights reserved.
Expand Down Expand Up @@ -40,7 +41,7 @@ public protocol GlobalContextsConfigurationProtocol: AnyObject {

/// This class allows the setup of Global Contexts which are attached to selected events.
@objc(SPGlobalContextsConfiguration)
public class GlobalContextsConfiguration: Configuration, GlobalContextsConfigurationProtocol {
public class GlobalContextsConfiguration: NSObject, GlobalContextsConfigurationProtocol, ConfigurationProtocol {
@objc
public var contextGenerators: [String : GlobalContext] = [:]

Expand All @@ -61,17 +62,6 @@ public class GlobalContextsConfiguration: Configuration, GlobalContextsConfigura
}
return toDelete
}

// MARK: - NSCopying

@objc
public override func copy(with zone: NSZone? = nil) -> Any {
let copy = GlobalContextsConfiguration()
copy.contextGenerators = contextGenerators
return copy
}

// MARK: - NSCoding (No coding possible as we can't encode and decode the contextGenerators)

// MARK: - Internal functions

Expand Down
5 changes: 3 additions & 2 deletions Sources/Snowplow/Configurations/NetworkConfiguration.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// SPNetworkConfiguration.swift
//
// NetworkConfiguration.swift
// Snowplow
//
// Copyright (c) 2013-2022 Snowplow Analytics Ltd. All rights reserved.
Expand All @@ -22,7 +23,7 @@ import Foundation

/// Represents the network communication configuration allowing the tracker to be able to send events to the Snowplow collector.
@objc(SPNetworkConfiguration)
public class NetworkConfiguration: Configuration {
public class NetworkConfiguration: SerializableConfiguration, ConfigurationProtocol {
/// URL (without schema/protocol) used to send events to the collector.
@objc
private(set) public var endpoint: String?
Expand Down
32 changes: 1 addition & 31 deletions Sources/Snowplow/Configurations/PluginConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ extension PluginConfigurationProtocol {
/// Configuration for a custom tracker plugin.
/// Enables you to add closures to be called when and after events are tracked in the tracker.
@objc(SPPluginConfiguration)
public class PluginConfiguration: Configuration, PluginConfigurationProtocol {
public class PluginConfiguration: NSObject, PluginConfigurationProtocol, ConfigurationProtocol {
/// Unique identifier of the plugin within the tracker.
public private(set) var identifier: String
/// Closure configuration that is called after events are tracked.
Expand Down Expand Up @@ -130,34 +130,4 @@ public class PluginConfiguration: Configuration, PluginConfigurationProtocol {
closure: closure
)
}

// MARK: - NSCopying

@objc
public override func copy(with zone: NSZone? = nil) -> Any {
let copy = PluginConfiguration(identifier: identifier)
if let afterTrack = afterTrackConfiguration {
copy.afterTrack(schemas: afterTrack.schemas, closure: afterTrack.closure)
}
if let entities = entitiesConfiguration {
copy.entities(schemas: entities.schemas, closure: entities.closure)
}
return copy
}

// MARK: - NSSecureCoding

@objc
public override class var supportsSecureCoding: Bool { return true }

@objc
public override func encode(with coder: NSCoder) {
coder.encode(identifier, forKey: "identifier")
}

required init?(coder: NSCoder) {
identifier = coder.decodeObject(forKey: "identifier") as? String ?? ""
super.init()
}

}
Loading

0 comments on commit 41ad8f6

Please sign in to comment.