diff --git a/.gitignore b/.gitignore index ba181d30..bb2a750a 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,8 @@ backend/.env /result-* clients/haskell/result clients/haskell/dist-newstyle +clients/swift/*.out +clients/swift/**/*/xcuserdata # dev bacon.toml docker-compose/localstack/export_cyphers.sh @@ -34,4 +36,4 @@ test_logs .cargo # pre-commit config .pre-commit-config.yaml -.cargo \ No newline at end of file +.cargo diff --git a/clients/swift/Config.xcconfig b/clients/swift/Config.xcconfig new file mode 100644 index 00000000..a27c793f --- /dev/null +++ b/clients/swift/Config.xcconfig @@ -0,0 +1,25 @@ +// +// Config.xcconfig +// swift +// +// Created by naman agarwal on 03/08/24. +// + +// Configuration settings file format documentation can be found at: +// https://help.apple.com/xcode/#/dev745c5c974 + +// Change env manually +// SUPERPOSITION_LIB_PATH=$(SRCROOT)/../../target/debug +// SUPERPOSITION_INCLUDE_PATH=$(SRCROOT)/../../headers + +// Library Search Paths +LIBRARY_SEARCH_PATHS = $(inherited) $(SUPERPOSITION_LIB_PATH) + +// Add your header search paths +HEADER_SEARCH_PATHS = $(inherited) $(SUPERPOSITION_INCLUDE_PATH) + +// Linker Flags +OTHER_LDFLAGS = $(inherited) -lcac_client -lexperimentation_client + +// Bridging Header +SWIFT_OBJC_BRIDGING_HEADER = $(SRCROOT)/swift/Bridging-Header.h diff --git a/clients/swift/README.md b/clients/swift/README.md new file mode 100644 index 00000000..eeb88e4d --- /dev/null +++ b/clients/swift/README.md @@ -0,0 +1,23 @@ +## CAC and Experimentation clients in `Swift` + +- ## Walkthrough + - Create bridging file for each module + - add the `#include` statement for the C header file + - `using nix`: use `swiftc` to compile modules using `-L`, `-l`, `I` and `-import-objc-header` flags to specify object files search dir, object-module name, header search path and bridging file path respectively. (checkout : [default.nix](default.nix)) + - `using xcode`: create a command-line project in xcode and configure `-L`, `-l` and `-import-objc-header` flags using `Build Settings` in the GUI. Settings equivalent for each compiler flags (checkout : [Config](Config.xcconfig)) + + - `-L` : `LIBRARY_SEARCH_PATHS` + - `-l` : `OTHER_LDFLAGS` + - `-import-objc-header` : `SWIFT_OBJC_BRIDGING_HEADER` + - `I` : `HEADER_SEARCH_PATHS` + +- ## setup + - ### using nix : + 1. cd to `clients/swift` + 2. spawn devShell `nix develop .#swift` + 3. run `compileTest` + 4. use generated bins + + - ### using xcode : + 1. open project (`clients/swift`) in xcode + 2. build & run the project diff --git a/clients/swift/default.nix b/clients/swift/default.nix new file mode 100644 index 00000000..695a6b16 --- /dev/null +++ b/clients/swift/default.nix @@ -0,0 +1,24 @@ +{ + perSystem = { config, pkgs, self', lib, ... }: { + devShells.swift = let + compileTest = pkgs.writeShellScriptBin "compileTest" '' + swiftc \ + swift/main.swift swift/cac.swift swift/exp.swift swift/types.swift swift/utils.swift swift/test.swift \ + -L$SUPERPOSITION_LIB_PATH \ + -I$SUPERPOSITION_INCLUDE_PATH \ + -lcac_client \ + -lexperimentation_client \ + -import-objc-header swift/Bridging-Header.h \ + -o test_client.out + ''; + in + pkgs.mkShell { + name = "superposition-swift-clients"; + buildInputs = with pkgs; [ + swift + swiftPackages.Foundation + compileTest + ]; + }; + }; +} diff --git a/clients/swift/swift.xcodeproj/project.pbxproj b/clients/swift/swift.xcodeproj/project.pbxproj new file mode 100644 index 00000000..13c1f9a5 --- /dev/null +++ b/clients/swift/swift.xcodeproj/project.pbxproj @@ -0,0 +1,357 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 56; + objects = { + +/* Begin PBXBuildFile section */ + 29FE37482C54DD7B00F7BE64 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29FE37472C54DD7B00F7BE64 /* main.swift */; }; + 29FE374F2C54DD8C00F7BE64 /* cac.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29FE374E2C54DD8C00F7BE64 /* cac.swift */; }; + 29FE37512C54DD9500F7BE64 /* exp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29FE37502C54DD9500F7BE64 /* exp.swift */; }; + 29FE37542C54DDDD00F7BE64 /* types.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29FE37532C54DDDD00F7BE64 /* types.swift */; }; + 29FE37562C54DE7E00F7BE64 /* utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29FE37552C54DE7E00F7BE64 /* utils.swift */; }; + 29FE37582C54DFA300F7BE64 /* test.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29FE37572C54DFA300F7BE64 /* test.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + 29FE37422C54DD7B00F7BE64 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + 29C786A02C5E54C400354E13 /* Config.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Config.xcconfig; sourceTree = ""; }; + 29FE37442C54DD7B00F7BE64 /* swift */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = swift; sourceTree = BUILT_PRODUCTS_DIR; }; + 29FE37472C54DD7B00F7BE64 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; + 29FE374E2C54DD8C00F7BE64 /* cac.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = cac.swift; sourceTree = ""; }; + 29FE37502C54DD9500F7BE64 /* exp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = exp.swift; sourceTree = ""; }; + 29FE37522C54DDA200F7BE64 /* Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Bridging-Header.h"; sourceTree = ""; }; + 29FE37532C54DDDD00F7BE64 /* types.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = types.swift; sourceTree = ""; }; + 29FE37552C54DE7E00F7BE64 /* utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = utils.swift; sourceTree = ""; }; + 29FE37572C54DFA300F7BE64 /* test.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = test.swift; sourceTree = ""; }; + 29FE37592C54E10800F7BE64 /* swift.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = swift.entitlements; sourceTree = ""; }; + 29FE375A2C54E48800F7BE64 /* default.nix */ = {isa = PBXFileReference; lastKnownFileType = text; path = default.nix; sourceTree = ""; }; + 29FE375B2C54E7FD00F7BE64 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 29FE37412C54DD7B00F7BE64 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 29FE373B2C54DD7B00F7BE64 = { + isa = PBXGroup; + children = ( + 29C786A02C5E54C400354E13 /* Config.xcconfig */, + 29FE375B2C54E7FD00F7BE64 /* README.md */, + 29FE375A2C54E48800F7BE64 /* default.nix */, + 29FE37462C54DD7B00F7BE64 /* swift */, + 29FE37452C54DD7B00F7BE64 /* Products */, + ); + sourceTree = ""; + }; + 29FE37452C54DD7B00F7BE64 /* Products */ = { + isa = PBXGroup; + children = ( + 29FE37442C54DD7B00F7BE64 /* swift */, + ); + name = Products; + sourceTree = ""; + }; + 29FE37462C54DD7B00F7BE64 /* swift */ = { + isa = PBXGroup; + children = ( + 29FE37592C54E10800F7BE64 /* swift.entitlements */, + 29FE37472C54DD7B00F7BE64 /* main.swift */, + 29FE374E2C54DD8C00F7BE64 /* cac.swift */, + 29FE37502C54DD9500F7BE64 /* exp.swift */, + 29FE37522C54DDA200F7BE64 /* Bridging-Header.h */, + 29FE37532C54DDDD00F7BE64 /* types.swift */, + 29FE37552C54DE7E00F7BE64 /* utils.swift */, + 29FE37572C54DFA300F7BE64 /* test.swift */, + ); + path = swift; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 29FE37432C54DD7B00F7BE64 /* swift */ = { + isa = PBXNativeTarget; + buildConfigurationList = 29FE374B2C54DD7B00F7BE64 /* Build configuration list for PBXNativeTarget "swift" */; + buildPhases = ( + 29FE37402C54DD7B00F7BE64 /* Sources */, + 29FE37412C54DD7B00F7BE64 /* Frameworks */, + 29FE37422C54DD7B00F7BE64 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = swift; + productName = swift; + productReference = 29FE37442C54DD7B00F7BE64 /* swift */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 29FE373C2C54DD7B00F7BE64 /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + LastSwiftUpdateCheck = 1520; + LastUpgradeCheck = 1520; + TargetAttributes = { + 29FE37432C54DD7B00F7BE64 = { + CreatedOnToolsVersion = 15.2; + }; + }; + }; + buildConfigurationList = 29FE373F2C54DD7B00F7BE64 /* Build configuration list for PBXProject "swift" */; + compatibilityVersion = "Xcode 14.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 29FE373B2C54DD7B00F7BE64; + productRefGroup = 29FE37452C54DD7B00F7BE64 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 29FE37432C54DD7B00F7BE64 /* swift */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + 29FE37402C54DD7B00F7BE64 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 29FE37562C54DE7E00F7BE64 /* utils.swift in Sources */, + 29FE37582C54DFA300F7BE64 /* test.swift in Sources */, + 29FE37482C54DD7B00F7BE64 /* main.swift in Sources */, + 29FE37512C54DD9500F7BE64 /* exp.swift in Sources */, + 29FE374F2C54DD8C00F7BE64 /* cac.swift in Sources */, + 29FE37542C54DDDD00F7BE64 /* types.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 29FE37492C54DD7B00F7BE64 /* Debug */ = { + isa = XCBuildConfiguration; + baseConfigurationReference = 29C786A02C5E54C400354E13 /* Config.xcconfig */; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + LIBRARY_SEARCH_PATHS = "$(inherited)"; + "LIBRARY_SEARCH_PATHS[arch=*]" = "$(inherited)"; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MACOSX_DEPLOYMENT_TARGET = 13.5; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + OTHER_LDFLAGS = "$(inherited)"; + "OTHER_SWIFT_FLAGS[arch=*]" = ""; + SDKROOT = macosx; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)"; + SWIFT_OBJC_BRIDGING_HEADER = "$(inherited)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 29FE374A2C54DD7B00F7BE64 /* Release */ = { + isa = XCBuildConfiguration; + baseConfigurationReference = 29C786A02C5E54C400354E13 /* Config.xcconfig */; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + LIBRARY_SEARCH_PATHS = "$(inherited)"; + "LIBRARY_SEARCH_PATHS[arch=*]" = "$(inherited)"; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MACOSX_DEPLOYMENT_TARGET = 13.5; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + OTHER_LDFLAGS = "$(inherited)"; + SDKROOT = macosx; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OBJC_BRIDGING_HEADER = "$(inherited)"; + }; + name = Release; + }; + 29FE374C2C54DD7B00F7BE64 /* Debug */ = { + isa = XCBuildConfiguration; + baseConfigurationReference = 29C786A02C5E54C400354E13 /* Config.xcconfig */; + buildSettings = { + CODE_SIGN_ENTITLEMENTS = swift/swift.entitlements; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 32XH2DF5HY; + ENABLE_HARDENED_RUNTIME = YES; + LIBRARY_SEARCH_PATHS = "$(inherited)"; + "LIBRARY_SEARCH_PATHS[arch=*]" = "$(inherited)"; + OTHER_LDFLAGS = ( + "-v", + "-lexperimentation_client", + "-lcac_client", + ); + "OTHER_LDFLAGS[arch=*]" = ( + "-v", + "-lexperimentation_client", + "-lcac_client", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + "SWIFT_OBJC_BRIDGING_HEADER[arch=*]" = "$(SRCROOT)/swift/Bridging-Header.h"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 29FE374D2C54DD7B00F7BE64 /* Release */ = { + isa = XCBuildConfiguration; + baseConfigurationReference = 29C786A02C5E54C400354E13 /* Config.xcconfig */; + buildSettings = { + CODE_SIGN_ENTITLEMENTS = swift/swift.entitlements; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 32XH2DF5HY; + ENABLE_HARDENED_RUNTIME = YES; + LIBRARY_SEARCH_PATHS = "$(inherited)"; + OTHER_LDFLAGS = ( + "-lexperimentation_client", + "-lcac_client", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 29FE373F2C54DD7B00F7BE64 /* Build configuration list for PBXProject "swift" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 29FE37492C54DD7B00F7BE64 /* Debug */, + 29FE374A2C54DD7B00F7BE64 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Debug; + }; + 29FE374B2C54DD7B00F7BE64 /* Build configuration list for PBXNativeTarget "swift" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 29FE374C2C54DD7B00F7BE64 /* Debug */, + 29FE374D2C54DD7B00F7BE64 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Debug; + }; +/* End XCConfigurationList section */ + }; + rootObject = 29FE373C2C54DD7B00F7BE64 /* Project object */; +} diff --git a/clients/swift/swift.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/clients/swift/swift.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 00000000..919434a6 --- /dev/null +++ b/clients/swift/swift.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/clients/swift/swift.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/clients/swift/swift.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 00000000..18d98100 --- /dev/null +++ b/clients/swift/swift.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/clients/swift/swift.xcodeproj/xcshareddata/xcschemes/swift.xcscheme b/clients/swift/swift.xcodeproj/xcshareddata/xcschemes/swift.xcscheme new file mode 100644 index 00000000..97859273 --- /dev/null +++ b/clients/swift/swift.xcodeproj/xcshareddata/xcschemes/swift.xcscheme @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/clients/swift/swift/Bridging-Header.h b/clients/swift/swift/Bridging-Header.h new file mode 100644 index 00000000..f8f187a6 --- /dev/null +++ b/clients/swift/swift/Bridging-Header.h @@ -0,0 +1,16 @@ +// +// Bridging-Header.h +// swift +// +// Created by naman agarwal on 27/07/24. +// + +#ifndef Bridging_Header_h +#define Bridging_Header_h + +// cac +#include "libcac_client.h" +// exp +#include "libexperimentation_client.h" + +#endif /* Bridging_Header_h */ diff --git a/clients/swift/swift/cac.swift b/clients/swift/swift/cac.swift new file mode 100644 index 00000000..14380b94 --- /dev/null +++ b/clients/swift/swift/cac.swift @@ -0,0 +1,118 @@ +// +// cac.swift +// swift +// +// Created by naman agarwal on 27/07/24. +// + +import Foundation + +enum MergeStrategy { + case MERGE + case REPLACE + + var show: String { + switch self { + case .MERGE: + return "MERGE" + case .REPLACE: + return "REPLACE" + } + } +} + +func createCacClient(tenant: String, frequency: UInt, hostname: String) -> Bool { + return tenant.withCString { t -> Bool in + return hostname.withCString { h -> Bool in + let resp = cac_new_client(t, frequency, h); + if (resp == 0) { + return true + } + return false + } + } +} + +func getCacClient(tenant: String) -> UnknownClientPointer? { + return tenant.withCString { t -> UnknownClientPointer? in + return cac_get_client(t) + } +} + +func cacStartPolling(tenant: String) { + tenant.withCString { t in + cac_start_polling_update(t) + } +} + +func getCacLastModified(client: UnknownClientPointer) -> String? { + let resp = cac_get_last_modified(client) + return resp.map { String(cString: $0) } +} + +func getResolvedConfig(client: UnknownClientPointer, context: String, filterKeys: [String]? = nil) -> Value? { + let keys = filterKeys.map { $0.joined(separator: "|") } + + return context.withCString { c -> Value? in + return MergeStrategy.MERGE.show.withCString { m -> Value? in + let rawData : UnsafePointer? + if let k = keys { + rawData = k.withCString { ck -> UnsafePointer? in + return cac_get_resolved_config(client, c, ck, m) + } + } else { + rawData = cac_get_resolved_config(client, c, nil, m) + } + return rawData.map { String(cString: $0) }.flatMap { parseJson(jsonString: $0) } + } + } +} + +func getDefaultConfig(client: UnknownClientPointer, filterKeys: [String]? = nil) -> Value? { + let keys = filterKeys.map { $0.joined(separator: "|") } + let rawData : UnsafePointer? + if let k = keys { + rawData = k.withCString { ck -> UnsafePointer? in + return cac_get_default_config(client, ck) + } + } else { + rawData = cac_get_default_config(client, nil) + } + return rawData.map { String(cString: $0) }.flatMap { parseJson(jsonString: $0) } +} + +func getConfig(client: UnknownClientPointer, filterQuery: [String]?, filterPrefix: [String]?) -> Value? { + let query = filterQuery.map { $0.joined(separator: "|") } + let prefix = filterPrefix.map { $0.joined(separator: "|") } + let rawData : UnsafePointer? + + if let q = query { + rawData = + q.withCString { qs -> UnsafePointer? in + if let p = prefix { + return p.withCString { ps -> UnsafePointer? in + return cac_get_config(client, qs, ps) + } + } else { + return cac_get_config(client, qs, nil) + } + } + } else { + if let p = prefix { + rawData = p.withCString { ps -> UnsafePointer? in + return cac_get_config(client, nil, ps) + } + } else { + rawData = cac_get_config(client, nil, nil) + } + } + return rawData.map { String(cString: $0) }.flatMap { parseJson(jsonString: $0) } +} + +func cacFreeClient(client: UnknownClientPointer) { + cac_free_client(client) +} + +func cacLastErrorMessage() -> String? { + return cac_last_error_message().map { String(cString: $0) } +} diff --git a/clients/swift/swift/exp.swift b/clients/swift/swift/exp.swift new file mode 100644 index 00000000..1b701c38 --- /dev/null +++ b/clients/swift/swift/exp.swift @@ -0,0 +1,79 @@ +// +// exp.swift +// swift +// +// Created by naman agarwal on 27/07/24. +// + +import Foundation + + +func createExptClient(tenant: String, frequency: UInt, hostname: String) -> Bool { + return tenant.withCString { t -> Bool in + return hostname.withCString { h -> Bool in + let resp = expt_new_client(t, frequency, h); + if (resp == 0) { + return true + } + return false + } + } +} + +func getExptClient(tenant: String) -> UnknownClientPointer? { + return tenant.withCString { t -> UnknownClientPointer? in + return expt_get_client(tenant) + } +} + +func exptStartPolling(tenant: String) { + tenant.withCString { t in + expt_start_polling_update(t) + } +} + +func getApplicableVariant(client: UnknownClientPointer, context: String, toss: Int16) -> Value? { + return context.withCString { c -> Value? in + let rawData = expt_get_applicable_variant(client, c, toss) + return rawData.map { String(cString: $0) }.flatMap { parseJson(jsonString: $0) } + } +} + +func getSatisfiedExperiments(client: UnknownClientPointer, context: String, filterPrefix: [String]? = nil) -> Value? { + let keys = filterPrefix.map { $0.joined(separator: "|") } + let rawData = context.withCString { c -> UnsafeMutablePointer? in + if let k = keys { + return k.withCString { kc -> UnsafeMutablePointer? in + return expt_get_satisfied_experiments(client, c, kc) + } + } else { + return expt_get_satisfied_experiments(client, c, nil) + } + } + return rawData.map { String(cString: $0) }.flatMap { parseJson(jsonString: $0) } +} + +func getRunningExperiments(client: UnknownClientPointer) -> Value? { + let resp = expt_get_running_experiments(client) + return resp.map { String(cString: $0) }.flatMap { parseJson(jsonString: $0) } +} + + +func getFilteredSatisfiedExperiments(client: UnknownClientPointer, context: String, filterPrefix: [String]) -> Value? { + let keys = filterPrefix.joined(separator: "|") + return context.withCString { c -> Value? in + return keys.withCString { k -> Value? in + let resp = expt_get_filtered_satisfied_experiments(client, c, k) + return resp.map { String(cString: $0) }.flatMap { parseJson(jsonString: $0) } + } + } +} + +func exptFreeClient(client: UnknownClientPointer) { + expt_free_client(client) +} + +func exptLastErrorMessage() -> String? { + return expt_last_error_message().map { String(cString: $0) } +} + diff --git a/clients/swift/swift/main.swift b/clients/swift/swift/main.swift new file mode 100644 index 00000000..6c598f46 --- /dev/null +++ b/clients/swift/swift/main.swift @@ -0,0 +1,12 @@ +// +// main.swift +// swift +// +// Created by naman agarwal on 27/07/24. +// + +import Foundation + +// unit tests +test_cac() +test_exp() diff --git a/clients/swift/swift/swift.entitlements b/clients/swift/swift/swift.entitlements new file mode 100644 index 00000000..8cc185af --- /dev/null +++ b/clients/swift/swift/swift.entitlements @@ -0,0 +1,8 @@ + + + + + com.apple.security.cs.disable-library-validation + + + diff --git a/clients/swift/swift/test.swift b/clients/swift/swift/test.swift new file mode 100644 index 00000000..10256b9a --- /dev/null +++ b/clients/swift/swift/test.swift @@ -0,0 +1,90 @@ +// +// test.swift +// swift +// +// Created by naman agarwal on 27/07/24. +// + +import Foundation + +// test CAC functions +func test_cac() { + let t = "dev" + let f : UInt = 300 + let h = "http://localhost:8080" + + print("\n") + if (createCacClient(tenant: t, frequency: f, hostname: h)) { + print("createCacClient success!") + + if let client = getCacClient(tenant: t) { + print("getCacClient success!") + + let thread = Thread { + cacStartPolling(tenant: t) + } + thread.start() + print("cacStartPolling: thread started!") + + let m = getCacLastModified(client: client) + let r = getResolvedConfig(client: client, context: "{}") + let d = getDefaultConfig(client: client, filterKeys: []) + let c = getConfig(client: client, filterQuery: nil, filterPrefix: nil) + + print("Last Modified: \(m)") + print("Resolved Config: \(r)") + print("Default Config: \(d)") + print("Get Config: \(c)") + + thread.cancel() + print("cacStartPolling: thread closed!") + + cacFreeClient(client: client) + print("Free client memory!") + } else { + print("getCacClient failed!") + } + } else { + print("createCacClient failed!") + } +} + +// test EXP functions +func test_exp() { + let t = "dev" + let f : UInt = 300 + let h = "http://localhost:8080" + + print("\n") + if (createExptClient(tenant: t, frequency: f, hostname: h)) { + print("createExptClient successs!") + if let client = getExptClient(tenant: t) { + print("getExptClient successs!") + + let thread = Thread { + exptStartPolling(tenant: t) + } + thread.start() + print("cacStartPolling: thread started!") + + let m = getApplicableVariant(client: client, context: "{}", toss: 1) + let r = getSatisfiedExperiments(client: client, context: "{}", filterPrefix: []) + let d = getRunningExperiments(client: client) + let v = getFilteredSatisfiedExperiments(client: client, context: "{}", filterPrefix: []) + print("Applicable Variants: \(r)") + print("Satisfied Experiments: \(m)") + print("Running Experiments: \(d)") + print("Filtered Satisfied Experiments: \(v)") + + thread.cancel() + print("cacStartPolling: thread closed!") + + exptFreeClient(client: client) + print("Free client memory!") + } else { + print("getExptClient failed!") + } + } else { + print("createExptClient failed!") + } +} diff --git a/clients/swift/swift/types.swift b/clients/swift/swift/types.swift new file mode 100644 index 00000000..fe6e248a --- /dev/null +++ b/clients/swift/swift/types.swift @@ -0,0 +1,9 @@ +// +// types.swift +// swift +// +// Created by naman agarwal on 27/07/24. +// + +typealias UnknownClientPointer = OpaquePointer +typealias Value = [String: Any] diff --git a/clients/swift/swift/utils.swift b/clients/swift/swift/utils.swift new file mode 100644 index 00000000..9a9c91db --- /dev/null +++ b/clients/swift/swift/utils.swift @@ -0,0 +1,19 @@ +// +// utils.swift +// swift +// +// Created by naman agarwal on 27/07/24. +// + +import Foundation + +func parseJson(jsonString: String) -> Value? { + if let jsonData = jsonString.data(using: .utf8) { + do { + return try JSONSerialization.jsonObject(with: jsonData, options: []) as? Value + } catch { + return nil + } + } + return nil +} diff --git a/flake.nix b/flake.nix index 0c651811..6721b3db 100644 --- a/flake.nix +++ b/flake.nix @@ -21,6 +21,7 @@ ./nix/pre-commit.nix ./clients/haskell ./nix/rust.nix + ./clients/swift ]; perSystem = { pkgs, self', config, ... }: {