private TestStruct ReadStruct(ref JReader r)
        {
            var obj = r.ObjectOrNull();

            if (!obj.IsDefined)
            {
                return(null);
            }
            var ret = new TestStruct();

            while (obj.Next(ref r))
            {
                switch (obj.Name)
                {
                case var n when n == "str":
                    ret.Str = r.String();
                    break;

                case var n when n == "nums":
                    ret.Nums = ReadInts(ref r);
                    break;

                case var n when n == "bool":
                    ret.Bool = r.Bool();
                    break;

                case var n when n == "nested":
                    ret.Nested = ReadStruct(ref r);
                    break;
                }
            }
            return(ret);
        }
        private static FullDataSet <ItemDescriptor> ParseJson(ref JReader r, int version)
        {
            var flagsBuilder    = ImmutableList.CreateBuilder <KeyValuePair <string, ItemDescriptor> >();
            var segmentsBuilder = ImmutableList.CreateBuilder <KeyValuePair <string, ItemDescriptor> >();

            for (var obj = r.Object(); obj.Next(ref r);)
            {
                switch (obj.Name.ToString())
                {
                case "flags":
                    for (var subObj = r.ObjectOrNull(); subObj.Next(ref r);)
                    {
                        var key  = subObj.Name.ToString();
                        var flag = FeatureFlagSerialization.Instance.ReadJson(ref r) as FeatureFlag;
                        flagsBuilder.Add(new KeyValuePair <string, ItemDescriptor>(key, new ItemDescriptor(version,
                                                                                                           FlagWithVersion(flag, version))));
                    }
                    break;

                case "flagValues":
                    for (var subObj = r.ObjectOrNull(); subObj.Next(ref r);)
                    {
                        var key   = subObj.Name.ToString();
                        var value = LdValueConverter.ReadJsonValue(ref r);
                        var flag  = FlagWithValue(key, value, version);
                        flagsBuilder.Add(new KeyValuePair <string, ItemDescriptor>(key, new ItemDescriptor(version, flag)));
                    }
                    break;

                case "segments":
                    for (var subObj = r.ObjectOrNull(); subObj.Next(ref r);)
                    {
                        var key     = subObj.Name.ToString();
                        var segment = SegmentSerialization.Instance.ReadJson(ref r) as Segment;
                        segmentsBuilder.Add(new KeyValuePair <string, ItemDescriptor>(key, new ItemDescriptor(version,
                                                                                                              SegmentWithVersion(segment, version))));
                    }
                    break;
                }
            }
            return(new FullDataSet <ItemDescriptor>(ImmutableList.Create <KeyValuePair <DataKind, KeyedItems <ItemDescriptor> > >(
                                                        new KeyValuePair <DataKind, KeyedItems <ItemDescriptor> >(DataModel.Features,
                                                                                                                  new KeyedItems <ItemDescriptor>(flagsBuilder.ToImmutable())),
                                                        new KeyValuePair <DataKind, KeyedItems <ItemDescriptor> >(DataModel.Segments,
                                                                                                                  new KeyedItems <ItemDescriptor>(segmentsBuilder.ToImmutable()))
                                                        )));
        }
        private static FlagFileData ParseJson(ref JReader r)
        {
            var ret = new FlagFileData
            {
                Flags      = new Dictionary <string, FeatureFlag>(),
                FlagValues = new Dictionary <string, LdValue>(),
                Segments   = new Dictionary <string, Segment>()
            };

            for (var obj = r.Object(); obj.Next(ref r);)
            {
                switch (obj.Name.ToString())
                {
                case "flags":
                    for (var subObj = r.ObjectOrNull(); subObj.Next(ref r);)
                    {
                        ret.Flags[subObj.Name.ToString()] = FeatureFlagSerialization.Instance.ReadJson(ref r) as FeatureFlag;
                    }
                    break;

                case "flagValues":
                    for (var subObj = r.ObjectOrNull(); subObj.Next(ref r);)
                    {
                        ret.FlagValues[subObj.Name.ToString()] = LdValueConverter.ReadJsonValue(ref r);
                    }
                    break;

                case "segments":
                    for (var subObj = r.ObjectOrNull(); subObj.Next(ref r);)
                    {
                        ret.Segments[subObj.Name.ToString()] = SegmentSerialization.Instance.ReadJson(ref r) as Segment;
                    }
                    break;
                }
            }
            return(ret);
        }
        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));
        }
예제 #5
0
        internal static VariationOrRollout ReadVariationOrRollout(ref JReader r)
        {
            int?    variation = null;
            Rollout?rollout   = null;

            for (var obj = r.ObjectOrNull(); obj.Next(ref r);)
            {
                switch (obj.Name)
                {
                case var n when n == "variation":
                    variation = r.IntOrNull();
                    break;

                case var n when n == "rollout":
                    rollout = ReadRollout(ref r);
                    break;
                }
            }
            return(new VariationOrRollout(variation, rollout));
        }
예제 #6
0
        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));
        }
예제 #7
0
            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());
            }
예제 #8
0
            private static EvaluationReason?ReadJsonInternal(ref JReader reader, bool nullable)
            {
                var obj = (nullable ? reader.ObjectOrNull() : reader.Object())
                          .WithRequiredProperties(_requiredProperties);

                if (!obj.IsDefined)
                {
                    return(null);
                }
                try
                {
                    EvaluationReasonKind kind           = EvaluationReasonKind.Error;
                    int?   ruleIndex                    = null;
                    string ruleId                       = null;
                    string prerequisiteKey              = null;
                    EvaluationErrorKind?errorKind       = null;
                    bool inExperiment                   = false;
                    BigSegmentsStatus?bigSegmentsStatus = null;

                    while (obj.Next(ref reader))
                    {
                        var name = obj.Name;
                        if (name == "kind")
                        {
                            try
                            {
                                kind = EvaluationReasonKindConverter.FromIdentifier(reader.String());
                            }
                            catch (ArgumentException)
                            {
                                throw new SyntaxException("unsupported value for \"kind\"", 0);
                            }
                        }
                        else if (name == "ruleIndex")
                        {
                            ruleIndex = reader.Int();
                        }
                        else if (name == "ruleId")
                        {
                            ruleId = reader.String();
                        }
                        else if (name == "prerequisiteKey")
                        {
                            prerequisiteKey = reader.String();
                        }
                        else if (name == "errorKind")
                        {
                            try
                            {
                                errorKind = EvaluationErrorKindConverter.FromIdentifier(reader.String());
                            }
                            catch (ArgumentException)
                            {
                                throw new SyntaxException("unsupported value for \"errorKind\"", 0);
                            }
                        }
                        else if (name == "inExperiment")
                        {
                            inExperiment = reader.Bool();
                        }
                        else if (name == "bigSegmentsStatus")
                        {
                            try
                            {
                                bigSegmentsStatus = BigSegmentsStatusConverter.FromIdentifier(reader.String());
                            }
                            catch (ArgumentException)
                            {
                                throw new SyntaxException("unsupported value for \"bigSegmentsStatus\"", 0);
                            }
                        }
                    }

                    EvaluationReason reason;
                    switch (kind) // it's guaranteed to have a value, otherwise there'd be a required property error above
                    {
                    case EvaluationReasonKind.Off:
                        reason = EvaluationReason.OffReason;
                        break;

                    case EvaluationReasonKind.Fallthrough:
                        reason = EvaluationReason.FallthroughReason;
                        break;

                    case EvaluationReasonKind.TargetMatch:
                        reason = EvaluationReason.TargetMatchReason;
                        break;

                    case EvaluationReasonKind.RuleMatch:
                        reason = EvaluationReason.RuleMatchReason(ruleIndex ?? 0, ruleId);
                        break;

                    case EvaluationReasonKind.PrerequisiteFailed:
                        reason = EvaluationReason.PrerequisiteFailedReason(prerequisiteKey);
                        break;

                    case EvaluationReasonKind.Error:
                        reason = EvaluationReason.ErrorReason(errorKind ?? EvaluationErrorKind.Exception);
                        break;

                    default:
                        return(null);
                    }
                    if (inExperiment)
                    {
                        reason = reason.WithInExperiment(true);
                    }
                    if (bigSegmentsStatus.HasValue)
                    {
                        reason = reason.WithBigSegmentsStatus(bigSegmentsStatus);
                    }
                    return(reason);
                }
                catch (Exception e)
                {
                    throw reader.TranslateException(e);
                }
            }