示例#1
0
        /// <summary>
        /// Unfolds a TomlDocument, recursing as needed, and unboxes a value if it's available.
        /// </summary>
        private void ProcessSingleIndex(object index, out object result)
        {
            if (index is string)
            {
                string idx = index as string;
                List<TomlHash> vars = _variables.Where(v => v.MatchesName(idx)).ToList();
                if (vars.Count > 0)
                {
                    List<TomlHash> fin =
                        vars.Select(
                                    hash =>
                                    new TomlHash {Data = hash.Data, NameParts = new List<string>(hash.NameParts)})
                            .ToList();
                    foreach (var hash in fin)
                    {
                        hash.TrimName(idx);
                    }

                    var cleared = fin.Where(v => v.Name == "").ToList();
                    switch (cleared.Count)
                    {
                        case 0:
                            result = new TomlDocument(fin);
                            break;
                        case 1:
                            result = UnboxTomlObject(cleared[ 0 ].Data);
                            break;
                        default:
                            throw new KeyNotFoundException();
                    }
                    return;
                }
                throw new IndexOutOfRangeException();
            }

            if (index is int)
            {
                int idx = (int) index;
                if (idx < 0 || idx >= _arrays.Count)
                {
                    throw new IndexOutOfRangeException();
                }

                result = UnboxTomlObject(_arrays[ idx ]);
                return;
            }

            throw new ArgumentException("Invalid key type: Only strings or ints allowed.");
        }
示例#2
0
        /// <summary>
        /// The type of data you're trying to retrieve (either a keygroup or an actual key)
        /// will determine if you receive a "child" TomlDocument or an actual value.
        /// </summary>
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            List<TomlHash> lth = _variables.Where(hash => hash.NameParts.First() == binder.Name).ToList();
            switch (lth.Count)
            {
                case 0:
                    result = null;
                    return false;
                case 1:
                    if (lth[ 0 ].NameParts.Count == 1)
                    {
                        result = UnboxTomlObject(lth[ 0 ].Data);
                        return true;
                    }
                    TomlHash snip = new TomlHash
                    {
                        Data = lth[ 0 ].Data,
                        NameParts = lth[ 0 ].NameParts.Skip(1).ToList()
                    };
                    result = new TomlDocument(new List<TomlHash>{snip});
                    return true;
                default:
                    List<TomlHash> fin =
                        lth.Select(
                                   hash =>
                                   new TomlHash {Data = hash.Data, NameParts = new List<string>(hash.NameParts)})
                           .ToList();

                    foreach (var hash in fin)
                    {
                        hash.NameParts.RemoveAt(0);
                    }
                    result = new TomlDocument(fin);
                    return true;
            }
        }