Esempio n. 1
0
        public void WriteJson(object value, IValueWriter writer)
        {
            var flag = (FeatureFlag)value;
            var obj  = writer.Object();

            obj.Name("key").String(flag.Key);
            obj.Name("version").Int(flag.Version);
            obj.Name("deleted").Bool(flag.Deleted);
            obj.Name("on").Bool(flag.On);

            var prereqsArr = obj.Name("prerequisites").Array();

            foreach (var p in flag.Prerequisites)
            {
                var prereqObj = prereqsArr.Object();
                prereqObj.Name("key").String(p.Key);
                prereqObj.Name("variation").Int(p.Variation);
                prereqObj.End();
            }
            prereqsArr.End();

            var targetsArr = obj.Name("targets").Array();

            foreach (var t in flag.Targets)
            {
                var targetObj = targetsArr.Object();
                targetObj.Name("variation").Int(t.Variation);
                SerializationHelpers.WriteStrings(targetObj.Name("values"), t.Values);
                targetObj.End();
            }
            targetsArr.End();

            var rulesArr = obj.Name("rules").Array();

            foreach (var r in flag.Rules)
            {
                var ruleObj = rulesArr.Object();
                ruleObj.Name("id").String(r.Id);
                SerializationHelpers.WriteVariationOrRollout(ref ruleObj, r.Variation, r.Rollout);
                SerializationHelpers.WriteClauses(ruleObj.Name("clauses"), r.Clauses);
                ruleObj.Name("trackEvents").Bool(r.TrackEvents);
                ruleObj.End();
            }
            rulesArr.End();

            var fallthroughObj = obj.Name("fallthrough").Object();

            SerializationHelpers.WriteVariationOrRollout(ref fallthroughObj, flag.Fallthrough.Variation, flag.Fallthrough.Rollout);
            fallthroughObj.End();

            if (flag.OffVariation.HasValue)
            {
                obj.Name("offVariation").Int(flag.OffVariation.Value);
            }
            SerializationHelpers.WriteValues(obj.Name("variations"), flag.Variations);
            obj.Name("salt").String(flag.Salt);
            obj.MaybeName("trackEvents", flag.TrackEvents).Bool(flag.TrackEvents);
            obj.MaybeName("trackEventsFallthrough", flag.TrackEventsFallthrough).Bool(flag.TrackEventsFallthrough);
            if (flag.DebugEventsUntilDate.HasValue)
            {
                obj.Name("debugEventsUntilDate").Long(flag.DebugEventsUntilDate.Value.Value);
            }
            obj.Name("clientSide").Bool(flag.ClientSide);

            obj.End();
        }
Esempio n. 2
0
        public object ReadJson(ref JReader reader)
        {
            string key = null;
            int    version = 0;
            bool   deleted = false;
            ImmutableList <string>      included = null, excluded = null;
            ImmutableList <SegmentRule> rules = null;
            string salt       = null;
            bool   unbounded  = false;
            int?   generation = null;

            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 == "included":
                    included = SerializationHelpers.ReadStrings(ref reader);
                    break;

                case var n when n == "excluded":
                    excluded = SerializationHelpers.ReadStrings(ref reader);
                    break;

                case var n when n == "rules":
                    var rulesBuilder = ImmutableList.CreateBuilder <SegmentRule>();
                    for (var rulesArr = reader.Array(); rulesArr.Next(ref reader);)
                    {
                        rulesBuilder.Add(ReadSegmentRule(ref reader));
                    }
                    rules = rulesBuilder.ToImmutable();
                    break;

                case var n when n == "salt":
                    salt = reader.StringOrNull();
                    break;

                case var n when n == "unbounded":
                    unbounded = reader.Bool();
                    break;

                case var n when n == "generation":
                    generation = reader.IntOrNull();
                    break;
                }
            }
            if (key is null && !deleted)
            {
                throw new RequiredPropertyException("key", 0);
            }
            return(new Segment(key, version, deleted, included, excluded, rules, salt, unbounded, generation));
        }
Esempio n. 3
0
        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));
        }