Skip to content

Commit 3b93185

Browse files
fix: lint issue
Signed-off-by: Thomas Poignant <[email protected]>
1 parent 57b8eef commit 3b93185

File tree

6 files changed

+42
-49
lines changed

6 files changed

+42
-49
lines changed

.swiftlint.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ force_try:
4444
severity: warning # explicitly
4545
# rules that have both warning and error levels, can set just the warning level
4646
# implicitly
47-
line_length: 110
47+
line_length: 120
4848
# they can set both implicitly with an array
4949
type_body_length:
5050
- 300 # warning
@@ -71,5 +71,5 @@ identifier_name:
7171
- url
7272
- GlobalAPIKey
7373
- key
74-
-
74+
- dto
7575
reporter: "xcode" # reporter type (xcode, json, csv, checkstyle, codeclimate, junit, html, emoji, sonarqube, markdown, github-actions-logging, summary)

Sources/go-feature-flag-provider/controller/ofrep_api.swift

+10-10
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ class OfrepAPI {
55
private let networkingService: NetworkingService
66
private var etag: String = ""
77
private let options: GoFeatureFlagProviderOptions
8-
8+
99
init(networkingService: NetworkingService, options: GoFeatureFlagProviderOptions) {
1010
self.networkingService = networkingService
1111
self.options = options
1212
}
13-
13+
1414
func postBulkEvaluateFlags(context: EvaluationContext?) async throws -> (OfrepEvaluationResponse, HTTPURLResponse) {
1515
guard let context = context else {
1616
throw OpenFeatureError.invalidContextError
1717
}
1818
try validateContext(context: context)
19-
19+
2020
guard let url = URL(string: options.endpoint) else {
2121
throw InvalidOptions.invalidEndpoint(message: "endpoint [" + options.endpoint + "] is not valid")
2222
}
@@ -28,19 +28,19 @@ class OfrepAPI {
2828
"application/json",
2929
forHTTPHeaderField: "Content-Type"
3030
)
31-
31+
3232
if etag != "" {
3333
request.setValue(etag, forHTTPHeaderField: "If-None-Match")
3434
}
35-
35+
3636
let (data, response) = try await networkingService.doRequest(for: request)
37-
37+
3838
print(response)
3939

4040
guard let httpResponse = response as? HTTPURLResponse else {
4141
throw OfrepError.httpResponseCastError
4242
}
43-
43+
4444
if httpResponse.statusCode == 401 {
4545
throw OfrepError.apiUnauthorizedError(response: httpResponse)
4646
}
@@ -56,14 +56,14 @@ class OfrepAPI {
5656
if httpResponse.statusCode == 304 {
5757
return (OfrepEvaluationResponse(flags: [], errorCode: nil, errorDetails: nil), httpResponse)
5858
}
59-
59+
6060
// Store ETag to use it in the next request
6161
if let etagHeaderValue = httpResponse.value(forHTTPHeaderField: "ETag") {
6262
if etagHeaderValue != "" && httpResponse.statusCode == 200 {
6363
etag = etagHeaderValue
6464
}
6565
}
66-
66+
6767
do {
6868
let dto = try JSONDecoder().decode(EvaluationResponseDTO.self, from: data)
6969
let evaluationResponse = OfrepEvaluationResponse.fromEvaluationResponseDTO(dto: dto)
@@ -72,7 +72,7 @@ class OfrepAPI {
7272
throw OfrepError.unmarshallError(error: error)
7373
}
7474
}
75-
75+
7676
private func validateContext(context: EvaluationContext) throws {
7777
let targetingKey = context.getTargetingKey()
7878
if targetingKey.isEmpty {

Sources/go-feature-flag-provider/go_feature_flag_provider.swift

+20-29
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ final class GoFeatureFlagProvider: FeatureProvider {
1414
private let ofrepAPI: OfrepAPI
1515

1616
private var inMemoryCache: [String: OfrepEvaluationResponseFlag] = [:]
17-
private var apiRetryAfter: Date? = nil
17+
private var apiRetryAfter: Date?
1818
private var timer: DispatchSourceTimer?
1919

20-
2120
init(options: GoFeatureFlagProviderOptions) {
2221
self.options = options
2322

@@ -29,8 +28,6 @@ final class GoFeatureFlagProvider: FeatureProvider {
2928
self.ofrepAPI = OfrepAPI(networkingService: networkService, options: self.options)
3029
}
3130

32-
33-
3431
func observe() -> AnyPublisher<OpenFeature.ProviderEvent, Never> {
3532
return eventHandler.observe()
3633
}
@@ -47,7 +44,7 @@ final class GoFeatureFlagProvider: FeatureProvider {
4744
self.startPolling(pollInterval: self.options.pollInterval)
4845
}
4946

50-
if status == .success_with_changes {
47+
if status == .successWithChanges {
5148
self.eventHandler.send(.ready)
5249
return
5350
}
@@ -64,25 +61,24 @@ final class GoFeatureFlagProvider: FeatureProvider {
6461
self.eventHandler.send(.stale)
6562
self.evaluationContext = newContext
6663
Task {
67-
do{
64+
do {
6865
let status = try await self.evaluateFlags(context: newContext)
69-
if(status == .success_with_changes || status == .success_no_changes){
66+
if(status == .successWithChanges || status == .successNoChanges ) {
7067
self.eventHandler.send(.ready)
7168
}
72-
} catch let err as OfrepError {
73-
switch err{
69+
} catch let error as OfrepError {
70+
switch error {
7471
case .apiTooManyRequestsError:
7572
return // we want to stay stale in that case so we ignore the error.
7673
default:
77-
throw err
74+
throw error
7875
}
7976
} catch {
8077
self.eventHandler.send(.error)
8178
}
8279
}
8380
}
8481

85-
8682
func getBooleanEvaluation(key: String, defaultValue: Bool,
8783
context: EvaluationContext?) throws -> ProviderEvaluation<Bool> {
8884
let flagCached = try genericEvaluation(key: key)
@@ -95,14 +91,13 @@ final class GoFeatureFlagProvider: FeatureProvider {
9591
reason: flagCached.reason)
9692
}
9793

98-
9994
private func genericEvaluation(key: String) throws -> OfrepEvaluationResponseFlag {
10095
guard let flagCached = self.inMemoryCache[key] else {
10196
throw OpenFeatureError.flagNotFoundError(key: key)
10297
}
10398

10499
if flagCached.isError() {
105-
switch flagCached.errorCode{
100+
switch flagCached.errorCode {
106101
case .flagNotFound:
107102
throw OpenFeatureError.flagNotFoundError(key: key)
108103
case .invalidContext:
@@ -123,7 +118,6 @@ final class GoFeatureFlagProvider: FeatureProvider {
123118
return flagCached
124119
}
125120

126-
127121
func getStringEvaluation(key: String, defaultValue: String,
128122
context: EvaluationContext?) throws -> ProviderEvaluation<String> {
129123
let flagCached = try genericEvaluation(key: key)
@@ -186,7 +180,7 @@ final class GoFeatureFlagProvider: FeatureProvider {
186180

187181
if arrayValue != nil {
188182
var convertedValue: [Value] = []
189-
arrayValue?.forEach{ item in
183+
arrayValue?.forEach { item in
190184
convertedValue.append(item.toValue())
191185
}
192186
return ProviderEvaluation<Value>(
@@ -200,17 +194,17 @@ final class GoFeatureFlagProvider: FeatureProvider {
200194
private func evaluateFlags(context: EvaluationContext?) async throws -> BulkEvaluationStatus {
201195
if self.apiRetryAfter != nil && self.apiRetryAfter! > Date() {
202196
// we don't want to call the API because we got a 429
203-
return BulkEvaluationStatus.rate_limited
197+
return BulkEvaluationStatus.rateLimited
204198
}
205199

206200
do {
207201
let (ofrepEvalResponse, httpResp) = try await self.ofrepAPI.postBulkEvaluateFlags(context: context)
208202

209203
if httpResp.statusCode == 304 {
210-
return BulkEvaluationStatus.success_no_changes
204+
return BulkEvaluationStatus.successNoChanges
211205
}
212206

213-
if ofrepEvalResponse.isError(){
207+
if ofrepEvalResponse.isError() {
214208
switch ofrepEvalResponse.errorCode {
215209
case .providerNotReady:
216210
throw OpenFeatureError.providerNotReadyError
@@ -232,9 +226,9 @@ final class GoFeatureFlagProvider: FeatureProvider {
232226
}
233227
}
234228
self.inMemoryCache = inMemoryCacheNew
235-
return BulkEvaluationStatus.success_with_changes
236-
} catch let error as OfrepError{
237-
switch error{
229+
return BulkEvaluationStatus.successWithChanges
230+
} catch let error as OfrepError {
231+
switch error {
238232
case .apiTooManyRequestsError(let response):
239233
self.apiRetryAfter = getRetryAfterDate(from: response.allHeaderFields)
240234
throw error
@@ -269,25 +263,22 @@ final class GoFeatureFlagProvider: FeatureProvider {
269263
return dateFormatter.date(from: retryAfterValue)
270264
}
271265

272-
273-
274-
275266
func startPolling(pollInterval: TimeInterval) {
276267
timer = DispatchSource.makeTimerSource(queue: DispatchQueue.global())
277268
timer?.schedule(deadline: .now(), repeating: pollInterval, leeway: .milliseconds(100))
278269
timer?.setEventHandler { [weak self] in
279270
guard let weakSelf = self else { return }
280271
Task {
281-
do{
272+
do {
282273
let status = try await weakSelf.evaluateFlags(context: weakSelf.evaluationContext)
283-
if status == .success_with_changes{
274+
if status == .successWithChanges {
284275
weakSelf.eventHandler.send(.configurationChanged)
285276
}
286-
} catch let err as OfrepError {
287-
switch err{
277+
} catch let error as OfrepError {
278+
switch error {
288279
case .apiTooManyRequestsError:
289280
weakSelf.eventHandler.send(.stale)
290-
throw err
281+
throw error
291282
default:
292283
weakSelf.eventHandler.send(.error)
293284
}

Sources/go-feature-flag-provider/model/bulk_evaluation_response.swift

+3-5
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,23 @@ struct EvaluationResponseFlagDTO: Codable {
1616
// let metadata: [String:Value]?
1717
}
1818

19-
struct OfrepEvaluationResponse{
19+
struct OfrepEvaluationResponse {
2020
let flags: [OfrepEvaluationResponseFlag]
2121
let errorCode: ErrorCode?
2222
let errorDetails: String?
2323

24-
2524
func isError() -> Bool {
2625
return errorCode != nil
2726
}
2827

2928
static func fromEvaluationResponseDTO(dto: EvaluationResponseDTO) -> OfrepEvaluationResponse {
3029
var flagsConverted: [OfrepEvaluationResponseFlag] = []
31-
var errCode: ErrorCode? = nil
30+
var errCode: ErrorCode?
3231
let errDetails = dto.errorDetails
3332

3433
if let flagsDTO = dto.flags {
3534
for flag in flagsDTO {
36-
var errorCode: ErrorCode? = nil
35+
var errorCode: ErrorCode?
3736
if let erroCodeValue = flag.errorCode {
3837
errorCode = convertErrorCode(code: erroCodeValue)
3938
}
@@ -57,7 +56,6 @@ struct OfrepEvaluationResponse{
5756
return OfrepEvaluationResponse(flags: flagsConverted, errorCode: errCode, errorDetails: errDetails)
5857
}
5958

60-
6159
static func convertErrorCode(code: String) -> ErrorCode {
6260
switch code {
6361
case "PROVIDER_NOT_READY":
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
enum BulkEvaluationStatus {
2-
case success_no_changes
3-
case success_with_changes
4-
case rate_limited
2+
case successNoChanges
3+
case successWithChanges
4+
case rateLimited
55
}

Tests/go-feature-flag-providerTests/provider_tests.swift

+4
Original file line numberDiff line numberDiff line change
@@ -468,4 +468,8 @@ class ProviderTests: XCTestCase {
468468
XCTAssertEqual(details2.reason, "TARGETING_MATCH")
469469
XCTAssertEqual(details2.variant, "variantB")
470470
}
471+
472+
/*
473+
should test all types
474+
*/
471475
}

0 commit comments

Comments
 (0)