Skip to content

Commit

Permalink
feat: Add PulsarInstant and PulsarTimestamp as PulsarPayload to exten…
Browse files Browse the repository at this point in the history
…d schemas
  • Loading branch information
flexlixrup committed Jan 2, 2025
1 parent 2ada2a7 commit dfb5151
Show file tree
Hide file tree
Showing 41 changed files with 432 additions and 212 deletions.
2 changes: 1 addition & 1 deletion .spi.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 1
builder:
configs:
- documentation_targets: [Pulsar]
- documentation_targets: [Pulsar, SchemaTypes]
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "SchemaTypes"
BuildableName = "SchemaTypes"
BlueprintName = "SchemaTypes"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
Expand Down
377 changes: 201 additions & 176 deletions LICENSE

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ let package = Package(
.library(
name: "Pulsar",
targets: ["Pulsar"]
),
.library(
name: "SchemaTypes",
targets: ["SchemaTypes"]
)
],
dependencies: [
Expand All @@ -25,7 +29,8 @@ let package = Package(
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
.product(name: "NIOSSL", package: "swift-nio-ssl"),
.product(name: "NIOFoundationCompat", package: "swift-nio"),
.product(name: "Logging", package: "swift-log")
.product(name: "Logging", package: "swift-log"),
.target(name: "SchemaTypes")
]

),
Expand All @@ -38,7 +43,8 @@ let package = Package(
.testTarget(
name: "PulsarTests",
dependencies: ["Pulsar"]
)
),
.target(name: "SchemaTypes")
],
swiftLanguageModes: [.v6]
)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/BackoffStrategy.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/ByteBuffer+PulsarPayload.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/CRC32C.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/ClientHandler/PulsarClientHandler.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/ConsumerCache.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/ConsumerStateHandler.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/HandlerStateMap.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/Message.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/ProducerAccessMode.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/ProducerCache.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/ProducerStateManager.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/Protos.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/PulsarClient/PulsarClient+Consumers.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/PulsarClient/PulsarClient+Producers.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
3 changes: 2 additions & 1 deletion Sources/Pulsar/PulsarClient/PulsarClient.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand All @@ -14,6 +14,7 @@

import Logging
import NIO
@_exported import SchemaTypes

/// The core Pulsar Client used to connect to the server. All control of the library, like consumers and producers and its settings are controlled via the Client.
public final actor PulsarClient {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/PulsarClientError.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/PulsarConsumer.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/PulsarFrameDecoder.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/PulsarFrameEncoder.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/PulsarMesage.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
47 changes: 46 additions & 1 deletion Sources/Pulsar/PulsarPayload.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand All @@ -15,13 +15,58 @@
import Foundation
import NIOCore
import NIOFoundationCompat
import SchemaTypes

/// The Pulsar Payload protocol enables types to be used as a payload of a Pulsar Message.
public protocol PulsarPayload: Sendable {
func encode() -> ByteBuffer
static func decode(from buffer: ByteBuffer) throws -> Self
}

extension PulsarInstant: PulsarPayload {
/// Encode into a ByteBuffer.
/// - Returns: The ByteBuffer.
public func encode() -> ByteBuffer {
var buffer = ByteBufferAllocator().buffer(capacity: 12)
buffer.writeInteger(seconds)
buffer.writeInteger(nanos)
return buffer
}
/// Decode from a ByteBuffer.
/// - Parameter buffer: The buffer to decode.
/// - Returns: The decoded type.
/// - Throws: DecodingError.dataCorrupted when the decoding fails.
public static func decode(from buffer: ByteBuffer) throws -> PulsarInstant {
var mutableBuffer = buffer
guard let seconds = mutableBuffer.readInteger(as: Int64.self) else {
throw DecodingError.dataCorrupted(.init(codingPath: [], debugDescription: "Failed to decode seconds."))
}
guard let nanos = mutableBuffer.readInteger(as: Int32.self) else {
throw DecodingError.dataCorrupted(.init(codingPath: [], debugDescription: "Failed to decode nanos."))
}
return PulsarInstant(seconds: seconds, nanos: nanos)

}

}

extension PulsarTimestamp: PulsarPayload {
/// Encode into a ByteBuffer.
/// - Returns: The ByteBuffer.
public func encode() -> ByteBuffer {
pulsarDate.encode()
}
/// Decode from a ByteBuffer.
/// - Parameter buffer: The buffer to decode.
/// - Returns: The decoded type.
/// - Throws: DecodingError.dataCorrupted when the decoding fails.
public static func decode(from buffer: ByteBuffer) throws -> PulsarTimestamp {
let timestamp = try Int64.decode(from: buffer)
return PulsarTimestamp(timestamp: timestamp)
}

}

extension Data: PulsarPayload {
/// Encode into a ByteBuffer.
/// - Returns: The ByteBuffer.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/PulsarProducer.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/PulsarSchema+Decode.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/PulsarSchema.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/SubscriptionMode.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulsar/SubscriptionType.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/PulsarExample/PulsarExample.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Felix Ruppert
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
Expand Down
28 changes: 28 additions & 0 deletions Sources/SchemaTypes/Date+PulsarInstant.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an AS IS BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation

extension Date {
/// The ``PulsarInstant`` representation of the date.
public var pulsarInstant: PulsarInstant {
PulsarInstant(date: self)
}

/// Initialize a Date with ``PulsarInstant``.
/// - Parameter pulsarInstant: The object to initialize with.
public init(pulsarInstant: PulsarInstant) {
self = pulsarInstant.date
}
}
28 changes: 28 additions & 0 deletions Sources/SchemaTypes/Date+PulsarTimestamp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2025 Felix Ruppert
//
// Licensed under the Apache License, Version 2.0 (the License );
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an AS IS BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation

extension Date {
/// The ``PulsarTimestamp`` representation of the date.
public var pulsarTimestamp: PulsarTimestamp {
PulsarTimestamp(timestamp: Int64(timeIntervalSince1970 * 1000))
}

/// Initialize a Date with ``pulsarTimestamp``.
/// - Parameter pulsarTimestamp: The object to initialize with.
public init(pulsarTimestamp: PulsarTimestamp) {
self = Date(timeIntervalSince1970: Double(pulsarTimestamp.pulsarDate) / 1000)
}
}
Loading

0 comments on commit dfb5151

Please sign in to comment.