tools/Elastic.CommonSchema.Generator/Views/PropDispatch.Generated.cshtml (182 lines of code) (raw):

@* ReSharper disable once RedundantUsingDirective *@ @using System @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="BaseFieldSet.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 AssignField(string path, object value) { var assigned = LogTemplateProperties.All.Contains(path) && TrySet(this, path, value); if (!assigned && LogTemplateEntities.All.Contains(path)) assigned = TrySetEntity(this, path, value); if (!assigned) SetMetaOrLabel(this, path, value); } } internal static partial class PropDispatch { internal static bool TrySetEntity(EcsDocument document, string path, object value) { bool TypeCheck(@Raw("Dictionary<string, object>") templatedObject, string typeName) => templatedObject.TryGetValue("$type", out var t) && t is string s && s == typeName; switch (path.ToLowerInvariant()) { @foreach (var property in Model.Base.EntityProperties) { var variableName = $"@{property.Entity.Name.ToLowerInvariant()}"; <text> case "@(property.Entity.Name.ToLowerInvariant())" when value is @(property.Entity.Name) @variableName: document.@property.Name = @variableName; return true; case "@(property.Entity.Name.ToLowerInvariant())" when value is @Raw("Dictionary<string, object>") @variableName: if (!TypeCheck(@variableName, LogTemplateEntities.@property.Entity.Name)) return false; foreach (var kvp in @variableName) { if (kvp.Value == null || kvp.Key == "$type") continue; if (!TrySet@(property.Entity.Name)(document, $"@(property.Entity.Name){kvp.Key}", kvp.Value)) SetMetaOrLabel(document, $"@(property.Entity.Name){kvp.Key}", kvp.Value); } return true; </text> } @foreach (var entity in Model.EntityClasses) { var variableName = $"@{entity.Name.ToLowerInvariant()}"; <text> case "@(entity.Name.ToLowerInvariant())" when value is @(entity.Name) @variableName: document.@entity.Name = @variableName; return true; case "@(entity.Name.ToLowerInvariant())" when value is @Raw("Dictionary<string, object>") @variableName: if (!TypeCheck(@variableName, LogTemplateEntities.@entity.Name)) return false; foreach (var kvp in @variableName) { if (kvp.Value == null || kvp.Key == "$type") continue; if (!TrySet@(entity.Name)(document, $"@(entity.Name){kvp.Key}", kvp.Value)) SetMetaOrLabel(document, $"@(entity.Name){kvp.Key}", kvp.Value); } return true; </text> } default: return false; } } internal static bool TrySet(EcsDocument document, string path, object value) { switch (path) { @foreach (var prop in Model.Base.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.SettableProperties.Any()) { continue; } @foreach (var prop in entity.SettableProperties) { <text> case "@prop.FullPath": case "@prop.LogTemplateAlternative": </text> } <text> return TrySet@(@entity.Name)(document, path, value); </text> } default: return false; } } 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.DispatchProperties) { <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 dispatch in Model.AssignablePropDispatches) { var entity = dispatch.Entity; <text> public static Func@(Raw("<"))@(dispatch.FuncTarget), object, bool@(Raw(">")) TryAssign@(dispatch.AssignEntity)(string path) { Func@(Raw("<"))@(dispatch.FuncTarget), object, bool@(Raw(">")) assign = path switch { @foreach (var prop in dispatch.AssignableProperties) { if (!prop.IsEntityDispatch) { <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> } else { <text> "@prop.FullPath" => static (e, v) => @(prop.CastFromObject)("@(prop.JsonProperty)")(e.@(prop.ContainerPath) ??= new @(prop.ContainerPathEntity)(),v), "@prop.LogTemplateAlternative" => static (e, v) => @(prop.CastFromObject)("@(prop.JsonProperty)")(e.@(prop.ContainerPath) ??= new @(prop.ContainerPathEntity)(),v), </text> } } _ => null }; return assign; } public static bool TrySet@(dispatch.AssignEntity)(@dispatch.AssignParameter document, string path, object value) { var assign = TryAssign@(dispatch.AssignEntity)(path); if (assign == null) return false; var entity = document.@(dispatch.AssignTarget) ?? new @(entity.Name)(); var assigned = assign(entity, value); if (assigned) document.@(dispatch.AssignTarget) = entity; return assigned; } </text> } } }