-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Objective C bindings to work with contexts (#213)
- Loading branch information
Showing
7 changed files
with
200 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
LaunchDarkly/LaunchDarkly/ObjectiveC/ObjcLDContext.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import Foundation | ||
|
||
@objc(LDContext) | ||
public final class ObjcLDContext: NSObject { | ||
var context: LDContext | ||
|
||
init(_ context: LDContext) { | ||
self.context = context | ||
} | ||
|
||
@objc public func fullyQualifiedKey() -> String { context.fullyQualifiedKey() } | ||
@objc public func isMulti() -> Bool { context.isMulti() } | ||
@objc public func contextKeys() -> [String: String] { context.contextKeys() } | ||
@objc public func getValue(reference: ObjcLDReference) -> ObjcLDValue? { | ||
guard let value = context.getValue(reference.reference) | ||
else { return nil } | ||
|
||
return ObjcLDValue(wrappedValue: value) | ||
} | ||
} | ||
|
||
@objc(LDContextBuilder) | ||
public final class ObjcLDContextBuilder: NSObject { | ||
var builder: LDContextBuilder | ||
|
||
@objc public init(key: String) { | ||
builder = LDContextBuilder(key: key) | ||
} | ||
|
||
// Initializer to wrap the Swift LDContextBuilder into ObjcLDContextBuilder for use in | ||
// Objective-C apps. | ||
init(_ builder: LDContextBuilder) { | ||
self.builder = builder | ||
} | ||
|
||
@objc public func kind(kind: String) { builder.kind(kind) } | ||
@objc public func key(key: String) { builder.key(key) } | ||
@objc public func name(name: String) { builder.name(name) } | ||
@objc public func secondary(secondary: String) { builder.secondary(secondary) } | ||
@objc public func transient(transient: Bool) { builder.transient(transient) } | ||
@objc public func addPrivateAttribute(reference: ObjcLDReference) { builder.addPrivateAttribute(reference.reference) } | ||
@objc public func removePrivateAttribute(reference: ObjcLDReference) { builder.removePrivateAttribute(reference.reference) } | ||
|
||
@discardableResult | ||
@objc public func trySetValue(name: String, value: ObjcLDValue) -> Bool { | ||
builder.trySetValue(name, value.wrappedValue) | ||
} | ||
|
||
@objc public func build() -> ContextBuilderResult { | ||
switch builder.build() { | ||
case .success(let context): | ||
return ContextBuilderResult.fromSuccess(context) | ||
case .failure(let error): | ||
return ContextBuilderResult.fromError(error) | ||
} | ||
} | ||
} | ||
|
||
@objc(LDMultiContextBuilder) | ||
public final class ObjcLDMultiContextBuilder: NSObject { | ||
var builder: LDMultiContextBuilder | ||
|
||
@objc public override init() { | ||
builder = LDMultiContextBuilder() | ||
} | ||
|
||
@objc public func addContext(context: ObjcLDContext) { | ||
builder.addContext(context.context) | ||
} | ||
|
||
// Initializer to wrap the Swift LDMultiContextBuilder into ObjcLDMultiContextBuilder for use in | ||
// Objective-C apps. | ||
init(_ builder: LDMultiContextBuilder) { | ||
self.builder = builder | ||
} | ||
|
||
@objc public func build() -> ContextBuilderResult { | ||
switch builder.build() { | ||
case .success(let context): | ||
return ContextBuilderResult.fromSuccess(context) | ||
case .failure(let error): | ||
return ContextBuilderResult.fromError(error) | ||
} | ||
} | ||
} | ||
|
||
@objc public class ContextBuilderResult: NSObject { | ||
@objc public private(set) var success: ObjcLDContext? | ||
@objc public private(set) var failure: NSError? | ||
|
||
private override init() { | ||
super.init() | ||
success = nil | ||
failure = nil | ||
} | ||
|
||
public static func fromSuccess(_ success: LDContext) -> ContextBuilderResult { | ||
ContextBuilderResult(success, nil) | ||
} | ||
|
||
public static func fromError(_ error: ContextBuilderError) -> ContextBuilderResult { | ||
ContextBuilderResult(nil, error) | ||
} | ||
|
||
private convenience init(_ arg1: LDContext?, _ arg2: ContextBuilderError?) { | ||
self.init() | ||
success = arg1.map { ObjcLDContext($0) } | ||
failure = arg2.map { $0 as NSError } | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
LaunchDarkly/LaunchDarkly/ObjectiveC/ObjcLDReference.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Foundation | ||
|
||
@objc(Reference) | ||
public final class ObjcLDReference: NSObject { | ||
var reference: Reference | ||
|
||
@objc public init(value: String) { | ||
reference = Reference(value) | ||
} | ||
|
||
// Initializer to wrap the Swift Reference into ObjcLDReference for use in | ||
// Objective-C apps. | ||
init(_ reference: Reference) { | ||
self.reference = reference | ||
} | ||
|
||
@objc public func isValid() -> Bool { reference.isValid() } | ||
|
||
@objc public func getError() -> NSError? { | ||
guard let error = reference.getError() | ||
else { return nil } | ||
|
||
return error as NSError | ||
} | ||
} | ||
|
||
@objc(ReferenceError) | ||
public final class ObjcLDReferenceError: NSObject { | ||
var error: ReferenceError | ||
|
||
// Initializer to wrap the Swift ReferenceError into ObjcLDReferenceError for use in | ||
// Objective-C apps. | ||
init(_ error: ReferenceError) { | ||
self.error = error | ||
} | ||
|
||
override public var description: String { self.error.description } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters