Skip to content

Commit

Permalink
Merge branch 'main' into MetadataDictionaryConverter_BadTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz authored Jan 16, 2023
2 parents 526c09c + 39cf5ee commit 981de12
Show file tree
Hide file tree
Showing 44 changed files with 4,668 additions and 87 deletions.
16 changes: 14 additions & 2 deletions ecs-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Elastic.Ingest.Elasticsearc
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Elastic.Ingest.IntegrationTests", "tests\Elastic.Ingest.IntegrationTests\Elastic.Ingest.IntegrationTests.csproj", "{6A62695B-C2D2-47EA-8F74-232B87E835E7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elastic.CommonSchema.Serilog.Sink", "src\Elastic.CommonSchema.Serilog.Sink\Elastic.CommonSchema.Serilog.Sink.csproj", "{30080079-D3EE-4BDC-9BE9-9D1B3B2BEF8D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Elastic.CommonSchema.Log4net", "src\Elastic.CommonSchema.Log4net\Elastic.CommonSchema.Log4net.csproj", "{DD7D6E56-58DB-4E13-9DFC-AE031F1C31B3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Elastic.CommonSchema.Log4net.Tests", "tests\Elastic.CommonSchema.Log4net.Tests\Elastic.CommonSchema.Log4net.Tests.csproj", "{14BFAF67-8DB6-48D0-B57E-84767BA2A239}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elastic.CommonSchema.Serilog.Sinks.IntegrationTests", "tests\Elastic.CommonSchema.Serilog.Sink.IntegrationTests\Elastic.CommonSchema.Serilog.Sinks.IntegrationTests.csproj", "{622CC10E-B475-4649-8411-CABC31E7C252}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -209,9 +213,7 @@ Global
{D87AE73E-8112-444C-8F2F-CFBC4F738026}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D87AE73E-8112-444C-8F2F-CFBC4F738026}.Release|Any CPU.Build.0 = Release|Any CPU
{80D7CE12-D0C9-44E2-9BF9-5762D52ADA05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{80D7CE12-D0C9-44E2-9BF9-5762D52ADA05}.Debug|Any CPU.Build.0 = Debug|Any CPU
{80D7CE12-D0C9-44E2-9BF9-5762D52ADA05}.Release|Any CPU.ActiveCfg = Release|Any CPU
{80D7CE12-D0C9-44E2-9BF9-5762D52ADA05}.Release|Any CPU.Build.0 = Release|Any CPU
{2AC1BCC2-335C-4B1C-B279-B34452969BFF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2AC1BCC2-335C-4B1C-B279-B34452969BFF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2AC1BCC2-335C-4B1C-B279-B34452969BFF}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -236,6 +238,14 @@ Global
{6A62695B-C2D2-47EA-8F74-232B87E835E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6A62695B-C2D2-47EA-8F74-232B87E835E7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6A62695B-C2D2-47EA-8F74-232B87E835E7}.Release|Any CPU.Build.0 = Release|Any CPU
{30080079-D3EE-4BDC-9BE9-9D1B3B2BEF8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{30080079-D3EE-4BDC-9BE9-9D1B3B2BEF8D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{30080079-D3EE-4BDC-9BE9-9D1B3B2BEF8D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{30080079-D3EE-4BDC-9BE9-9D1B3B2BEF8D}.Release|Any CPU.Build.0 = Release|Any CPU
{622CC10E-B475-4649-8411-CABC31E7C252}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{622CC10E-B475-4649-8411-CABC31E7C252}.Debug|Any CPU.Build.0 = Debug|Any CPU
{622CC10E-B475-4649-8411-CABC31E7C252}.Release|Any CPU.ActiveCfg = Release|Any CPU
{622CC10E-B475-4649-8411-CABC31E7C252}.Release|Any CPU.Build.0 = Release|Any CPU
{DD7D6E56-58DB-4E13-9DFC-AE031F1C31B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DD7D6E56-58DB-4E13-9DFC-AE031F1C31B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DD7D6E56-58DB-4E13-9DFC-AE031F1C31B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -283,6 +293,8 @@ Global
{D6F0D170-39D7-4868-86EE-990B6B05C14D} = {7610B796-BB3E-4CB2-8296-79BBFF6D23FC}
{68128AE4-350C-4FB2-A971-C9272A1F3829} = {7610B796-BB3E-4CB2-8296-79BBFF6D23FC}
{6A62695B-C2D2-47EA-8F74-232B87E835E7} = {3582B07D-C2B0-49CC-B676-EAF806EB010E}
{30080079-D3EE-4BDC-9BE9-9D1B3B2BEF8D} = {7610B796-BB3E-4CB2-8296-79BBFF6D23FC}
{622CC10E-B475-4649-8411-CABC31E7C252} = {3582B07D-C2B0-49CC-B676-EAF806EB010E}
{DD7D6E56-58DB-4E13-9DFC-AE031F1C31B3} = {7610B796-BB3E-4CB2-8296-79BBFF6D23FC}
{14BFAF67-8DB6-48D0-B57E-84767BA2A239} = {3582B07D-C2B0-49CC-B676-EAF806EB010E}
EndGlobalSection
Expand Down
1 change: 0 additions & 1 deletion examples/aspnetcore-with-serilog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

- Elastic.CommonSchema.Serilog
- ASP.NET Core
- netcoreapp3.0

## Configuration

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="RazorLight" Version="2.3.0" />
<PackageReference Include="Cogito.Json.Schema.Validation" Version="1.0.1" />
<PackageReference Include="CsQuery.Core" Version="2.0.1" />
<PackageReference Include="JsonDiffPatch" Version="2.0.49" />
<PackageReference Include="JsonDiffPatch.Net" Version="2.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="RazorLight.Unofficial" Version="2.0.0-beta1.3" />
<PackageReference Include="ShellProgressBar" Version="5.0.0" />
<PackageReference Include="YamlDotNet" Version="6.0.0" />
</ItemGroup>
Expand Down
4 changes: 3 additions & 1 deletion src/Elastic.CommonSchema.Generator/FileGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public static void Generate(CommonSchemaTypesProjection commonSchemaTypesProject
{
{ m => Generate(m, "EcsDocument"), "Base ECS Document" },
{ m => Generate(m, "EcsDocumentJsonConverter"), "Base ECS Document Json Converter" },
{ m => Generate(m, "LogTemplateProperties"), "Strongly types ECS fields supported in log templates" },
{ m => Generate(m, "PropDispatch"), "ECS key value setter generation" },
{ m => Generate(m, "EcsJsonContext"), "Ecs System Text Json Source Generators" },
{ m => Generate(m, "FieldSets"), "Field Sets" },
{ m => Generate(m, "Entities"), "Entities" },
Expand All @@ -35,7 +37,7 @@ public static void Generate(CommonSchemaTypesProjection commonSchemaTypesProject
};

using (var progressBar = new ProgressBar(actions.Count, "Generating code",
new ProgressBarOptions { BackgroundColor = ConsoleColor.DarkGray }))
new ProgressBarOptions { BackgroundColor = ConsoleColor.DarkGray }))
{
foreach (var kv in actions)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public static class ProjectionTypeExtensions
public static string PascalCase(this string s) => new CultureInfo("en-US")
.TextInfo
.ToTitleCase(s.ToLowerInvariant())
.Replace("@", string.Empty)
.Replace("_", string.Empty)
.Replace(".", string.Empty);

Expand All @@ -23,6 +24,35 @@ public static string GetClrType(this Field field)
return field.Normalize.Contains("array") ? $"{baseType}[]" : baseType;
}

public static string GetCastFromObject(this Field field)
{
if (field.Normalize.Contains("array")) return null;
switch (field.Type)
{
case FieldType.Keyword:
case FieldType.ConstantKeyword:
case FieldType.Flattened:
case FieldType.MatchOnlyText:
case FieldType.Wildcard:
case FieldType.Text:
case FieldType.Ip:
return "TrySetString";
case FieldType.Boolean:
return "TrySetBool";
case FieldType.ScaledFloat:
case FieldType.Float:
return "TrySetFloat";
case FieldType.Long:
return "TrySetLong";
case FieldType.Integer:
return "TrySetInt";
case FieldType.Date:
return "TrySetDateTimeOffset";
default: return null;
}

}

private static string GetClrType(this FieldType fieldType)
{
switch (fieldType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ protected PropertyReference(string localPath, string fullPath)

public string LocalPath { get; }
public string FullPath { get; }
public string LogTemplateAlternative => FullPath.PascalCase();

public abstract string Description { get; }
public abstract string Example { get; }
Expand Down Expand Up @@ -79,11 +80,12 @@ public class ValueTypePropertyReference : PropertyReference
public ValueTypePropertyReference(string parentPath, string fullPath, Field field) : base(parentPath, fullPath)
{
ClrType = field.GetClrType();
CastFromObject = field.GetCastFromObject();
Description = GetFieldDescription(field);
Example = field.Example?.ToString() ?? string.Empty;

}

public string CastFromObject { get; }
public string ClrType { get; }
public override string Description { get; }
public override string Example { get; }
Expand Down
3 changes: 3 additions & 0 deletions src/Elastic.CommonSchema.Generator/Projection/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class FieldSetBaseClass

public Dictionary<string, PropertyReference> Properties { get; } = new();

public IEnumerable<ValueTypePropertyReference> SettableProperties =>
ValueProperties.Where(p => !string.IsNullOrEmpty(p.CastFromObject));

public IEnumerable<ValueTypePropertyReference> ValueProperties =>
Properties.Values.OfType<ValueTypePropertyReference>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ namespace Elastic.CommonSchema

/// <summary>
/// Container for additional metadata against this event.
/// <para/>
/// When working with unknown fields use <see cref="SetAnyField"/>. <br/>
/// <para> This will try to assign valid ECS fields to their respective property
/// Failing that it will assign strings to <see cref="Labels"/> and everything else to <see cref="Metadata"/> </para>
/// </summary>
[JsonPropertyName("metadata"), DataMember(Name = "metadata")]
[JsonConverter(typeof(MetadataDictionaryConverter))]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
@using RazorLight
@using System
@using Generator
@inherits Elastic.CommonSchema.Generator.Views.CodeTemplatePage<Elastic.CommonSchema.Generator.Projection.CommonSchemaTypesProjection>
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

/*
IMPORTANT NOTE
==============
This file has been generated.
If you wish to submit a PR please modify the original csharp file and submit the PR with that change. Thanks!
*/

// ReSharper disable RedundantUsingDirective
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
using Elastic.CommonSchema.Serialization;
using static Elastic.CommonSchema.PropDispatch;

namespace Elastic.CommonSchema
{
public static class LogTemplateProperties
{
@foreach (var prop in Model.Base.BaseFieldSet.SettableProperties)
{
<text> public static string @prop.LogTemplateAlternative = nameof(@prop.LogTemplateAlternative);
</text>
}
@foreach (var entity in Model.EntityClasses)
{
@foreach (var prop in entity.BaseFieldSet.SettableProperties)
{
<text> public static string @prop.LogTemplateAlternative = nameof(@prop.LogTemplateAlternative);
</text>

}
}

public static readonly HashSet@(Raw("<string>")) All = new()
{
@foreach (var prop in Model.Base.BaseFieldSet.SettableProperties)
{
<text> "@prop.FullPath", @prop.LogTemplateAlternative,
</text>

}
@foreach (var entity in Model.EntityClasses)
{
@foreach (var prop in entity.BaseFieldSet.SettableProperties)
{
<text> "@prop.FullPath", @prop.LogTemplateAlternative,
</text>

}
}
};
}

}
128 changes: 128 additions & 0 deletions src/Elastic.CommonSchema.Generator/Views/PropDispatch.Generated.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
@using RazorLight
@using System
@using Generator
@using System.Linq;
@inherits Elastic.CommonSchema.Generator.Views.CodeTemplatePage<Elastic.CommonSchema.Generator.Projection.CommonSchemaTypesProjection>
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

/*
IMPORTANT NOTE
==============
This file has been generated.
If you wish to submit a PR please modify the original csharp file and submit the PR with that change. Thanks!
*/

// ReSharper disable RedundantUsingDirective
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
using Elastic.CommonSchema.Serialization;
using static Elastic.CommonSchema.PropDispatch;

namespace Elastic.CommonSchema
{
///<inheritdoc cref="@Model.Base.BaseFieldSet.Name"/>
public partial class @Model.Base.Name : @Model.Base.BaseFieldSet.Name
{
/// <summary>
/// Set ECS fields by name on <see cref="EcsDocument"/>.
/// <para>Allows valid ECS fields to be set from log message templates.</para>
/// Given <paramref name="value"/>'s type matches the corresponding property on <see cref="EcsDocument"/>
/// <para></para>
/// <para>See <see cref="LogTemplateProperties"/> for a strongly typed list of valid ECS log template properties</para>
/// <para>If its not a supported ECS log template property or using the wrong type:</para>
/// <list type="bullet">
/// <item>Assigns strings to <see cref="EcsDocument.Labels"/> on <see cref="EcsDocument"/></item>
/// <item>Assigns everything else to <see cref="EcsDocument.Metadata"/> on <see cref="EcsDocument"/></item>
/// </list>
/// </summary>
/// <para@(Raw("m")) name="path">Either a supported ECS Log Template property or any key</para@(Raw("m"))>
/// <para@(Raw("m")) name="value">The value to persist</para@(Raw("m"))>
public void SetLogMessageProperty(string path, object value)
{
var assigned = LogTemplateProperties.All.Contains(path) && TrySet(this, path, value);
if (!assigned)
SetMetaOrLabel(this, path, value);
}
}
internal static partial class PropDispatch
{
public static bool TrySet(EcsDocument document, string path, object value)
{
switch (path)
{
@foreach (var prop in Model.Base.BaseFieldSet.SettableProperties)
{
<text> case "@prop.FullPath":
case "@prop.LogTemplateAlternative":
</text>
}
return TrySet@(@Model.Base.Name)(document, path, value);
@foreach (var entity in Model.EntityClasses)
{
if (!entity.BaseFieldSet.SettableProperties.Any())
{
continue;
}
@foreach (var prop in entity.BaseFieldSet.SettableProperties)
{
<text> case "@prop.FullPath":
case "@prop.LogTemplateAlternative":
</text>
}
<text> return TrySet@(@entity.Name)(document, path, value);
</text>
}
default:
SetMetaOrLabel(document, path, value);
return true;
}
}

public static bool TrySet@(@Model.Base.Name)(EcsDocument document, string path, object value)
{
Func@(Raw("<"))@(Model.Base.Name), object, bool@(Raw(">")) assign = path switch
{
@foreach (var prop in Model.Base.BaseFieldSet.SettableProperties)
{
<text> "@prop.FullPath" => static (e, v) => @(prop.CastFromObject)(e, v, static (ee, p) => ee.@(prop.Name) = p),
"@prop.LogTemplateAlternative" => static (e, v) => @(prop.CastFromObject)(e, v, static (ee, p) => ee.@(prop.Name) = p),
</text>
}
_ => null
};
return assign != null && assign(document, value);
}
@foreach (var entity in Model.EntityClasses)
{
<text>
public static bool TrySet@(entity.Name)(EcsDocument document, string path, object value)
{
Func@(Raw("<"))@(entity.Name), object, bool@(Raw(">")) assign = path switch
{
@foreach (var prop in entity.BaseFieldSet.SettableProperties)
{
<text> "@prop.FullPath" => static (e, v) => @(prop.CastFromObject)(e, v, static (ee, p) => ee.@(prop.Name) = p),
"@prop.LogTemplateAlternative" => static (e, v) => @(prop.CastFromObject)(e, v, static (ee, p) => ee.@(prop.Name) = p),
</text>
}
_ => null
};
if (assign == null) return false;

var entity = document.@(entity.Name) ?? new @(entity.Name)();
var assigned = assign(entity, value);
if (assigned) document.@(entity.Name) = entity;
return assigned;
}
</text>
}
}
}
Loading

0 comments on commit 981de12

Please sign in to comment.