Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add default values on decoding for ResolveResponse #187

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Sources/Confidence/RemoteResolveConfidenceClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,39 @@ struct ResolvedFlag: Codable {
var variant: String = ""
var flagSchema: StructFlagSchema? = StructFlagSchema(schema: [:])
var reason: ResolveReason

enum CodingKeys: String, CodingKey {
case flag, value, variant, flagSchema, reason
}

init(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I understand correctly that this function will throw if flag and reason is not set but will return correctly if other fields being missing?

How come we make this change now? What prompted it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this change came from trying out a different version of our backend resolver that would return no entries in the JSON rather than empty ones, in some cases

from decoder: Decoder
) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
flag = try container.decode(String.self, forKey: .flag)
value = try container.decodeIfPresent(NetworkStruct.self, forKey: .value) ?? NetworkStruct(fields: [:])
variant = try container.decodeIfPresent(String.self, forKey: .variant) ?? ""
flagSchema = try container.decodeIfPresent(
StructFlagSchema.self, forKey: .flagSchema) ?? StructFlagSchema(schema: [:])
reason = try container.decode(ResolveReason.self, forKey: .reason)
}

init(
flag: String,
value: NetworkStruct? = NetworkStruct(fields: [:]),
variant: String = "",
flagSchema: StructFlagSchema? = StructFlagSchema(schema: [:]),
reason: ResolveReason
) {
self.flag = flag
self.value = value
self.variant = variant
self.flagSchema = flagSchema
self.reason = reason
}
}


public enum ResolveReason: String, Codable, CaseIterableDefaultsLast {
case unspecified = "RESOLVE_REASON_UNSPECIFIED"
case match = "RESOLVE_REASON_MATCH"
Expand Down