private static async Task <Dictionary <String, JsonProperty> > WriteManySide(JsonSchema4 schema, JsonSchema4 other, RelationshipSettings relationship)
        {
            var name  = other.GetKeyName() + "s"; //Should be xxId so adding s should be fine
            var props = new Dictionary <String, JsonProperty>();

            if (!schema.Properties.ContainsKey(name)) //Don't write if schema defined property.
            {
                props.Add(name, new JsonProperty()
                {
                    Type          = JsonObjectType.Array,
                    Item          = await TypeToSchemaGenerator.CreateSchema(other.GetKeyType()),
                    Parent        = schema,
                    ExtensionData = relationship.CopyExtensions(),
                    Title         = relationship.OriginalPropertyDefinition?.Title
                });
            }

            return(props);
        }
        private static async Task <Dictionary <String, JsonProperty> > WriteOneSide(JsonSchema4 schema, JsonSchema4 other, RelationshipSettings relationship)
        {
            var name  = other.GetKeyName();
            var props = new Dictionary <String, JsonProperty>();

            if (!schema.Properties.ContainsKey(name)) //Don't write if schema defined property.
            {
                var propSchema = await TypeToSchemaGenerator.CreateSchema(other.GetKeyType());

                props.Add(name, new JsonProperty()
                {
                    Type          = propSchema.Type,
                    Format        = propSchema.Format,
                    Parent        = schema,
                    ExtensionData = relationship.CopyExtensions(),
                    Title         = relationship.OriginalPropertyDefinition?.Title
                });
            }

            return(props);
        }
Exemplo n.º 3
0
        public async Task Configure()
        {
            if (Path.GetExtension(Source) != ".json")
            {
                if (!Directory.Exists(AppOutDir))
                {
                    throw new DirectoryNotFoundException($"Cannot find app out directory {AppOutDir}");
                }

                String ns;
                try
                {
                    var assembly = ProjectAssemblyLoader.LoadProjectAssembly(AppOutDir, out ns);
                    AppNamespace = ns;
                    var type = assembly.GetType(Source);

                    if (type == null)
                    {
                        throw new InvalidOperationException($"Cannot find type {Source} in assembly {assembly.FullName}.");
                    }

                    Schema = await TypeToSchemaGenerator.CreateSchema(type);

                    foreach (var relationship in Schema.GetRelationshipSettings())
                    {
                        if (relationship.Kind != RelationKind.None)
                        {
                            var otherClrName = relationship.OtherModelClrName;
                            var otherType    = assembly.GetType(otherClrName);

                            if (otherType == null)
                            {
                                throw new InvalidOperationException($"Cannot find related type {otherClrName}");
                            }

                            var otherSchema = await TypeToSchemaGenerator.CreateSchema(otherType);

                            OtherSchemas[otherSchema.Title] = otherSchema;
                        }
                    }
                }
                catch (FileLoadException ex)
                {
                    throw new RunOnFullFrameworkException(ex);
                }
            }
            else
            {
                if (!File.Exists(Source))
                {
                    throw new MessageException($"Cannot find schema file {Source}.");
                }

                Schema = await JsonSchema4.FromFileAsync(Source);
            }

            if (Schema.ExtensionData == null) //Make sure this exists
            {
                Schema.ExtensionData = new Dictionary <String, Object>();
            }

            ModelName       = Schema.Title;
            PluralModelName = Schema.GetPluralName();
            UiController    = Schema.GetUiControllerName();

            //Make sure directories exist before trying to write files
            WriteApp   = WriteApp && Directory.Exists(AppOutDir);
            WriteTests = WriteTests && Directory.Exists(TestOutDir);

            //Validate
            if (String.IsNullOrWhiteSpace(AppNamespace))
            {
                throw new MessageException($"You must provide an app namespace, one could not be found. Please pass {{--{nameof(AppNamespace)} Your.Namespace}}");
            }
        }