Skip to content

Remove useless Codable requirement #386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Examples/other_plugins/NotificationTracking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class NotificationTracking: Plugin {
var type: PluginType = .utility
weak var analytics: Analytics?

func trackNotification(_ properties: [String: Codable], fromLaunch launch: Bool) {
func trackNotification(_ properties: [String: Encodable], fromLaunch launch: Bool) {
if launch {
analytics?.track(name: "Push Notification Tapped", properties: properties)
} else {
Expand All @@ -55,7 +55,7 @@ class NotificationTracking: Plugin {
// determination if a push notification caused the app to open.
extension NotificationTracking: RemoteNotifications {
func receivedRemoteNotification(userInfo: [AnyHashable: Any]) {
if let notification = userInfo as? [String: Codable] {
if let notification = userInfo as? [String: Encodable] {
trackNotification(notification, fromLaunch: false)
}
}
Expand Down Expand Up @@ -88,7 +88,7 @@ import UIKit

extension NotificationTracking: iOSLifecycle {
func application(_ application: UIApplication?, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) {
if let notification = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [String: Codable] {
if let notification = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [String: Encodable] {
trackNotification(notification, fromLaunch: true)
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Segment/Analytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ extension Analytics {
}

/// Returns the traits that were specified in the last identify call.
public func traits<T: Codable>() -> T? {
public func traits<T: Decodable>() -> T? {
if let userInfo: UserInfo = store.currentState() {
return userInfo.traits.codableValue()
}
Expand Down Expand Up @@ -385,7 +385,7 @@ extension Analytics {
}
```
*/
public func openURL<T: Codable>(_ url: URL, options: T? = nil) {
public func openURL<T: Encodable>(_ url: URL, options: T? = nil) {
guard let jsonProperties = try? JSON(with: options) else { return }
guard let dict = jsonProperties.dictionaryValue else { return }
openURL(url, options: dict)
Expand Down
20 changes: 10 additions & 10 deletions Sources/Segment/Events.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension Analytics {
/// - name: Name of the action, e.g., 'Purchased a T-Shirt'
/// - properties: Properties specific to the named event. For example, an event with
/// the name 'Purchased a Shirt' might have properties like revenue or size.
public func track<P: Codable>(name: String, properties: P?) {
public func track<P: Encodable>(name: String, properties: P?) {
do {
if let properties = properties {
let jsonProperties = try JSON(with: properties)
Expand Down Expand Up @@ -50,7 +50,7 @@ extension Analytics {
/// generate the UUID and Apple's policies on IDs, see
/// https://segment.io/libraries/ios#ids
/// - traits: A dictionary of traits you know about the user. Things like: email, name, plan, etc.
public func identify<T: Codable>(userId: String, traits: T?) {
public func identify<T: Encodable>(userId: String, traits: T?) {
do {
if let traits = traits {
let jsonTraits = try JSON(with: traits)
Expand All @@ -70,7 +70,7 @@ extension Analytics {
/// Associate a user with their unique ID and record traits about them.
/// - Parameters:
/// - traits: A dictionary of traits you know about the user. Things like: email, name, plan, etc.
public func identify<T: Codable>(traits: T) {
public func identify<T: Encodable>(traits: T) {
do {
let jsonTraits = try JSON(with: traits)
store.dispatch(action: UserInfo.SetTraitsAction(traits: jsonTraits))
Expand All @@ -93,7 +93,7 @@ extension Analytics {
process(incomingEvent: event)
}

public func screen<P: Codable>(title: String, category: String? = nil, properties: P?) {
public func screen<P: Encodable>(title: String, category: String? = nil, properties: P?) {
do {
if let properties = properties {
let jsonProperties = try JSON(with: properties)
Expand All @@ -112,7 +112,7 @@ extension Analytics {
screen(title: title, category: category, properties: nil as ScreenEvent?)
}

public func group<T: Codable>(groupId: String, traits: T?) {
public func group<T: Encodable>(groupId: String, traits: T?) {
do {
if let traits = traits {
let jsonTraits = try JSON(with: traits)
Expand Down Expand Up @@ -234,7 +234,7 @@ extension Analytics {
/// - properties: Properties specific to the named event. For example, an event with
/// the name 'Purchased a Shirt' might have properties like revenue or size.
/// - enrichments: Enrichments to be applied to this specific event only, or `nil` for none.
public func track<P: Codable>(name: String, properties: P?, enrichments: [EnrichmentClosure]?) {
public func track<P: Encodable>(name: String, properties: P?, enrichments: [EnrichmentClosure]?) {
do {
if let properties = properties {
let jsonProperties = try JSON(with: properties)
Expand Down Expand Up @@ -287,7 +287,7 @@ extension Analytics {
/// https://segment.io/libraries/ios#ids
/// - traits: A dictionary of traits you know about the user. Things like: email, name, plan, etc.
/// - enrichments: Enrichments to be applied to this specific event only, or `nil` for none.
public func identify<T: Codable>(userId: String, traits: T?, enrichments: [EnrichmentClosure]?) {
public func identify<T: Encodable>(userId: String, traits: T?, enrichments: [EnrichmentClosure]?) {
do {
if let traits = traits {
let jsonTraits = try JSON(with: traits)
Expand All @@ -308,7 +308,7 @@ extension Analytics {
/// - Parameters:
/// - traits: A dictionary of traits you know about the user. Things like: email, name, plan, etc.
/// - enrichments: Enrichments to be applied to this specific event only, or `nil` for none.
public func identify<T: Codable>(traits: T, enrichments: [EnrichmentClosure]?) {
public func identify<T: Encodable>(traits: T, enrichments: [EnrichmentClosure]?) {
do {
let jsonTraits = try JSON(with: traits)
store.dispatch(action: UserInfo.SetTraitsAction(traits: jsonTraits))
Expand Down Expand Up @@ -366,7 +366,7 @@ extension Analytics {
/// - category: A category to the type of screen if it applies.
/// - properties: Any extra metadata associated with the screen. e.g. method of access, size, etc.
/// - enrichments: Enrichments to be applied to this specific event only, or `nil` for none.
public func screen<P: Codable>(title: String, category: String? = nil, properties: P?, enrichments: [EnrichmentClosure]?) {
public func screen<P: Encodable>(title: String, category: String? = nil, properties: P?, enrichments: [EnrichmentClosure]?) {
do {
if let properties = properties {
let jsonProperties = try JSON(with: properties)
Expand Down Expand Up @@ -411,7 +411,7 @@ extension Analytics {
process(incomingEvent: event, enrichments: enrichments)
}

public func group<T: Codable>(groupId: String, traits: T?, enrichments: [EnrichmentClosure]?) {
public func group<T: Encodable>(groupId: String, traits: T?, enrichments: [EnrichmentClosure]?) {
do {
if let traits = traits {
let jsonTraits = try JSON(with: traits)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Segment/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public struct Settings: Codable {
return result
}

public func integrationSettings<T: Codable>(forKey key: String) -> T? {
public func integrationSettings<T: Decodable>(forKey key: String) -> T? {
var result: T? = nil
guard let settings = integrations?.dictionaryValue else { return nil }
if let dict = settings[key], let jsonData = try? JSONSerialization.data(withJSONObject: dict) {
Expand All @@ -85,7 +85,7 @@ public struct Settings: Codable {
return result
}

public func integrationSettings<T: Codable>(forPlugin plugin: DestinationPlugin) -> T? {
public func integrationSettings<T: Decodable>(forPlugin plugin: DestinationPlugin) -> T? {
return integrationSettings(forKey: plugin.key)
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Segment/Utilities/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public enum JSON: Equatable {
}

// For Value types
public init<T: Codable>(with value: T) throws {
public init<T: Encodable>(with value: T) throws {
let encoder = JSONSafeEncoder.default
let json = try encoder.encode(value)
let output = try JSONSerialization.jsonObject(with: json, options: .fragmentsAllowed)
Expand Down Expand Up @@ -222,7 +222,7 @@ extension JSON {
return result as Any
}

public func codableValue<T: Codable>() -> T? {
public func codableValue<T: Decodable>() -> T? {
var result: T? = nil
if let dict = dictionaryValue, let jsonData = try? JSONSerialization.data(withJSONObject: dict) {
do {
Expand Down
6 changes: 3 additions & 3 deletions Sources/Segment/Utilities/Storage/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal class Storage: Subscriber {
}
}

func write<T: Codable>(_ key: Storage.Constants, value: T?) {
func write<T: Encodable>(_ key: Storage.Constants, value: T?) {
switch key {
case .events:
if let event = value as? RawEvent {
Expand Down Expand Up @@ -98,7 +98,7 @@ internal class Storage: Subscriber {
return nil
}

func read<T: Codable>(_ key: Storage.Constants) -> T? {
func read<T: Decodable>(_ key: Storage.Constants) -> T? {
var result: T? = nil
switch key {
case .events:
Expand Down Expand Up @@ -134,7 +134,7 @@ internal class Storage: Subscriber {
}
}

func isBasicType<T: Codable>(value: T?) -> Bool {
func isBasicType<T: Encodable>(value: T?) -> Bool {
var result = false
if value == nil {
result = true
Expand Down