Skip to content

Commit

Permalink
codes refactored based on new lang version
Browse files Browse the repository at this point in the history
  • Loading branch information
vahid committed Nov 22, 2023
1 parent 7e1a54e commit dabca76
Show file tree
Hide file tree
Showing 82 changed files with 501 additions and 479 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*.cs]

# Default severity for analyzer diagnostics with category 'Naming'
dotnet_analyzer_diagnostic.category-Naming.severity = none
6 changes: 2 additions & 4 deletions 01-Core/Jinget.Core/Attributes/AuthenticationRequired.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ namespace Jinget.Core.Attributes
/// <summary>
/// This attribute are useful in scenarios like Service Hub, where prior to calling an API we need to authenticate it
/// </summary>
public class AuthenticationRequiredAttribute : Attribute
public class AuthenticationRequiredAttribute(bool required) : Attribute
{
public AuthenticationRequiredAttribute(bool required) => Required = required;

public bool Required { get; set; }
public bool Required { get; set; } = required;
}
}
4 changes: 4 additions & 0 deletions 01-Core/Jinget.Core/Attributes/ClaimAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ namespace Jinget.Core.Attributes
/// <summary>
/// Used for assigning claims for actions
/// </summary>
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

public class ClaimAttribute : AuthorizeAttribute
{
/// <summary>
/// Gets or sets the claim title.
/// </summary>
public string Title { get; set; }
}
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

}
13 changes: 6 additions & 7 deletions 01-Core/Jinget.Core/Attributes/SummaryAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ namespace Jinget.Core.Attributes
/// <summary>
/// Manage Method Summary Attribute
/// </summary>
public class SummaryAttribute : Attribute
/// <remarks>
/// provide method summary for using in access management
/// </remarks>
/// <param name="description"></param>
public class SummaryAttribute(string description) : Attribute
{
/// <summary>
/// provide method summary for using in access management
/// </summary>
/// <param name="description"></param>
public SummaryAttribute(string description) => Description = description;

/// <summary>
/// Action's description. this value will be shown to the end user
/// </summary>
public string Description { get; set; }
public string Description { get; set; } = description;
}
}
30 changes: 16 additions & 14 deletions 01-Core/Jinget.Core/CodeDom/JingetDynamicCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ private class Compiler
/// <param name="jingetSource">During the dynamic code execution process, given sourceCode might change. Changed version of the sourceCode are stored in this output parameter</param>
/// <param name="isTopLevelStatement">C# 9.0 enables you to write top level statements.</param>
/// <param name="references">In order to compile the given sourceCode, some external references might needed to be added. Required references are being passed using this parameter</param>
internal byte[] Compile(string sourceCode, MethodOptions args, out List<string> errors, out string jingetSource,
bool isTopLevelStatement = true, List<string> references = null)
internal byte[]? Compile(string sourceCode, MethodOptions? args, out List<string> errors, out string jingetSource,
bool isTopLevelStatement = true, List<string>? references = null)
{
jingetSource = string.Empty;
errors = new List<string>();
errors = [];

sourceCode = sourceCode.Trim();
sourceCode = sourceCode.StartsWith(Environment.NewLine)
Expand Down Expand Up @@ -70,7 +70,7 @@ CSharpCompilation GenerateAssembly(string sourceCode, List<string> externalRefer

var parsedSyntaxTree = SyntaxFactory.ParseSyntaxTree(codeString, options);

externalReferences ??= new List<string>();
externalReferences ??= [];

var defaultReferences = new[] { "System.Private.CoreLib", "netstandard", "System.Runtime" };

Expand All @@ -92,9 +92,9 @@ CSharpCompilation GenerateAssembly(string sourceCode, List<string> externalRefer
/// Generate source code using given expression. This method tries to put the given expression inside a method in a class
/// so that it could be invoked
/// </summary>
string GenerateSourceCode(string expression, MethodOptions methodOptions)
static string GenerateSourceCode(string expression, MethodOptions? methodOptions)
{
CodeNamespace globalCodeNamespace = new CodeNamespace();
CodeNamespace globalCodeNamespace = new();
//add default using: using System;
globalCodeNamespace.Imports.Add(new CodeNamespaceImport("System"));

Expand All @@ -113,16 +113,16 @@ string GenerateSourceCode(string expression, MethodOptions methodOptions)
}
}

CodeNamespace myNamespace = new CodeNamespace("JingetDynamic");
CodeTypeDeclaration classDeclaration = new CodeTypeDeclaration
CodeNamespace myNamespace = new("JingetDynamic");
CodeTypeDeclaration classDeclaration = new()
{
IsClass = true,
Name = "DynamicInvoker",
TypeAttributes = TypeAttributes.NotPublic | TypeAttributes.Sealed

};

CodeMemberMethod myMethod = new CodeMemberMethod
CodeMemberMethod myMethod = new()
{
Name = "DynamicInvoke",
Attributes = MemberAttributes.Public | MemberAttributes.Final,
Expand All @@ -141,15 +141,15 @@ string GenerateSourceCode(string expression, MethodOptions methodOptions)

myNamespace.Types.Add(classDeclaration);

CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
CodeCompileUnit codeCompileUnit = new();
codeCompileUnit.Namespaces.Add(globalCodeNamespace);
codeCompileUnit.Namespaces.Add(myNamespace);

var source = new StringBuilder();
using (var sw = new StringWriter(source))
{
ICodeGenerator generator = new CSharpCodeProvider().CreateGenerator(sw);
CodeGeneratorOptions codeOpts = new CodeGeneratorOptions();
CodeGeneratorOptions codeOpts = new();
generator.GenerateCodeFromCompileUnit(codeCompileUnit, sw, codeOpts);
sw.Flush();
sw.Close();
Expand All @@ -169,7 +169,7 @@ string GenerateSourceCode(string expression, MethodOptions methodOptions)
}

[MethodImpl(MethodImplOptions.NoInlining)]
public object Execute(string sourceCode, out List<string> errors, out string compiledSourceCode, MethodOptions options = null, bool isTopLevelStatement = true, bool compileOnly = false, List<string> references = null)
public object? Execute(string sourceCode, out List<string> errors, out string compiledSourceCode, MethodOptions? options = null, bool isTopLevelStatement = true, bool compileOnly = false, List<string>? references = null)
{
if (sourceCode.Length > 10000)
{
Expand All @@ -190,7 +190,7 @@ public object Execute(string sourceCode, out List<string> errors, out string com

return method?.Invoke(instance, options?.Parameters.Select(x => x.Value).ToArray());
}

#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public class MethodOptions
{
/// <summary>
Expand All @@ -202,13 +202,15 @@ public class MethodOptions
/// <summary>
/// What are the input parameters for dynamically injected Invoke method?
/// </summary>
public List<ParameterOptions> Parameters { get; set; } = new List<ParameterOptions>();
public List<ParameterOptions> Parameters { get; set; } = [];

public class ParameterOptions
{
public Type Type { get; set; }
public string Name { get; set; }
public object Value { get; set; }
}
}
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/// <summary>
/// add support for 'required' keyword using latest lang version and .net standard 2.1
/// </summary>
public class CompilerFeatureRequiredAttribute : Attribute
public class CompilerFeatureRequiredAttribute(string name) : Attribute

Check warning on line 6 in 01-Core/Jinget.Core/Compiler/CompilerFeatureRequiredAttribute.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'name' is unread.

Check warning on line 6 in 01-Core/Jinget.Core/Compiler/CompilerFeatureRequiredAttribute.cs

View workflow job for this annotation

GitHub Actions / release

Parameter 'name' is unread.
{
public CompilerFeatureRequiredAttribute(string name) { }
}
4 changes: 2 additions & 2 deletions 01-Core/Jinget.Core/Contracts/IUserContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public interface IUserContext : IPrincipal
/// <param name="apiName">Name of the API.</param>
/// <param name="actionTitle">The action title.</param>
/// <param name="claim">The claim.</param>
Task<bool> HasAccess(string userIdentifier, string subSystemName, string apiName, string actionTitle, string claim);
Task<bool> HasAccessAsync(string userIdentifier, string subSystemName, string apiName, string actionTitle, string claim);

/// <summary>
/// Determines whether the token is valid for the given user or not.
/// </summary>
/// <param name="userIdentifier">The user identifier.</param>
/// <param name="token">The token.</param>
Task<bool> IsTokenValid(string userIdentifier, string token);
Task<bool> IsTokenValidAsync(string userIdentifier, string token);
}
}
21 changes: 7 additions & 14 deletions 01-Core/Jinget.Core/Exceptions/JingetException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,20 @@ namespace Jinget.Core.Exceptions
/// Implements the <see cref="Exception" />
/// </summary>
/// <seealso cref="Exception" />
public class JingetException : Exception
/// <param name="message">Exception message to be thrown</param>
/// <param name="ex">The inner exception to be thrown</param>
/// <param name="code">Custom code of exception</param>
/// <param name="type">Type of exception <see cref="ExceptionType"></param>
public class JingetException(string message, Exception ex, int code = -1, ExceptionType type = ExceptionType.JingetInternal) : Exception(message, ex)
{
public int Code { get; set; }
public int Code { get; set; } = code;

public ExceptionType Type { get; set; }
public ExceptionType Type { get; set; } = type;

/// <param name="message">Exception message to be thrown</param>
/// <param name="code">Custom code of exception</param>
/// <param name="type">Type of exception <see cref="ExceptionType"></param>
public JingetException(string message, int code = -1, ExceptionType type = ExceptionType.JingetInternal)
: this(message, null, code, type) { }

Check warning on line 25 in 01-Core/Jinget.Core/Exceptions/JingetException.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 25 in 01-Core/Jinget.Core/Exceptions/JingetException.cs

View workflow job for this annotation

GitHub Actions / release

Cannot convert null literal to non-nullable reference type.

/// <param name="message">Exception message to be thrown</param>
/// <param name="ex">The inner exception to be thrown</param>
/// <param name="code">Custom code of exception</param>
/// <param name="type">Type of exception <see cref="ExceptionType"></param>
public JingetException(string message, Exception ex, int code = -1, ExceptionType type = ExceptionType.JingetInternal)
: base(message, ex)
{
Code = code;
Type = type;
}
}
}
12 changes: 8 additions & 4 deletions 01-Core/Jinget.Core/ExpressionToSql/Internal/OrderBy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace Jinget.Core.ExpressionToSql.Internal
/// <summary>
/// Provides the order by functionality used in query handling
/// </summary>
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

public class OrderBy
{
public OrderBy()
Expand All @@ -35,19 +37,21 @@ public OrderBy()
/// </summary>
public override string ToString()
{
StringBuilder orderByClause = new StringBuilder();
orderByClause.Append("[");
StringBuilder orderByClause = new();
orderByClause.Append('[');

if (Name.Body is MemberExpression expression && expression.Expression.NodeType != ExpressionType.Convert)

Check warning on line 43 in 01-Core/Jinget.Core/ExpressionToSql/Internal/OrderBy.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 43 in 01-Core/Jinget.Core/ExpressionToSql/Internal/OrderBy.cs

View workflow job for this annotation

GitHub Actions / release

Dereference of a possibly null reference.
orderByClause.Append(Expression.Lambda(expression).Compile().DynamicInvoke());
else
orderByClause.Append(Name.Stringfy());

orderByClause.Append("]");
orderByClause.Append(" ");
orderByClause.Append(']');
orderByClause.Append(' ');
orderByClause.Append(Direction.GetDescription());

return orderByClause.ToString();
}
}
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

}
10 changes: 5 additions & 5 deletions 01-Core/Jinget.Core/ExpressionToSql/Internal/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ namespace Jinget.Core.ExpressionToSql.Internal
{
public abstract class Query
{
public (QueryBuilder query, Dictionary<string, object> parameters) ToSql() => ToSql(new StringBuilder());
public (QueryBuilder query, Dictionary<string, object>? parameters) ToSql() => ToSql(new StringBuilder());

private (QueryBuilder query, Dictionary<string, object> parameters) ToSql(StringBuilder sb)
private (QueryBuilder query, Dictionary<string, object>? parameters) ToSql(StringBuilder sb)
{
var result = ToSql(new QueryBuilder(sb));
return (result.query, result.parameters);
var (query, parameters) = ToSql(new QueryBuilder(sb));
return (query, parameters);
}

internal abstract (QueryBuilder query, Dictionary<string, object> parameters) ToSql(QueryBuilder query);
internal abstract (QueryBuilder query, Dictionary<string, object>? parameters) ToSql(QueryBuilder query);
}
}
18 changes: 9 additions & 9 deletions 01-Core/Jinget.Core/ExpressionToSql/Internal/QueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,20 @@ public QueryBuilder AddParameter(string parameterName)
/// </summary>
public QueryBuilder AddAttribute(string attributeName, string aliasName = AliasName)
{
_sb.Append(" ");
_sb.Append(' ');

if (!string.IsNullOrWhiteSpace(aliasName))
{
_sb.Append(aliasName).Append(".");
_sb.Append(aliasName).Append('.');
}

AppendEscapedValue(attributeName);
return this;
}

public QueryBuilder AddValue(object value)
public QueryBuilder AddValue(object? value)
{
_sb.Append(" ").Append(value);
_sb.Append(' ').Append(value);
return this;
}

Expand All @@ -61,7 +61,7 @@ public QueryBuilder AddValue(object value)
/// </summary>
public QueryBuilder AddSeparator()
{
_sb.Append(",");
_sb.Append(',');
return this;
}

Expand All @@ -81,7 +81,7 @@ public QueryBuilder AddTable(Table table, string aliasName = AliasName)
if (!string.IsNullOrWhiteSpace(table.Schema))
{
AppendEscapedValue(table.Schema);
_sb.Append(".");
_sb.Append('.');
}

AppendEscapedValue(table.Name);
Expand All @@ -107,14 +107,14 @@ private void AppendEscapedValue(string attributeName)
}

// Table value function. [ and ] should added to function name only, not to its parameters
if (attributeName.Contains("(") && attributeName.Contains(")"))
if (attributeName.Contains('(') && attributeName.Contains(')'))
{
string functionName = attributeName[..attributeName.IndexOf("(", StringComparison.Ordinal)];
string functionParameters = attributeName[attributeName.IndexOf("(", StringComparison.Ordinal)..];
_sb.Append("[").Append(functionName).Append("]").Append(functionParameters);
_sb.Append('[').Append(functionName).Append(']').Append(functionParameters);
}
else
_sb.Append("[").Append(attributeName).Append("]");
_sb.Append('[').Append(attributeName).Append(']');
}
}
}
4 changes: 2 additions & 2 deletions 01-Core/Jinget.Core/ExpressionToSql/Internal/Select.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Select<T, R> : Query
private readonly int? _take;
private readonly Table _table;

public Where<T, R> Where(Expression<Func<T, bool>> predicate) => new Where<T, R>(this, predicate);
public Where<T, R> Where(Expression<Func<T, bool>> predicate) => new(this, predicate);

internal Select(Expression<Func<T, R>> select, int? take, Table table)
{
Expand All @@ -21,7 +21,7 @@ internal Select(Expression<Func<T, R>> select, int? take, Table table)
_table = table;
}

internal override (QueryBuilder query, Dictionary<string, object> parameters) ToSql(QueryBuilder query)
internal override (QueryBuilder query, Dictionary<string, object>? parameters) ToSql(QueryBuilder query)
{
if (_take.HasValue)
{
Expand Down
2 changes: 2 additions & 0 deletions 01-Core/Jinget.Core/ExpressionToSql/Internal/Table.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace Jinget.Core.ExpressionToSql.Internal
{
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public class Table
{
public virtual string Name { get; set; }

public string Schema { get; set; } = "[dbo]";
}
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
}
Loading

0 comments on commit dabca76

Please sign in to comment.