Beispiel #1
0
        /// <summary>
        /// Renames a schema item with the provided fromKey to the provided toKey on the
        /// </summary>
        /// <param name="fromKey"></param>
        /// <param name="toKey"></param>
        /// <param name="schema"></param>
        internal void MoveSchemaObject(string fromKey, string toKey, MapSchemaItem schema)
        {
            if (_schema == null)
            {
                throw new Exception("No parent schema found.");
            }

            MapSchemaItem current = _schema;

            foreach (var part in Path.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries))
            {
                try {
                    if (current.Type == "Array" || current.Container == true)
                    {
                        var contained = current.Contains;
                        if (contained == null)
                        {
                            current.Contains = contained = new MapSchemaItem();
                        }

                        current = contained;
                    }
                    current = current.Fields[part];
                } catch {
                    throw new Exception($"Failed to find schema at path {Path}. Path not found {part}");
                }
            }

            if (current.Container != true)
            {
                current.Fields[toKey] = schema;
                current.Fields.Remove(fromKey);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Returns the MapSchemaItem in the base schema which is associated to the provided key on this map.
        /// </summary>
        /// <param name="keyIn">The key to find the schema for</param>
        /// <returns></returns>
        public MapSchemaItem Schema(string keyIn = null)
        {
            if (_schema == null)
            {
                return(null);
            }

            MapSchemaItem current = _schema;

            if (Path != String.Empty)
            {
                foreach (var part in Path.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    try {
                        if (current.Type == "Array" || current.Container == true)
                        {
                            var contained = current.Contains;
                            if (contained == null)
                            {
                                current.Contains = contained = new MapSchemaItem();
                            }

                            current = contained;
                        }

                        current = current.GetField(part);
                        if (keyIn == part)
                        {
                            return(current);
                        }
                    }
                    catch {
                        throw new Exception($"Failed to find schema at path {Path}. Path not found {part}");
                    }
                }
            }

            if (current == null)
            {
                throw new Exception($"No schema found for {Path}. Key requested: {keyIn}");
            }

            if (keyIn == null)
            {
                return(current);
            }

            MapSchemaItem schemaOut   = null;
            var           isContainer = current.Container ?? false;

            if (current.Type == "Array" || isContainer == true)
            {
                var contained = current.Contains;
                if (contained != null && contained.Type == "Map" && !contained.Fields.ContainsKey(keyIn))
                {
                    contained.Fields.Add(keyIn, new MapSchemaItem());
                }

                if (contained == null)
                {
                    current.Contains = contained = new MapSchemaItem();
                    contained.Type   = "Map";
                    contained.Fields.Add(keyIn, new MapSchemaItem());
                }

                current = contained;

                if (isContainer || !current.Fields.ContainsKey(keyIn))
                {
                    schemaOut = current;
                }
                else
                {
                    if (current.Type == null)
                    {
                        current.Type = "Map";
                    }

                    schemaOut = current.GetField(keyIn);
                }
            }
            else
            {
                schemaOut = current.GetField(keyIn);
            }

            return(schemaOut);
        }