Beispiel #1
0
        private void ParseObject(HoconValue owner, bool root, string currentPath)
        {
            if (!owner.IsObject())
            {
                owner.NewValue(new HoconObject());
            }

            HoconObject currentObject = owner.GetObject();

            while (!_reader.EoF)
            {
                Token t = _reader.PullNext();
                switch (t.Kind)
                {
                case TokenKind.Include:
                    if (_includeCallback == null)
                    {
                        throw new InvalidOperationException("include callback is null");
                    }

                    var included      = _includeCallback(t.Value);
                    var substitutions = included.Substitutions;
                    foreach (var substitution in substitutions)
                    {
                        substitution.Path = currentPath + "." + substitution.Path;
                    }
                    this.substitutions.AddRange(substitutions);
                    var otherObj = included.Value.GetObject();
                    owner.GetObject().Merge(otherObj);

                    break;

                case TokenKind.EoF:
                    break;

                case TokenKind.Key:
                    HoconValue value    = currentObject.GetOrCreateKey(t.Value);
                    var        nextPath = currentPath == "" ? t.Value : currentPath + "." + t.Value;
                    ParseKeyContent(value, nextPath);
                    if (!root)
                    {
                        return;
                    }
                    break;

                case TokenKind.ObjectEnd:
                    return;
                }
            }
        }
Beispiel #2
0
        public static HoconImmutableElement ToHoconImmutable(this HoconValue value)
        {
            switch (value.Type)
            {
            case HoconType.Object:
                return(new HoconImmutableObjectBuilder()
                       .Merge(value.GetObject())
                       .Build());

            case HoconType.Array:
                return(new HoconImmutableArrayBuilder()
                       .AddRange(value)
                       .Build());

            case HoconType.Literal:
                return(new HoconImmutableLiteralBuilder()
                       .Append(value)
                       .Build());

            case HoconType.Empty:
                return(HoconImmutableLiteral.Null);

            default:
                // Should never reach this line.
                throw new HoconException($"Unknown Hocon field type:{value.Type}");
            }
        }
        private static void ParseObject(
            HoconValue owner,
            string currentPath,
            IConfiguration conf)
        {
            if (!owner.IsObject())
            {
                owner.NewValue(new HoconObject());
            }

            HoconObject hoconObject = owner.GetObject();

            foreach (var section in conf.GetChildren())
            {
                if (int.TryParse(section.Key, out _))
                {
                    if (!owner.Values[0].IsArray())
                    {
                        owner.Clear();
                        owner.NewValue(new HoconArray());
                    }

                    var array = (HoconArray)owner.Values[0];
                    var value = new HoconValue();
                    ParseSection(currentPath, section, value);
                    array.Add(value);
                }
                else
                {
                    ParseSection(currentPath, section, hoconObject.GetOrCreateKey(section.Key));
                }
            }
        }
        public static JToken?ToJToken(this HoconValue hoconValue, Func <JValue, JValue>?jValueHandler = null)
        {
            if (hoconValue == null)
            {
                throw new ArgumentNullException(nameof(hoconValue));
            }

            switch (hoconValue.Type)
            {
            case HoconType.Object:
                return(hoconValue.GetObject().ToJObject(jValueHandler));

            case HoconType.Array:
                return(hoconValue.GetArray().ToJArray(jValueHandler));

            case HoconType.String:
            case HoconType.Boolean:
            case HoconType.Number:
                if (hoconValue.Count == 1)
                {
                    var hoconElement = hoconValue[0];
                    if (hoconElement is HoconSubstitution hoconSubstitution)
                    {
                        return(hoconSubstitution.ResolvedValue.ToJToken(jValueHandler));
                    }

                    if (hoconElement is HoconLiteral hoconLiteral)
                    {
                        return(hoconLiteral.ToJValue(jValueHandler));
                    }

                    throw new InvalidOperationException($"Invalid Hocon element type when hocon type is Literal and has only one element: {hoconElement.GetType().FullName}");
                }
                else
                {
                    var jValue = JValue.CreateString(hoconValue.GetString());
                    if (jValueHandler != null)
                    {
                        return(jValueHandler(jValue));
                    }
                    else
                    {
                        return(jValue);
                    }
                }

            case HoconType.Empty:
#pragma warning disable S1168 // Empty arrays and collections should be returned instead of null
                return(null);

#pragma warning restore S1168 // Empty arrays and collections should be returned instead of null
            default:
                throw new InvalidOperationException($"Unknown Type: {hoconValue.Type}");
            }
        }
Beispiel #5
0
        private void VisitObject(HoconValue value)
        {
            switch (value.Type)
            {
            case HoconType.Object:
                VisitHoconObject(value.GetObject());
                break;

            case HoconType.Array:
                VisitArray(value.GetArray());
                break;

            case HoconType.Literal:
                VisitPrimitive(value);
                break;
            }
        }
        private void VisitObject(HoconValue value)
        {
            switch (value.Type)
            {
            case HoconType.Object:
                VisitHoconObject(value.GetObject());
                break;

            case HoconType.Array:
                VisitArray(value.GetArray());
                break;

            case HoconType.Boolean:
            case HoconType.Number:
            case HoconType.String:
                VisitPrimitive(value);
                break;
            }
        }
Beispiel #7
0
        private void VisitHoconValue(string path, HoconValue value)
        {
            if (value.IsEmpty)
            {
                return;
            }

            if (value.IsString())
            {
                Data.Add(path, value.GetString());
            }
            else if (value.IsObject())
            {
                VisitHoconObject(path, value.GetObject());
            }
            else if (value.IsArray())
            {
                VisitHoconArray(path, value.GetArray());
            }
        }
Beispiel #8
0
        private void VisitHoconValue(string path, HoconValue value)
        {
            if (value.IsEmpty)
            {
                return;
            }

            if (value.IsObject())
            {
                VisitHoconObject(path, value.GetObject());
            }
            else if (value.IsArray())
            {
                VisitHoconArray(path, value.GetArray());
            }
            else if (value.IsString())
            {
                Output.WriteLine($"{path} = {value.GetString()}");
            }
        }