Skip to content

Commit

Permalink
Liquid filter support and l10n revision
Browse files Browse the repository at this point in the history
  • Loading branch information
gumbarros committed Dec 5, 2024
1 parent 75871e6 commit 52726a0
Show file tree
Hide file tree
Showing 40 changed files with 150 additions and 136 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dotnet_diagnostic.RCS1105.severity = error
dotnet_diagnostic.RCS1112.severity = error
dotnet_diagnostic.RCS1156.severity = error
dotnet_diagnostic.RCS1240.severity = error
dotnet_diagnostic.RCS1192.severity = error
dotnet_diagnostic.RCS1192.severity = none
csharp_style_namespace_declarations = file_scoped:error
resharper_arrange_object_creation_when_type_not_evident_highlighting = none
resharper_redundant_cast_highlighting=error
Expand Down
2 changes: 1 addition & 1 deletion src/Commons/Configuration/MasterDataServiceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public MasterDataServiceBuilder WithEntityProvider(string connectionString, Data
WithEntityProvider<OracleProvider>();
break;
default:
throw new ArgumentOutOfRangeException(nameof(provider), provider, "Provider is not currently supported.");
throw new ArgumentOutOfRangeException(nameof(provider), provider, @"Provider is not currently supported.");
}

return this;
Expand Down
4 changes: 2 additions & 2 deletions src/Commons/Data/Entity/Repository/EntityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public Task<bool> ExecuteBatchAsync(string script, Guid? connectionId = null)
{
var dataAccess = GetDataAccess(element.ConnectionId);
if (primaryKeys.Count == 0)
throw new ArgumentException("Your need at least one value at your primary keys.", nameof(primaryKeys));
throw new ArgumentException(@"Your need at least one value at your primary keys.", nameof(primaryKeys));

var totalOfRecords =
new DataAccessParameter("@qtdtotal", 1, DbType.Int32, 0, ParameterDirection.InputOutput);
Expand Down Expand Up @@ -163,7 +163,7 @@ public Task<bool> ExecuteBatchAsync(string script, Guid? connectionId = null)
Dictionary<string, object> primaryKeys)
{
if (primaryKeys.Count == 0)
throw new ArgumentException("Your need at least one value at your primary keys.", nameof(primaryKeys));
throw new ArgumentException(@"Your need at least one value at your primary keys.", nameof(primaryKeys));

var totalOfRecords =
new DataAccessParameter("@qtdtotal", 1, DbType.Int32, 0, ParameterDirection.InputOutput);
Expand Down
6 changes: 3 additions & 3 deletions src/Commons/Logging/BatchingLoggerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public TimeSpan FlushPeriod
{
if (value <= TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(value), $"{nameof(FlushPeriod)} must be positive.");
throw new ArgumentOutOfRangeException(nameof(value), @$"{nameof(FlushPeriod)} must be positive.");
}

_flushPeriod = value;
Expand All @@ -43,7 +43,7 @@ public int? BackgroundQueueSize
if (value < 0)
{
throw new ArgumentOutOfRangeException(nameof(value),
$"{nameof(BackgroundQueueSize)} must be non-negative.");
@$"{nameof(BackgroundQueueSize)} must be non-negative.");
}

_backgroundQueueSize = value;
Expand All @@ -61,7 +61,7 @@ public int? BatchSize
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException(nameof(value), $"{nameof(BatchSize)} must be positive.");
throw new ArgumentOutOfRangeException(nameof(value), @$"{nameof(BatchSize)} must be positive.");
}

_batchSize = value;
Expand Down
4 changes: 2 additions & 2 deletions src/Commons/Logging/File/FileLoggerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public int? FileSizeLimit
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException(nameof(value), $"{nameof(FileSizeLimit)} must be positive.");
throw new ArgumentOutOfRangeException(nameof(value), @$"{nameof(FileSizeLimit)} must be positive.");
}
_fileSizeLimit = value;
}
Expand All @@ -36,7 +36,7 @@ public int? RetainedFileCountLimit
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException(nameof(value), $"{nameof(RetainedFileCountLimit)} must be positive.");
throw new ArgumentOutOfRangeException(nameof(value), @$"{nameof(RetainedFileCountLimit)} must be positive.");
}
_retainedFileCountLimit = value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Commons/Util/EnumerableHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private static Type GetTypeFromField(FieldType fieldType)
case FieldType.UniqueIdentifier:
return typeof(Guid);
default:
throw new ArgumentOutOfRangeException(nameof(fieldType), fieldType, "Unknown FieldType");
throw new ArgumentOutOfRangeException(nameof(fieldType), fieldType, @"Unknown FieldType");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Commons/Util/StringManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ public static string FirstCharToUpper(this string input)
return input switch
{
null => throw new ArgumentNullException(nameof(input)),
"" => throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input)),
"" => throw new ArgumentException(@$"{nameof(input)} cannot be empty", nameof(input)),
_ => string.Concat(input[0].ToString().ToUpper(), input.AsSpan(1))
};
#else
Expand Down
3 changes: 3 additions & 0 deletions src/Core/Configuration/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using JJMasterData.Core.DataDictionary.Repository.Abstractions;
using JJMasterData.Core.DataManager.Exportation;
using JJMasterData.Core.DataManager.Exportation.Abstractions;
using JJMasterData.Core.Html;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -53,6 +54,8 @@ private static void AddMasterDataCoreServices(this IServiceCollection services)

services.AddScoped<IDataDictionaryRepository, SqlDataDictionaryRepository>();

services.AddTransient(typeof(HtmlTemplateRenderer<>));

services.AddScoped<IExcelWriter, ExcelWriter>();
services.AddScoped<ITextWriter, TextWriter>();

Expand Down
6 changes: 0 additions & 6 deletions src/Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@
<Compile Update="UI\Components\Widgets\HeadingSize.cs">
<DependentUpon>JJTitle.cs</DependentUpon>
</Compile>
<Compile Update="UI\Html\HtmlBuilder.Attributes.cs">
<DependentUpon>HtmlBuilder.cs</DependentUpon>
</Compile>
<Compile Update="UI\Html\HtmlBuilder.Children.cs">
<DependentUpon>HtmlBuilder.cs</DependentUpon>
</Compile>
<Compile Update="UI\Components\JsonComponentResult.cs">
<DependentUpon>ComponentResult.cs</DependentUpon>
</Compile>
Expand Down
2 changes: 1 addition & 1 deletion src/Core/DataDictionary/Models/FormElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public FormElement(Element element)
public FormElement(DataTable schema) : this()
{
if (schema == null)
throw new ArgumentNullException(nameof(schema), "DataTable schema cannot be null");
throw new ArgumentNullException(nameof(schema), @"DataTable schema cannot be null");

Name = schema.TableName;
TableName = schema.TableName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public IEnumerable<FormElementInfo> GetMetadataInfoList(DataDictionaryFilter fil
private string GetFullFileName(string elementName)
{
if (string.IsNullOrEmpty(elementName))
throw new ArgumentNullException(nameof(elementName), "Dictionary invalid");
throw new ArgumentNullException(nameof(elementName), @"Dictionary invalid");

if (!elementName.EndsWith(".json"))
elementName += ".json";
Expand Down
6 changes: 3 additions & 3 deletions src/Core/DataManager/IO/FormFilePathBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class FormFilePathBuilder(FormElement formElement)
public string GetFolderPath(FormElementField field, Dictionary<string, object> formValues)
{
if (field.DataFile == null)
throw new ArgumentException($"{nameof(FormElementField.DataFile)} not defined.", field.Name);
throw new ArgumentException(@$"{nameof(FormElementField.DataFile)} not defined.", field.Name);

//Pks concat with underline
string pkval = DataHelper.ParsePkValues(formElement, formValues, '_');
Expand All @@ -20,9 +20,9 @@ public string GetFolderPath(FormElementField field, Dictionary<string, object> f
string path = field.DataFile.FolderPath;

if (string.IsNullOrEmpty(path))
throw new ArgumentException($"{nameof(FormElementField.DataFile.FolderPath)} cannot be empty.", field.Name);
throw new ArgumentException(@$"{nameof(FormElementField.DataFile.FolderPath)} cannot be empty.", field.Name);

char separator = Path.DirectorySeparatorChar;
var separator = Path.DirectorySeparatorChar;

string appPath = FileIO.GetApplicationPath().TrimEnd(separator);

Expand Down
26 changes: 8 additions & 18 deletions src/Core/DataManager/Services/HtmlTemplateService.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Fluid;
using Fluid.Values;
using JJMasterData.Commons.Data.Entity.Repository.Abstractions;
using JJMasterData.Commons.Localization;
using JJMasterData.Commons.Util;
using JJMasterData.Core.DataDictionary;
using JJMasterData.Core.DataDictionary.Models.Actions;
using JJMasterData.Core.DataManager.Expressions;
using JJMasterData.Core.Html;
using JJMasterData.Core.UI.Components;
using JJMasterData.Core.UI.Html;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;

using static JJMasterData.Core.DataManager.Services.HtmlTemplateFunctions;
using static JJMasterData.Core.Html.HtmlTemplateHelper;

namespace JJMasterData.Core.DataManager.Services;

public class HtmlTemplateService(
HtmlTemplateRenderer<MasterDataResources> htmlTemplateRenderer,
IEntityRepository entityRepository,
IStringLocalizer<MasterDataResources> stringLocalizer,
ILogger<HtmlTemplateService> logger,
FluidParser fluidParser)
ILogger<HtmlTemplateService> logger)
{
public async Task<HtmlBuilder> RenderTemplate(
HtmlTemplateAction action,
Expand All @@ -49,7 +52,7 @@ public async Task<HtmlBuilder> RenderTemplate(
var html = new HtmlBuilder();
html.AppendDiv(div =>
{
div.WithCssClass("text-end").AppendComponent(new JJLinkButton(stringLocalizer)
div.WithCssClass("text-end").AppendComponent(new JJLinkButton
{
Icon = IconType.Print,
ShowAsButton = true,
Expand All @@ -69,19 +72,6 @@ public async Task<HtmlBuilder> RenderTemplate(

public ValueTask<string> RenderTemplate(string templateString, Dictionary<string, object> values)
{
if (!fluidParser.TryParse(templateString, out var template, out var error))
{
return new(error);
}

var context = new TemplateContext(values);

context.SetValue("isNullOrWhiteSpace", IsNullOrWhiteSpace);
context.SetValue("isNullOrEmpty",IsNullOrEmpty);
context.SetValue("substring", Substring);
context.SetValue("formatDate", FormatDate);
context.SetValue("localize", GetLocalizerFunction(stringLocalizer));

return template.RenderAsync(context);
return htmlTemplateRenderer.RenderTemplate(templateString, values);
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ public sealed class HtmlBuilderTag(HtmlTag htmlTag)
HtmlTag.Ol => "ol",
HtmlTag.Style => "style",
HtmlTag.Iframe => "iframe",
_ => throw new ArgumentOutOfRangeException(nameof(HtmlTag), HtmlTag, "HTML tag not implemented.")
_ => throw new ArgumentOutOfRangeException(nameof(HtmlTag), HtmlTag, @"HTML tag not implemented.")
};
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
#nullable enable

using System;
using System.Linq;
using System.Threading.Tasks;
using Fluid;
using Fluid.Values;
using JJMasterData.Commons.Localization;
using Microsoft.Extensions.Localization;

namespace JJMasterData.Core.DataManager.Services;
namespace JJMasterData.Core.Html;

public static class HtmlTemplateFunctions
public static class HtmlTemplateHelper
{
public static FunctionValue GetLocalizerFunction(IStringLocalizer<MasterDataResources> stringLocalizer)
public static FilterDelegate GetLocalizeFilter(IStringLocalizer stringLocalizer)
{
return (input, args, _) =>
{
var inputString = input.ToStringValue();
var argsValues = args.Values;

string localizedString;

if (argsValues is not null)
{
var localizerArgs = argsValues.Select(v => v.ToStringValue()).ToArray();

localizedString = stringLocalizer[inputString, localizerArgs.ToArray()].Value;
}
else
{
localizedString = stringLocalizer[inputString];
}
return new ValueTask<FluidValue>(new StringValue(localizedString));
};
}

public static FunctionValue GetLocalizeFunction(IStringLocalizer stringLocalizer)
{
var localize = new FunctionValue((args, _) =>
{
Expand Down Expand Up @@ -65,7 +90,7 @@ public static FunctionValue GetLocalizerFunction(IStringLocalizer<MasterDataReso

var substring = args.Count > 2
? str.Substring(startIndex, length)
: str.Substring(startIndex);
: str[startIndex..];

return new StringValue(substring);
});
Expand Down
30 changes: 30 additions & 0 deletions src/Core/Html/HtmlTemplateRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Fluid;
using Microsoft.Extensions.Localization;
using static JJMasterData.Core.Html.HtmlTemplateHelper;

namespace JJMasterData.Core.Html;

public class HtmlTemplateRenderer<TResource>(FluidParser fluidParser, IStringLocalizer<TResource> stringLocalizer)
{
public ValueTask<string> RenderTemplate(string templateString, Dictionary<string, object> values)
{
if (!fluidParser.TryParse(templateString, out var template, out var error))
{
return new(error);
}

var context = new TemplateContext(values);

context.Options.Filters.AddFilter("localize", GetLocalizeFilter(stringLocalizer));

context.SetValue("isNullOrWhiteSpace", IsNullOrWhiteSpace);
context.SetValue("isNullOrEmpty",IsNullOrEmpty);
context.SetValue("substring", Substring);
context.SetValue("formatDate", FormatDate);
context.SetValue("localize", GetLocalizeFunction(stringLocalizer));

return template.RenderAsync(context);
}
}
6 changes: 2 additions & 4 deletions src/Core/UI/Components/Controls/ComboBox/ComboBoxFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ namespace JJMasterData.Core.UI.Components;
internal sealed class ComboBoxFactory(
IFormValues formValues,
DataItemService dataItemService,
IStringLocalizer<MasterDataResources> stringLocalizer,
ILoggerFactory loggerFactory)
IStringLocalizer<MasterDataResources> stringLocalizer)
: IControlFactory<JJComboBox>
{
public JJComboBox Create()
{
return new JJComboBox(
formValues,
dataItemService,
stringLocalizer,
loggerFactory.CreateLogger<JJComboBox>());
stringLocalizer);
}

public JJComboBox Create(FormElement formElement, FormElementField field, ControlContext controlContext)
Expand Down
Loading

0 comments on commit 52726a0

Please sign in to comment.