Last active
March 3, 2023 22:27
-
-
Save kevinmbeaulieu/5ff14a14b7f8fe2b1442e30a306289f5 to your computer and use it in GitHub Desktop.
Thumbtack Swift Dependency Injector
This file contains hidden or 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
import Foundation | |
/// Thread-safe Swift dependency injector | |
/// | |
/// **Using Injector** | |
/// 1. Call `Injector.setup()` early in the application lifecycle. | |
/// 2. In dependency classes, conform to one of `Injectable`, `Singleton`, `WeakSingleton`, `EagerSingleton`. | |
/// 3. In application code, construct dependencies using property injection like: | |
/// ``` | |
/// private let logger: TTLogger = inject() | |
/// private let graphQLService: TTGraphQLService = inject() | |
/// ... | |
/// ``` | |
/// | |
/// **Testing with Injector** | |
/// 1. Define a mocked dependency as a subclass of the real dependency, overriding any functions you would like to mock. | |
/// 2. At the beginning of the unit test (e.g., in `XCTestCase.setUp()`), construct an instance of the mocked dependency | |
/// and set it as the stub for injection of the dependency type: | |
/// ``` | |
/// override func setUp() { | |
/// super.setUp() | |
/// | |
/// let logger = TTLoggerMock() | |
/// stub(TTLogger.self, with: logger) | |
/// } | |
/// | |
/// class TTLoggerMock: TTLogger { | |
/// // Override TTLogger functionality as needed. | |
/// } | |
/// ``` | |
/// Any code that `inject`s `TTLogger` after `stub(_:with:)` is called will be given the mock instance. | |
/// 3. At the end of the test (e.g., in `XCTestCase.tearDown()`), call `removeAllStubs()` to reset the state. | |
/// | |
/// **Injectable protocols** | |
/// *Injectable*: `inject()` calls `init()` to construct a new instance every time. | |
/// If `inject()` is called N times, then N instances of dependency will be created. | |
/// *Singleton*: `inject()` calls `init()` the first time and holds a strong reference to this instance to return on subsequent calls to `inject()`. | |
/// 1 instance of dependency exists at all times (after `inject()` is called at least once). | |
/// *WeakSingleton*: `inject()` calls `init()` the first time and holds a *weak* reference to this instance to return on subsequent calls to `inject()`. | |
/// ≤1 instance of dependency exists at any given time. | |
public enum Injector { | |
private static let injectorQueueSpecificKey = DispatchSpecificKey<Void>() | |
private static let injectorQueue = DispatchQueue.main | |
private static var singletons: [ObjectIdentifier: Singleton] = [:] | |
private static var eagerSingletons: [ObjectIdentifier: EagerSingleton] = [:] | |
private static var weakSingletons: NSMapTable<NSString, WeakSingleton> = NSMapTable(keyOptions: .strongMemory, valueOptions: .weakMemory) | |
private static var boundClassTypes: [ObjectIdentifier: Injectable.Type] = [:] | |
private static var boundSingletonClassTypes: [ObjectIdentifier: Singleton.Type] = [:] | |
private static var isSetup = false | |
#if IS_TEST_BUILD | |
fileprivate static var instanceStubs: [ObjectIdentifier: Stub] = [:] | |
fileprivate static var singletonStubs: [ObjectIdentifier: Stub] = [:] | |
fileprivate static var eagerSingletonStubs: [ObjectIdentifier: Stub] = [:] | |
fileprivate static var weakSingletonStubs: [ObjectIdentifier: Stub] = [:] | |
private static var usedStubIdentifiers: Set<ObjectIdentifier> = [] | |
#endif | |
public static func setup() { | |
injectorQueue.setSpecific(key: injectorQueueSpecificKey, value: ()) | |
isSetup = true | |
} | |
public static func bind<T: EagerSingleton>(instance: T, to type: T.Type) { | |
enforceIsSetup() | |
let identifier = ObjectIdentifier(type) | |
eagerSingletons[identifier] = instance | |
} | |
public static func bind<T: Injectable, U: Injectable>(type: T.Type, to superclassType: U.Type) { | |
enforceIsSetup() | |
let identifier = ObjectIdentifier(superclassType) | |
boundClassTypes[identifier] = type | |
} | |
public static func bind<T: Singleton, U: Singleton>(type: T.Type, to superclassType: U.Type) { | |
enforceIsSetup() | |
let identifier = ObjectIdentifier(superclassType) | |
boundSingletonClassTypes[identifier] = type | |
} | |
/// Constructs and retains the singleton yet does not return the instance. | |
/// Intended for use by singletons that are not directly interacted with, but instead receive | |
/// their input via other means, e.g NotificationCenter. | |
public static func internalize<T: Singleton>(_ type: T.Type) { | |
_ = get(T.self) | |
} | |
public static func existingWeakSingleton<T: WeakSingleton>(forKey key: String) -> T? { | |
guard let stored = weakSingletons.object(forKey: key as NSString) else { return nil } | |
guard let instance = stored as? T else { | |
preconditionFailure("A weak singleton instance with a different type exists for this key. " + | |
"This should never happen. Expected: \(type(of: T.self)), actual: \(type(of: stored))") | |
} | |
return instance | |
} | |
/// Hold a weak reference to the singleton for that key. | |
/// - Precondition: there must be no singleton instance for that key. use existingWeakSingleton(forKey:) to check | |
public static func setWeakSingleton<T: WeakSingleton>(_ repository: T, forKey key: String) { | |
precondition(weakSingletons.object(forKey: key as NSString) == nil, "Weak singletons cannot be replaced while in use.") | |
weakSingletons.setObject(repository, forKey: key as NSString) | |
} | |
// MARK: - Private | |
@usableFromInline | |
internal static func get<T: Singleton>(_ type: T.Type) -> T { | |
enforceIsSetup() | |
return sync { | |
var singletonType = type | |
var identifier = ObjectIdentifier(type) | |
var superclassIdentifier: ObjectIdentifier? | |
var superclassSingletonType: T.Type? | |
if let superclassType = boundSingletonClassTypes[identifier] { | |
superclassIdentifier = ObjectIdentifier(superclassType) | |
superclassSingletonType = (superclassType as! T.Type) | |
} | |
#if IS_TEST_BUILD | |
let instance: T? = sync { // singletonStubs may be mutated during testing. | |
// First check if there is a stub for T. | |
if let stub = singletonStubs[identifier] { | |
usedStubIdentifiers.insert(identifier) | |
return (stub.instance as! T) | |
} | |
// If not, then check if there is a stub for T's bound class type. | |
if let superclassIdentifier = superclassIdentifier, let stub = singletonStubs[superclassIdentifier] { | |
usedStubIdentifiers.insert(superclassIdentifier) | |
return (stub.instance as! T) | |
} | |
return nil | |
} | |
if let instance = instance { | |
return instance | |
} | |
#endif | |
singletonType = superclassSingletonType ?? singletonType | |
identifier = superclassIdentifier ?? identifier | |
if let instance = singletons[identifier] { | |
return instance as! T | |
} | |
let singleton = singletonType.init() | |
singletons[identifier] = singleton | |
return singleton | |
} | |
} | |
@usableFromInline | |
internal static func get<T: WeakSingleton>(_ type: T.Type, identifier: String) -> T { | |
enforceIsSetup() | |
return sync { | |
#if IS_TEST_BUILD | |
let objectIdentifier = ObjectIdentifier(type) | |
if let stub = weakSingletonStubs[objectIdentifier] { | |
usedStubIdentifiers.insert(objectIdentifier) | |
return (stub.instance as! T) | |
} | |
#endif | |
if let existing: T = existingWeakSingleton(forKey: identifier) { | |
return existing | |
} | |
let repository = type.init() | |
setWeakSingleton(repository, forKey: identifier) | |
return repository | |
} | |
} | |
@usableFromInline | |
internal static func get<T: Injectable>(_ type: T.Type) -> T { | |
enforceIsSetup() | |
let identifier = ObjectIdentifier(type) | |
#if IS_TEST_BUILD | |
let instance: T? = sync { // instanceStubs may be mutated during testing. | |
if let stub = instanceStubs[identifier] { | |
usedStubIdentifiers.insert(identifier) | |
return (stub.instance as! T) | |
} | |
return nil | |
} | |
if let instance = instance { | |
return instance | |
} | |
#endif | |
if let boundClassType = boundClassTypes[identifier] { | |
return boundClassType.init() as! T | |
} | |
return type.init() | |
} | |
@usableFromInline | |
internal static func get<T: EagerSingleton>(_ type: T.Type) -> T { | |
enforceIsSetup() | |
let identifier = ObjectIdentifier(type) | |
#if IS_TEST_BUILD | |
let instance: T? = sync { // eagerSingletonStubs may be mutated during testing. | |
if let stub = eagerSingletonStubs[identifier] { | |
usedStubIdentifiers.insert(identifier) | |
return (stub.instance as! T) | |
} | |
return nil | |
} | |
if let instance = instance { | |
return instance | |
} | |
#endif | |
if let instance = eagerSingletons[identifier] { | |
return instance as! T | |
} | |
fatalError("Bound singleton for type \(type) does not exist.") | |
} | |
#if IS_TEST_BUILD | |
fileprivate static func stub<T>(_ type: T.Type, with object: T, isGlobal: Bool, stubs: inout [ObjectIdentifier: Stub], file: StaticString = #file, line: UInt = #line) { | |
enforceIsSetup() | |
sync { | |
let identifier = ObjectIdentifier(type) | |
let stub = Stub(instance: object, location: (file: file, line: line), isGlobal: isGlobal) | |
stubs[identifier] = stub | |
} | |
} | |
public static var unusedStubLocations: [StubLocation] { | |
enforceIsSetup() | |
return sync { | |
let stubCollections = [instanceStubs, singletonStubs, eagerSingletonStubs, weakSingletonStubs] | |
return stubCollections.reduce(into: [StubLocation]()) { result, collection in | |
let unusedIdentifiers = Set(collection.keys).subtracting(usedStubIdentifiers) | |
let locations = unusedIdentifiers | |
.compactMap { collection[$0] } | |
.filter { !$0.isGlobal } | |
.map { $0.location } | |
result.append(contentsOf: locations) | |
} | |
} | |
} | |
public static func removeAllStubs() { | |
enforceIsSetup() | |
sync { | |
instanceStubs.removeAll() | |
singletonStubs.removeAll() | |
eagerSingletonStubs.removeAll() | |
weakSingletonStubs.removeAll() | |
} | |
} | |
#endif | |
// MARK: - Private | |
#if IS_TEST_BUILD | |
fileprivate struct Stub { | |
let instance: Any | |
let location: StubLocation | |
let isGlobal: Bool | |
} | |
#endif | |
private static func sync<T>(block: () -> T) -> T { | |
if DispatchQueue.getSpecific(key: injectorQueueSpecificKey) == nil { | |
// We're not on the main queue but may still be on the main thread, in which case using synchonrous dispatch | |
// can cause a deadlock. | |
if Thread.isMainThread { | |
return block() | |
} else { | |
return injectorQueue.sync(execute: block) | |
} | |
} else { | |
return block() | |
} | |
} | |
private static func enforceIsSetup() { | |
guard !isSetup else { return } | |
assertionFailure("Injector cannot be used until after it has been setup.") | |
} | |
} | |
public protocol Injectable: AnyObject { | |
init() | |
} | |
/// Only one instance should exist at any given time. The instance is created upon first use, and then | |
/// is persisted in memory for the lifetime of the app. | |
public protocol Singleton: Injectable { | |
// Intentionally empty, used for specialization. | |
} | |
/// A singleton that cannot be constructed using `init()` and therefore must be manually constructed | |
/// via some arbitrary meams before being bound to its injectable type. | |
/// | |
/// For example: | |
/// let instance = SomeClass(someArgument: value) | |
/// Injector.bind(eager: instance, to: SomeClass.self) | |
/// | |
/// Once bound, the instance is persisted in memory for the lifetime of the app. | |
public protocol EagerSingleton: AnyObject { | |
// Intentionally empty, used for specialization. | |
} | |
/// Only one instance should exist at any given time, or no instance if it is not being used. | |
@objc public protocol WeakSingleton: AnyObject { | |
init() | |
} | |
/// Re: @inlinable | |
/// | |
/// The expected behavior of a generic function is effectively that of a compile-time | |
/// template, in that it should inline itself and be statically dispatched from any call | |
/// site. That said, this behavior is not enabled by default when the generic function is | |
/// invoked across module boundaries. Instead, runtime machinery is setup for these | |
/// cross-module generic methods and calls to them are dispatched dynamically on the Swift | |
/// runtime. | |
/// | |
/// The @inlinable annotation (+ its accompanying @usableFromInline annotation) add additional | |
/// metadata to the target that enable cross-module calls of generic functions to be inlined | |
/// and statically dispatched. Standard Library methods like map() and reduce() leverage it | |
/// for that very purpose. | |
/// | |
/// https://github.com/apple/swift-evolution/blob/master/proposals/0193-cross-module-inlining-and-specialization.md | |
@inlinable | |
public func inject<T: Singleton>(_ type: T.Type? = nil) -> T { | |
Injector.get(T.self) | |
} | |
@inlinable | |
public func inject<T: EagerSingleton>(_ type: T.Type? = nil) -> T { | |
Injector.get(T.self) | |
} | |
@inlinable | |
public func inject<T: Injectable>(_ type: T.Type? = nil) -> T { | |
Injector.get(T.self) | |
} | |
@inlinable | |
public func inject<T: WeakSingleton>(_ type: T.Type? = nil, identifier: String = String(describing: T.self)) -> T { | |
Injector.get(T.self, identifier: identifier) | |
} | |
#if IS_TEST_BUILD | |
public func stub<T: Injectable>(_ type: T.Type, with object: T, isGlobal: Bool = false, file: StaticString = #file, line: UInt = #line) { | |
Injector.stub(T.self, with: object, isGlobal: isGlobal, stubs: &Injector.instanceStubs, file: file, line: line) | |
} | |
public func stub<T: Singleton>(_ type: T.Type, with object: T, isGlobal: Bool = false, file: StaticString = #file, line: UInt = #line) { | |
Injector.stub(T.self, with: object, isGlobal: isGlobal, stubs: &Injector.singletonStubs, file: file, line: line) | |
} | |
public func stub<T: EagerSingleton>(_ type: T.Type, with object: T, isGlobal: Bool = false, file: StaticString = #file, line: UInt = #line) { | |
Injector.stub(T.self, with: object, isGlobal: isGlobal, stubs: &Injector.eagerSingletonStubs, file: file, line: line) | |
} | |
public func stub<T: WeakSingleton>(_ type: T.Type, with object: T, isGlobal: Bool = false, file: StaticString = #file, line: UInt = #line) { | |
Injector.stub(T.self, with: object, isGlobal: isGlobal, stubs: &Injector.weakSingletonStubs, file: file, line: line) | |
} | |
public func removeAllStubs() { | |
Injector.removeAllStubs() | |
} | |
public var unusedStubLocations: [StubLocation] { | |
Injector.unusedStubLocations | |
} | |
public typealias StubLocation = (file: StaticString, line: UInt) | |
#endif |
This file contains hidden or 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
import XCTest | |
/// Unit tests for `Injector` | |
class InjectorTest: XCTestCase { | |
func testSwiftSingleton() { | |
let instance1: SwiftSingleton = inject() | |
let instance2: SwiftSingleton = inject() | |
XCTAssertTrue(instance1 === instance2) | |
} | |
func testSwiftSingletonIsDistinct() { | |
let instance: SwiftSingleton = inject() | |
let otherInstance: OtherSwiftSingleton = inject() | |
XCTAssertFalse(instance === otherInstance) | |
} | |
func testSwiftSingletonWithGenericTypeIsDistinct() { | |
let stringInstance1: SwiftSingletonGeneric<String> = inject() | |
let stringInstance2: SwiftSingletonGeneric<String> = inject() | |
XCTAssertTrue(stringInstance1 === stringInstance2) | |
let intInstance: SwiftSingletonGeneric<Int> = inject() | |
XCTAssertFalse(stringInstance1 === intInstance) | |
} | |
func testSwiftSingletonSubclass() { | |
let instance1: SwiftSingletonSubclass1 = inject() | |
let instance2: SwiftSingletonSubclass1 = inject() | |
XCTAssertTrue(instance1 === instance2) | |
} | |
func testSwiftSingletonSubclassIsDistinct() { | |
let superclass: SwiftSingleton = inject() | |
let instance1: SwiftSingletonSubclass1 = inject() | |
let instance2: SwiftSingletonSubclass2 = inject() | |
XCTAssertFalse(instance1 === instance2) | |
XCTAssertFalse(superclass === instance1) | |
XCTAssertFalse(superclass === instance2) | |
} | |
func testSwiftSingletonOnBackgroundThread() { | |
var backgroundInstance: SwiftSingleton? | |
let completed = expectation(description: "background singleton") | |
DispatchQueue.global().async { | |
backgroundInstance = inject() | |
completed.fulfill() | |
} | |
waitForExpectations(timeout: 10) { error in | |
if let error = error { | |
XCTFail(error.localizedDescription) | |
} | |
XCTAssertNotNil(backgroundInstance) | |
let instance: SwiftSingleton = inject() | |
XCTAssertTrue(backgroundInstance! === instance) | |
} | |
} | |
func testSwiftTransient() { | |
let instance1: SwiftTransient = inject() | |
let instance2: SwiftTransient = inject() | |
XCTAssertFalse(instance1 === instance2) | |
} | |
func testBoundSingleton() { | |
let instance1 = BoundSingleton() | |
Injector.bind(instance: instance1, to: BoundSingleton.self) | |
let instance2: BoundSingleton = inject() | |
XCTAssertTrue(instance1 === instance2) | |
} | |
func testBoundSingletonSubclass() { | |
let instance1 = BoundSingletonSubclass() | |
Injector.bind(instance: instance1, to: BoundSingleton.self) | |
let instance2: BoundSingleton = inject() | |
XCTAssertTrue(instance1 === instance2) | |
} | |
func testBindSubclassToSuperclass() { | |
Injector.bind(type: InjectableSubclass.self, to: InjectableSuperclass.self) | |
let instance1: InjectableSuperclass = inject() | |
XCTAssertTrue(type(of: instance1) == InjectableSubclass.self) | |
let instance2: InjectableSubclass = inject() | |
XCTAssertTrue(type(of: instance2) == InjectableSubclass.self) | |
} | |
func testBindSingletonSubclassToSuperclass() { | |
Injector.bind(type: InjectableSingletonSubclass.self, to: InjectableSingletonSuperclass.self) | |
let instance1: InjectableSingletonSuperclass = inject() | |
XCTAssertTrue(type(of: instance1) == InjectableSingletonSubclass.self) | |
let instance2: InjectableSingletonSuperclass = inject() | |
XCTAssertTrue(instance1 === instance2) | |
let instance3: InjectableSingletonSubclass = inject() | |
XCTAssertTrue(instance2 === instance3) | |
} | |
func testReentrantSingletonInjectionDoesNotDeadlock() { | |
_ = inject(ReentrantSingleton1.self) | |
} | |
func testNotificationCenterCanBeInjected() { | |
XCTAssertNoThrow(inject(NotificationCenter.self)) | |
} | |
func testUserDefaultsCanBeInjected() { | |
XCTAssertNoThrow(inject(UserDefaults.self)) | |
} | |
func testInjectWeakSingleton() { | |
autoreleasepool { | |
let s1: SomeWeakSingleton? = inject(SomeWeakSingleton.self) | |
let s2: SomeWeakSingleton? = inject(SomeWeakSingleton.self) | |
XCTAssertEqual(1, SomeWeakSingleton.instantiationCount) | |
XCTAssertTrue(s1 === s2, "WeakSingleton should be a shared instance while in use") | |
} | |
let nilSingleton: SomeWeakSingleton? = Injector.existingWeakSingleton(forKey: String(describing: SomeWeakSingleton.self)) | |
XCTAssertNil(nilSingleton, "Shared instance should be released once there are no more references to it") | |
} | |
} | |
private class SwiftSingleton: Singleton { | |
required init() {} | |
} | |
private class SwiftSingletonGeneric<T>: Singleton { | |
required init() {} | |
} | |
private class SwiftSingletonSubclass1: SwiftSingleton { | |
required init() {} | |
} | |
private class SwiftSingletonSubclass2: SwiftSingleton { | |
required init() {} | |
} | |
private class OtherSwiftSingleton: Singleton { | |
required init() {} | |
} | |
private class SomeWeakSingleton: WeakSingleton { | |
static var instantiationCount = 0 | |
required init() { | |
Self.instantiationCount += 1 | |
} | |
} | |
private class SwiftTransient: Injectable { | |
required init() {} | |
} | |
private class BoundSingleton: EagerSingleton { | |
required init() {} | |
} | |
private class BoundSingletonSubclass: BoundSingleton { | |
required init() {} | |
} | |
private class InjectableSuperclass: Injectable { | |
required init() {} | |
} | |
private class InjectableSubclass: InjectableSuperclass { | |
required init() {} | |
} | |
private class InjectableSingletonSuperclass: Singleton { | |
required init() {} | |
} | |
private class InjectableSingletonSubclass: InjectableSingletonSuperclass { | |
required init() {} | |
} | |
private class ReentrantSingleton1: Singleton { | |
private let singleton2: ReentrantSingleton2 | |
required convenience init() { | |
self.init(singleton2: inject()) | |
} | |
required init(singleton2: ReentrantSingleton2) { | |
self.singleton2 = singleton2 | |
} | |
} | |
private class ReentrantSingleton2: Singleton { | |
required init() {} | |
} |
This file contains hidden or 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
Apache License | |
Version 2.0, January 2004 | |
http://www.apache.org/licenses/ | |
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION | |
1. Definitions. | |
"License" shall mean the terms and conditions for use, reproduction, | |
and distribution as defined by Sections 1 through 9 of this document. | |
"Licensor" shall mean the copyright owner or entity authorized by | |
the copyright owner that is granting the License. | |
"Legal Entity" shall mean the union of the acting entity and all | |
other entities that control, are controlled by, or are under common | |
control with that entity. For the purposes of this definition, | |
"control" means (i) the power, direct or indirect, to cause the | |
direction or management of such entity, whether by contract or | |
otherwise, or (ii) ownership of fifty percent (50%) or more of the | |
outstanding shares, or (iii) beneficial ownership of such entity. | |
"You" (or "Your") shall mean an individual or Legal Entity | |
exercising permissions granted by this License. | |
"Source" form shall mean the preferred form for making modifications, | |
including but not limited to software source code, documentation | |
source, and configuration files. | |
"Object" form shall mean any form resulting from mechanical | |
transformation or translation of a Source form, including but | |
not limited to compiled object code, generated documentation, | |
and conversions to other media types. | |
"Work" shall mean the work of authorship, whether in Source or | |
Object form, made available under the License, as indicated by a | |
copyright notice that is included in or attached to the work | |
(an example is provided in the Appendix below). | |
"Derivative Works" shall mean any work, whether in Source or Object | |
form, that is based on (or derived from) the Work and for which the | |
editorial revisions, annotations, elaborations, or other modifications | |
represent, as a whole, an original work of authorship. For the purposes | |
of this License, Derivative Works shall not include works that remain | |
separable from, or merely link (or bind by name) to the interfaces of, | |
the Work and Derivative Works thereof. | |
"Contribution" shall mean any work of authorship, including | |
the original version of the Work and any modifications or additions | |
to that Work or Derivative Works thereof, that is intentionally | |
submitted to Licensor for inclusion in the Work by the copyright owner | |
or by an individual or Legal Entity authorized to submit on behalf of | |
the copyright owner. For the purposes of this definition, "submitted" | |
means any form of electronic, verbal, or written communication sent | |
to the Licensor or its representatives, including but not limited to | |
communication on electronic mailing lists, source code control systems, | |
and issue tracking systems that are managed by, or on behalf of, the | |
Licensor for the purpose of discussing and improving the Work, but | |
excluding communication that is conspicuously marked or otherwise | |
designated in writing by the copyright owner as "Not a Contribution." | |
"Contributor" shall mean Licensor and any individual or Legal Entity | |
on behalf of whom a Contribution has been received by Licensor and | |
subsequently incorporated within the Work. | |
2. Grant of Copyright License. Subject to the terms and conditions of | |
this License, each Contributor hereby grants to You a perpetual, | |
worldwide, non-exclusive, no-charge, royalty-free, irrevocable | |
copyright license to reproduce, prepare Derivative Works of, | |
publicly display, publicly perform, sublicense, and distribute the | |
Work and such Derivative Works in Source or Object form. | |
3. Grant of Patent License. Subject to the terms and conditions of | |
this License, each Contributor hereby grants to You a perpetual, | |
worldwide, non-exclusive, no-charge, royalty-free, irrevocable | |
(except as stated in this section) patent license to make, have made, | |
use, offer to sell, sell, import, and otherwise transfer the Work, | |
where such license applies only to those patent claims licensable | |
by such Contributor that are necessarily infringed by their | |
Contribution(s) alone or by combination of their Contribution(s) | |
with the Work to which such Contribution(s) was submitted. If You | |
institute patent litigation against any entity (including a | |
cross-claim or counterclaim in a lawsuit) alleging that the Work | |
or a Contribution incorporated within the Work constitutes direct | |
or contributory patent infringement, then any patent licenses | |
granted to You under this License for that Work shall terminate | |
as of the date such litigation is filed. | |
4. Redistribution. You may reproduce and distribute copies of the | |
Work or Derivative Works thereof in any medium, with or without | |
modifications, and in Source or Object form, provided that You | |
meet the following conditions: | |
(a) You must give any other recipients of the Work or | |
Derivative Works a copy of this License; and | |
(b) You must cause any modified files to carry prominent notices | |
stating that You changed the files; and | |
(c) You must retain, in the Source form of any Derivative Works | |
that You distribute, all copyright, patent, trademark, and | |
attribution notices from the Source form of the Work, | |
excluding those notices that do not pertain to any part of | |
the Derivative Works; and | |
(d) If the Work includes a "NOTICE" text file as part of its | |
distribution, then any Derivative Works that You distribute must | |
include a readable copy of the attribution notices contained | |
within such NOTICE file, excluding those notices that do not | |
pertain to any part of the Derivative Works, in at least one | |
of the following places: within a NOTICE text file distributed | |
as part of the Derivative Works; within the Source form or | |
documentation, if provided along with the Derivative Works; or, | |
within a display generated by the Derivative Works, if and | |
wherever such third-party notices normally appear. The contents | |
of the NOTICE file are for informational purposes only and | |
do not modify the License. You may add Your own attribution | |
notices within Derivative Works that You distribute, alongside | |
or as an addendum to the NOTICE text from the Work, provided | |
that such additional attribution notices cannot be construed | |
as modifying the License. | |
You may add Your own copyright statement to Your modifications and | |
may provide additional or different license terms and conditions | |
for use, reproduction, or distribution of Your modifications, or | |
for any such Derivative Works as a whole, provided Your use, | |
reproduction, and distribution of the Work otherwise complies with | |
the conditions stated in this License. | |
5. Submission of Contributions. Unless You explicitly state otherwise, | |
any Contribution intentionally submitted for inclusion in the Work | |
by You to the Licensor shall be under the terms and conditions of | |
this License, without any additional terms or conditions. | |
Notwithstanding the above, nothing herein shall supersede or modify | |
the terms of any separate license agreement you may have executed | |
with Licensor regarding such Contributions. | |
6. Trademarks. This License does not grant permission to use the trade | |
names, trademarks, service marks, or product names of the Licensor, | |
except as required for reasonable and customary use in describing the | |
origin of the Work and reproducing the content of the NOTICE file. | |
7. Disclaimer of Warranty. Unless required by applicable law or | |
agreed to in writing, Licensor provides the Work (and each | |
Contributor provides its Contributions) on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | |
implied, including, without limitation, any warranties or conditions | |
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A | |
PARTICULAR PURPOSE. You are solely responsible for determining the | |
appropriateness of using or redistributing the Work and assume any | |
risks associated with Your exercise of permissions under this License. | |
8. Limitation of Liability. In no event and under no legal theory, | |
whether in tort (including negligence), contract, or otherwise, | |
unless required by applicable law (such as deliberate and grossly | |
negligent acts) or agreed to in writing, shall any Contributor be | |
liable to You for damages, including any direct, indirect, special, | |
incidental, or consequential damages of any character arising as a | |
result of this License or out of the use or inability to use the | |
Work (including but not limited to damages for loss of goodwill, | |
work stoppage, computer failure or malfunction, or any and all | |
other commercial damages or losses), even if such Contributor | |
has been advised of the possibility of such damages. | |
9. Accepting Warranty or Additional Liability. While redistributing | |
the Work or Derivative Works thereof, You may choose to offer, | |
and charge a fee for, acceptance of support, warranty, indemnity, | |
or other liability obligations and/or rights consistent with this | |
License. However, in accepting such obligations, You may act only | |
on Your own behalf and on Your sole responsibility, not on behalf | |
of any other Contributor, and only if You agree to indemnify, | |
defend, and hold each Contributor harmless for any liability | |
incurred by, or claims asserted against, such Contributor by reason | |
of your accepting any such warranty or additional liability. | |
END OF TERMS AND CONDITIONS | |
APPENDIX: How to apply the Apache License to your work. | |
To apply the Apache License to your work, attach the following | |
boilerplate notice, with the fields enclosed by brackets "[]" | |
replaced with your own identifying information. (Don't include | |
the brackets!) The text should be enclosed in the appropriate | |
comment syntax for the file format. We also recommend that a | |
file or class name and description of purpose be included on the | |
same "printed page" as the copyright notice for easier | |
identification within third-party archives. | |
Copyright 2021 Thumbtack, Inc. | |
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment