Skip to content

Commit aaa1eed

Browse files
committed
Ensure ecs.version is always written (#232)
1 parent de65897 commit aaa1eed

20 files changed

+384
-220
lines changed

src/Elastic.CommonSchema.Generator/Elastic.CommonSchema.Generator.csproj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,28 @@
1818
<PackageReference Include="ShellProgressBar" Version="5.0.0" />
1919
<PackageReference Include="YamlDotNet" Version="6.0.0" />
2020
</ItemGroup>
21+
22+
<ItemGroup>
23+
<None Remove="Views\EcsDocument.Generated.cshtml" />
24+
<Content Include="Views\EcsDocument.Generated.cshtml" />
25+
<None Remove="Views\EcsDocumentJsonConverter.Generated.cshtml" />
26+
<Content Include="Views\EcsDocumentJsonConverter.Generated.cshtml" />
27+
<None Remove="Views\EcsJsonContext.Generated.cshtml" />
28+
<Content Include="Views\EcsJsonContext.Generated.cshtml" />
29+
<None Remove="Views\EcsLogJsonConverter.Generated.cshtml" />
30+
<None Remove="Views\Entities.Generated.cshtml" />
31+
<Content Include="Views\Entities.Generated.cshtml" />
32+
<None Remove="Views\FieldSets.Generated.cshtml" />
33+
<Content Include="Views\FieldSets.Generated.cshtml" />
34+
<None Remove="Views\IndexComponents.Generated.cshtml" />
35+
<Content Include="Views\IndexComponents.Generated.cshtml" />
36+
<None Remove="Views\IndexTemplates.Generated.cshtml" />
37+
<Content Include="Views\IndexTemplates.Generated.cshtml" />
38+
<None Remove="Views\InlineObjects.Generated.cshtml" />
39+
<Content Include="Views\InlineObjects.Generated.cshtml" />
40+
<None Remove="Views\LogTemplateProperties.Generated.cshtml" />
41+
<Content Include="Views\LogTemplateProperties.Generated.cshtml" />
42+
<None Remove="Views\PropDispatch.Generated.cshtml" />
43+
<Content Include="Views\PropDispatch.Generated.cshtml" />
44+
</ItemGroup>
2145
</Project>

src/Elastic.CommonSchema.Generator/FileGenerator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ public static void Generate(CommonSchemaTypesProjection commonSchemaTypesProject
2727
{ m => Generate(m, "EcsDocument"), "Base ECS Document" },
2828

2929
{ m => Generate(m, "EcsDocumentJsonConverter", "Serialization"), "Base ECS Document Json Converter" },
30-
{ m => Generate(m, "EcsLogJsonConverter", "Serialization"), "Log fieldset needs a custom converter to omit level on write" },
30+
{ m => Generate(m, "EcsShouldSerializeJsonConverter", "Serialization"), "Generate converters for entities with optional serialization behaviour" },
3131
{ m => Generate(m, "EcsJsonContext", "Serialization"), "Ecs System Text Json Source Generators" },
3232

3333
{ m => Generate(m, "LogTemplateProperties"), "Strongly types ECS fields supported in log templates" },
3434
{ m => Generate(m, "PropDispatch"), "ECS key value setter generation" },
3535
{ m => Generate(m, "FieldSets"), "Field Sets" },
3636
{ m => Generate(m, "Entities"), "Entities" },
37+
{ m => Generate(m, "Entities.ShouldSerialize"), "Generate ShouldSerialize special handling for Log and ECS" },
3738
{ m => Generate(m, "InlineObjects"), "Inline Objects" },
3839
{ m => Generate(m, "IndexTemplates"), "Elasticsearch index templates" },
3940
{ m => Generate(m, "IndexComponents"), "Elasticsearch index components" },

src/Elastic.CommonSchema.Generator/Projection/TypeProjector.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public class CommonSchemaTypesProjection
1919
public IReadOnlyCollection<FieldSetBaseClass> FieldSets { get; set; }
2020
public IReadOnlyCollection<EntityClass> EntityClasses { get; set; }
2121
public EntityClass Base { get; set; }
22-
public EntityClass Log => EntityClasses.First(e => e.Name == "Log");
22+
public IReadOnlyDictionary<EntityClass, string[]> EntitiesWithPropertiesAtRoot { get; set; }
23+
//public EntityClass Log => EntityClasses.First(e => e.Name == "Log");
2324
public IReadOnlyCollection<EntityClass> NestedEntityClasses { get; set; }
2425
public IReadOnlyCollection<InlineObject> InlineObjects { get; set; }
2526
public ReadOnlyCollection<string> Warnings { get; set; }
@@ -103,6 +104,11 @@ public CommonSchemaTypesProjection CreateProjection()
103104
GitRef = Schema.GitRef,
104105
FieldSets = FieldSetsBaseClasses.Values.Where(e=>e.FieldSet.Root != true || e.FieldSet.Name == "base" ).ToList(),
105106
EntityClasses = EntityClasses.Values.Where(e=>e.Name != "EcsDocument" && e.BaseFieldSet.FieldSet.Root != true).ToList(),
107+
EntitiesWithPropertiesAtRoot = new Dictionary<EntityClass, string[]>
108+
{
109+
{ EntityClasses.Values.First(e=>e.Name == "Log"), new []{"level"}},
110+
{ EntityClasses.Values.First(e=>e.Name == "Ecs"), new []{"version"}},
111+
},
106112
Base = EntityClasses.Values.First(e=>e.Name == "EcsDocument"),
107113
InlineObjects = InlineObjects.Values.ToList(),
108114
NestedEntityClasses = nestedEntityTypes.Values.ToList(),

src/Elastic.CommonSchema.Generator/Projection/Types.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public EntityClass(string name, FieldSetBaseClass baseFieldSet)
7474

7575
public string Name { get; }
7676
public FieldSetBaseClass BaseFieldSet { get; }
77-
public bool Partial => Name is "EcsDocument" or "Log";
77+
public bool Partial => Name is "EcsDocument" or "Log" or "Ecs";
7878

7979
public Dictionary<string, EntityPropertyReference> EntityReferences { get; } = new();
8080

src/Elastic.CommonSchema.Generator/Views/EcsDocumentJsonConverter.Generated.cshtml

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ namespace Elastic.CommonSchema.Serialization
2525
ref Utf8JsonReader reader,
2626
TBase ecsEvent,
2727
ref DateTimeOffset? timestamp,
28-
ref string loglevel
28+
ref string loglevel,
29+
ref string ecsVersion
2930
)
3031
{
3132
var propertyName = reader.GetString();
3233
reader.Read();
3334
return propertyName switch
3435
{
3536
"log.level" => ReadString(ref reader, ref loglevel),
37+
"ecs.version" => ReadString(ref reader, ref ecsVersion),
3638
"metadata" => ReadProp@(Raw("<MetadataDictionary>"))(ref reader, "metadata", ecsEvent, (b, v) => b.Metadata = v),
3739
@foreach (var property in Model.Base.BaseFieldSet.ValueProperties)
3840
{
@@ -63,31 +65,6 @@ namespace Elastic.CommonSchema.Serialization
6365
: false
6466
};
6567
}
66-
@{
67-
var manualLogFields = new List<string>() { "level" };
68-
}
69-
public void WriteLogEntity(Utf8JsonWriter writer, Log value) {
70-
if (value == null) return;
71-
// only write the log object if it has values other then log.level
72-
if (
73-
@foreach (var field in Model.Log.BaseFieldSet.ValueProperties)
74-
{
75-
if (manualLogFields.Contains(field.JsonProperty))
76-
{
77-
continue;
78-
}
79-
<text> value.@field.Name == null &&
80-
</text>
81-
}
82-
@foreach (var property in Model.Log.BaseFieldSet.InlineObjectProperties)
83-
{
84-
<text> value.@(property.Name) == null &&
85-
</text>
86-
}
87-
true) return;
88-
89-
WriteProp(writer, "log", value, EcsJsonContext.Default.Log);
90-
}
9168

9269
public override void Write(Utf8JsonWriter writer, TBase value, JsonSerializerOptions options)
9370
{
@@ -101,11 +78,13 @@ namespace Elastic.CommonSchema.Serialization
10178
WriteTimestamp(writer, value);
10279
WriteLogLevel(writer, value);
10380
WriteMessage(writer, value);
81+
WriteEcsVersion(writer, value);
10482
WriteLogEntity(writer, value.Log);
83+
WriteEcsEntity(writer, value.Ecs);
10584

10685
// Base fields
10786
@{
108-
var manualFields = new List<string>() { "@timestamp", "message", "log" };
87+
var manualFields = new List<string>() { "@timestamp", "message", "log", "ecs" };
10988
}
11089
@foreach (var field in Model.Base.BaseFieldSet.ValueProperties)
11190
{

src/Elastic.CommonSchema.Generator/Views/EcsLogJsonConverter.Generated.cshtml

Lines changed: 0 additions & 95 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
@using System.Collections.Generic
2+
@using Generator
3+
@using System.Linq
4+
@inherits Elastic.CommonSchema.Generator.Views.CodeTemplatePage<Elastic.CommonSchema.Generator.Projection.CommonSchemaTypesProjection>
5+
// Licensed to Elasticsearch B.V under one or more agreements.
6+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
7+
// See the LICENSE file in the project root for more information
8+
9+
/*
10+
IMPORTANT NOTE
11+
==============
12+
This file has been generated.
13+
If you wish to submit a PR please modify the original csharp file and submit the PR with that change. Thanks!
14+
*/
15+
16+
using System;
17+
using System.Collections.Generic;
18+
using System.Text.Json;
19+
20+
namespace Elastic.CommonSchema.Serialization;
21+
22+
@foreach (var kv in Model.EntitiesWithPropertiesAtRoot)
23+
{
24+
var entity = kv.Key;
25+
<text>
26+
public partial class Ecs@(entity.Name)JsonConverter : EcsShouldSerializeJsonConverter@(Raw("<"))@(entity.Name)@Raw(">")
27+
{
28+
protected override bool ReadProperties(ref Utf8JsonReader reader, @entity.Name ecsEvent)
29+
{
30+
var propertyName = reader.GetString();
31+
reader.Read();
32+
return propertyName switch
33+
{
34+
@foreach (var property in entity.BaseFieldSet.ValueProperties)
35+
{
36+
var name = property.JsonProperty;
37+
<text> "@(name)" => ReadProp@(Raw(property.ReadJsonType))(ref reader, "@name", ecsEvent, (b, v) => b.@(property.Name) = v),
38+
</text>
39+
}
40+
@foreach (var property in entity.BaseFieldSet.InlineObjectProperties)
41+
{
42+
var name = property.JsonProperty;
43+
<text> "@(name)" => ReadProp<@(Raw(property.InlineObject.Name))>(ref reader, "@name", ecsEvent, (b, v) => b.@(property.Name) = v),
44+
</text>
45+
}
46+
_ => false
47+
};
48+
}
49+
50+
public override void Write(Utf8JsonWriter writer, @entity.Name value, JsonSerializerOptions options)
51+
{
52+
if (value == null || !value.ShouldSerialize)
53+
{
54+
writer.WriteNullValue();
55+
return;
56+
}
57+
writer.WriteStartObject();
58+
59+
@foreach (var field in entity.BaseFieldSet.ValueProperties)
60+
{
61+
if (kv.Value.Contains(field.JsonProperty))
62+
{
63+
continue;
64+
}
65+
<text> WriteProp(writer, "@field.JsonProperty", value.@field.Name);
66+
</text>
67+
}
68+
@foreach (var property in entity.BaseFieldSet.InlineObjectProperties)
69+
{
70+
var name = property.JsonProperty;
71+
<text> WriteProp(writer, "@(name)", value.@(property.Name));
72+
</text>
73+
}
74+
75+
writer.WriteEndObject();
76+
}
77+
}
78+
</text>
79+
80+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
@using System.Collections.Generic
2+
@using System.Globalization
3+
@using System.Linq
4+
@using Generator
5+
@inherits Elastic.CommonSchema.Generator.Views.CodeTemplatePage<Elastic.CommonSchema.Generator.Projection.CommonSchemaTypesProjection>
6+
// Licensed to Elasticsearch B.V under one or more agreements.
7+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
8+
// See the LICENSE file in the project root for more information
9+
10+
/*
11+
IMPORTANT NOTE
12+
==============
13+
This file has been generated.
14+
If you wish to submit a PR please modify the original csharp file and submit the PR with that change. Thanks!
15+
*/
16+
17+
using System;
18+
using System.Collections.Generic;
19+
using System.Text.Json;
20+
using System.Text.Json.Serialization;
21+
22+
namespace Elastic.CommonSchema;
23+
24+
@foreach (var kv in Model.EntitiesWithPropertiesAtRoot)
25+
{
26+
var entity = kv.Key;
27+
var fields = entity.BaseFieldSet.ValueProperties.Where(field => !kv.Value.Contains(field.JsonProperty)).ToList();
28+
var emptyCheck = string.Join(" || ", fields.Select(f => $"{f.Name} != null").ToList());
29+
if (string.IsNullOrEmpty(emptyCheck))
30+
{
31+
emptyCheck = "false";
32+
}
33+
34+
<text> public partial class @entity.Name
35+
{
36+
[JsonIgnore]
37+
internal bool ShouldSerialize => @Raw(emptyCheck);
38+
}
39+
</text>
40+
}

src/Elastic.CommonSchema/Entities.Generated.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public class Dns : DnsFieldSet {
243243
}
244244

245245
///<inheritdoc cref="EcsFieldSet"/>
246-
public class Ecs : EcsFieldSet {
246+
public partial class Ecs : EcsFieldSet {
247247
}
248248

249249
///<inheritdoc cref="ElfFieldSet"/>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
/*
6+
IMPORTANT NOTE
7+
==============
8+
This file has been generated.
9+
If you wish to submit a PR please modify the original csharp file and submit the PR with that change. Thanks!
10+
*/
11+
12+
using System;
13+
using System.Collections.Generic;
14+
using System.Text.Json;
15+
using System.Text.Json.Serialization;
16+
17+
namespace Elastic.CommonSchema;
18+
19+
public partial class Log
20+
{
21+
[JsonIgnore]
22+
internal bool ShouldSerialize => FilePath != null || Logger != null || OriginFileLine != null || OriginFileName != null || OriginFunction != null;
23+
}
24+
public partial class Ecs
25+
{
26+
[JsonIgnore]
27+
internal bool ShouldSerialize => false;
28+
}

0 commit comments

Comments
 (0)