Skip to content

Commit

Permalink
Ci/add swift lint and swiftformat (#53)
Browse files Browse the repository at this point in the history
* Add swiftlint

* reorder Package.swift

* Disable the local swiftlint, add swiftformat, avoid removing the internal keyword from the Trait, format all the files in the SDK

* Restore internal init on FlagsmithError

* Run the linter with --fix to catch a few of the obvious issues

* All of the linter errors covered, before running through swiftformat again

* More changes, can now run swiftlint and swiftformat together and they're both happy

* Tidy up the Package.swift and add some docs for contributors

---------

Co-authored-by: Matthew Elwell <[email protected]>
  • Loading branch information
gazreese and matthewelwell authored May 2, 2024
1 parent e78bd04 commit 16bbfb0
Show file tree
Hide file tree
Showing 29 changed files with 1,283 additions and 1,130 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,18 @@ jobs:
run: swift build -v
- name: Run tests
run: swift test -v

swift-lint:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Run SwiftLint
uses: norio-nomura/[email protected]
# TODO: enable these settings:
# env:
# DIFF_BASE: ${{ github.base_ref }}
# with:
# args: --strict
1 change: 1 addition & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--disable redundantInternal
17 changes: 17 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
excluded: # paths to ignore during linting. Takes precedence over `included`.
- Carthage
- .build

disabled_rules: # rule identifiers to exclude from running
- nesting
- trailing_whitespace
- opening_brace
- trailing_comma

line_length:
warning: 140
error: 160
ignores_comments: true
ignores_urls: true


53 changes: 27 additions & 26 deletions Example/FlagsmithClient/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,31 @@
import UIKit
import FlagsmithClient

func isSuccess<T,F>(_ result: Result<T,F>) -> Bool {
func isSuccess<T, F>(_ result: Result<T, F>) -> Bool {
if case .success = result { return true } else { return false }
}

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
let concurrentQueue = DispatchQueue(label: "concurrentQueue", attributes: .concurrent)

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
Flagsmith.shared.apiKey = "<add your API key from the Flagsmith settings page>"

// set default flags
Flagsmith.shared.defaultFlags = [Flag(featureName: "feature_a", enabled: false),
Flag(featureName: "font_size", intValue:12, enabled: true),
Flag(featureName: "my_name", stringValue:"Testing", enabled: true)]
Flag(featureName: "font_size", intValue: 12, enabled: true),
Flag(featureName: "my_name", stringValue: "Testing", enabled: true)]

// set cache on / off (defaults to off)
Flagsmith.shared.cacheConfig.useCache = true

// set custom cache to use (defaults to shared URLCache)
//Flagsmith.shared.cacheConfig.cache = <CUSTOM_CACHE>
// Flagsmith.shared.cacheConfig.cache = <CUSTOM_CACHE>

// set skip API on / off (defaults to off)
Flagsmith.shared.cacheConfig.skipAPI = false
Expand All @@ -42,53 +43,53 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

// set analytics on or off
Flagsmith.shared.enableAnalytics = true

// set the analytics flush period in seconds
Flagsmith.shared.analyticsFlushPeriod = 10
Flagsmith.shared.getFeatureFlags() { (result) in

Flagsmith.shared.getFeatureFlags { (result) in
print(result)
}
Flagsmith.shared.hasFeatureFlag(withID: "freeze_delinquent_accounts") { (result) in
print(result)
}

// Try getting the feature flags concurrently to ensure that this does not cause any issues
// This was originally highlighted in https://github.com/Flagsmith/flagsmith-ios-client/pull/40
for _ in 1...20 {
concurrentQueue.async {
Flagsmith.shared.getFeatureFlags() { (result) in
Flagsmith.shared.getFeatureFlags { (_) in
}
}
}
//Flagsmith.shared.setTrait(Trait(key: "<my_key>", value: "<my_value>"), forIdentity: "<my_identity>") { (result) in print(result) }
//Flagsmith.shared.getIdentity("<my_key>") { (result) in print(result) }

// Flagsmith.shared.setTrait(Trait(key: "<my_key>", value: "<my_value>"), forIdentity: "<my_identity>") { (result) in print(result) }
// Flagsmith.shared.getIdentity("<my_key>") { (result) in print(result) }
return true
}

func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

#if swift(>=5.5.2)
/// (Example) Setup the app based on the available feature flags.
///
Expand All @@ -98,7 +99,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
@available(iOS 13.0, *)
func determineAppConfiguration() async {
let flagsmith = Flagsmith.shared

do {
if try await flagsmith.hasFeatureFlag(withID: "ab_test_enabled") {
if let theme = try await flagsmith.getValueForFeature(withID: "app_theme") {
Expand All @@ -116,7 +117,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
print(error)
}
}

func setTheme(_ theme: TypedValue) {}
func processFlags(_ flags: [Flag]) {}
#endif
Expand Down
12 changes: 1 addition & 11 deletions Example/FlagsmithClient/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,5 @@ import UIKit
import FlagsmithClient

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}
40 changes: 17 additions & 23 deletions FlagsmithClient/Classes/Feature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,21 @@ import Foundation
A Feature represents a flag or remote configuration value on the server.
*/
public struct Feature: Codable, Sendable {
enum CodingKeys: String, CodingKey {
case name
case type
case description
}

/// The name of the feature
public let name: String
public let type: String?
public let description: String?

init(name: String, type: String?, description: String?) {
self.name = name
self.type = type
self.description = description
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.name, forKey: .name)
try container.encodeIfPresent(self.type, forKey: .type)
try container.encodeIfPresent(self.description, forKey: .description)
}
enum CodingKeys: String, CodingKey {
case name
case type
case description
}

/// The name of the feature
public let name: String
public let type: String?
public let description: String?

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(name, forKey: .name)
try container.encodeIfPresent(type, forKey: .type)
try container.encodeIfPresent(description, forKey: .description)
}
}
105 changes: 61 additions & 44 deletions FlagsmithClient/Classes/Flag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,66 @@
import Foundation

/**
A Flag represents a feature flag on the server.
*/
A Flag represents a feature flag on the server.
*/
public struct Flag: Codable, Sendable {
enum CodingKeys: String, CodingKey {
case feature
case value = "feature_state_value"
case enabled
}

public let feature: Feature
public let value: TypedValue
public let enabled: Bool

public init(featureName:String, boolValue: Bool, enabled: Bool, featureType:String? = nil, featureDescription:String? = nil) {
self.init(featureName: featureName, value: TypedValue.bool(boolValue), enabled: enabled, featureType: featureType, featureDescription: featureDescription)
}

public init(featureName:String, floatValue: Float, enabled: Bool, featureType:String? = nil, featureDescription:String? = nil) {
self.init(featureName: featureName, value: TypedValue.float(floatValue), enabled: enabled, featureType: featureType, featureDescription: featureDescription)
}

public init(featureName:String, intValue: Int, enabled: Bool, featureType:String? = nil, featureDescription:String? = nil) {
self.init(featureName: featureName, value: TypedValue.int(intValue), enabled: enabled, featureType: featureType, featureDescription: featureDescription)
}

public init(featureName:String, stringValue: String, enabled: Bool, featureType:String? = nil, featureDescription:String? = nil) {
self.init(featureName: featureName, value: TypedValue.string(stringValue), enabled: enabled, featureType: featureType, featureDescription: featureDescription)
}

public init(featureName:String, enabled: Bool, featureType:String? = nil, featureDescription:String? = nil) {
self.init(featureName: featureName, value: TypedValue.null, enabled: enabled, featureType: featureType, featureDescription: featureDescription)
}

public init(featureName:String, value: TypedValue, enabled: Bool, featureType:String? = nil, featureDescription:String? = nil) {
self.feature = Feature(name: featureName, type: featureType, description: featureDescription)
self.value = value
self.enabled = enabled
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.feature, forKey: .feature)
try container.encode(self.value, forKey: .value)
try container.encode(self.enabled, forKey: .enabled)
}
enum CodingKeys: String, CodingKey {
case feature
case value = "feature_state_value"
case enabled
}

public let feature: Feature
public let value: TypedValue
public let enabled: Bool

public init(featureName: String, boolValue: Bool, enabled: Bool,
featureType: String? = nil, featureDescription: String? = nil)
{
self.init(featureName: featureName, value: TypedValue.bool(boolValue), enabled: enabled,
featureType: featureType, featureDescription: featureDescription)
}

public init(featureName: String, floatValue: Float, enabled: Bool,
featureType: String? = nil, featureDescription: String? = nil)
{
self.init(featureName: featureName, value: TypedValue.float(floatValue), enabled: enabled,
featureType: featureType, featureDescription: featureDescription)
}

public init(featureName: String, intValue: Int, enabled: Bool,
featureType: String? = nil, featureDescription: String? = nil)
{
self.init(featureName: featureName, value: TypedValue.int(intValue), enabled: enabled,
featureType: featureType, featureDescription: featureDescription)
}

public init(featureName: String, stringValue: String, enabled: Bool,
featureType: String? = nil, featureDescription: String? = nil)
{
self.init(featureName: featureName, value: TypedValue.string(stringValue), enabled: enabled,
featureType: featureType, featureDescription: featureDescription)
}

public init(featureName: String, enabled: Bool,
featureType: String? = nil, featureDescription: String? = nil)
{
self.init(featureName: featureName, value: TypedValue.null, enabled: enabled,
featureType: featureType, featureDescription: featureDescription)
}

public init(featureName: String, value: TypedValue, enabled: Bool,
featureType: String? = nil, featureDescription: String? = nil)
{
feature = Feature(name: featureName, type: featureType, description: featureDescription)
self.value = value
self.enabled = enabled
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(feature, forKey: .feature)
try container.encode(value, forKey: .value)
try container.encode(enabled, forKey: .enabled)
}
}
Loading

0 comments on commit 16bbfb0

Please sign in to comment.