Skip to content

Support legacy SplitChanges DTO #641

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

Open
wants to merge 1 commit into
base: FME-3345
Choose a base branch
from
Open
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions Split.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,11 @@
C539CAD72D8B5AD00050C732 /* TargetingRulesChange.swift in Sources */ = {isa = PBXBuildFile; fileRef = C539CAD32D8B5AD00050C732 /* TargetingRulesChange.swift */; };
C539CAD92D8B5AF00050C732 /* ProcessedRuleBasedSegmentChange.swift in Sources */ = {isa = PBXBuildFile; fileRef = C539CAD82D8B5AF00050C732 /* ProcessedRuleBasedSegmentChange.swift */; };
C539CADA2D8B5AF00050C732 /* ProcessedRuleBasedSegmentChange.swift in Sources */ = {isa = PBXBuildFile; fileRef = C539CAD82D8B5AF00050C732 /* ProcessedRuleBasedSegmentChange.swift */; };
C53EDFA92DD295E5000DCDBC /* TargetingRulesChangeDecoderTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C53EDFA82DD295E5000DCDBC /* TargetingRulesChangeDecoderTest.swift */; };
C53EDFAB2DD29640000DCDBC /* TargetingRulesChangeDecoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = C53EDFAA2DD29640000DCDBC /* TargetingRulesChangeDecoder.swift */; };
C53EDFAC2DD29640000DCDBC /* TargetingRulesChangeDecoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = C53EDFAA2DD29640000DCDBC /* TargetingRulesChangeDecoder.swift */; };
C53EDFAE2DD299CB000DCDBC /* RestClientSplitChangesTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C53EDFAD2DD299CB000DCDBC /* RestClientSplitChangesTest.swift */; };
C53EDFB02DD38572000DCDBC /* RestClientCustomDecoderTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C53EDFAF2DD38572000DCDBC /* RestClientCustomDecoderTest.swift */; };
C53F3C472DCB956900655753 /* SplitsSyncHelperTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C53F3C462DCB956900655753 /* SplitsSyncHelperTest.swift */; };
C53F3C4F2DCD112400655753 /* RuleBasedSegmentChangeProcessorStub.swift in Sources */ = {isa = PBXBuildFile; fileRef = C53F3C4E2DCD110700655753 /* RuleBasedSegmentChangeProcessorStub.swift */; };
C58F33732BDAC4AC00D66549 /* split_unsupported_matcher.json in Resources */ = {isa = PBXBuildFile; fileRef = C58F33722BDAC4AC00D66549 /* split_unsupported_matcher.json */; };
Expand Down Expand Up @@ -1956,6 +1961,10 @@
C539CAD22D8B5AD00050C732 /* RuleBasedSegmentChange.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RuleBasedSegmentChange.swift; sourceTree = "<group>"; };
C539CAD32D8B5AD00050C732 /* TargetingRulesChange.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TargetingRulesChange.swift; sourceTree = "<group>"; };
C539CAD82D8B5AF00050C732 /* ProcessedRuleBasedSegmentChange.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProcessedRuleBasedSegmentChange.swift; sourceTree = "<group>"; };
C53EDFA82DD295E5000DCDBC /* TargetingRulesChangeDecoderTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TargetingRulesChangeDecoderTest.swift; sourceTree = "<group>"; };
C53EDFAA2DD29640000DCDBC /* TargetingRulesChangeDecoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TargetingRulesChangeDecoder.swift; sourceTree = "<group>"; };
C53EDFAD2DD299CB000DCDBC /* RestClientSplitChangesTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RestClientSplitChangesTest.swift; sourceTree = "<group>"; };
C53EDFAF2DD38572000DCDBC /* RestClientCustomDecoderTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RestClientCustomDecoderTest.swift; sourceTree = "<group>"; };
C53F3C462DCB956900655753 /* SplitsSyncHelperTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SplitsSyncHelperTest.swift; sourceTree = "<group>"; };
C53F3C4E2DCD110700655753 /* RuleBasedSegmentChangeProcessorStub.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RuleBasedSegmentChangeProcessorStub.swift; sourceTree = "<group>"; };
C58F33722BDAC4AC00D66549 /* split_unsupported_matcher.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = split_unsupported_matcher.json; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2347,6 +2356,7 @@
3B6DEEB420EA6AE20067435E /* SplitModel */ = {
isa = PBXGroup;
children = (
C53EDFAA2DD29640000DCDBC /* TargetingRulesChangeDecoder.swift */,
C539CAD22D8B5AD00050C732 /* RuleBasedSegmentChange.swift */,
C539CAD32D8B5AD00050C732 /* TargetingRulesChange.swift */,
3B6DEEB520EA6AE20067435E /* Split.swift */,
Expand Down Expand Up @@ -2833,6 +2843,9 @@
592C6AA6211B6C99002D120C /* SplitTests */ = {
isa = PBXGroup;
children = (
C53EDFAF2DD38572000DCDBC /* RestClientCustomDecoderTest.swift */,
C53EDFAD2DD299CB000DCDBC /* RestClientSplitChangesTest.swift */,
C53EDFA82DD295E5000DCDBC /* TargetingRulesChangeDecoderTest.swift */,
C5977C0E2BF2832A003E293A /* Matcher */,
95ABF4B1292FDE60006ED016 /* Collections */,
95ABF4B0292FDDC0006ED016 /* Init */,
Expand Down Expand Up @@ -4187,6 +4200,7 @@
3B6DEF6320EA6AE50067435E /* ContainsStringMatcher.swift in Sources */,
955A5A51258D04DF00CAEE9F /* PeriodicRecorderWorker.swift in Sources */,
95F3F06725ACDE0400084AF8 /* EmptyMySegmentsStorage.swift in Sources */,
C53EDFAB2DD29640000DCDBC /* TargetingRulesChangeDecoder.swift in Sources */,
59B2042924F42DE80092F2E9 /* SseNotificationProcessor.swift in Sources */,
3B6DEF6D20EA6AE50067435E /* MatchesStringMatcher.swift in Sources */,
3B6DEF3420EA6AE50067435E /* Splitter.swift in Sources */,
Expand Down Expand Up @@ -4533,6 +4547,7 @@
95F3F006258D49B600084AF8 /* PersistentEventsStorageStub.swift in Sources */,
C53F3C4F2DCD112400655753 /* RuleBasedSegmentChangeProcessorStub.swift in Sources */,
5959C477227B89820064F968 /* FactoryMonitorTest.swift in Sources */,
C53EDFA92DD295E5000DCDBC /* TargetingRulesChangeDecoderTest.swift in Sources */,
592C6ABE211B718E002D120C /* ArrayBlockingQueueTest.swift in Sources */,
5919017D24A28E1D005BD12A /* HttpRequestListTest.swift in Sources */,
593225F824A384B300496D8B /* EndpointFactoryTest.swift in Sources */,
Expand Down Expand Up @@ -4572,10 +4587,12 @@
59F4AAC12513C2EC00A1C69A /* PeriodicTimerStub.swift in Sources */,
9580C98226FB8A840021D729 /* StreamingControlResetTest.swift in Sources */,
95737E162ADEC794007FD15C /* StreamingTestingHelper.swift in Sources */,
C53EDFB02DD38572000DCDBC /* RestClientCustomDecoderTest.swift in Sources */,
594F5F58253A340F00A945B4 /* StreamingOccupancyTest.swift in Sources */,
59B2047624F9B8230092F2E9 /* PushManagerEventBroadcasterTest.swift in Sources */,
95F3F0302590E4BE00084AF8 /* HttpImpressionsRecorderStub.swift in Sources */,
C5977C2C2BF2C068003E293A /* LessThanOrEqualToSemverMatcherTest.swift in Sources */,
C53EDFAE2DD299CB000DCDBC /* RestClientSplitChangesTest.swift in Sources */,
954F9C5C25780FDD00140B81 /* HttpMySegmentsFetcherTest.swift in Sources */,
59F4AA9924FE8DA200A1C69A /* NotificationProcessorStub.swift in Sources */,
595AD21324E1849500A7B750 /* JwtParserStub.swift in Sources */,
Expand Down Expand Up @@ -4974,6 +4991,7 @@
95B02D8928D0BDC30030EC8B /* HttpTask.swift in Sources */,
95B02D8A28D0BDC30030EC8B /* HttpDataRequest.swift in Sources */,
95B02D8B28D0BDC30030EC8B /* HttpStreamResponse.swift in Sources */,
C53EDFAC2DD29640000DCDBC /* TargetingRulesChangeDecoder.swift in Sources */,
95B02D8C28D0BDC30030EC8B /* HttpStreamRequest.swift in Sources */,
95B02D8D28D0BDC30030EC8B /* HttpError.swift in Sources */,
95B02D8E28D0BDC30030EC8B /* HttpRequestList.swift in Sources */,
Expand Down
61 changes: 61 additions & 0 deletions Split/Models/SplitModel/TargetingRulesChangeDecoder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// TargetingRulesChangeDecoder.swift
// Split
//
// Created on 12/05/2025.
// Copyright © 2025 Split. All rights reserved.
//

import Foundation

class TargetingRulesChangeDecoder {

/// Decodes a JSON response into a TargetingRulesChange object.
/// This decoder can handle both the new TargetingRulesChange format and the legacy SplitChange format.
/// If the legacy format is detected, it will create a TargetingRulesChange with the SplitChange data
/// and an empty RuleBasedSegmentChange.
///
/// - Parameter data: The JSON data to decode
/// - Returns: A TargetingRulesChange object
/// - Throws: Decoding errors if the JSON cannot be parsed
static func decode(from data: Data) throws -> TargetingRulesChange {
// First try to decode as TargetingRulesChange
do {
let decoder = JSONDecoder()
return try decoder.decode(TargetingRulesChange.self, from: data)
} catch {
// If that fails, try to handle the legacy format with full keys (splits, since, till)
do {
let splitChangeJson = try JSONSerialization.jsonObject(with: data, options: [])

if let jsonDict = splitChangeJson as? [String: Any],
let splitsArray = jsonDict["splits"],
let since = jsonDict["since"] as? Int64,
let till = jsonDict["till"] as? Int64 {

// Convert to the format with short keys that SplitChange expects
var newJsonDict = [String: Any]()
newJsonDict["d"] = splitsArray
newJsonDict["s"] = since
newJsonDict["t"] = till

let newJsonData = try JSONSerialization.data(withJSONObject: newJsonDict, options: [])
let decoder = JSONDecoder()
let splitChange = try decoder.decode(SplitChange.self, from: newJsonData)

// Create a TargetingRulesChange with the SplitChange data and an empty RuleBasedSegmentChange
return TargetingRulesChange(
featureFlags: splitChange,
ruleBasedSegments: RuleBasedSegmentChange.empty()
)
}

// If we get here, no format matched
throw error
} catch {
// If all decodings fail, throw the original error
throw error
}
}
}
}
1 change: 1 addition & 0 deletions Split/Network/RestClient/RestClient+SplitChanges.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extension DefaultRestClient: RestClientSplitChanges {
endpoint: endpointFactory.splitChangesEndpoint,
parameters: buildParameters(since: since, rbSince: rbSince, till: till),
headers: headers,
customDecoder: TargetingRulesChangeDecoder.decode,
completion: completion)
}

Expand Down
Loading