|
| 1 | +package cloud.eppo.ufc.dto.adapters; |
| 2 | + |
| 3 | +import static cloud.eppo.Utils.getISODate; |
| 4 | +import static cloud.eppo.Utils.parseUtcISODateNode; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.core.JacksonException; |
| 7 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 8 | +import com.fasterxml.jackson.core.JsonParser; |
| 9 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 10 | +import com.fasterxml.jackson.databind.JsonNode; |
| 11 | +import com.fasterxml.jackson.databind.SerializerProvider; |
| 12 | +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; |
| 13 | +import com.fasterxml.jackson.databind.ser.std.StdSerializer; |
| 14 | + |
| 15 | +import org.slf4j.Logger; |
| 16 | +import org.slf4j.LoggerFactory; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Date; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.Iterator; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.concurrent.ConcurrentHashMap; |
| 28 | +import java.util.function.BiConsumer; |
| 29 | + |
| 30 | +import cloud.eppo.api.EppoValue; |
| 31 | +import cloud.eppo.model.ShardRange; |
| 32 | +import cloud.eppo.ufc.dto.Allocation; |
| 33 | +import cloud.eppo.ufc.dto.BanditFlagVariation; |
| 34 | +import cloud.eppo.ufc.dto.BanditReference; |
| 35 | +import cloud.eppo.ufc.dto.FlagConfig; |
| 36 | +import cloud.eppo.ufc.dto.FlagConfigResponse; |
| 37 | +import cloud.eppo.ufc.dto.OperatorType; |
| 38 | +import cloud.eppo.ufc.dto.Shard; |
| 39 | +import cloud.eppo.ufc.dto.Split; |
| 40 | +import cloud.eppo.ufc.dto.TargetingCondition; |
| 41 | +import cloud.eppo.ufc.dto.TargetingRule; |
| 42 | +import cloud.eppo.ufc.dto.Variation; |
| 43 | +import cloud.eppo.ufc.dto.VariationType; |
| 44 | + |
| 45 | +/** |
| 46 | + * Hand-rolled serializer so that we don't rely on annotations and method names, which can be |
| 47 | + * unreliable when ProGuard minification is in-use and not configured to protect |
| 48 | + * JSON-serialization-related classes and annotations. |
| 49 | + */ |
| 50 | +public class FlagConfigResponseSerializer extends StdSerializer<FlagConfigResponse> { |
| 51 | + private static final Logger log = LoggerFactory.getLogger(FlagConfigResponseSerializer.class); |
| 52 | + private final EppoValueSerializer eppoValueSerializer = new EppoValueSerializer(); |
| 53 | + |
| 54 | + protected FlagConfigResponseSerializer(Class<FlagConfigResponse> vc) { |
| 55 | + super(vc); |
| 56 | + } |
| 57 | + |
| 58 | + public FlagConfigResponseSerializer() { |
| 59 | + this(null); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void serialize(FlagConfigResponse src, JsonGenerator jgen, SerializerProvider provider) |
| 64 | + throws IOException, JacksonException { |
| 65 | + jgen.writeStartObject(); |
| 66 | + final FlagConfigResponse.Format format = src.getFormat(); |
| 67 | + if (format != null) { |
| 68 | + jgen.writeStringField("format", format.name()); |
| 69 | + } |
| 70 | + final Map<String, FlagConfig> flags = src.getFlags(); |
| 71 | + if (flags != null) { |
| 72 | + jgen.writeFieldName("flags"); |
| 73 | + jgen.writeStartObject(); |
| 74 | + for (Map.Entry<String, FlagConfig> entry : src.getFlags().entrySet()) { |
| 75 | + jgen.writeFieldName(entry.getKey()); |
| 76 | + serializeFlag(entry.getValue(), jgen, provider); |
| 77 | + } |
| 78 | + jgen.writeEndObject(); |
| 79 | + } |
| 80 | + final Map<String, BanditReference> banditReferences = src.getBanditReferences(); |
| 81 | + if (banditReferences != null) { |
| 82 | + jgen.writeFieldName("banditReferences"); |
| 83 | + jgen.writeStartObject(); |
| 84 | + for (Map.Entry<String, BanditReference> entry : banditReferences.entrySet()) { |
| 85 | + jgen.writeFieldName(entry.getKey()); |
| 86 | + serializeBanditReference(entry.getValue(), jgen); |
| 87 | + } |
| 88 | + jgen.writeEndObject(); |
| 89 | + } |
| 90 | + jgen.writeEndObject(); |
| 91 | + } |
| 92 | + |
| 93 | + private void serializeFlag(FlagConfig flagConfig, JsonGenerator jgen, SerializerProvider provider) |
| 94 | + throws IOException { |
| 95 | + jgen.writeStartObject(); |
| 96 | + jgen.writeStringField("key", flagConfig.getKey()); |
| 97 | + jgen.writeBooleanField("enabled", flagConfig.isEnabled()); |
| 98 | + jgen.writeNumberField("totalShards", flagConfig.getTotalShards()); |
| 99 | + final VariationType variationType = flagConfig.getVariationType(); |
| 100 | + if (variationType != null) { |
| 101 | + jgen.writeStringField("variationType", variationType.value); |
| 102 | + } |
| 103 | + final Map<String, Variation> variations = flagConfig.getVariations(); |
| 104 | + if (variations != null) { |
| 105 | + jgen.writeFieldName("variations"); |
| 106 | + jgen.writeStartObject(); |
| 107 | + for (Map.Entry<String, Variation> entry : variations.entrySet()) { |
| 108 | + jgen.writeFieldName(entry.getKey()); |
| 109 | + serializeVariation(entry.getValue(), jgen); |
| 110 | + } |
| 111 | + jgen.writeEndObject(); |
| 112 | + } |
| 113 | + final List<Allocation> allocations = flagConfig.getAllocations(); |
| 114 | + if (allocations != null) { |
| 115 | + jgen.writeFieldName("allocations"); |
| 116 | + jgen.writeStartArray(); |
| 117 | + for (Allocation allocation : allocations) { |
| 118 | + serializeAllocation(allocation, jgen, provider); |
| 119 | + } |
| 120 | + jgen.writeEndArray(); |
| 121 | + } |
| 122 | + jgen.writeEndObject(); |
| 123 | + } |
| 124 | + |
| 125 | + private void serializeVariation(Variation variation, JsonGenerator jgen) |
| 126 | + throws IOException { |
| 127 | + jgen.writeStartObject(); |
| 128 | + jgen.writeStringField("key", variation.getKey()); |
| 129 | + jgen.writeObjectField("value", variation.getValue()); |
| 130 | + jgen.writeEndObject(); |
| 131 | + } |
| 132 | + |
| 133 | + private void serializeAllocation(Allocation allocation, JsonGenerator jgen, SerializerProvider provider) |
| 134 | + throws IOException { |
| 135 | + jgen.writeStartObject(); |
| 136 | + jgen.writeStringField("key", allocation.getKey()); |
| 137 | + final Set<TargetingRule> rules = allocation.getRules(); |
| 138 | + if (rules != null) { |
| 139 | + jgen.writeFieldName("rules"); |
| 140 | + jgen.writeStartArray(); |
| 141 | + for (TargetingRule rule : rules) { |
| 142 | + serializeTargetingRule(rule, jgen, provider); |
| 143 | + } |
| 144 | + jgen.writeEndArray(); |
| 145 | + } |
| 146 | + final Date startAt = allocation.getStartAt(); |
| 147 | + if (startAt != null) { |
| 148 | + jgen.writeStringField("startAt", getISODate(startAt)); |
| 149 | + } |
| 150 | + final Date endAt = allocation.getEndAt(); |
| 151 | + if (endAt != null) { |
| 152 | + jgen.writeStringField("endAt", getISODate(endAt)); |
| 153 | + } |
| 154 | + final List<Split> splits = allocation.getSplits(); |
| 155 | + if (splits != null) { |
| 156 | + jgen.writeFieldName("splits"); |
| 157 | + jgen.writeStartArray(); |
| 158 | + for (Split split : splits) { |
| 159 | + serializeSplit(split, jgen); |
| 160 | + } |
| 161 | + jgen.writeEndArray(); |
| 162 | + } |
| 163 | + jgen.writeBooleanField("doLog", allocation.doLog()); |
| 164 | + |
| 165 | + jgen.writeEndObject(); |
| 166 | + } |
| 167 | + |
| 168 | + private void serializeTargetingRule(TargetingRule rule, JsonGenerator jgen, SerializerProvider provider) |
| 169 | + throws IOException { |
| 170 | + jgen.writeStartObject(); |
| 171 | + final Set<TargetingCondition> conditions = rule.getConditions(); |
| 172 | + if (conditions != null) { |
| 173 | + jgen.writeFieldName("conditions"); |
| 174 | + jgen.writeStartArray(); |
| 175 | + for (TargetingCondition condition : conditions) { |
| 176 | + jgen.writeStartObject(); |
| 177 | + jgen.writeStringField("attribute", condition.getAttribute()); |
| 178 | + final OperatorType operator = condition.getOperator(); |
| 179 | + if (operator != null) { |
| 180 | + jgen.writeStringField("operator", operator.value); |
| 181 | + } |
| 182 | + final EppoValue value = condition.getValue(); |
| 183 | + if (value != null) { |
| 184 | + jgen.writeFieldName("value"); |
| 185 | + eppoValueSerializer.serialize(value, jgen, provider); |
| 186 | + } |
| 187 | + |
| 188 | + jgen.writeEndObject(); |
| 189 | + } |
| 190 | + jgen.writeEndArray(); |
| 191 | + } |
| 192 | + jgen.writeEndObject(); |
| 193 | + } |
| 194 | + |
| 195 | + private void serializeSplit(Split split, JsonGenerator jgen) |
| 196 | + throws IOException { |
| 197 | + jgen.writeStartObject(); |
| 198 | + jgen.writeStringField("variationKey", split.getVariationKey()); |
| 199 | + final Set<Shard> shards = split.getShards(); |
| 200 | + if (shards != null) { |
| 201 | + jgen.writeFieldName("shards"); |
| 202 | + jgen.writeStartArray(); |
| 203 | + for (Shard shard : shards) { |
| 204 | + serializeShard(shard, jgen); |
| 205 | + } |
| 206 | + jgen.writeEndArray(); |
| 207 | + } |
| 208 | + Map<String, String> extraLogging = split.getExtraLogging(); |
| 209 | + if (extraLogging != null) { |
| 210 | + jgen.writeFieldName("extraLogging"); |
| 211 | + jgen.writeStartObject(); |
| 212 | + for (Map.Entry<String, String> extraLog : extraLogging.entrySet()) { |
| 213 | + jgen.writeStringField(extraLog.getKey(), extraLog.getValue()); |
| 214 | + } |
| 215 | + jgen.writeEndObject(); |
| 216 | + } |
| 217 | + jgen.writeEndObject(); |
| 218 | + } |
| 219 | + |
| 220 | + private void serializeShard(Shard shard, JsonGenerator jgen) |
| 221 | + throws IOException { |
| 222 | + jgen.writeStartObject(); |
| 223 | + jgen.writeStringField("salt", shard.getSalt()); |
| 224 | + final Set<ShardRange> ranges = shard.getRanges(); |
| 225 | + if (ranges != null) { |
| 226 | + jgen.writeFieldName("ranges"); |
| 227 | + jgen.writeStartArray(); |
| 228 | + for (ShardRange range : ranges) { |
| 229 | + jgen.writeStartObject(); |
| 230 | + jgen.writeNumberField("start", range.getStart()); |
| 231 | + jgen.writeNumberField("end", range.getEnd()); |
| 232 | + jgen.writeEndObject(); |
| 233 | + } |
| 234 | + jgen.writeEndArray(); |
| 235 | + } |
| 236 | + jgen.writeEndObject(); |
| 237 | + } |
| 238 | + |
| 239 | + private void serializeBanditReference(BanditReference banditReference, JsonGenerator jgen) |
| 240 | + throws IOException { |
| 241 | + jgen.writeStartObject(); |
| 242 | + jgen.writeStringField("modelVersion", banditReference.getModelVersion()); |
| 243 | + List<BanditFlagVariation> flagVariations = banditReference.getFlagVariations(); |
| 244 | + if (flagVariations != null) { |
| 245 | + jgen.writeFieldName("flagVariations"); |
| 246 | + jgen.writeStartArray(); |
| 247 | + for (BanditFlagVariation flagVariation : flagVariations) { |
| 248 | + jgen.writeStartObject(); |
| 249 | + jgen.writeStringField("key", flagVariation.getBanditKey()); |
| 250 | + jgen.writeStringField("flagKey", flagVariation.getFlagKey()); |
| 251 | + jgen.writeStringField("allocationKey", flagVariation.getAllocationKey()); |
| 252 | + jgen.writeStringField("variationKey", flagVariation.getVariationKey()); |
| 253 | + jgen.writeStringField("variationValue", flagVariation.getVariationValue()); |
| 254 | + jgen.writeEndObject(); |
| 255 | + } |
| 256 | + jgen.writeEndArray(); |
| 257 | + } |
| 258 | + jgen.writeEndObject(); |
| 259 | + } |
| 260 | +} |
0 commit comments