public void SchemaGeneratorShouldWarnAboutMissingKeysIfHasForeignKey() { TypeSchemaGenerator generator = new TypeSchemaGenerator(); SchemaDefinitionCreateResult result = generator.CreateSchemaDefinition(new Type[] { typeof(NoId) }); Expect.IsTrue(result.MissingColumns); Expect.AreEqual(1, result.Warnings.MissingKeyColumns.Length); }
public void ShouldBeAbleToRenderPocoTemplate() { TypeSchemaGenerator generator = new TypeSchemaGenerator(); TypeSchema typeSchema = generator.CreateTypeSchema(typeof(TestContainer)); WrapperModel dtoModel = new WrapperModel(typeof(TestContainer), typeSchema); string output = dtoModel.Render(); OutLine(output, ConsoleColor.Cyan); }
public void ShouldGetFksForDto() { TypeSchemaGenerator generator = new TypeSchemaGenerator(); List <Type> oneToManyTypes = generator.GetForeignKeyTypes(typeof(Parent)).ToList().Select(fk => fk.ForeignKeyType).ToList(); Expect.AreEqual(2, oneToManyTypes.Count); Expect.IsTrue(oneToManyTypes.Contains(typeof(Daughter))); Expect.IsTrue(oneToManyTypes.Contains(typeof(Son))); }
public ProtoFileGenerator(TypeSchemaGenerator typeSchemaGenerator, IPropertyNumberer propertyNumberer, Func <PropertyInfo, bool> propertyFilter = null) { Args.ThrowIfNull(typeSchemaGenerator); Args.ThrowIfNull(propertyNumberer, nameof(propertyNumberer)); TypeSchemaGenerator = typeSchemaGenerator; PropertyNumberer = propertyNumberer; OutputDirectory = ".\\Generated_Protobuf"; PropertyFilter = propertyFilter ?? ((p) => true); }
public void GetTableTypesShouldGetAllAppropriateTypes() { TypeSchemaGenerator generator = new TypeSchemaGenerator(); HashSet <Type> types = generator.GetTableTypes(typeof(TestContainer)); Expect.IsTrue(types.Contains(typeof(Parent))); Expect.IsTrue(types.Contains(typeof(Daughter))); Expect.IsTrue(types.Contains(typeof(Son))); Expect.IsTrue(types.Contains(typeof(House))); }
public void ShouldGetXrefsForDto() { TypeSchemaGenerator generator = new TypeSchemaGenerator(); List <TypeXref> xrefTypes = generator.GetXrefTypes(typeof(Parent)).ToList(); Expect.AreEqual(1, xrefTypes.Count); Expect.AreEqual(typeof(Parent), xrefTypes[0].Left); Expect.AreEqual(typeof(House), xrefTypes[0].Right); Expect.IsTrue(xrefTypes[0].LeftCollectionProperty.IsEnumerable()); OutLineFormat("{0}", ConsoleColor.DarkCyan, xrefTypes[0].LeftCollectionProperty.PropertyType.FullName); }
public void TypeSchemaCheck() { TypeSchemaGenerator gen = new TypeSchemaGenerator(); TypeSchema ts = gen.CreateTypeSchema(new[] { typeof(Parent) }); ts.Tables.Each(t => OutFormat("{0}\r\n", ConsoleColor.Blue, t.Name)); ts.ForeignKeys.Each(fk => OutFormat("{0}->{1}\r\n", ConsoleColor.Cyan, fk.ForeignKeyType.Name, fk.PrimaryKeyType.Name)); ts.Xrefs.Each(x => OutFormat("{0}<->{1}\r\n", ConsoleColor.DarkGreen, x.Left.Name, x.Right.Name)); Expect.AreEqual(1, ts.ForeignKeys.Count); Expect.AreEqual(2, ts.Xrefs.Count); }
public string GenerateProtoFile(IEnumerable <Type> clrTypes, string protoFileDirectory = null) { try { FireEvent(ProtoGenerationStarted); TypeSchema typeSchema = TypeSchemaGenerator.CreateTypeSchema(clrTypes); protoFileDirectory = protoFileDirectory ?? OutputDirectory; DirectoryInfo outputDir = new DirectoryInfo(protoFileDirectory); string nameSpace = GetNamespace(clrTypes); string targetNamespace = string.IsNullOrEmpty(TargetNamespace) ? $"{nameSpace}.Protobuf" : TargetNamespace; string filePath = Path.Combine(outputDir.FullName, $"{nameSpace}.proto"); string moveExistingFileTo = new FileInfo(filePath).FullName.GetNextFileName(); if (File.Exists(filePath)) { File.Move(filePath, moveExistingFileTo); } StringBuilder protoMessages = new StringBuilder(); protoMessages.AppendLine("syntax = \"proto3\";"); protoMessages.AppendLine($"package {targetNamespace};"); foreach (Type type in typeSchema.Tables) { ProtocolBufferType protoType = new ProtocolBufferType(type, PropertyNumberer, PropertyFilter); StringBuilder properties = new StringBuilder(); foreach (ProtocolBufferProperty prop in protoType.Properties) { string propertyFormat = prop.IsRepeated ? ArrayPropertyFormat : PropertyFormat; properties.Append(propertyFormat.NamedFormat(prop)); } ProtocolBufferTypeModel model = new ProtocolBufferTypeModel { TypeName = protoType.TypeName, Properties = properties.ToString() }; protoMessages.Append(MessageFormat.NamedFormat(model)); } protoMessages.ToString().SafeWriteToFile(filePath); FireEvent(ProtoGenerationComplete); return(filePath); } catch (Exception ex) { Message = ex.Message; FireEvent(ProtoGenerationError); return(string.Empty); } }
public static HashSet <DataRelationship> FromInstance(object instance) { Type instanceType = instance.GetType(); TypeSchema typeSchema = new TypeSchemaGenerator().CreateTypeSchema(new[] { instanceType }); HashSet <DataRelationship> relationships = new HashSet <DataRelationship>(); string instanceCuid = instance.Property <string>("Cuid"); typeSchema.ForeignKeys.Where(fk => fk.PrimaryKeyType == instanceType).Each(fk => { foreach (object referencer in (IEnumerable)fk.CollectionProperty.GetValue(instance)) { relationships.Add(new DataRelationship { LeftCuid = instanceCuid, RightCuid = referencer.Property <string>("Cuid"), RelationshipDescription = $"{fk.ForeignKeyType.Namespace}.{fk.ForeignKeyType.Name}->{fk.PrimaryKeyType.Namespace}.{fk.PrimaryKeyType.Name}" }); } }); typeSchema.Xrefs.Where(x => x.Left == instanceType).Each(x => { foreach (object rightInstance in (IEnumerable)x.RightCollectionProperty.GetValue(instance)) { relationships.Add(new DataRelationship { LeftCuid = instanceCuid, RightCuid = rightInstance.Property <string>("Cuid"), RelationshipDescription = $"{x.Left.Namespace}.{x.Left.Name}<->{x.Right.Namespace}.{x.Right.Name}" }); } }); typeSchema.Xrefs.Where(x => x.Right == instanceType).Each(x => { foreach (object leftInstance in (IEnumerable)x.LeftCollectionProperty.GetValue(instance)) { relationships.Add(new DataRelationship { LeftCuid = leftInstance.Property <string>("Cuid"), RightCuid = instanceCuid, RelationshipDescription = $"{x.Left.Namespace}.{x.Left.Name}<->{x.Right.Namespace}.{x.Right.Name}" }); } }); return(relationships); }
public DaoProtoFileGenerator(TypeSchemaGenerator typeSchemaGenerator, IPropertyNumberer propertyNumberer) : base(typeSchemaGenerator, propertyNumberer, (pi) => pi.HasCustomAttributeOfType <ColumnAttribute>()) { }