Skip to content
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

fix: expose plan and ingestion metadata #41

Merged
merged 1 commit into from
Feb 21, 2023
Merged
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
35 changes: 29 additions & 6 deletions Sources/Amplitude/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,38 @@
import Foundation

public struct Plan: Codable {
var branch: String?
var source: String?
var version: String?
var versionId: String?
public var branch: String?
public var source: String?
public var version: String?
public var versionId: String?

public init(branch: String? = nil, source: String? = nil, version: String? = nil, versionId: String? = nil) {
self.branch = branch
self.source = source
self.version = version
self.versionId = versionId
}
}

public struct IngestionMetadata: Codable {
var sourceName: String?
var sourceVersion: String?
public var sourceName: String?
public var sourceVersion: String?

enum CodingKeys: String, CodingKey {
case sourceName = "source_name"
case sourceVersion = "source_version"
}

public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
sourceName = try values.decodeIfPresent(String.self, forKey: .sourceName)
sourceVersion = try values.decodeIfPresent(String.self, forKey: .sourceVersion)
}

public init(sourceName: String? = nil, sourceVersion: String? = nil) {
self.sourceName = sourceName
self.sourceVersion = sourceVersion
}
}

public typealias EventCallback = (BaseEvent, Int, String) -> Void
Expand Down
68 changes: 68 additions & 0 deletions Tests/AmplitudeTests/Events/BaseEventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ import XCTest
final class BaseEventTests: XCTestCase {
func testToString() {
let baseEvent = BaseEvent(
plan: Plan(
branch: "test-branch",
source: "test-source",
version: "test-version",
versionId: "test-version-id"
),
ingestionMetadata: IngestionMetadata(
sourceName: "test-source-name",
sourceVersion: "test-source-version"
),
eventType: "test",
eventProperties: [
"integer": 1,
Expand Down Expand Up @@ -40,6 +50,30 @@ final class BaseEventTests: XCTestCase {
baseEventDict!["event_properties"]!["array" as NSString] as! Array,
[1, 2, 3]
)
XCTAssertEqual(
baseEventDict!["plan"]!["branch" as NSString] as! String,
"test-branch"
)
XCTAssertEqual(
baseEventDict!["plan"]!["source" as NSString] as! String,
"test-source"
)
XCTAssertEqual(
baseEventDict!["plan"]!["version" as NSString] as! String,
"test-version"
)
XCTAssertEqual(
baseEventDict!["plan"]!["versionId" as NSString] as! String,
"test-version-id"
)
XCTAssertEqual(
baseEventDict!["ingestion_metadata"]!["source_name" as NSString] as! String,
"test-source-name"
)
XCTAssertEqual(
baseEventDict!["ingestion_metadata"]!["source_version" as NSString] as! String,
"test-source-version"
)
}

func testToString_withNilValues() {
Expand Down Expand Up @@ -89,6 +123,16 @@ final class BaseEventTests: XCTestCase {
"integer": 1,
"string": "stringValue",
"array": [1, 2, 3]
},
"plan": {
"branch": "test-branch",
"source": "test-source",
"version": "test-version",
"versionId": "test-version-id",
},
"ingestion_metadata": {
"source_name": "test-source-name",
"source_version": "test-source-version"
}
}
"""
Expand All @@ -110,6 +154,30 @@ final class BaseEventTests: XCTestCase {
event?.eventProperties!["array"] as! [Double],
[1, 2, 3]
)
XCTAssertEqual(
event?.plan?.branch,
"test-branch"
)
XCTAssertEqual(
event?.plan?.source,
"test-source"
)
XCTAssertEqual(
event?.plan?.version,
"test-version"
)
XCTAssertEqual(
event?.plan?.versionId,
"test-version-id"
)
XCTAssertEqual(
event?.ingestionMetadata?.sourceName,
"test-source-name"
)
XCTAssertEqual(
event?.ingestionMetadata?.sourceVersion,
"test-source-version"
)
}

func testFromString_withNullValues() {
Expand Down