private void GenerateColumnAttribute(IProperty property) { var columnName = property.GetColumnBaseName(); var columnType = property.GetConfiguredColumnType(); var delimitedColumnName = columnName != null && columnName != property.Name ? _code.Literal(columnName) : null; var delimitedColumnType = columnType != null?_code.Literal(columnType) : null; if ((delimitedColumnName ?? delimitedColumnType) != null) { var columnAttribute = new AttributeWriter(nameof(ColumnAttribute)); if (delimitedColumnName != null) { columnAttribute.AddParameter(delimitedColumnName); } if (delimitedColumnType != null) { columnAttribute.AddParameter($"{nameof(ColumnAttribute.TypeName)} = {delimitedColumnType}"); } _sb.AppendLine(columnAttribute.ToString()); } }
/// <summary> /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to /// the same compatibility standards as public APIs. It may be changed or removed without notice in /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// </summary> protected virtual void GeneratePropertyDataAnnotations(IProperty property) { Check.NotNull(property, nameof(property)); GenerateKeyAttribute(property); GenerateRequiredAttribute(property); GenerateColumnAttribute(property); GenerateMaxLengthAttribute(property); GenerateUnicodeAttribute(property); GeneratePrecisionAttribute(property); var annotations = _annotationCodeGenerator .FilterIgnoredAnnotations(property.GetAnnotations()) .ToDictionary(a => a.Name, a => a); _annotationCodeGenerator.RemoveAnnotationsHandledByConventions(property, annotations); foreach (var attribute in _annotationCodeGenerator.GenerateDataAnnotationAttributes(property, annotations)) { var attributeWriter = new AttributeWriter(attribute.Type.Name); foreach (var argument in attribute.Arguments) { attributeWriter.AddParameter(_code.UnknownLiteral(argument)); } } }
private void GenerateIndexAttributes(IEntityType entityType) { // Do not generate IndexAttributes for indexes which // would be generated anyway by convention. foreach (var index in entityType.GetIndexes().Where( i => ConfigurationSource.Convention != ((IConventionIndex)i).GetConfigurationSource())) { // If there are annotations that cannot be represented using an IndexAttribute then use fluent API instead. var annotations = _annotationCodeGenerator .FilterIgnoredAnnotations(index.GetAnnotations()) .ToDictionary(a => a.Name, a => a); _annotationCodeGenerator.RemoveAnnotationsHandledByConventions(index, annotations); if (annotations.Count == 0) { var indexAttribute = new AttributeWriter(nameof(IndexAttribute)); foreach (var property in index.Properties) { indexAttribute.AddParameter($"nameof({property.Name})"); } if (index.Name != null) { indexAttribute.AddParameter($"{nameof(IndexAttribute.Name)} = {_code.Literal(index.Name)}"); } if (index.IsUnique) { indexAttribute.AddParameter($"{nameof(IndexAttribute.IsUnique)} = {_code.Literal(index.IsUnique)}"); } _sb.AppendLine(indexAttribute.ToString()); } } }
private void GenerateColumnAttribute(IProperty property) { var columnName = property.Relational().ColumnName; var columnType = property.GetConfiguredColumnType(); var delimitedColumnName = columnName != null && columnName != property.Name ? CSharpUtilities.DelimitString(columnName) : null; var delimitedColumnType = columnType != null?CSharpUtilities.DelimitString(columnType) : null; if ((delimitedColumnName ?? delimitedColumnType) != null) { var columnAttribute = new AttributeWriter(nameof(ColumnAttribute)); if (delimitedColumnName != null) { columnAttribute.AddParameter(delimitedColumnName); } if (delimitedColumnType != null) { columnAttribute.AddParameter($"{nameof(ColumnAttribute.TypeName)} = {delimitedColumnType}"); } _propertyAnnotations.Add(new Dictionary <string, object> { { "property-annotation", columnAttribute }, }); } }
public void update_info_panel() { if (slot == null) { return; } AttributeWriter.write_attribute_text(infoT, slot.get_unit()); }
public void Dispose_WriterIsDisposed() { var stream = new StreamMock(); var writer = new AttributeWriter(new BinaryWriter(stream)); Assert.IsFalse(stream.IsDisposed); writer.Dispose(); Assert.IsTrue(stream.IsDisposed); }
//------------------------------------------------------------------------------------------------------------------------------------------------- public PropertyDecorationBuilder Attribute <TAttribute>(Action <AttributeArgumentWriter <TAttribute> > values = null) where TAttribute : Attribute { var attributes = new AttributeWriter(); attributes.Set <TAttribute>(values); m_OwnerProperty.AddAttributes(p => attributes); return(this); }
public void Write_DoNotWriteAttributesWithNullValue() { var output = new MemoryStream(); var writer = new AttributeWriter(new BinaryWriter(output)); writer.Write(new Attribute("test", new IntValue(null), AttributeSource.Custom)); CollectionAssert.AreEqual(new byte[] {}, output.ToArray()); }
public void AttributeWithProperty() { AttributeTemplate template = new AttributeTemplate("test"); template.Properties.Add("key", Code.String("value")); AttributeWriter writer = new AttributeWriter(); writer.Write(template, this.output); Assert.AreEqual("[test(key = \"value\")]", this.output.ToString()); }
private void GenerateForeignKeyAttribute(ISkipNavigation navigation) { if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey()) { var foreignKeyAttribute = new AttributeWriter(nameof(ForeignKeyAttribute)); foreignKeyAttribute.AddParameter( _code.Literal( string.Join(",", navigation.ForeignKey.Properties.Select(p => p.Name)))); _sb.AppendLine(foreignKeyAttribute.ToString()); } }
public static ICollection <string> GetMemberAttributes(string assemblyFilePath, uint moduleToken, uint typeToken, uint memberToken, SupportedLanguage language) { IMemberDefinition member = GetMember(assemblyFilePath, moduleToken, typeToken, memberToken, language); StringWriter stringWriter = new StringWriter(); AttributeWriter attributeWriter = GetAttributeWriter(member, language, stringWriter); attributeWriter.WriteMemberAttributesAndNewLine(member); return(stringWriter.ToString().Split('\n').Where(s => !string.IsNullOrEmpty(s)).ToList()); }
public static ICollection <string> GetTypeAttributes(string assemblyFilePath, uint moduleToken, uint typeToken, SupportedLanguage language) { TypeDefinition type = GetTypeDefinition(assemblyFilePath, moduleToken, typeToken); StringWriter stringWriter = new StringWriter(); AttributeWriter attributeWriter = GetAttributeWriter(type, language, stringWriter); attributeWriter.WriteMemberAttributesAndNewLine(type); return(stringWriter.ToString().Split('\n')); }
public void Json_test_15() { var sb = new StringWriter(); var writer = new AttributeWriter(sb); var json = JsonObject.FromObject(new { a = new[] { 1, 2, 3 }, }); writer.WriteJsonObject(json); Assert.Equal(@"{""a"":{""L"":[{""N"":""1""},{""N"":""2""},{""N"":""3""}]}}", actual: sb.ToString()); }
private void GenerateCommentAttribute(string comment) { if (string.IsNullOrEmpty(comment)) { return; } comment = comment.Replace("\n", " ").Replace("\r", string.Empty); var commentAttribute = new AttributeWriter(nameof(CommentAttribute)); commentAttribute.AddParameter(_code.Literal(System.Security.SecurityElement.Escape(comment))); _sb.AppendLine(commentAttribute.ToString()); }
private void GenerateForeignKeyAttribute(INavigation navigation) { if (navigation.IsDependentToPrincipal()) { if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey()) { var foreignKeyAttribute = new AttributeWriter(nameof(ForeignKeyAttribute)); foreignKeyAttribute.AddParameter( this.code.Literal( string.Join(",", navigation.ForeignKey.Properties.Select(p => p.Name)))); this.IndentedStringBuilder.AppendLine(foreignKeyAttribute.ToString()); } } }
public void Json_test_19() { var sb = new StringWriter(); var writer = new AttributeWriter(sb); var value = new DbValue(new AttributeCollection { { "a", 1 }, { "b", "boat" } }); writer.WriteDbValue(value); Assert.Equal(@"{""M"":{""a"":{""N"":""1""},""b"":{""S"":""boat""}}}", sb.ToString()); }
private void GenerateInversePropertyAttribute(INavigation navigation) { if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey()) { var inverseNavigation = navigation.Inverse; if (inverseNavigation != null) { var inversePropertyAttribute = new AttributeWriter(nameof(InversePropertyAttribute)); inversePropertyAttribute.AddParameter(_code.Literal(inverseNavigation.Name)); _sb.AppendLine(inversePropertyAttribute.ToString()); } } }
private void GenerateMaxLengthAttribute(IProperty property) { var maxLength = property.GetMaxLength(); if (maxLength.HasValue) { var lengthAttribute = new AttributeWriter( property.ClrType == typeof(string) ? nameof(StringLengthAttribute) : nameof(MaxLengthAttribute)); lengthAttribute.AddParameter(_code.Literal(maxLength.Value)); _sb.AppendLine(lengthAttribute.ToString()); } }
private void GenerateInversePropertyAttribute(INavigation navigation) { if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey()) { var inverseNavigation = navigation.FindInverse(); if (inverseNavigation != null) { var inversePropertyAttribute = new AttributeWriter(nameof(InversePropertyAttribute)); inversePropertyAttribute.AddParameter(CSharpUtilities.DelimitString(inverseNavigation.Name)); _sb.AppendLine(inversePropertyAttribute.ToString()); } } }
private void GenerateInversePropertyAttribute(INavigation navigation) { if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey()) { var inverseNavigation = navigation.FindInverse(); if (inverseNavigation != null) { var inversePropertyAttribute = new AttributeWriter(nameof(InversePropertyAttribute)); inversePropertyAttribute.AddParameter($"nameof({inverseNavigation.DeclaringEntityType.Name}.{inverseNavigation.Name})"); _sb.AppendLine(inversePropertyAttribute.ToString()); } } }
private void GenerateForeignKeyAttribute(INavigation navigation) { if (navigation.IsDependentToPrincipal()) { if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey()) { var foreignKeyAttribute = new AttributeWriter(nameof(ForeignKeyAttribute)); foreignKeyAttribute.AddParameter( CSharpUtilities.DelimitString( string.Join(",", navigation.ForeignKey.Properties.Select(p => p.Name)))); _sb.AppendLine(foreignKeyAttribute.ToString()); } } }
public void Json_test_5() { var sb = new StringWriter(); var writer = new AttributeWriter(sb); var json = JsonObject.FromObject(new { a = new HashSet <string>(new[] { "a" }) }); writer.WriteJsonObject(json); Assert.Equal( expected: @"{""a"":{""SS"":[""a""]}}", actual: sb.ToString()); }
private void GeneratePrecisionAttribute(IProperty property) { var precision = property.GetPrecision(); if (precision.HasValue) { var precisionAttribute = new AttributeWriter(nameof(PrecisionAttribute)); precisionAttribute.AddParameter(_code.Literal(precision.Value)); var scale = property.GetScale(); if (scale.HasValue) { precisionAttribute.AddParameter(_code.Literal(scale.Value)); } _sb.AppendLine(precisionAttribute.ToString()); } }
private void GenerateInversePropertyAttribute(INavigation navigation) { if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey()) { var inverseNavigation = navigation.FindInverse(); if (inverseNavigation != null) { var inversePropertyAttribute = new AttributeWriter(nameof(InversePropertyAttribute)); inversePropertyAttribute.AddParameter( navigation.Name != inverseNavigation.DeclaringEntityType.Name ? $"nameof({inverseNavigation.DeclaringEntityType.Name}.{inverseNavigation.Name})" : this._code.Literal(inverseNavigation.Name)); this.IndentedStringBuilder.AppendLine(inversePropertyAttribute.ToString()); } } }
private void GenerateUnicodeAttribute(IProperty property) { if (property.ClrType != typeof(string)) { return; } var unicode = property.IsUnicode(); if (unicode.HasValue) { var unicodeAttribute = new AttributeWriter(nameof(UnicodeAttribute)); if (!unicode.Value) { unicodeAttribute.AddParameter(_code.Literal(false)); } _sb.AppendLine(unicodeAttribute.ToString()); } }
private void GenerateMaxLengthAttribute(IProperty property) { var maxLength = property.GetMaxLength(); if (maxLength.HasValue) { var lengthAttribute = new AttributeWriter( property.ClrType == typeof(string) ? nameof(StringLengthAttribute) : nameof(MaxLengthAttribute)); lengthAttribute.AddParameter(CSharpHelper.Literal(maxLength.Value)); PropertyAnnotationsData.Add(new Dictionary <string, object> { { "property-annotation", lengthAttribute.ToString() }, }); } }
public void Write_StringAttriubte() { var output = new MemoryStream(); var attrWriter = new AttributeWriter(new BinaryWriter(output)); attrWriter.Write(new Attribute("test", new StringValue("value"), AttributeSource.Custom)); var actualData = output.ToArray(); CollectionAssert.AreEqual(new byte[] { // type 0x03, 0x00, // name (byte)'t', (byte)'e', (byte)'s', (byte)'t', 0x00, // value (byte)'v', (byte)'a', (byte)'l', (byte)'u', (byte)'e', 0x00, }, actualData); }
public void Write_IntAttriubte() { var output = new MemoryStream(); var attrWriter = new AttributeWriter(new BinaryWriter(output)); attrWriter.Write(new Attribute("test", new IntValue(0x12345678), AttributeSource.Custom)); var actualData = output.ToArray(); CollectionAssert.AreEqual(new byte[] { // type 0x01, 0x00, // name (byte)'t', (byte)'e', (byte)'s', (byte)'t', 0x00, // value 0x78, 0x56, 0x34, 0x12, }, actualData); }
private void GenerateInversePropertyAttribute(INavigation navigation) { if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey()) { var inverseNavigation = navigation.FindInverse(); if (inverseNavigation != null) { var inversePropertyAttribute = new AttributeWriter(nameof(InversePropertyAttribute)); inversePropertyAttribute.AddParameter(CSharpHelper.Literal(inverseNavigation.Name)); NavPropertyAnnotations.Add(new Dictionary <string, object> { { "nav-property-annotation", inversePropertyAttribute.ToString() }, }); } } }
private void GenerateForeignKeyAttribute(INavigation navigation) { if (navigation.IsDependentToPrincipal()) { if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey()) { var foreignKeyAttribute = new AttributeWriter(nameof(ForeignKeyAttribute)); foreignKeyAttribute.AddParameter( CSharpHelper.Literal( string.Join(",", navigation.ForeignKey.Properties.Select(p => p.Name)))); NavPropertyAnnotations.Add(new Dictionary <string, object> { { "nav-property-annotation", foreignKeyAttribute.ToString() }, }); } } }