예제 #1
0
        public JSONProperty GetStructure(Options options)
        {
            this.reference = options.ReferenceTable;
            pluralization  = PluralizationService.CreateService(CultureInfo.GetCultureInfo("en"));
            Table table = null;

            foreach (Table t in tables)
            {
                if (t.Name == reference)
                {
                    table = t;
                    break;
                }
            }

            JSONProperty property = ProcessTables(table, new JSONProperty()
            {
                type = "object", title = reference, schema = "http://json-schema.org/draft-07/schema#"
            }, new HashSet <string>(), options, options.Verbose == 1);

            if (options.Multiple == 1)
            {
                JSONProperty array = new JSONProperty();
                array.type      = "array";
                array.title     = property.title;
                array.schema    = property.schema;
                property.title  = null;
                property.schema = null;
                array.items     = property;
                return(array);
            }
            return(property);
        }
예제 #2
0
 private void AddProperty(JSONProperty props, string name, JSONProperty prop)
 {
     if (props.properties == null)
     {
         props.properties = new Dictionary <string, JSONProperty>();
     }
     props.properties.Add(name, prop);
 }
예제 #3
0
        public void Generate(Options options)
        {
            JSONSchema   schema   = new JSONSchema(tables);
            JSONProperty property = schema.GetStructure(options);

            Parse(property, string.Empty, new List <string>(), new List <string>(), false, true);
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("SchemaIdentifier,SchemaMajorVersion,SchemaMajorVersionDate,SchemaLabel,SchemaDescription,SchemaTemplateId,SchemaMinorVersions,SchemaMinorVersionDates,SchemaMinorVersionDescriptions,SchemaRequirementIdentifier,SchemaRequirementOrder,SchemaRequirementLabel,SchemaRequirementMandatory,SchemaRequirementDescription,SchemaRequirementMinOccurances,SchemaRequirementMaxOccurances,SchemaRequirementNote,SchemaRequirementFieldIdentifier,SchemaRequirementFieldOrder,SchemaRequirementFieldDescription,SchemaRequirementFieldMandatory,SchemaRequirementFieldMinOccurances,SchemaRequirementFieldMaxOccurances,SchemaRequirementFieldHelp,FieldIdentifier,FieldLabel,FieldType,FieldDescription,FieldPattern,FieldURIStubs");
            foreach (CSVSchemaRow row in rows)
            {
                sb.AppendLine(row.ToString());
            }

            File.WriteAllText(FileUtility.CreatePath(options, "schema.csv", ".csv"), sb.ToString());
        }
예제 #4
0
        private JSONProperty ProcessTables(Table table, JSONProperty props, HashSet <string> previousTables, Options options, bool followExternalReferences = true)
        {
            List <string> requires = new List <string>();

            foreach (Column field in table.Columns)
            {
                Table refTable = null;
                foreach (ForeignKeyReference foreignKey in table.ForeignKeys)
                {
                    if (field.Name == foreignKey.TableField)
                    {
                        foreach (Table t in tables)
                        {
                            if (t.Name == foreignKey.ReferenceTableName)
                            {
                                refTable = t;
                                break;
                            }
                        }
                    }
                }

                if (refTable != null && previousTables.Contains(refTable.Name))
                {
                    //do not include fields that are in fact thier parents
                    continue;
                }

                if (refTable == null && field.Required)
                {
                    requires.Add(field.Name);
                }

                if (refTable != null)
                {
                    JSONProperty prop = new JSONProperty()
                    {
                        properties = new Dictionary <string, JSONProperty>()
                    };
                    prop.SetType("object");
                    prop = ProcessTables(refTable, prop, previousTables, options, followExternalReferences);
                    AddProperty(props, refTable.Name, prop);
                    if (field.Required)
                    {
                        requires.Add(refTable.Name);
                    }
                }
                else if (!field.IsHidden && (!field.IsDeprecated || options.IncludeDeprecated == 1))
                {
                    JSONProperty prop = new JSONProperty();
                    prop.SetType(field.Type);
                    prop.SetFormat(field.Format);
                    if (field.Enum != null)
                    {
                        prop.enumValues = new List <string>(field.Enum);
                    }
                    if (!string.IsNullOrEmpty(field.Description))
                    {
                        prop.description = field.Description;
                    }
                    if (field.Schemas != null && field.Schemas.Length > 0)
                    {
                        foreach (SchemaURI schema in field.Schemas)
                        {
                            if (!string.IsNullOrEmpty(prop.description))
                            {
                                prop.description += "\r\n";
                            }
                            prop.description += " - ";
                            prop.description += schema.ToString();
                        }
                    }
                    AddProperty(props, field.Name, prop);
                }
            }

            if (requires.Count > 0)
            {
                props.required = requires;
            }

            if (followExternalReferences && (string.IsNullOrEmpty(reference) || table.Name == reference))
            {
                previousTables.Add(table.Name);

                foreach (Table t in tables)
                {
                    if (t.Name == table.Name)
                    {
                        continue;
                    }

                    foreach (ForeignKeyReference foreignKey in t.ForeignKeys)
                    {
                        if (foreignKey.ReferenceTableName == table.Name)
                        {
                            JSONProperty prop = new JSONProperty()
                            {
                                items = new JSONProperty()
                                {
                                    properties = new Dictionary <string, JSONProperty>()
                                }
                            };
                            prop.SetType("array");
                            prop.items.SetType("object");
                            prop.items = ProcessTables(t, prop.items, previousTables, options, false);
                            AddProperty(props, Pluralize(t.Name), prop);
                        }
                    }
                }
            }

            return(props);
        }
예제 #5
0
        private void Parse(JSONProperty property, string parentPropertyName, List <string> tableNames, List <string> required, bool multiple, bool tableMandatory)
        {
            if (property == null)
            {
                return;
            }

            if (!string.IsNullOrEmpty(property.title))
            {
                parentPropertyName += property.title;
                tableNames.Add(property.title);
            }

            foreach (KeyValuePair <string, JSONProperty> kvp in property.properties)
            {
                if (kvp.Value.type == "object")
                {
                    List <string> tNames = new List <string>(tableNames);
                    tNames.Add(kvp.Key);
                    Parse(kvp.Value, parentPropertyName + "_" + kvp.Key, tNames, kvp.Value.required, multiple, tableMandatory);
                }
                else if (kvp.Value.type == "array")
                {
                    List <string> tNames = new List <string>(tableNames);
                    tNames.Add(kvp.Key);
                    Parse(kvp.Value.items, parentPropertyName + "_" + kvp.Key, tNames, kvp.Value.items.required, true, false);
                }
                else
                {
                    bool   mandatory     = false;
                    int    minOccurances = 0;
                    int    maxOccurances = 1;
                    string fieldType     = kvp.Value.type;
                    string fieldPattern  = kvp.Value.pattern;

                    if (required.Contains(kvp.Key))
                    {
                        mandatory     = true;
                        minOccurances = 1;
                    }

                    if (multiple)
                    {
                        maxOccurances = 99;
                    }

                    if (kvp.Value.enumValues != null && kvp.Value.enumValues.Count > 0)
                    {
                        fieldType    = "EncodedList";
                        fieldPattern = string.Format("({0})", string.Join("|", kvp.Value.enumValues));
                    }

                    if (kvp.Value.format == "uri")
                    {
                        fieldType = "URI";
                    }
                    else if (kvp.Value.format == "date")
                    {
                        fieldType = "Date";
                    }

                    int sort = 0;
                    if (!tableCount.ContainsKey(parentPropertyName))
                    {
                        sort = (tableCount.Count + 1) * 1000;
                        tableCount.Add(parentPropertyName, tableCount.Count + 1);
                    }
                    else
                    {
                        sort = tableCount[parentPropertyName] * 1000;
                    }

                    if (!tableColumnCount.ContainsKey(parentPropertyName))
                    {
                        tableColumnCount.Add(parentPropertyName, 1);
                        sort += 1;
                    }
                    else
                    {
                        sort += tableColumnCount[parentPropertyName] = tableColumnCount[parentPropertyName] + 1;
                    }

                    fieldType = FirstLetterToUpper(fieldType);

                    rows.Add(new CSVSchemaRow()
                    {
                        SchemaRequirementIdentifier = GetRequirementIdentifier(tableNames),
                        SchemaRequirementLabel      = GetRequirementLabel(GetRequirementIdentifier(tableNames)),
                        SchemaRequirementOrder      = tableCount[parentPropertyName].ToString(),
                        SchemaRequirementMandatory  = tableMandatory.ToString(),
                        FieldIdentifier             = GetFieldName(tableNames, kvp.Key),
                        FieldLabel = kvp.Key,
                        SchemaRequirementFieldIdentifier = kvp.Key,
                        FieldType        = fieldType,
                        FieldDescription = kvp.Value.description,
                        FieldPattern     = fieldPattern,
                        SchemaRequirementFieldMandatory     = mandatory.ToString(),
                        SchemaRequirementFieldMinOccurances = minOccurances.ToString(),
                        SchemaRequirementFieldMaxOccurances = maxOccurances.ToString(),
                        SchemaRequirementFieldDescription   = kvp.Value.description,
                        SchemaRequirementFieldOrder         = tableColumnCount[parentPropertyName].ToString(),
                        SchemaRequirementMinOccurances      = minOccurances.ToString(),
                        SchemaRequirementMaxOccurances      = maxOccurances.ToString(),
                        Sort = sort
                    });

                    rows.Sort();
                }
            }
        }