Skip to content

Commit 2e32611

Browse files
author
Nacho Bonafonte
committed
Add first set of tests
Rename folder to follow naming conventions (first capital letters) Add some Equatable conformance
1 parent 459f32d commit 2e32611

21 files changed

+267
-57
lines changed

Sources/OpenTelemetryApi/Common/AttributeValue.swift

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,37 @@ public enum AttributeValue: Equatable, CustomStringConvertible, Hashable {
6161
}
6262
}
6363

64-
internal struct AttributeValueExplicitCodable : Codable {
64+
public extension AttributeValue {
65+
init(_ value: String) {
66+
self = .string(value)
67+
}
68+
69+
init(_ value: Bool) {
70+
self = .bool(value)
71+
}
72+
73+
init(_ value: Int) {
74+
self = .int(value)
75+
}
76+
77+
init(_ value: Double) {
78+
self = .double(value)
79+
}
80+
81+
init(_ value: [String]) {
82+
self = .stringArray(value)
83+
}
84+
85+
init(_ value: [Int]) {
86+
self = .intArray(value)
87+
}
88+
89+
init(_ value: [Double]) {
90+
self = .doubleArray(value)
91+
}
92+
}
93+
94+
internal struct AttributeValueExplicitCodable: Codable {
6595
let attributeValue: AttributeValue
6696

6797
enum CodingKeys: String, CodingKey {
@@ -122,32 +152,31 @@ internal struct AttributeValueExplicitCodable : Codable {
122152
}
123153

124154
public func encode(to encoder: Encoder) throws {
125-
126155
var container = encoder.container(keyedBy: CodingKeys.self)
127156

128157
switch self.attributeValue {
129-
case .string(let value):
158+
case let .string(value):
130159
var nestedContainer = container.nestedContainer(keyedBy: AssociatedValueCodingKeys.self, forKey: .string)
131160
try nestedContainer.encode(value, forKey: .associatedValue)
132-
case .bool(let value):
161+
case let .bool(value):
133162
var nestedContainer = container.nestedContainer(keyedBy: AssociatedValueCodingKeys.self, forKey: .bool)
134163
try nestedContainer.encode(value, forKey: .associatedValue)
135-
case .int(let value):
164+
case let .int(value):
136165
var nestedContainer = container.nestedContainer(keyedBy: AssociatedValueCodingKeys.self, forKey: .int)
137166
try nestedContainer.encode(value, forKey: .associatedValue)
138-
case .double(let value):
167+
case let .double(value):
139168
var nestedContainer = container.nestedContainer(keyedBy: AssociatedValueCodingKeys.self, forKey: .double)
140169
try nestedContainer.encode(value, forKey: .associatedValue)
141-
case .stringArray(let value):
170+
case let .stringArray(value):
142171
var nestedContainer = container.nestedContainer(keyedBy: AssociatedValueCodingKeys.self, forKey: .stringArray)
143172
try nestedContainer.encode(value, forKey: .associatedValue)
144-
case .boolArray(let value):
173+
case let .boolArray(value):
145174
var nestedContainer = container.nestedContainer(keyedBy: AssociatedValueCodingKeys.self, forKey: .boolArray)
146175
try nestedContainer.encode(value, forKey: .associatedValue)
147-
case .intArray(let value):
176+
case let .intArray(value):
148177
var nestedContainer = container.nestedContainer(keyedBy: AssociatedValueCodingKeys.self, forKey: .intArray)
149178
try nestedContainer.encode(value, forKey: .associatedValue)
150-
case .doubleArray(let value):
179+
case let .doubleArray(value):
151180
var nestedContainer = container.nestedContainer(keyedBy: AssociatedValueCodingKeys.self, forKey: .doubleArray)
152181
try nestedContainer.encode(value, forKey: .associatedValue)
153182
}
@@ -157,11 +186,10 @@ internal struct AttributeValueExplicitCodable : Codable {
157186
#if swift(>=5.5)
158187
// swift 5.5 supports synthesizing Codable for enums with associated values
159188
// see https://github.com/apple/swift-evolution/blob/main/proposals/0295-codable-synthesis-for-enums-with-associated-values.md
160-
extension AttributeValue: Codable { }
189+
extension AttributeValue: Codable {}
161190
#else
162191
// for older swift versions use a forward compatible explicit Codable implementation
163192
extension AttributeValue: Codable {
164-
165193
public init(from decoder: Decoder) throws {
166194
let explicitDecoded = try AttributeValueExplicitCodable(from: decoder)
167195

Sources/OpenTelemetrySdk/Common/InstrumentationLibraryInfo.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Foundation
77

88
/// Holds information about the instrumentation library specified when creating an instance of
99
/// TracerSdk using TracerProviderSdk.
10-
public struct InstrumentationScopeInfo: Hashable, Codable {
10+
public struct InstrumentationScopeInfo: Hashable, Codable, Equatable {
1111
public private(set) var name: String = ""
1212
public private(set) var version: String?
1313
public private(set) var schemaUrl : String?
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// Copyright The OpenTelemetry Authors
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
6+
import Foundation
7+
import OpenTelemetryApi
8+
9+
public class ExemplarData: Equatable {
10+
internal init(epochNanos: UInt64, filteredAttributes: [String: AttributeValue], spanContext: SpanContext? = nil) {
11+
self.filteredAttributes = filteredAttributes
12+
self.epochNanos = epochNanos
13+
self.spanContext = spanContext
14+
}
15+
16+
public var filteredAttributes: [String: AttributeValue]
17+
public var epochNanos: UInt64
18+
public var spanContext: OpenTelemetryApi.SpanContext?
19+
20+
public static func == (lhs: ExemplarData, rhs: ExemplarData) -> Bool {
21+
return type(of: lhs) == type(of: rhs) && lhs.isEqual(to: rhs)
22+
}
23+
24+
func isEqual(to other: ExemplarData) -> Bool {
25+
return self.epochNanos == other.epochNanos &&
26+
self.spanContext == other.spanContext &&
27+
self.filteredAttributes == other.filteredAttributes
28+
}
29+
}
30+
31+
public final class DoubleExemplarData: ExemplarData {
32+
public var value: Double
33+
34+
internal init(value: Double, epochNanos: UInt64, filteredAttributes: [String: AttributeValue], spanContext: SpanContext? = nil) {
35+
self.value = value
36+
super.init(epochNanos: epochNanos, filteredAttributes: filteredAttributes, spanContext: spanContext)
37+
}
38+
39+
override func isEqual(to other: ExemplarData) -> Bool {
40+
return self.value == (other as! DoubleExemplarData).value &&
41+
(self as ExemplarData).isEqual(to: other)
42+
}
43+
}
44+
45+
public final class LongExemplarData: ExemplarData {
46+
public var value: Int
47+
internal init(value: Int, epochNanos: UInt64, filteredAttributes: [String: AttributeValue], spanContext: SpanContext? = nil) {
48+
self.value = value
49+
super.init(epochNanos: epochNanos, filteredAttributes: filteredAttributes, spanContext: spanContext)
50+
}
51+
52+
override func isEqual(to other: ExemplarData) -> Bool {
53+
return self.value == (other as! LongExemplarData).value &&
54+
(self as ExemplarData).isEqual(to: other)
55+
}
56+
}

0 commit comments

Comments
 (0)