Skip to content

Commit

Permalink
Merge pull request #1 from mahdighorbanpour/feature/upgradePackages
Browse files Browse the repository at this point in the history
Feature/upgrade packages
  • Loading branch information
mahdighorbanpour authored Dec 1, 2021
2 parents 05dd393 + 28737ed commit 5f9aaf9
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 121 deletions.
33 changes: 17 additions & 16 deletions Telstra.Twins.Common/Telstra.Twins.Common.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>1.0.1</Version>
<Authors>Telstra ADT Team</Authors>
<Company>Telstra</Company>
<Description>Digital Twins Common</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>1.0.2</Version>
<Authors>Telstra ADT Team</Authors>
<Company>Telstra</Company>
<Description>Digital Twins Common</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
</ItemGroup>

</Project>
24 changes: 20 additions & 4 deletions Telstra.Twins/Helpers/ReflectionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ public static List<PropertyInfo> GetModelProperties(this Type t) =>
.Where(p => Attribute.IsDefined(p, typeof(TwinPropertyAttribute)))
.ToList();

public static List<PropertyInfo> GetModelPropertiesFromAbstractParent(this Type t) =>
t.BaseType.IsAbstract && t.BaseType.Name != typeof(TwinBase).Name && !Attribute.IsDefined(t.BaseType.GetModelPropertyType(), typeof(DigitalTwinAttribute))
? t.BaseType.GetProperties()
.Where(p => p.DeclaringType == t.BaseType)
.Where(p => Attribute.IsDefined(p, typeof(TwinPropertyAttribute)))
.ToList()
: new();

public static List<PropertyInfo> GetModelOnlyProperties(this Type t) =>
t.GetProperties()
.Where(p => p.DeclaringType == t)
Expand All @@ -90,6 +98,14 @@ public static List<PropertyInfo> GetModelRelationships(this Type t) =>
.Where(p => Attribute.IsDefined(p, typeof(TwinRelationshipAttribute)))
.ToList();

public static List<PropertyInfo> GetModelRelationshipsFromAbstractParent(this Type t) =>
t.BaseType.IsAbstract && t.BaseType.Name != typeof(TwinBase).Name && !Attribute.IsDefined(t.BaseType.GetModelPropertyType(), typeof(DigitalTwinAttribute))
? t.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.DeclaringType == t.BaseType)
.Where(p => Attribute.IsDefined(p, typeof(TwinRelationshipAttribute)))
.ToList()
: new();

public static List<PropertyInfo> GetTwinRelationships(this Type t) =>
t.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)
.Where(p => Attribute.IsDefined(p, typeof(TwinRelationshipAttribute)))
Expand Down Expand Up @@ -154,7 +170,7 @@ public static string GetTwinPropertyName(this PropertyInfo p)
}
else if (Attribute.IsDefined(p, typeof(JsonPropertyAttribute)))
{
var customAttribute = p.GetCustomAttribute<JsonPropertyAttribute>();
var customAttribute = p.GetCustomAttribute<JsonPropertyAttribute>();
twinPropertyName = customAttribute?.PropertyName;
}

Expand All @@ -172,7 +188,7 @@ public static string GetModelPropertyName(this PropertyInfo p)
}
else if (Attribute.IsDefined(p, typeof(TwinPropertyAttribute)))
{
var modelOnlyPropertyAttribute = p.GetCustomAttribute<JsonPropertyAttribute>();
var modelOnlyPropertyAttribute = p.GetCustomAttribute<JsonPropertyAttribute>();
modelPropertyName = modelOnlyPropertyAttribute?.PropertyName;
}

Expand All @@ -187,10 +203,10 @@ public static string GetJsonPropertyName(this PropertyInfo p)
{
var jsonNameAttribute = p.GetCustomAttribute<JsonPropertyNameAttribute>();
jsonPropertyName = jsonNameAttribute?.Name;
}
}
else if (Attribute.IsDefined(p, typeof(JsonPropertyAttribute)))
{
var jsonNameAttribute = p.GetCustomAttribute<JsonPropertyAttribute>();
var jsonNameAttribute = p.GetCustomAttribute<JsonPropertyAttribute>();
jsonPropertyName = jsonNameAttribute?.PropertyName;
}

Expand Down
5 changes: 4 additions & 1 deletion Telstra.Twins/Models/ModelComponent.Factory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Reflection;
using Telstra.Twins.Helpers;
using Telstra.Twins.Common;
using Telstra.Twins.Attributes;

namespace Telstra.Twins.Models
{
Expand All @@ -9,9 +10,11 @@ public partial class ModelComponent : Content
public static ModelComponent Create(PropertyInfo info)
{
var t = info.PropertyType;
var attr = info.GetCustomAttribute<TwinComponentAttribute>();

return new ModelComponent()
{
Name = t.Name.ToCamelCase(),
Name = attr.Name?.ToCamelCase() ?? t.Name.ToCamelCase(),
Schema = t.GetDigitalTwinModelId()
};
}
Expand Down
8 changes: 5 additions & 3 deletions Telstra.Twins/Models/ModelProperty.Factory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ public static ModelProperty Create(PropertyInfo info)
private static object SchemaFromType(PropertyInfo info)
{
var propertyType = info.PropertyType;
var nullableType = Nullable.GetUnderlyingType(propertyType);
if (SchemaMap.ContainsKey(propertyType))
{
return SchemaMap[propertyType];
else if (nullableType != null && SchemaMap.ContainsKey(nullableType))
{
return SchemaMap[nullableType];
}

if (propertyType.IsArray)
else if (propertyType.IsArray)
{
var schema = new Dictionary<string, string>();
schema.Add("@type", "Array");
Expand Down
1 change: 1 addition & 0 deletions Telstra.Twins/Models/ModelRelationship.Factory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public static ModelRelationship Create(PropertyInfo info)

return new ModelRelationship(info.Name.ToCamelCase())
{
Name = attr.Name?.ToCamelCase() ?? info.Name.ToCamelCase(),
DisplayName = attr.DisplayName,
Comment = attr.Comment,
Description = attr.Description,
Expand Down
11 changes: 11 additions & 0 deletions Telstra.Twins/Serialization/ClassToTwinModelConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ protected static void AddPropertiesContent(List<Content> contents)
var modelProperties = typeToAnalyze.GetModelProperties()
.ToList()
.Select(ModelProperty.Create);
var parentProperties = typeToAnalyze.GetModelPropertiesFromAbstractParent()
.ToList()
.Select(ModelProperty.Create);

contents.AddRange(parentProperties);
contents.AddRange(modelProperties);
}

Expand All @@ -187,6 +192,12 @@ protected static void AddModelRelationshipsContent(List<Content> contents)
var modelProperties = typeToAnalyze.GetModelRelationships()
.ToList()
.Select(ModelRelationship.Create);

var parentProperties = typeToAnalyze.GetModelRelationshipsFromAbstractParent()
.ToList()
.Select(ModelRelationship.Create);

contents.AddRange(parentProperties);
contents.AddRange(modelProperties);
}

Expand Down
75 changes: 37 additions & 38 deletions Telstra.Twins/Telstra.Twins.csproj
Original file line number Diff line number Diff line change
@@ -1,46 +1,45 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>Telstra.Twins</RootNamespace>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>1.0.3</Version>
<Authors>Telstra ADT Team</Authors>
<Company>Telstra</Company>
<Description>Telstra Digital Twins Core</Description>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>Telstra.Twins</RootNamespace>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>1.0.4</Version>
<Authors>Telstra ADT Team</Authors>
<Company>Telstra</Company>
<Description>Telstra Digital Twins Core</Description>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Utils\TwinClassGenerator\SampleTwin.cs" />
</ItemGroup>
<ItemGroup>
<Compile Remove="Utils\TwinClassGenerator\SampleTwin.cs" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Utils\TwinClassGenerator\SampleTwin.cs">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Utils\TwinClassGenerator\SampleTwin.cs">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.DigitalTwins.Core" Version="1.2.0" />
<PackageReference Include="Azure.Messaging.EventHubs" Version="5.3.0-beta.4" />
<PackageReference Include="Azure.Messaging.EventHubs.Processor" Version="5.3.0-beta.4" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.7.0" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.9" />
<PackageReference Include="Microsoft.Azure.DigitalTwins.Parser" Version="3.12.5" />
<PackageReference Include="Microsoft.Azure.EventGrid" Version="3.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Validation" Version="16.8.33" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Polly" Version="7.2.2" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Azure.DigitalTwins.Core" Version="1.2.2" />
<PackageReference Include="Azure.Messaging.EventHubs" Version="5.6.2" />
<PackageReference Include="Azure.Messaging.EventHubs.Processor" Version="5.6.2" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.10.0" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.Azure.DigitalTwins.Parser" Version="3.12.7" />
<PackageReference Include="Microsoft.Azure.EventGrid" Version="3.2.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Validation" Version="17.0.34" />
<PackageReference Include="Polly" Version="7.2.2" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Telstra.Twins.Common\Telstra.Twins.Common.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Telstra.Twins.Common\Telstra.Twins.Common.csproj" />
</ItemGroup>

</Project>
31 changes: 25 additions & 6 deletions Tests/Telstra.Twins.Test/SerializationTests.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text.Json;
using System.Text.Json.Serialization;
using Azure.DigitalTwins.Core;
using Telstra.Twins.Attributes;
using Telstra.Twins.Core;
using Telstra.Twins.Models;
using Telstra.Twins.Services;
using Xunit;
using Xunit.Abstractions;
using FluentAssertions;
using System.IO;
using Telstra.Twins.Helpers;

namespace Telstra.Twins.Test
{
Expand All @@ -36,6 +32,29 @@ public void ShouldSerialiseModelToDTDL(string expectedModel, Type twinType)

}

[Theory]
[InlineData(typeof(Building))]
[InlineData(typeof(Floor))]
public void ShouldSerialiseModelToDTDLCustomized(Type type)
{
var model = Serializer.SerializeModel(type);
if (!Directory.Exists("DTDL Models"))
{
Directory.CreateDirectory("DTDL Models");
}
File.WriteAllText(Path.Combine("DTDL Models", $"{type.Name}.json"), model);
}

[Fact]
public void Building_RelationName_Should_MatchWithAttribute()
{
var model = typeof(Building).GetModelRelationships();
var moelRel = ModelRelationship.Create(model[0]);
Assert.Equal("contains", moelRel.Name);

var modelLibrary = new ModelLibrary();
}

[Theory]
[MemberData(nameof(TwinTestData))]
public void ShouldSerialiseTwinToDTDL(string twinDTDL, object twinObject)
Expand Down
49 changes: 25 additions & 24 deletions Tests/Telstra.Twins.Test/Telstra.Twins.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="6.1.0" />
<PackageReference Include="FluentAssertions.Json" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="6.2.0" />
<PackageReference Include="FluentAssertions.Json" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Telstra.Twins\Telstra.Twins.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Telstra.Twins\Telstra.Twins.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 5f9aaf9

Please sign in to comment.