66import Foundation
77import OpenTelemetryApi
88
9- public protocol AttributeProcessorProtocol {
9+ public protocol AttributeProcessor {
1010 func process( incoming : [ String : AttributeValue ] ) -> [ String : AttributeValue ]
1111}
12- public class AttributeProcessor : AttributeProcessorProtocol {
13-
14- public func then( other : AttributeProcessor ) -> AttributeProcessor {
12+
13+ public extension AttributeProcessor {
14+ func then( other : AttributeProcessor ) -> AttributeProcessor {
1515 if type ( of: other) == NoopAttributeProcessor . self {
1616 return self
1717 }
1818 if type ( of: self ) == NoopAttributeProcessor . self {
1919 return other
2020 }
2121
22- if type ( of : other ) == JoinedAttributeProcessor . self {
23- return ( other as! JoinedAttributeProcessor ) . prepend ( processor: self )
22+ if let joined = self as? JoinedAttributeProcessor {
23+ return joined . append ( processor: other )
2424 }
25+ if let joined = other as? JoinedAttributeProcessor {
26+ return joined. prepend ( processor: self )
27+ }
28+
2529 return JoinedAttributeProcessor ( [ self , other] )
2630 }
31+ }
32+
33+ public class SimpleAttributeProcessor : AttributeProcessor {
34+ let attributeProcessor : ( [ String : AttributeValue ] ) -> [ String : AttributeValue ]
2735
36+ init ( attributeProcessor : @escaping ( [ String : AttributeValue ] ) -> [ String : AttributeValue ] ) {
37+ self . attributeProcessor = attributeProcessor
38+ }
2839
2940 public func process( incoming: [ String : AttributeValue ] ) -> [ String : AttributeValue ] {
30- return incoming
41+ return attributeProcessor ( incoming)
3142 }
3243
33- public static func filterByKeyName( nameFilter : @escaping ( String ) -> Bool ) -> AttributeProcessor {
44+ static func filterByKeyName( nameFilter : @escaping ( String ) -> Bool ) -> AttributeProcessor {
3445 return SimpleAttributeProcessor { attributes in
3546 attributes. filter { key, value in
3647 nameFilter ( key)
3748 }
3849 }
3950 }
4051
41- public static func append( attributes: [ String : AttributeValue ] ) -> AttributeProcessor {
52+ static func append( attributes: [ String : AttributeValue ] ) -> AttributeProcessor {
4253 SimpleAttributeProcessor { incoming in
4354 incoming. merging ( attributes) { a, b in
4455 b
4556 }
4657 }
4758 }
48-
49-
50- }
51-
52- internal class SimpleAttributeProcessor : AttributeProcessor {
53-
54- let attributeProcessor : ( [ String : AttributeValue ] ) -> [ String : AttributeValue ]
55-
56- init ( attributeProcessor : @escaping ( [ String : AttributeValue ] ) -> [ String : AttributeValue ] ) {
57- self . attributeProcessor = attributeProcessor
58-
59- }
60-
61- override func process( incoming: [ String : OpenTelemetryApi . AttributeValue ] ) -> [ String : OpenTelemetryApi . AttributeValue ] {
62- return attributeProcessor ( incoming)
63- }
64-
65-
6659}
6760
6861
6962public class JoinedAttributeProcessor : AttributeProcessor {
7063
71- override public func process( incoming: [ String : OpenTelemetryApi . AttributeValue ] ) -> [ String : OpenTelemetryApi . AttributeValue ] {
64+ public func process( incoming: [ String : AttributeValue ] ) -> [ String : AttributeValue ] {
7265 var result = incoming
7366 for processor in processors {
7467 result = processor. process ( incoming: result)
7568 }
7669 return result
7770 }
7871
79- override public func then ( other : AttributeProcessor ) -> AttributeProcessor {
72+ public func append ( processor : AttributeProcessor ) -> JoinedAttributeProcessor {
8073 var newList = processors
81- if let otherJoined = other as? JoinedAttributeProcessor {
82- newList. append ( contentsOf: otherJoined . processors)
74+ if let joinedProcessor = processor as? JoinedAttributeProcessor {
75+ newList. append ( contentsOf: joinedProcessor . processors)
8376 } else {
84- newList. append ( other )
77+ newList. append ( processor )
8578 }
8679 return JoinedAttributeProcessor ( newList)
8780 }
8881
89- public func prepend( processor: AttributeProcessor ) -> AttributeProcessor {
82+ public func prepend( processor: AttributeProcessor ) -> JoinedAttributeProcessor {
9083 var newProcessors = [ processor]
9184 newProcessors. append ( contentsOf: processors)
9285 return JoinedAttributeProcessor ( newProcessors)
@@ -101,8 +94,8 @@ public class JoinedAttributeProcessor : AttributeProcessor {
10194
10295public class NoopAttributeProcessor : AttributeProcessor {
10396 static let noop = NoopAttributeProcessor ( )
104- private override init ( ) { }
105- override public func process( incoming: [ String : AttributeValue ] ) -> [ String : AttributeValue ] {
97+ private init ( ) { }
98+ public func process( incoming: [ String : AttributeValue ] ) -> [ String : AttributeValue ] {
10699 return incoming
107100 }
108101}
0 commit comments