internal static ImmutableList <string> ReadStrings(ref JReader r) { var builder = ImmutableList.CreateBuilder <string>(); for (var arr = r.ArrayOrNull(); arr.Next(ref r);) { builder.Add(r.String()); } return(builder.ToImmutable()); }
internal static ImmutableList <LdValue> ReadValues(ref JReader r) { var builder = ImmutableList.CreateBuilder <LdValue>(); for (var arr = r.ArrayOrNull(); arr.Next(ref r);) { builder.Add(LdValueConverter.ReadJsonValue(ref r)); } return(builder.ToImmutable()); }
internal static Rollout?ReadRollout(ref JReader r) { ImmutableList <WeightedVariation> variations = null; UserAttribute?bucketBy = null; var obj = r.ObjectOrNull(); if (!obj.IsDefined) { return(null); } while (obj.Next(ref r)) { switch (obj.Name) { case var n when n == "variations": var listBuilder = ImmutableList.CreateBuilder <WeightedVariation>(); for (var arr = r.ArrayOrNull(); arr.Next(ref r);) { int variation = 0, weight = 0; for (var wvObj = r.Object(); wvObj.Next(ref r);) { switch (wvObj.Name) { case var nn when nn == "variation": variation = r.Int(); break; case var nn when nn == "weight": weight = r.Int(); break; } } listBuilder.Add(new WeightedVariation(variation, weight)); } variations = listBuilder.ToImmutable(); break; case var n when n == "bucketBy": var s = r.StringOrNull(); bucketBy = s is null ? (UserAttribute?)null : UserAttribute.ForName(s); break; } } return(new Rollout(variations, bucketBy)); }
internal static ImmutableList <Clause> ReadClauses(ref JReader r) { var builder = ImmutableList.CreateBuilder <Clause>(); for (var arr = r.ArrayOrNull(); arr.Next(ref r);) { UserAttribute attribute = new UserAttribute(); Operator op = null; ImmutableList <LdValue> values = null; bool negate = false; for (var obj = r.Object(); obj.Next(ref r);) { switch (obj.Name) { case var n when n == "attribute": attribute = UserAttribute.ForName(r.String()); break; case var n when n == "op": op = Operator.ForName(r.String()); // Operator.ForName never returns null - unrecognized operators return a stub object break; case var n when n == "values": values = ReadValues(ref r); break; case var n when n == "negate": negate = r.Bool(); break; } } builder.Add(new Clause(attribute, op, values, negate)); } return(builder.ToImmutable()); }
internal static Rollout?ReadRollout(ref JReader r) { ImmutableList <WeightedVariation> variations = null; UserAttribute?bucketBy = null; RolloutKind kind = RolloutKind.Rollout; int? seed = null; var obj = r.ObjectOrNull(); if (!obj.IsDefined) { return(null); } while (obj.Next(ref r)) { switch (obj.Name) { case var n when n == "variations": var listBuilder = ImmutableList.CreateBuilder <WeightedVariation>(); for (var arr = r.ArrayOrNull(); arr.Next(ref r);) { int variation = 0, weight = 0; bool untracked = false; for (var wvObj = r.Object(); wvObj.Next(ref r);) { switch (wvObj.Name) { case var nn when nn == "variation": variation = r.Int(); break; case var nn when nn == "weight": weight = r.Int(); break; case var nn when nn == "untracked": untracked = r.Bool(); break; } } listBuilder.Add(new WeightedVariation(variation, weight, untracked)); } variations = listBuilder.ToImmutable(); break; case var n when n == "bucketBy": var s = r.StringOrNull(); bucketBy = s is null ? (UserAttribute?)null : UserAttribute.ForName(s); break; case var n when n == "kind": var kindStr = r.StringOrNull(); kind = "experiment".Equals(kindStr) ? RolloutKind.Experiment : RolloutKind.Rollout; break; case var n when n == "seed": seed = r.IntOrNull(); break; } } return(new Rollout(kind, seed, variations, bucketBy)); }
public object ReadJson(ref JReader reader) { string key = null; int version = 0; bool deleted = false; bool on = false; ImmutableList <Prerequisite> prerequisites = null; ImmutableList <Target> targets = null; ImmutableList <FlagRule> rules = null; string salt = null; VariationOrRollout fallthrough = new VariationOrRollout(); int?offVariation = null; ImmutableList <LdValue> variations = null; bool trackEvents = false, trackEventsFallthrough = false; UnixMillisecondTime?debugEventsUntilDate = null; bool clientSide = false; for (var obj = reader.Object().WithRequiredProperties(_requiredProperties); obj.Next(ref reader);) { switch (obj.Name) { case var n when n == "key": key = reader.String(); break; case var n when n == "version": version = reader.Int(); break; case var n when n == "deleted": deleted = reader.Bool(); break; case var n when n == "on": on = reader.Bool(); break; case var n when n == "prerequisites": var prereqsBuilder = ImmutableList.CreateBuilder <Prerequisite>(); for (var arr = reader.ArrayOrNull(); arr.Next(ref reader);) { prereqsBuilder.Add(ReadPrerequisite(ref reader)); } prerequisites = prereqsBuilder.ToImmutable(); break; case var n when n == "targets": var targetsBuilder = ImmutableList.CreateBuilder <Target>(); for (var arr = reader.ArrayOrNull(); arr.Next(ref reader);) { targetsBuilder.Add(ReadTarget(ref reader)); } targets = targetsBuilder.ToImmutable(); break; case var n when n == "rules": var rulesBuilder = ImmutableList.CreateBuilder <FlagRule>(); for (var arr = reader.ArrayOrNull(); arr.Next(ref reader);) { rulesBuilder.Add(ReadFlagRule(ref reader)); } rules = rulesBuilder.ToImmutable(); break; case var n when n == "fallthrough": fallthrough = ReadVariationOrRollout(ref reader); break; case var n when n == "offVariation": offVariation = reader.IntOrNull(); break; case var n when n == "variations": variations = SerializationHelpers.ReadValues(ref reader); break; case var n when n == "salt": salt = reader.StringOrNull(); break; case var n when n == "trackEvents": trackEvents = reader.Bool(); break; case var n when n == "trackEventsFallthrough": trackEventsFallthrough = reader.Bool(); break; case var n when n == "debugEventsUntilDate": var dt = reader.LongOrNull(); debugEventsUntilDate = dt.HasValue ? UnixMillisecondTime.OfMillis(dt.Value) : (UnixMillisecondTime?)null; break; case var n when n == "clientSide": clientSide = reader.Bool(); break; } } if (key is null && !deleted) { throw new RequiredPropertyException("key", 0); } return(new FeatureFlag(key, version, deleted, on, prerequisites, targets, rules, fallthrough, offVariation, variations, salt, trackEvents, trackEventsFallthrough, debugEventsUntilDate, clientSide)); }
public object ReadJson(ref JReader reader) { var obj = reader.ObjectOrNull().WithRequiredProperties(_requiredProperties); if (!obj.IsDefined) { return(null); } var builder = User.Builder(""); try { while (obj.Next(ref reader)) { switch (obj.Name.ToString()) { case "key": builder.Key(reader.String()); break; case "secondary": builder.Secondary(reader.StringOrNull()); break; case "ip": builder.IPAddress(reader.StringOrNull()); break; case "country": builder.Country(reader.StringOrNull()); break; case "firstName": builder.FirstName(reader.StringOrNull()); break; case "lastName": builder.LastName(reader.StringOrNull()); break; case "name": builder.Name(reader.StringOrNull()); break; case "avatar": builder.Avatar(reader.StringOrNull()); break; case "email": builder.Email(reader.StringOrNull()); break; case "anonymous": builder.AnonymousOptional(reader.BoolOrNull()); break; case "custom": for (var customObj = reader.ObjectOrNull(); customObj.Next(ref reader);) { builder.Custom(customObj.Name.ToString(), LdValueConverter.ReadJsonValue(ref reader)); } break; case "privateAttributeNames": var internalBuilder = builder as UserBuilder; for (var arr = reader.ArrayOrNull(); arr.Next(ref reader);) { internalBuilder.AddPrivateAttribute(reader.String()); } break; } } } catch (Exception e) { throw reader.TranslateException(e); } return(builder.Build()); }