Skip to content

Commit

Permalink
feat: Flatten OSCBundles (Issue-#8)
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Smallman <srsmallman@mac.com>
  • Loading branch information
sammysmallman committed Aug 14, 2023
1 parent 0ec3c03 commit 95a9371
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Sources/CoreOSC/CoreOSC.swift
Expand Up @@ -26,7 +26,7 @@ import Foundation
public enum CoreOSC {

/// This package's semantic version number, mirrored also in git history as a `git tag`.
public static let version: String = "1.2.1"
public static let version: String = "1.3.0"

/// The license agreement this repository is licensed under.
public static let license: String = {
Expand Down
14 changes: 14 additions & 0 deletions Sources/CoreOSC/OSCBundle.swift
Expand Up @@ -66,4 +66,18 @@ public struct OSCBundle: OSCPacket {
return result
}

/// Flatten the elements contained by the bundle, ignoring all `OSCTimeTag`'s.
/// - Returns: An array of `OSCMessage`'s.
public func flatten() -> [OSCMessage] {
elements.reduce([OSCMessage]()) {
if let message = $1 as? OSCMessage {
return $0 + [message]
} else if let bundle = $1 as? OSCBundle {
return $0 + bundle.flatten()
} else {
return $0
}
}
}

}
10 changes: 8 additions & 2 deletions Sources/CoreOSC/OSCMessage.swift
Expand Up @@ -24,8 +24,14 @@
import Foundation

/// An OSC Message.
public struct OSCMessage: OSCPacket {

public struct OSCMessage: OSCPacket, Equatable {

public static func == (lhs: OSCMessage, rhs: OSCMessage) -> Bool {
lhs.addressPattern == rhs.addressPattern &&
lhs.typeTagString == rhs.typeTagString &&
lhs.arguments.map { $0.oscData } == rhs.arguments.map { $0.oscData }
}

/// The OSC Address Pattern associated with the message that represents the full path to one
/// or more OSC Methods through pattern matching.
public private(set) var addressPattern: OSCAddressPattern
Expand Down
3 changes: 3 additions & 0 deletions Sources/CoreOSC/en.lproj/Localizable.strings
@@ -1,5 +1,8 @@
/* OSC Address Error */

/* OSC Version */
"OSC_VERSION" = "1.3.0";

/* OSC Address Error: Invalid Address */
"OSC_ADDRESS_ERROR_INVALID_ADDRESS" = "Invalid address";

Expand Down
3 changes: 2 additions & 1 deletion Tests/CoreOSCTests/CoreOSCTests.swift
Expand Up @@ -29,7 +29,8 @@ import XCTest
class CoreOSCTests: XCTestCase {

func testVersion() {
XCTAssertEqual(CoreOSC.version, "1.2.1")
XCTAssertEqual(CoreOSC.version, "1.3.0")
XCTAssertEqual(NSLocalizedString("OSC_VERSION", bundle: .module, comment: "OSC Version"), CoreOSC.version)
}

func testLicense() {
Expand Down
82 changes: 82 additions & 0 deletions Tests/CoreOSCTests/OSCBundleTests.swift
@@ -0,0 +1,82 @@
//
// OSCBundleTests.swift
// CoreOSCTests
//
// Created by Sam Smallman on 14/08/2023.
// Copyright © 2023 Sam Smallman. https://github.com/SammySmallman
//
// This file is part of CoreOSC
//
// CoreOSC is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// CoreOSC is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

import XCTest

@testable import CoreOSC

final class OSCABundleTests: XCTestCase {

func testSimpleFlatten() throws {
let messages = [
try! OSCMessage(with: "/core/osc/1"),
try! OSCMessage(with: "/core/osc/2"),
try! OSCMessage(with: "/core/osc/3")
]
let bundle = OSCBundle(
messages,
timeTag: .immediate
)
XCTAssertEqual(bundle.flatten(), messages)
}

func testRecursiveFlatten() throws {
let bundle = OSCBundle(
[
try! OSCMessage(with: "/core/osc/1"),
try! OSCMessage(with: "/core/osc/2"),
try! OSCMessage(with: "/core/osc/3"),
OSCBundle(
[
try! OSCMessage(with: "/core/osc/4"),
try! OSCMessage(with: "/core/osc/5"),
try! OSCMessage(with: "/core/osc/6"),
OSCBundle(
[
try! OSCMessage(with: "/core/osc/7"),
try! OSCMessage(with: "/core/osc/8"),
try! OSCMessage(with: "/core/osc/9"),

],
timeTag: .immediate
)
],
timeTag: .immediate
)
],
timeTag: .immediate
)
XCTAssertEqual(bundle.flatten(), [
try! OSCMessage(with: "/core/osc/1"),
try! OSCMessage(with: "/core/osc/2"),
try! OSCMessage(with: "/core/osc/3"),
try! OSCMessage(with: "/core/osc/4"),
try! OSCMessage(with: "/core/osc/5"),
try! OSCMessage(with: "/core/osc/6"),
try! OSCMessage(with: "/core/osc/7"),
try! OSCMessage(with: "/core/osc/8"),
try! OSCMessage(with: "/core/osc/9")
])
}

}

0 comments on commit 95a9371

Please sign in to comment.