|
| 1 | +/* |
| 2 | + * Copyright 2017-2019, Optimizely |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +using System.Linq; |
| 18 | + |
| 19 | +namespace OptimizelySDK.Entity |
| 20 | +{ |
| 21 | + /// <summary> |
| 22 | + /// Extension methods providing common functionality for IExperimentCore implementations |
| 23 | + /// </summary> |
| 24 | + public static class ExperimentCoreExtensions |
| 25 | + { |
| 26 | + /// <summary> |
| 27 | + /// Get variation by ID |
| 28 | + /// </summary> |
| 29 | + /// <param name="experimentCore">The experiment or holdout instance</param> |
| 30 | + /// <param name="id">Variation ID to search for</param> |
| 31 | + /// <returns>Variation with the specified ID, or null if not found</returns> |
| 32 | + public static Variation GetVariation(this IExperimentCore experimentCore, string id) |
| 33 | + { |
| 34 | + if (experimentCore?.Variations == null || string.IsNullOrEmpty(id)) |
| 35 | + { |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + return experimentCore.Variations.FirstOrDefault(v => v.Id == id); |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Get variation by key |
| 44 | + /// </summary> |
| 45 | + /// <param name="experimentCore">The experiment or holdout instance</param> |
| 46 | + /// <param name="key">Variation key to search for</param> |
| 47 | + /// <returns>Variation with the specified key, or null if not found</returns> |
| 48 | + public static Variation GetVariationByKey(this IExperimentCore experimentCore, string key) |
| 49 | + { |
| 50 | + if (experimentCore?.Variations == null || string.IsNullOrEmpty(key)) |
| 51 | + { |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + return experimentCore.Variations.FirstOrDefault(v => v.Key == key); |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Replace audience IDs with audience names in a condition string |
| 60 | + /// </summary> |
| 61 | + /// <param name="experimentCore">The experiment or holdout instance</param> |
| 62 | + /// <param name="conditionString">String containing audience conditions</param> |
| 63 | + /// <param name="audiencesMap">Map of audience ID to audience name</param> |
| 64 | + /// <returns>String with audience IDs replaced by names</returns> |
| 65 | + public static string ReplaceAudienceIdsWithNames(this IExperimentCore experimentCore, |
| 66 | + string conditionString, System.Collections.Generic.Dictionary<string, string> audiencesMap) |
| 67 | + { |
| 68 | + if (string.IsNullOrEmpty(conditionString) || audiencesMap == null) |
| 69 | + { |
| 70 | + return conditionString ?? string.Empty; |
| 71 | + } |
| 72 | + |
| 73 | + const string beginWord = "AUDIENCE("; |
| 74 | + const string endWord = ")"; |
| 75 | + var keyIdx = 0; |
| 76 | + var audienceId = string.Empty; |
| 77 | + var collect = false; |
| 78 | + var replaced = string.Empty; |
| 79 | + |
| 80 | + foreach (var ch in conditionString) |
| 81 | + { |
| 82 | + // Extract audience id in parenthesis (example: AUDIENCE("35") => "35") |
| 83 | + if (collect) |
| 84 | + { |
| 85 | + if (ch.ToString() == endWord) |
| 86 | + { |
| 87 | + // Output the extracted audienceId |
| 88 | + var audienceName = audiencesMap.ContainsKey(audienceId) ? audiencesMap[audienceId] : audienceId; |
| 89 | + replaced += $"\"{audienceName}\""; |
| 90 | + collect = false; |
| 91 | + audienceId = string.Empty; |
| 92 | + } |
| 93 | + else |
| 94 | + { |
| 95 | + audienceId += ch; |
| 96 | + } |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + // Walk-through until finding a matching keyword "AUDIENCE(" |
| 101 | + if (ch == beginWord[keyIdx]) |
| 102 | + { |
| 103 | + keyIdx++; |
| 104 | + if (keyIdx == beginWord.Length) |
| 105 | + { |
| 106 | + keyIdx = 0; |
| 107 | + collect = true; |
| 108 | + } |
| 109 | + continue; |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + if (keyIdx > 0) |
| 114 | + { |
| 115 | + replaced += beginWord.Substring(0, keyIdx); |
| 116 | + } |
| 117 | + keyIdx = 0; |
| 118 | + } |
| 119 | + |
| 120 | + // Pass through other characters |
| 121 | + replaced += ch; |
| 122 | + } |
| 123 | + |
| 124 | + return replaced; |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments