Skip to content

Commit

Permalink
Merge 0c6b7a4 into c0b73bd
Browse files Browse the repository at this point in the history
  • Loading branch information
hhoangnl authored Oct 19, 2020
2 parents c0b73bd + 0c6b7a4 commit cfd501f
Show file tree
Hide file tree
Showing 29 changed files with 377 additions and 126 deletions.
2 changes: 0 additions & 2 deletions src/Dash/src/Dash/Constants/DashModelFileConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ internal static class DashModelFileConstants
public const string BaseEntityIdAttributeName = "Id";
public const string BaseEntityIdAttributeDataType = "Int";

public const string DataTypeInteger = "int";

public const string UriSchemeDash = "dash";
}
}
6 changes: 4 additions & 2 deletions src/Dash/src/Dash/Dash.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Nullable>enable</Nullable>
<Version>0.4.0-alpha</Version>
<Version>0.4.1-alpha</Version>
<PackAsTool>true</PackAsTool>
<ToolCommandName>dotnet-dash</ToolCommandName>
<PackageOutputPath>./nupkg</PackageOutputPath>
<AssemblyName>dotnet-dash</AssemblyName>
<Authors>Huy Hoang</Authors>
<CodeAnalysisRuleSet>..\.sonarlint\dotnet-dash_dashcsharp.ruleset</CodeAnalysisRuleSet>
<Description>Dash is a command-line tool for fast model-driven code generation.</Description>
<PackageReleaseNotes>Added the possibility to configure whether entities should be outputted to a single file (default behavior), or written to its own file.</PackageReleaseNotes>
<PackageReleaseNotes>A default value is added to class properties (generated using the built-in EF poco template) in order to make warning CS8618 go away.

</PackageReleaseNotes>
<PackageProjectUrl>https://github.com/dotnet-dash</PackageProjectUrl>
<PackageIcon>packageicon.png</PackageIcon>
<Copyright>Huy Hoang</Copyright>
Expand Down
13 changes: 13 additions & 0 deletions src/Dash/src/Dash/Engine/DataTypes/BoolDataType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Huy Hoang. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace Dash.Engine.DataTypes
{
public class BoolDataType : IDataType
{
public string Name => "bool";
public bool IsNumeric => false;
public bool IsDateTime => false;
public bool IsBoolean => true;
}
}
35 changes: 35 additions & 0 deletions src/Dash/src/Dash/Engine/DataTypes/DataTypeFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Huy Hoang. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

using System;
using Dash.Extensions;

namespace Dash.Engine.DataTypes
{
public static class DataTypeFactory
{
private static IDataType[] DashDataTypes => new IDataType[]
{
new StringDataType(),
new IntDataType(),
new BoolDataType(),
new DateTimeDataType(),
new UnicodeDataType(),
new GuidDataType(),
new EmailDataType(),
};

public static IDataType Create(string dashDataType)
{
foreach (var item in DashDataTypes)
{
if (item.Name.IsSame(dashDataType))
{
return item;
}
}

throw new InvalidOperationException($"Unknown Dash data type '{dashDataType}'");
}
}
}
13 changes: 13 additions & 0 deletions src/Dash/src/Dash/Engine/DataTypes/DateTimeDataType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Huy Hoang. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace Dash.Engine.DataTypes
{
public class DateTimeDataType : IDataType
{
public string Name => "DateTime";
public bool IsNumeric => false;
public bool IsDateTime => true;
public bool IsBoolean => false;
}
}
13 changes: 13 additions & 0 deletions src/Dash/src/Dash/Engine/DataTypes/EmailDataType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Huy Hoang. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace Dash.Engine.DataTypes
{
public class EmailDataType : IDataType
{
public string Name => "email";
public bool IsNumeric => false;
public bool IsDateTime => false;
public bool IsBoolean => false;
}
}
13 changes: 13 additions & 0 deletions src/Dash/src/Dash/Engine/DataTypes/GuidDataType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Huy Hoang. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace Dash.Engine.DataTypes
{
public class GuidDataType : IDataType
{
public string Name => "guid";
public bool IsNumeric => false;
public bool IsDateTime => false;
public bool IsBoolean => false;
}
}
15 changes: 15 additions & 0 deletions src/Dash/src/Dash/Engine/DataTypes/IntDataType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Huy Hoang. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace Dash.Engine.DataTypes
{
public class IntDataType : IDataType
{
public string Name => "int";
public bool IsNumeric => true;
public bool IsDateTime => false;
public bool IsBoolean => false;

public static IntDataType Default => new IntDataType();
}
}
13 changes: 13 additions & 0 deletions src/Dash/src/Dash/Engine/DataTypes/StringDataType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Huy Hoang. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace Dash.Engine.DataTypes
{
public class StringDataType : IDataType
{
public string Name => "string";
public bool IsNumeric => false;
public bool IsDateTime => false;
public bool IsBoolean => false;
}
}
13 changes: 13 additions & 0 deletions src/Dash/src/Dash/Engine/DataTypes/UnicodeDataType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Huy Hoang. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace Dash.Engine.DataTypes
{
public class UnicodeDataType : IDataType
{
public string Name => "unicode";
public bool IsNumeric => false;
public bool IsDateTime => false;
public bool IsBoolean => false;
}
}
19 changes: 19 additions & 0 deletions src/Dash/src/Dash/Engine/IDataType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Huy Hoang. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace Dash.Engine
{
/// <summary>
/// Abstract representation of a Dash Data Type defined in the Model File.
/// </summary>
public interface IDataType
{
public string Name { get; }

public bool IsNumeric { get; }

public bool IsDateTime { get; }

bool IsBoolean { get; }
}
}
2 changes: 1 addition & 1 deletion src/Dash/src/Dash/Engine/ILanguageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public interface ILanguageProvider
string Guid { get; }
string String { get; }
string Unicode { get; }
public string Translate(string dashDataType);
public string Translate(IDataType dataType);
}
}
39 changes: 12 additions & 27 deletions src/Dash/src/Dash/Engine/LanguageProviders/BaseLanguageProvider.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Huy Hoang. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

using Dash.Constants;
using Dash.Engine.DataTypes;
using Dash.Exceptions;

namespace Dash.Engine.LanguageProviders
Expand All @@ -17,34 +17,19 @@ public abstract class BaseLanguageProvider : ILanguageProvider
public abstract string String { get; }
public abstract string Unicode { get; }

public string Translate(string dashDataType)
public string Translate(IDataType dataType)
{
switch (dashDataType.ToLower())
return dataType switch
{
case DashModelFileConstants.DataTypeInteger:
return Int;

case "bool":
return Bool;

case "datetime":
return DateTime;

case "email":
return Email;

case "guid":
return Guid;

case "string":
return String;

case "unicode":
return Unicode;

default:
throw new InvalidDataTypeException(dashDataType);
}
IntDataType _ => Int,
BoolDataType _ => Bool,
DateTimeDataType _ => DateTime,
EmailDataType _ => Email,
GuidDataType _ => Guid,
StringDataType _ => String,
UnicodeDataType _ => Unicode,
_ => throw new InvalidDataTypeException(dataType.Name),
};
}
}
}
8 changes: 7 additions & 1 deletion src/Dash/src/Dash/Engine/Models/AttributeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@

namespace Dash.Engine.Models
{
/// <summary>
/// This class represents an Attribute and could be consumed by an <see cref="ITemplateTransformer"/>.
/// </summary>
public class AttributeModel
{
public AttributeModel(string name, string dataType, bool isNullable, string? defaultValue)
public AttributeModel(string name, IDataType dashDataType, string dataType, bool isNullable, string? defaultValue)
{
Name = name;
DashDataType = dashDataType;
DataType = dataType;
IsNullable = isNullable;
DefaultValue = defaultValue;
}

public string Name { get; }

public IDataType DashDataType { get; }

public string DataType { get; }

public string? DefaultValue { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) Huy Hoang. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

using Dash.Engine.Models;

namespace Dash.Engine.TemplateTransformers.Scriban
{
public static class CSharpOutputHelpers
{
public static string GetCSharpLiteral(object value)
{
if (value == null)
{
return "null";
}

if (value is int intValue)
{
return intValue.ToString();
}

if (value is decimal decimalValue)
{
return $"{decimalValue}m";
}

var v = value.ToString()!.Replace("\"", "\\\"");
return $"\"{v}\"";
}

public static string GetPropertyDefaultValueAssignment(object value)
{
const string code = "= null!;";

if (value is ReferencedEntityModel referencedEntity)
{
if (!referencedEntity.IsNullable)
{
return code;
}
}
else if (value is AttributeModel attribute)
{
if (attribute.DashDataType.IsNumeric ||
attribute.DashDataType.IsDateTime ||
attribute.DashDataType.IsBoolean)
{
return string.Empty;
}

if (!attribute.IsNullable)
{
return code;
}
}

return string.Empty;
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ScribanTemplateTransformer : ITemplateTransformer
{
private readonly DashOptions _options;

public ScribanTemplateTransformer(IModelRepository modelRepository, IOptions<DashOptions> options)
public ScribanTemplateTransformer(IOptions<DashOptions> options)
{
_options = options.Value;
}
Expand All @@ -32,7 +32,7 @@ public async Task<string> Transform(string templateText, IEnumerable<EntityModel
{"modelName", Path.GetFileNameWithoutExtension(_options.InputFile!).StartWithCapitalLetter()},
{"entities", entities}
};
scriptObject.Import(typeof(GetCSharpLiteralFormatter));
scriptObject.Import(typeof(CSharpOutputHelpers));

var context = new TemplateContext();
context.PushGlobal(scriptObject);
Expand Down
Loading

0 comments on commit cfd501f

Please sign in to comment.