Skip to content

Commit

Permalink
90 refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
Sevitas committed Jan 12, 2023
1 parent da544d9 commit 805dd72
Show file tree
Hide file tree
Showing 26 changed files with 297 additions and 266 deletions.
38 changes: 11 additions & 27 deletions src/Kontent.Ai.ModelGenerator.Core/Common/Property.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,18 @@ public Property(string codename, string typeName, string id = null)
Id = id;
}

public static bool IsContentTypeSupported(string elementType, bool extendedDeliverModels)
{
return extendedDeliverModels
? ExtendedDeliverElementTypesDictionary.ContainsKey(elementType)
: DeliverElementTypesDictionary.ContainsKey(elementType);
}
public static bool IsContentTypeSupported(string elementType, bool extendedDeliverModels) => extendedDeliverModels
? ExtendedDeliverElementTypesDictionary.ContainsKey(elementType)
: DeliverElementTypesDictionary.ContainsKey(elementType);

public static bool IsContentTypeSupported(string elementType)
{
return DeliverElementTypesDictionary.ContainsKey(elementType);
}
public static bool IsContentTypeSupported(string elementType) => DeliverElementTypesDictionary.ContainsKey(elementType);

public static bool IsContentTypeSupported(ElementMetadataType elementType)
{
return ManagementElementTypesDictionary.ContainsKey(elementType);
}
public static bool IsContentTypeSupported(ElementMetadataType elementType) =>
ManagementElementTypesDictionary.ContainsKey(elementType);

public static Property FromContentTypeElement(string codename, string elementType)
{
if (IsContentTypeSupported(elementType))
{
return new Property(codename, DeliverElementTypesDictionary[elementType]);
}

throw new ArgumentException($"Unknown Content Type {elementType}", nameof(elementType));
}
public static Property FromContentTypeElement(string codename, string elementType) => IsContentTypeSupported(elementType)
? new Property(codename, DeliverElementTypesDictionary[elementType])
: throw new ArgumentException($"Unknown Content Type {elementType}", nameof(elementType));

public static Property FromContentTypeElement(ElementMetadataBase element)
{
Expand All @@ -117,10 +103,8 @@ public static Property FromContentTypeElement(ElementMetadataBase element)
throw new ArgumentException($"Unknown Content Type {element.Type}", nameof(element));
}

public static Property FromContentTypeElement(ElementMetadataBase element, string elementType)
{
return FromContentTypeElement(element, elementType, element.Codename);
}
public static Property FromContentTypeElement(ElementMetadataBase element, string elementType) =>
FromContentTypeElement(element, elementType, element.Codename);

public static Property FromContentTypeElement(ElementMetadataBase element, string elementType, string finalPropertyName)
{
Expand Down
23 changes: 0 additions & 23 deletions src/Kontent.Ai.ModelGenerator.Core/DeliveryCodeGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Kontent.Ai.Delivery.Abstractions;
using Kontent.Ai.ModelGenerator.Core.Common;
using Kontent.Ai.ModelGenerator.Core.Configuration;
using Kontent.Ai.ModelGenerator.Core.Generators;
using Kontent.Ai.ModelGenerator.Core.Generators.Class;
using Kontent.Ai.ModelGenerator.Core.Helpers;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -82,27 +80,6 @@ internal ClassCodeGenerator GetClassCodeGenerator(IContentType contentType)
return ClassCodeGeneratorFactory.CreateClassCodeGenerator(Options, classDefinition, classFilename);
}

private async Task GenerateTypeProvider()
{
var classCodeGenerators = await GetClassCodeGenerators();

if (!classCodeGenerators.Any())
{
Console.WriteLine(NoContentTypeAvailableMessage);
return;
}

var typeProviderCodeGenerator = new TypeProviderCodeGenerator(Options.Namespace);

foreach (var codeGenerator in classCodeGenerators)
{
typeProviderCodeGenerator.AddContentType(codeGenerator.ClassDefinition.Codename, codeGenerator.ClassDefinition.ClassName);
}

var typeProviderCode = typeProviderCodeGenerator.GenerateCode();
WriteToOutputProvider(typeProviderCode, TypeProviderCodeGenerator.ClassName, true);
}

private static void TryAddSystemProperty(ClassDefinition classDefinition)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ public class ContentItemClassCodeGenerator : ClassCodeGenerator
public const string DefaultContentItemClassName = "IContentItem";

public ContentItemClassCodeGenerator(string @namespace = DefaultNamespace)
: this(new ClassDefinition(DefaultContentItemClassName), DefaultContentItemClassName, @namespace)
{
}

private ContentItemClassCodeGenerator(ClassDefinition classDefinition, string classFilename, string @namespace = DefaultNamespace)
: base(classDefinition, classFilename, @namespace)
: base(new ClassDefinition(DefaultContentItemClassName), DefaultContentItemClassName, @namespace)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected override TypeDeclarationSyntax GetClassDeclaration()
return classDeclaration;
}

private MemberDeclarationSyntax[] Properties => ClassDefinition.Properties.OrderBy(p => p.Identifier).Select(element =>
protected override MemberDeclarationSyntax[] Properties => ClassDefinition.Properties.OrderBy(p => p.Identifier).Select(element =>
SyntaxFactory
.PropertyDeclaration(SyntaxFactory.ParseTypeName(element.TypeName), element.Identifier)
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,7 @@ private MemberDeclarationSyntax[] GetProperties()

private static string GetNonEnumerableTypeName(string typeName) => Regex.Match(typeName, "[^\\>|\\<]\\w+\\b(?<!\\bIEnumerable)").Value;

private static string GetOriginalPropertyName(string propertyName, string typeName)
{
if (propertyName == typeName)
{
return typeName;
}

return propertyName.Substring(0, propertyName.Length - typeName.Length);
}
private static string GetOriginalPropertyName(string propertyName, string typeName) => propertyName == typeName
? typeName
: propertyName.Substring(0, propertyName.Length - typeName.Length);
}
28 changes: 10 additions & 18 deletions src/Kontent.Ai.ModelGenerator.Core/Helpers/TextHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static class TextHelpers
/// </summary>
public static string GetValidPascalCaseIdentifierName(string name)
{
string sanitizedName = Regex.Replace(name, "[^a-zA-Z0-9]", WordSeparator, RegexOptions.IgnoreCase | RegexOptions.Multiline);
var sanitizedName = Regex.Replace(name, "[^a-zA-Z0-9]", WordSeparator, RegexOptions.IgnoreCase | RegexOptions.Multiline);

// Remove leading numbers and leading whitespace (e.g.: ' 123Name123' -> 'Name123'
sanitizedName = Regex.Replace(sanitizedName, "^(\\s|[0-9])+", "");
Expand All @@ -27,16 +27,14 @@ public static string GetValidPascalCaseIdentifierName(string name)
}

return sanitizedName
.ToLower()
.Split(new[] { WordSeparator }, StringSplitOptions.RemoveEmptyEntries)
.Select(word => char.ToUpper(word[0]) + word.Substring(1))
.Aggregate((previous, current) => previous + current);
.ToLower()
.Split(new[] { WordSeparator }, StringSplitOptions.RemoveEmptyEntries)
.Select(word => char.ToUpper(word[0]) + word.Substring(1))
.Aggregate((previous, current) => previous + current);
}

public static string NormalizeLineEndings(this string text)
{
return LineEndings.Replace(text, Environment.NewLine);
}
public static string NormalizeLineEndings(this string text) =>
LineEndings.Replace(text, Environment.NewLine);

public static string GenerateCommentString(string customComment)
{
Expand All @@ -54,13 +52,7 @@ public static string GenerateCommentString(string customComment)
// </auto-generated>{Environment.NewLine}{Environment.NewLine}";
}

public static string GetEnumerableType(string typeName)
{
if (string.IsNullOrWhiteSpace(typeName))
{
throw new ArgumentException("", nameof(typeName));
}

return $"{nameof(IEnumerable)}<{typeName}>";
}
public static string GetEnumerableType(string typeName) => string.IsNullOrWhiteSpace(typeName)
? throw new ArgumentException("", nameof(typeName))
: $"{nameof(IEnumerable)}<{typeName}>";
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Kontent.Ai.ModelGenerator.Core.Configuration;
using Kontent.Ai.ModelGenerator.Core.Generators.Class;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Kontent.Ai.Management.Extensions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ internal class ArgMappingsRegister
.Union(DeliveryMappings.Keys)
.Union(ManagementMappings.Keys)
.Union(ExtendedDeliveryMappings.Keys);
}
}
40 changes: 22 additions & 18 deletions test/Kontent.Ai.ModelGenerator.Tests/ArgHelpersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using FluentAssertions;
using Kontent.Ai.Delivery.Abstractions;
using Kontent.Ai.Management.Configuration;
using Kontent.Ai.ModelGenerator.Core.Configuration;
Expand Down Expand Up @@ -74,7 +75,7 @@ public void GetSwitchMappings_MissingMapiSwitch_ReturnsDeliveryMappings()
Guid.NewGuid().ToString()
});

Assert.Equal(ExpectedDeliveryMappings, result);
result.Should().BeEquivalentTo(ExpectedDeliveryMappings);
}

[Fact]
Expand All @@ -88,7 +89,7 @@ public void GetSwitchMappings_MapiSwitchIsFalse_ReturnsDeliveryMappings()
"false"
});

Assert.Equal(ExpectedDeliveryMappings, result);
result.Should().BeEquivalentTo(ExpectedDeliveryMappings);
}

[Fact]
Expand All @@ -102,7 +103,7 @@ public void GetSwitchMappings_MapiSwitchIsTrue_ReturnsManagementMappings()
"true"
});

Assert.Equal(ExpectedManagementMappings, result);
result.Should().BeEquivalentTo(ExpectedManagementMappings);
}

[Fact]
Expand All @@ -116,7 +117,7 @@ public void GetSwitchMappings_ExtendedDeliveryIsTrue_ReturnsManagementMappings()
"true"
});

Assert.Equal(ExpectedExtendedDeliveryMappings, result);
result.Should().BeEquivalentTo(ExpectedExtendedDeliveryMappings);
}

[Fact]
Expand All @@ -130,7 +131,7 @@ public void GetSwitchMappings_ExtendedDeliveryPreviewIsTrue_ReturnsManagementMap
"true"
});

Assert.Equal(ExpectedExtendedDeliveryMappings, result);
result.Should().BeEquivalentTo(ExpectedExtendedDeliveryMappings);
}

[Fact]
Expand All @@ -146,7 +147,7 @@ public void GetSwitchMappings_ExtendedDeliveryAndPreviewIsTrue_ReturnsManagement
"true"
});

Assert.Equal(ExpectedExtendedDeliveryMappings, result);
result.Should().BeEquivalentTo(ExpectedExtendedDeliveryMappings);
}

[Fact]
Expand All @@ -164,7 +165,7 @@ public void ContainsContainsValidArgs_SupportedDeliveryOptions_ReturnsTrue()

var result = ArgHelpers.ContainsValidArgs(args);

Assert.True(result);
result.Should().BeTrue();
}

[Theory]
Expand All @@ -181,7 +182,7 @@ public void ContainsContainsValidArgs_UnsupportedDeliveryOptions_ReturnsFalse(st
};
var result = ArgHelpers.ContainsValidArgs(args);

Assert.False(result);
result.Should().BeFalse();
}

[Fact]
Expand All @@ -198,7 +199,7 @@ public void ContainsContainsValidArgs_SupportedManagementOptions_ReturnsTrue()

var result = ArgHelpers.ContainsValidArgs(args);

Assert.True(result);
result.Should().BeTrue();
}

[Theory]
Expand All @@ -216,7 +217,7 @@ public void ContainsContainsValidArgs_UnsupportedManagementOptions_ReturnsFalse(
};
var result = ArgHelpers.ContainsValidArgs(args);

Assert.False(result);
result.Should().BeFalse();
}

[Fact]
Expand All @@ -234,7 +235,7 @@ public void ContainsContainsValidArgs_SupportedExtendedDeliveryOptions_ReturnsTr

var result = ArgHelpers.ContainsValidArgs(args);

Assert.True(result);
result.Should().BeTrue();
}

[Theory]
Expand All @@ -255,34 +256,31 @@ public void ContainsContainsValidArgs_UnsupportedExtendedDeliveryOptions_Returns

var result = ArgHelpers.ContainsValidArgs(args);

Assert.False(result);
result.Should().BeFalse();
}

[Fact]
public void GetProgramOptionsData_ManagementApi_ReturnsManagementProgramOptionsData()
{
var result = ArgHelpers.GetUsedSdkInfo(DesiredModelsType.Management);

Assert.Equal("management-sdk-net", result.Name);
Assert.Equal(Assembly.GetAssembly(typeof(ManagementOptions)).GetName().Version.ToString(3), result.Version);
AssertUsedSdkInfoResult(result, "management-sdk-net", typeof(ManagementOptions));
}

[Fact]
public void GetProgramOptionsData_DeliveryApi_ReturnsDeliveryProgramOptionsData()
{
var result = ArgHelpers.GetUsedSdkInfo(DesiredModelsType.Delivery);

Assert.Equal("delivery-sdk-net", result.Name);
Assert.Equal(Assembly.GetAssembly(typeof(DeliveryOptions)).GetName().Version.ToString(3), result.Version);
AssertUsedSdkInfoResult(result, "delivery-sdk-net", typeof(DeliveryOptions));
}

[Fact]
public void GetProgramOptionsData_ExtendedDeliveryApi_ReturnsExtendedDeliveryProgramOptionsData()
{
var result = ArgHelpers.GetUsedSdkInfo(DesiredModelsType.ExtendedDelivery);

Assert.Equal("delivery-sdk-net", result.Name);
Assert.Equal(Assembly.GetAssembly(typeof(DeliveryOptions)).GetName().Version.ToString(3), result.Version);
AssertUsedSdkInfoResult(result, "delivery-sdk-net", typeof(DeliveryOptions));
}

private static IEnumerable<string> AppendValuesToArgs(IDictionary<string, string> mappings) => AppendValuesToArgs(mappings.Keys);
Expand All @@ -302,4 +300,10 @@ private static IEnumerable<string> AppendValuesToArgs(IEnumerable<string> args)
}
return argsWithValue;
}

private static void AssertUsedSdkInfoResult(UsedSdkInfo result, string expectedName, Type expectedType)
{
result.Name.Should().Be(expectedName);
result.Version.Should().Be(Assembly.GetAssembly(expectedType).GetName().Version.ToString(3));
}
}
Loading

0 comments on commit 805dd72

Please sign in to comment.