@@ -547,33 +547,31 @@ open class OptimizelyClient: NSObject {
547
547
logger. i ( . userReceivedDefaultVariableValue( userId, featureKey, variableKey) )
548
548
}
549
549
550
- var typeName : String ?
550
+ var type : Constants . VariableValueType ?
551
551
var valueParsed : T ?
552
552
553
553
switch T . self {
554
554
case is String . Type :
555
- typeName = " string "
555
+ type = . string
556
556
valueParsed = featureValue as? T
557
557
case is Int . Type :
558
- typeName = " integer "
558
+ type = . integer
559
559
valueParsed = Int ( featureValue) as? T
560
560
case is Double . Type :
561
- typeName = " double "
561
+ type = . double
562
562
valueParsed = Double ( featureValue) as? T
563
563
case is Bool . Type :
564
- typeName = " boolean "
564
+ type = . boolean
565
565
valueParsed = Bool ( featureValue) as? T
566
566
case is OptimizelyJSON . Type :
567
- typeName = " json "
568
- if let optimizelyJSON = OptimizelyJSON ( payload: featureValue) {
569
- valueParsed = optimizelyJSON as? T
570
- }
567
+ type = . json
568
+ valueParsed = OptimizelyJSON ( payload: featureValue) as? T
571
569
default :
572
570
break
573
571
}
574
572
575
573
guard let value = valueParsed,
576
- variable . type == typeName else {
574
+ type? . rawValue == variable . type else {
577
575
throw OptimizelyError . variableValueInvalid ( variableKey)
578
576
}
579
577
@@ -591,12 +589,88 @@ open class OptimizelyClient: NSObject {
591
589
feature: featureFlag,
592
590
featureEnabled: featureEnabled,
593
591
variableKey: variableKey,
594
- variableType: typeName ,
592
+ variableType: variable . type ,
595
593
variableValue: value)
596
594
597
595
return value
598
596
}
599
597
598
+ /// Gets all the variables for a given feature.
599
+ ///
600
+ /// - Parameters:
601
+ /// - featureKey: The key for the feature flag.
602
+ /// - userId: The user ID to be used for bucketing.
603
+ /// - attributes: The user's attributes.
604
+ /// - Returns: all the variables for a given feature.
605
+ /// - Throws: `OptimizelyError` if feature parameter is not valid
606
+ public func getAllFeatureVariables( featureKey: String ,
607
+ userId: String ,
608
+ attributes: OptimizelyAttributes ? = nil ) throws -> OptimizelyJSON {
609
+ guard let config = self . config else { throw OptimizelyError . sdkNotReady }
610
+ var variableMap = [ String: Any] ( )
611
+ var enabled = false
612
+
613
+ guard let featureFlag = config. getFeatureFlag ( key: featureKey) else {
614
+ throw OptimizelyError . featureKeyInvalid ( featureKey)
615
+ }
616
+
617
+ let decision = self . decisionService. getVariationForFeature ( config: config,
618
+ featureFlag: featureFlag,
619
+ userId: userId,
620
+ attributes: attributes ?? OptimizelyAttributes ( ) )
621
+ if let featureEnabled = decision? . variation? . featureEnabled {
622
+ enabled = featureEnabled
623
+ }
624
+
625
+ for (_, v) in featureFlag. variablesMap {
626
+ var featureValue = v. value
627
+ if enabled, let variable = decision? . variation? . getVariable ( id: v. id) {
628
+ featureValue = variable. value
629
+ }
630
+
631
+ var valueParsed : Any ? = featureValue
632
+
633
+ if let valueType = Constants . VariableValueType ( rawValue: v. type) {
634
+ switch valueType {
635
+ case . string:
636
+ break
637
+ case . integer:
638
+ valueParsed = Int ( featureValue)
639
+ break
640
+ case . double:
641
+ valueParsed = Double ( featureValue)
642
+ break
643
+ case . boolean:
644
+ valueParsed = Bool ( featureValue)
645
+ break
646
+ case . json:
647
+ valueParsed = OptimizelyJSON ( payload: featureValue) ? . toMap ( )
648
+ break
649
+ }
650
+ }
651
+
652
+ if let value = valueParsed {
653
+ variableMap [ v. key] = value
654
+ } else {
655
+ logger. e ( OptimizelyError . variableValueInvalid ( v. key) )
656
+ }
657
+ }
658
+
659
+ guard let optimizelyJSON = OptimizelyJSON ( map: variableMap) else {
660
+ throw OptimizelyError . invalidDictionary
661
+ }
662
+
663
+ sendDecisionNotification ( decisionType: . allFeatureVariables,
664
+ userId: userId,
665
+ attributes: attributes,
666
+ experiment: decision? . experiment,
667
+ variation: decision? . variation,
668
+ feature: featureFlag,
669
+ featureEnabled: enabled,
670
+ variableValues: variableMap)
671
+ return optimizelyJSON
672
+ }
673
+
600
674
/// Get array of features that are enabled for the user.
601
675
///
602
676
/// - Parameters:
@@ -783,6 +857,7 @@ extension OptimizelyClient {
783
857
variableKey: String ? = nil ,
784
858
variableType: String ? = nil ,
785
859
variableValue: Any ? = nil ,
860
+ variableValues: [ String : Any ] ? = nil ,
786
861
async : Bool = true ) {
787
862
self . sendNotification ( type: . decision,
788
863
args: [ decisionType. rawValue,
@@ -795,7 +870,8 @@ extension OptimizelyClient {
795
870
featureEnabled: featureEnabled,
796
871
variableKey: variableKey,
797
872
variableType: variableType,
798
- variableValue: variableValue) ] ,
873
+ variableValue: variableValue,
874
+ variableValues: variableValues) ] ,
799
875
async : async )
800
876
}
801
877
@@ -810,7 +886,8 @@ extension OptimizelyClient {
810
886
featureEnabled: Bool ? = nil ,
811
887
variableKey: String ? = nil ,
812
888
variableType: String ? = nil ,
813
- variableValue: Any ? = nil ) -> [ String : Any ] {
889
+ variableValue: Any ? = nil ,
890
+ variableValues: [ String : Any ] ? = nil ) -> [ String : Any ] {
814
891
815
892
var decisionInfo = [ String: Any] ( )
816
893
@@ -821,7 +898,7 @@ extension OptimizelyClient {
821
898
decisionInfo [ Constants . ExperimentDecisionInfoKeys. experiment] = experiment. key
822
899
decisionInfo [ Constants . ExperimentDecisionInfoKeys. variation] = variation? . key ?? NSNull ( )
823
900
824
- case . feature, . featureVariable:
901
+ case . feature, . featureVariable, . allFeatureVariables :
825
902
guard let feature = feature, let featureEnabled = featureEnabled else { return decisionInfo }
826
903
827
904
decisionInfo [ Constants . DecisionInfoKeys. feature] = feature. key
@@ -841,15 +918,20 @@ extension OptimizelyClient {
841
918
842
919
if decisionType == . featureVariable {
843
920
guard let variableKey = variableKey, let variableType = variableType, let variableValue = variableValue else {
844
- return decisionInfo
921
+ return decisionInfo
845
922
}
846
923
847
924
decisionInfo [ Constants . DecisionInfoKeys. variable] = variableKey
848
925
decisionInfo [ Constants . DecisionInfoKeys. variableType] = variableType
849
926
decisionInfo [ Constants . DecisionInfoKeys. variableValue] = variableValue
927
+ } else if decisionType == . allFeatureVariables {
928
+ guard let variableValues = variableValues else {
929
+ return decisionInfo
930
+ }
931
+ decisionInfo [ Constants . DecisionInfoKeys. variableValues] = variableValues
850
932
}
851
933
}
852
-
934
+
853
935
return decisionInfo
854
936
}
855
937
0 commit comments