Skip to content

Commit 1fb3b1c

Browse files
committed
Updated parsers
1 parent 723a8a9 commit 1fb3b1c

File tree

3 files changed

+140
-3
lines changed

3 files changed

+140
-3
lines changed

src/Elastic.Markdown/Helpers/SemVersion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Elastic.Markdown.Helpers;
1111
/// <summary>
1212
/// A semver2 compatible version.
1313
/// </summary>
14-
public sealed class SemVersion :
14+
public class SemVersion :
1515
IEquatable<SemVersion>,
1616
IComparable<SemVersion>,
1717
IComparable

src/Elastic.Markdown/Myst/FrontMatterParser.cs

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ public class YamlFrontMatter
2727

2828

2929
[YamlMember(Alias = "applies")]
30-
public DeploymentType? AppliesTo { get; set; }
30+
public Deployment? AppliesTo { get; set; }
3131
}
3232

3333
[YamlSerializable]
34-
public class DeploymentType
34+
public class Deployment
3535
{
3636
[YamlMember(Alias = "self")]
3737
public SelfManagedDeployment? SelfManaged { get; set; }
@@ -40,6 +40,11 @@ public class DeploymentType
4040
public CloudManagedDeployment? Cloud { get; set; }
4141
}
4242

43+
public class AllVersions() : SemVersion(9999, 9999, 9999)
44+
{
45+
public static AllVersions Instance { get; } = new ();
46+
}
47+
4348
[YamlSerializable]
4449
public class SelfManagedDeployment
4550
{
@@ -51,6 +56,13 @@ public class SelfManagedDeployment
5156

5257
[YamlMember(Alias = "eck")]
5358
public SemVersion? Eck { get; set; }
59+
60+
public static SelfManagedDeployment All { get; } = new()
61+
{
62+
Stack = AllVersions.Instance,
63+
Ece = AllVersions.Instance,
64+
Eck = AllVersions.Instance
65+
};
5466
}
5567

5668
[YamlSerializable]
@@ -61,6 +73,13 @@ public class CloudManagedDeployment
6173

6274
[YamlMember(Alias = "serverless")]
6375
public SemVersion? Serverless { get; set; }
76+
77+
public static CloudManagedDeployment All { get; } = new()
78+
{
79+
Ess = AllVersions.Instance,
80+
Serverless = AllVersions.Instance
81+
};
82+
6483
}
6584

6685
public static class FrontMatterParser
@@ -72,6 +91,8 @@ public static YamlFrontMatter Deserialize(string yaml)
7291
var deserializer = new StaticDeserializerBuilder(new YamlFrontMatterStaticContext())
7392
.IgnoreUnmatchedProperties()
7493
.WithTypeConverter(new SemVersionConverter())
94+
.WithTypeConverter(new CloudManagedSerializer())
95+
.WithTypeConverter(new SelfManagedSerializer())
7596
.Build();
7697

7798
var frontMatter = deserializer.Deserialize<YamlFrontMatter>(input);
@@ -87,6 +108,10 @@ public class SemVersionConverter : IYamlTypeConverter
87108
public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
88109
{
89110
var value = parser.Consume<Scalar>();
111+
if (string.IsNullOrWhiteSpace(value.Value))
112+
return AllVersions.Instance;
113+
if (string.Equals(value.Value, "all", StringComparison.InvariantCultureIgnoreCase))
114+
return AllVersions.Instance;
90115
return (SemVersion)value.Value;
91116
}
92117

@@ -97,3 +122,65 @@ public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializ
97122
emitter.Emit(new Scalar(value.ToString()!));
98123
}
99124
}
125+
126+
public class CloudManagedSerializer : IYamlTypeConverter
127+
{
128+
public bool Accepts(Type type) => type == typeof(CloudManagedDeployment);
129+
130+
public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
131+
{
132+
if (parser.TryConsume<Scalar>(out var value))
133+
{
134+
if (string.IsNullOrWhiteSpace(value.Value))
135+
return CloudManagedDeployment.All;
136+
if (string.Equals(value.Value, "all", StringComparison.InvariantCultureIgnoreCase))
137+
return CloudManagedDeployment.All;
138+
}
139+
var x = rootDeserializer.Invoke(typeof(Dictionary<string, string>));
140+
if (x is not Dictionary<string, string> { Count: > 0 } dictionary)
141+
return null;
142+
143+
var cloudManaged = new CloudManagedDeployment();
144+
if (dictionary.TryGetValue("ess", out var v))
145+
cloudManaged.Ess = (SemVersion)v;
146+
if (dictionary.TryGetValue("serverless", out v))
147+
cloudManaged.Serverless = (SemVersion)v;
148+
return cloudManaged;
149+
150+
}
151+
152+
public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer) =>
153+
serializer.Invoke(value, type);
154+
}
155+
156+
public class SelfManagedSerializer : IYamlTypeConverter
157+
{
158+
public bool Accepts(Type type) => type == typeof(SelfManagedDeployment);
159+
160+
public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
161+
{
162+
if (parser.TryConsume<Scalar>(out var value))
163+
{
164+
if (string.IsNullOrWhiteSpace(value.Value))
165+
return SelfManagedDeployment.All;
166+
if (string.Equals(value.Value, "all", StringComparison.InvariantCultureIgnoreCase))
167+
return SelfManagedDeployment.All;
168+
}
169+
var x = rootDeserializer.Invoke(typeof(Dictionary<string, string>));
170+
if (x is not Dictionary<string, string> { Count: > 0 } dictionary)
171+
return null;
172+
173+
var deployment = new SelfManagedDeployment();
174+
if (dictionary.TryGetValue("stack", out var v))
175+
deployment.Stack = (SemVersion)v;
176+
if (dictionary.TryGetValue("ece", out v))
177+
deployment.Ece = (SemVersion)v;
178+
if (dictionary.TryGetValue("eck", out v))
179+
deployment.Eck = (SemVersion)v;
180+
return deployment;
181+
182+
}
183+
184+
public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer) =>
185+
serializer.Invoke(value, type);
186+
}

tests/Elastic.Markdown.Tests/FrontMatter/ProductConstraintTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information
44

55
using Elastic.Markdown.Helpers;
6+
using Elastic.Markdown.Myst;
67
using Elastic.Markdown.Tests.Directives;
78
using FluentAssertions;
89
using Xunit.Abstractions;
@@ -41,3 +42,52 @@ public void ReadsSubstitutions()
4142
}
4243
}
4344

45+
public class CanSpecifyAllForProductVersions(ITestOutputHelper output) : DirectiveTest(output,
46+
"""
47+
---
48+
title: Elastic Docs v3
49+
navigation_title: "Documentation Guide"
50+
applies:
51+
self:
52+
stack: all
53+
---
54+
"""
55+
)
56+
{
57+
[Fact]
58+
public void Assert() =>
59+
File.YamlFrontMatter!.AppliesTo!.SelfManaged!.Stack.Should().BeEquivalentTo(AllVersions.Instance);
60+
}
61+
62+
public class EmptyProductVersionMeansAll(ITestOutputHelper output) : DirectiveTest(output,
63+
"""
64+
---
65+
title: Elastic Docs v3
66+
navigation_title: "Documentation Guide"
67+
applies:
68+
self:
69+
stack:
70+
---
71+
"""
72+
)
73+
{
74+
[Fact]
75+
public void Assert() =>
76+
File.YamlFrontMatter!.AppliesTo!.SelfManaged!.Stack.Should().BeEquivalentTo(AllVersions.Instance);
77+
}
78+
79+
public class EmptyCloudSetsAllProductsToAll(ITestOutputHelper output) : DirectiveTest(output,
80+
"""
81+
---
82+
title: Elastic Docs v3
83+
navigation_title: "Documentation Guide"
84+
applies:
85+
cloud:
86+
---
87+
"""
88+
)
89+
{
90+
[Fact]
91+
public void Assert() =>
92+
File.YamlFrontMatter!.AppliesTo!.Cloud!.Ess.Should().BeEquivalentTo(AllVersions.Instance);
93+
}

0 commit comments

Comments
 (0)