Skip to content

Commit

Permalink
fix: expose plan and ingestion metadata (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyang1520 authored Feb 21, 2023
1 parent c71dd44 commit da85455
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 6 deletions.
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

0 comments on commit da85455

Please sign in to comment.