Пример #1
0
        public static object GenerateItem(MapSchemaItem localSchema, String path, MapSchema baseSchema)
        {
            var    type         = localSchema.Type;
            var    isContainer  = localSchema.Container ?? false;
            object defaultValue = localSchema.DefaultValue ?? null;

            switch (type)
            {
            case "Map":
                var item = new Map(baseSchema);
                item.Path = path;
                if (!isContainer)
                {
                    GenerateProperties(item, localSchema, path, baseSchema);
                }

                return(item);

            case "Array":
                return(new LucidJson.Array());

            case "String":
                return(defaultValue ?? String.Empty);

            case "Boolean":
                return(defaultValue ?? false);

            default:
                return(null);
            }
        }
Пример #2
0
 private static void GenerateProperties(Map map, MapSchemaItem localSchema, String path, MapSchema baseSchema)
 {
     foreach (var prop in localSchema.Fields.Where(f => f.Value.Deprecated != true))
     {
         map[prop.Key] = GenerateItem(prop.Value, $"{path}.{prop.Key}".Trim('.'), baseSchema);
     }
 }
Пример #3
0
        public MapSchemaItem GetField(string name)
        {
            if (!Fields.TryGetValue(name, out MapSchemaItem fieldOut))
            {
                fieldOut     = new MapSchemaItem();
                Fields[name] = fieldOut;
            }

            return(fieldOut);
        }
Пример #4
0
        public static MapSchemaItem FindSchemaItem(string path, string keyIn, MapSchema baseSchema)
        {
            if (baseSchema == null)
            {
                return(null);
            }

            MapSchemaItem current = baseSchema;

            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);
        }