Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from spotify/transfer-codebase
Browse files Browse the repository at this point in the history
Transfer codebase
  • Loading branch information
fabriziodemaria authored Feb 7, 2023
2 parents 3ae0b8a + a3253d7 commit ccf50b9
Show file tree
Hide file tree
Showing 50 changed files with 3,061 additions and 1 deletion.
26 changes: 26 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: CI

on:
pull_request:
branches:
- 'main'
push:
branches:
- 'main'

jobs:
test:
runs-on: macOS-latest
steps:
- uses: actions/checkout@v3
- name: Build and Test
run: swift test

swiftlint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: GitHub Action for SwiftLint
uses: norio-nomura/[email protected]
with:
args: --config .swiftlint.yml
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
.build
.mockingbird
project.json
56 changes: 56 additions & 0 deletions .swift-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"fileScopedDeclarationPrivacy" : {
"accessLevel" : "private"
},
"indentation" : {
"spaces" : 4
},
"indentConditionalCompilationBlocks" : true,
"indentSwitchCaseLabels" : false,
"lineBreakAroundMultilineExpressionChainComponents" : false,
"lineBreakBeforeControlFlowKeywords" : false,
"lineBreakBeforeEachArgument" : false,
"lineBreakBeforeEachGenericRequirement" : false,
"lineLength" : 120,
"maximumBlankLines" : 1,
"prioritizeKeepingFunctionOutputTogether" : false,
"respectsExistingLineBreaks" : true,
"rules" : {
"AllPublicDeclarationsHaveDocumentation" : false,
"AlwaysUseLowerCamelCase" : true,
"AmbiguousTrailingClosureOverload" : true,
"BeginDocumentationCommentWithOneLineSummary" : false,
"DoNotUseSemicolons" : true,
"DontRepeatTypeInStaticProperties" : true,
"FileScopedDeclarationPrivacy" : true,
"FullyIndirectEnum" : true,
"GroupNumericLiterals" : true,
"IdentifiersMustBeASCII" : true,
"NeverForceUnwrap" : false,
"NeverUseForceTry" : false,
"NeverUseImplicitlyUnwrappedOptionals" : false,
"NoAccessLevelOnExtensionDeclaration" : true,
"NoBlockComments" : true,
"NoCasesWithOnlyFallthrough" : true,
"NoEmptyTrailingClosureParentheses" : true,
"NoLabelsInCasePatterns" : true,
"NoLeadingUnderscores" : false,
"NoParensAroundConditions" : true,
"NoVoidReturnOnFunctionSignature" : true,
"OneCasePerLine" : true,
"OneVariableDeclarationPerLine" : true,
"OnlyOneTrailingClosureArgument" : true,
"OrderedImports" : true,
"ReturnVoidInsteadOfEmptyTuple" : true,
"UseEarlyExits" : false,
"UseLetInEveryBoundCaseVariable" : true,
"UseShorthandTypeNames" : true,
"UseSingleLinePropertyGetter" : true,
"UseSynthesizedInitializer" : true,
"UseTripleSlashForDocumentationComments" : true,
"UseWhereClausesInForLoops" : false,
"ValidateDocumentationComments" : false
},
"tabWidth" : 8,
"version" : 1
}
116 changes: 116 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
excluded:
- ${PWD}/Carthage
- ${PWD}/Pods
- ${PWD}/DerivedData
- ${PWD}/.build
- ${PWD}/Tools/.build
- ${PWD}/Tests/OpenFeatureTests/MockingbirdMocks/
- ${PWD}/Sources/OpenFeature/FlagResolver

disabled_rules:
- discarded_notification_center_observer
- notification_center_detachment
- orphaned_doc_comment
- todo
- unused_capture_list
- opening_brace

analyzer_rules:
- unused_import

opt_in_rules:
- array_init
- attributes
- closure_end_indentation
- closure_spacing
- collection_alignment
- colon # promote to error
- convenience_type
- discouraged_object_literal
- empty_collection_literal
- empty_count
- empty_string
- enum_case_associated_values_count
- fatal_error_message
- first_where
- force_unwrapping
- implicitly_unwrapped_optional
- indentation_width
- last_where
- legacy_random
- literal_expression_end_indentation
- multiline_arguments
- multiline_function_chains
- multiline_literal_brackets
- multiline_parameters
- multiline_parameters_brackets
- operator_usage_whitespace
- overridden_super_call
- pattern_matching_keywords
- prefer_self_type_over_type_of_self
- redundant_nil_coalescing
- redundant_type_annotation
- strict_fileprivate
- toggle_bool
- trailing_closure
- unneeded_parentheses_in_closure_argument
- vertical_whitespace_closing_braces
- vertical_whitespace_opening_braces
- yoda_condition


custom_rules:
array_constructor:
name: "Array/Dictionary initializer"
regex: '[let,var] .+ = (\[.+\]\(\))'
capture_group: 1
message: "Use explicit type annotation when initializing empty arrays and dictionaries"
severity: warning


attributes:
always_on_same_line:
- "@IBSegueAction"
- "@IBAction"
- "@NSManaged"
- "@objc"

force_cast: warning
force_try: warning
function_body_length:
warning: 60

legacy_hashing: error

identifier_name:
excluded:
- i
- id
- x
- y
- z

indentation_width:
indentation_width: 4

line_length:
ignores_urls: true
ignores_function_declarations: true
ignores_comments: true

multiline_arguments:
first_argument_location: next_line
only_enforce_after_first_closure_on_first_line: true

private_over_fileprivate:
validate_extensions: true

trailing_comma:
mandatory_comma: true

trailing_whitespace:
ignores_empty_lines: false
ignores_comments: true

vertical_whitespace:
max_empty_lines: 2
28 changes: 28 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// swift-tools-version: 5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "OpenFeature",
platforms: [
.iOS(.v14),
.macOS(.v12)
],
products: [
.library(
name: "OpenFeature",
targets: ["OpenFeature"])
],
dependencies: [
],
targets: [
.target(
name: "OpenFeature",
dependencies: []
),
.testTarget(
name: "OpenFeatureTests",
dependencies: ["OpenFeature"])
]
)
59 changes: 58 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,58 @@
# openfeature-swift-sdk
# OpenFeature

Swift implementation of the OpenFeature SDK.

## Usage

### Adding the package dependency

If you manage dependencies through Xcode go to "Add package" and enter `[email protected]:spotify/openfeature-swift-sdk.git`.

If you manage dependencies through SPM, in the dependencies section of Package.swift add:
```swift
.package(url: "[email protected]:spotify/openfeature-swift-sdk.git", from: "0.1.0")
```

and in the target dependencies section add:
```swift
.product(name: "OpenFeature", package: "openfeature-swift-sdk"),
```

### Resolving a flag

To enable the provider and start resolving flags add the following:

```swift
import OpenFeature

// Change this to your actual provider
OpenFeatureAPI.shared.provider = NoOpProvider()

let client = OpenFeatureAPI.shared.getClient()
let value = client.getBooleanValue(key: "flag", defaultValue: false)
```

## Development

Open the project in XCode and build by Product -> Build.

### Linting code

Code is automatically linted during build in XCode, if you need to manually lint:
```shell
brew install swiftlint
swiftlint
```

### Formatting code

You can automatically format your code using:
```shell
./scripts/swift-format
```

## Running tests from cmd-line

```shell
swift test
```
10 changes: 10 additions & 0 deletions Sources/OpenFeature/BaseEvaluation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Foundation

public protocol BaseEvaluation {
associatedtype ValueType
var value: ValueType { get }
var variant: String? { get }
var reason: String? { get }
var errorCode: ErrorCode? { get }
var errorMessage: String? { get }
}
17 changes: 17 additions & 0 deletions Sources/OpenFeature/Client.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Foundation

/// Interface used to resolve flags of varying types.
public protocol Client: Features {
var metadata: Metadata { get }

/// Return an optional client-level evaluation context.
var evaluationContext: EvaluationContext? { get set }

/// The hooks associated to this client.
var hooks: [AnyHook] { get }

/// Adds hooks for evaluation.
/// Hooks are run in the order they're added in the before stage. They are run in reverse order for all
/// other stages.
func addHooks(_ hooks: AnyHook...)
}
9 changes: 9 additions & 0 deletions Sources/OpenFeature/EvaluationContext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Foundation

public protocol EvaluationContext: Structure {
func getTargetingKey() -> String

func setTargetingKey(targetingKey: String)

func merge(overridingContext: EvaluationContext) -> EvaluationContext
}
23 changes: 23 additions & 0 deletions Sources/OpenFeature/FeatureProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Foundation

/// The interface implemented by upstream flag providers to resolve flags for their service.
public protocol FeatureProvider {
var hooks: [AnyHook] { get }
var metadata: Metadata { get }

func getBooleanEvaluation(key: String, defaultValue: Bool, ctx: EvaluationContext) throws -> ProviderEvaluation<
Bool
>
func getStringEvaluation(key: String, defaultValue: String, ctx: EvaluationContext) throws -> ProviderEvaluation<
String
>
func getIntegerEvaluation(key: String, defaultValue: Int64, ctx: EvaluationContext) throws -> ProviderEvaluation<
Int64
>
func getDoubleEvaluation(key: String, defaultValue: Double, ctx: EvaluationContext) throws -> ProviderEvaluation<
Double
>
func getObjectEvaluation(key: String, defaultValue: Value, ctx: EvaluationContext) throws -> ProviderEvaluation<
Value
>
}
Loading

0 comments on commit ccf50b9

Please sign in to comment.