Ejemplo n.º 1
0
        static void Set_LookUp(IDictionary <string, NJsonSchema.JsonSchema> p_parent, mmria.common.metadata.node p_node)
        {
//https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Schema_JsonSchema.htm

            /*
             * var schema = new NJsonSchema.JsonSchema();
             *
             * schema.Type =  NJsonSchema.JsonObjectType.Object;
             * schema.Title = "mmria_case";
             * schema.Description = "Here is a case for your ...!";
             *
             * schema.SchemaVersion = "http://json-schema.org/draft-06/schema#";
             */


            NJsonSchema.JsonSchemaProperty property = null;
            //schema.Properties.Add("name", new NJsonSchema.JsonSchemaProperty(){ Type = NJsonSchema.JsonObjectType.String});
            //schema.Properties.Add("prompt", new NJsonSchema.JsonSchemaProperty(){ Type = NJsonSchema.JsonObjectType.String});

            try
            {
                switch (p_node.type.ToLower())
                {
                case "list":
                    if (p_node.is_multiselect.HasValue && p_node.is_multiselect.Value == true)
                    {
                        property = new NJsonSchema.JsonSchemaProperty()
                        {
                            Type = NJsonSchema.JsonObjectType.Array
                        };
                        foreach (var value in p_node.values)
                        {
                            property.EnumerationNames.Add(value.value);
                        }
                    }
                    else
                    {
                        property = new NJsonSchema.JsonSchemaProperty()
                        {
                            Type = NJsonSchema.JsonObjectType.String
                        };
                        p_parent.Add(p_node.name, property);

                        foreach (var value in p_node.values)
                        {
                            property.EnumerationNames.Add(value.value);
                        }
                    }


                    break;
                }
            }
            catch (Exception ex)
            {
                System.Console.Write($"GetSchemaGetSchema(p_parent, p_node) Exception: {ex}");
            }
            //return p_parent;
        }
Ejemplo n.º 2
0
        static async Task <NJsonSchema.JsonSchema> GetSchema(IDictionary <string, NJsonSchema.JsonSchema> p_lookup, NJsonSchema.JsonSchema p_parent, mmria.common.metadata.node p_node)
        {
            //https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Schema_JsonSchema.htm

            /*
             * var schema = new NJsonSchema.JsonSchema();
             *
             * schema.Type =  NJsonSchema.JsonObjectType.Object;
             * schema.Title = "mmria_case";
             * schema.Description = "Here is a case for your ...!";
             *
             * schema.SchemaVersion = "http://json-schema.org/draft-06/schema#";
             */


            NJsonSchema.JsonSchemaProperty property      = null;
            NJsonSchema.JsonSchemaProperty property_list = null;
            //schema.Properties.Add("name", new NJsonSchema.JsonSchemaProperty(){ Type = NJsonSchema.JsonObjectType.String});
            //schema.Properties.Add("prompt", new NJsonSchema.JsonSchemaProperty(){ Type = NJsonSchema.JsonObjectType.String});

            try
            {
                switch (p_node.type.ToLower())
                {
                case "form":
                    if (p_node.type.ToLower() == "form" && p_node.cardinality == "*")
                    {
                        property = new NJsonSchema.JsonSchemaProperty()
                        {
                            Type = NJsonSchema.JsonObjectType.Object
                        };
                        foreach (var child in p_node.children)
                        {
                            await GetSchema(p_lookup, property, child);
                        }
                        p_lookup.Add(p_node.name + "_type", property);
                        property_list = new NJsonSchema.JsonSchemaProperty()
                        {
                            Type = NJsonSchema.JsonObjectType.Array, Item = p_lookup[p_node.name + "_type"]
                        };
                        //property_list.Properties..Items.Allof(property);
                        p_parent.Properties.Add(p_node.name + "_form", property_list);
                    }
                    else
                    {
                        property = new NJsonSchema.JsonSchemaProperty()
                        {
                            Type = NJsonSchema.JsonObjectType.Object
                        };
                        foreach (var child in p_node.children)
                        {
                            await GetSchema(p_lookup, property, child);
                        }
                        p_parent.Properties.Add(p_node.name, property);
                    }
                    break;

                case "grid":
                    property = new NJsonSchema.JsonSchemaProperty()
                    {
                        Type = NJsonSchema.JsonObjectType.Object
                    };
                    foreach (var child in p_node.children)
                    {
                        await GetSchema(p_lookup, property, child);
                    }
                    var number = -1;
                    var key    = p_node.name + "_row";
                    var suffix = "";
                    if (p_lookup.ContainsKey(key))
                    {
                        number = p_lookup.Count;
                        suffix = number.ToString();
                    }

                    var property_name = key + suffix;
                    p_lookup.Add(property_name, property);

                    property_list = new NJsonSchema.JsonSchemaProperty()
                    {
                        Type = NJsonSchema.JsonObjectType.Array, Item = p_lookup[property_name]
                    };

                    var grid_name = p_node.name + "_grid" + suffix;

                    p_parent.Properties.Add(grid_name, property_list);

                    break;

                case "app":
                case "group":

                    property = new NJsonSchema.JsonSchemaProperty()
                    {
                        Type = NJsonSchema.JsonObjectType.Object
                    };

                    p_parent.Properties.Add(p_node.name, property);

                    foreach (var child in p_node.children)
                    {
                        await GetSchema(p_lookup, property, child);
                    }
                    break;

                case "textarea":
                case "hidden":
                case "string":
                    var string_property = new NJsonSchema.JsonSchemaProperty()
                    {
                        Type = NJsonSchema.JsonObjectType.String
                    };

                    if (p_node.default_value != null)
                    {
                        string_property.Default = p_node.default_value;
                    }

                    if (p_node.is_required.HasValue && p_node.is_required.Value)
                    {
                        string_property.IsRequired = true;
                    }

                    p_parent.Properties.Add(p_node.name, string_property);
                    break;

                case "datetime":
                case "date":
                case "time":
                    var date_property = new NJsonSchema.JsonSchemaProperty()
                    {
                        Type = NJsonSchema.JsonObjectType.String, Format = "date-time"
                    };
                    if (p_node.is_required.HasValue && p_node.is_required.Value)
                    {
                        date_property.IsRequired = true;
                    }
                    p_parent.Properties.Add(p_node.name, date_property);
                    break;

                case "number":
                    var number_property = new NJsonSchema.JsonSchemaProperty()
                    {
                        Type = NJsonSchema.JsonObjectType.Number
                    };
                    if (p_node.default_value != null)
                    {
                        decimal decimal_value;
                        if (decimal.TryParse(p_node.default_value, out decimal_value))
                        {
                            number_property.Default = decimal_value;
                        }
                    }

                    if (p_node.is_required.HasValue && p_node.is_required.Value)
                    {
                        number_property.IsRequired = true;
                    }

                    if (p_node.min_value != null)
                    {
                        decimal number_value;
                        if (decimal.TryParse(p_node.min_value, out number_value))
                        {
                            number_property.Minimum = number_value;
                        }
                    }

                    if (p_node.max_value != null)
                    {
                        decimal number_value;
                        if (decimal.TryParse(p_node.max_value, out number_value))
                        {
                            number_property.Maximum = number_value;
                        }
                    }

                    p_parent.Properties.Add(p_node.name, number_property);
                    break;

                case "boolean":
                    var boolean_property = new NJsonSchema.JsonSchemaProperty()
                    {
                        Type = NJsonSchema.JsonObjectType.Boolean
                    };
                    if (p_node.is_required.HasValue && p_node.is_required.Value)
                    {
                        boolean_property.IsRequired = true;
                    }

                    if (p_node.default_value != null)
                    {
                        bool bool_value;

                        if (bool.TryParse(p_node.default_value, out bool_value))
                        {
                            boolean_property.Default = bool_value;
                        }
                    }
                    p_parent.Properties.Add(p_node.name, boolean_property);
                    break;

                case "list":
                    if (p_node.is_multiselect.HasValue && p_node.is_multiselect.Value == true)
                    {
                        property = new NJsonSchema.JsonSchemaProperty()
                        {
                            Type = NJsonSchema.JsonObjectType.Array
                        };
                        p_parent.Properties.Add(p_node.name, property);

                        if (!string.IsNullOrWhiteSpace(p_node.path_reference))
                        {
                            property.Reference = p_lookup[p_node.path_reference.Replace("lookup/", "")];
                            //p_parent.Properties.Add(p_node.name, property);
                        }
                        else
                        {
                            foreach (var value in p_node.values)
                            {
                                property.EnumerationNames.Add(value.value);
                            }
                        }
                    }
                    else
                    {
                        property = new NJsonSchema.JsonSchemaProperty()
                        {
                            Type = NJsonSchema.JsonObjectType.String
                        };
                        p_parent.Properties.Add(p_node.name, property);

                        if (!string.IsNullOrWhiteSpace(p_node.path_reference))
                        {
                            property.Reference = p_lookup[p_node.path_reference.Replace("lookup/", "")];
                            //p_parent.Properties.Add(p_node.name, property);
                        }
                        else
                        {
                            foreach (var value in p_node.values)
                            {
                                property.EnumerationNames.Add(value.value);
                            }
                        }
                    }


                    break;

                case "button":
                case "chart":
                case "label":
                    break;

                default:
                    System.Console.Write($"Convert.cs.GetSchema.switch.Missing: {p_node.type}");
                    break;
                }
            }
            catch (Exception ex)
            {
                System.Console.Write($"GetSchemaGetSchema(p_parent, p_node) Exception: {ex}");
            }
            return(p_parent);
        }