Skip to content

Commit 515404b

Browse files
authored
Update code block parser to handle YAML key warnings (#541)
Enhanced the `ApplicableTo` deserialization to detect and warn about invalid or outdated YAML keys. Added corresponding unit tests to ensure proper warning messages are generated for unsupported or old syntax in `applies_to` blocks.
1 parent 45a469d commit 515404b

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/Elastic.Markdown/Myst/CodeBlocks/EnhancedCodeBlockParser.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ private static void ProcessAppliesToDirective(AppliesToDirective appliesToDirect
116116
{
117117
var applicableTo = YamlSerialization.Deserialize<ApplicableTo>(yaml);
118118
appliesToDirective.AppliesTo = applicableTo;
119+
if (appliesToDirective.AppliesTo.Warnings is null)
120+
return;
121+
foreach (var warning in appliesToDirective.AppliesTo.Warnings)
122+
appliesToDirective.EmitWarning(warning);
123+
applicableTo.Warnings = null;
119124
}
120125
catch (Exception e)
121126
{

src/Elastic.Markdown/Myst/FrontMatter/ApplicableTo.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public record ApplicableTo
2424
[YamlMember(Alias = "product")]
2525
public ApplicabilityOverTime? Product { get; set; }
2626

27+
public string[]? Warnings { get; set; }
28+
2729
public static ApplicableTo All { get; } = new()
2830
{
2931
Stack = ApplicabilityOverTime.GenerallyAvailable,
@@ -87,6 +89,11 @@ public record ServerlessProjectApplicability
8789

8890
public class ApplicableToConverter : IYamlTypeConverter
8991
{
92+
private static readonly string[] KnownKeys =
93+
["stack", "deployment", "serverless", "product", "ece",
94+
"eck", "ess", "self", "elasticsearch", "observability","security"
95+
];
96+
9097
public bool Accepts(Type type) => type == typeof(ApplicableTo);
9198

9299
public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
@@ -103,7 +110,18 @@ public class ApplicableToConverter : IYamlTypeConverter
103110
if (deserialized is not Dictionary<object, object?> { Count: > 0 } dictionary)
104111
return null;
105112

113+
106114
var applicableTo = new ApplicableTo();
115+
var warnings = new List<string>();
116+
117+
var keys = dictionary.Keys.OfType<string>().ToArray();
118+
var oldStyleKeys = keys.Where(k => k.StartsWith(":")).ToList();
119+
if (oldStyleKeys.Count > 0)
120+
warnings.Add($"Applies block does not use valid yaml keys: {string.Join(", ", oldStyleKeys)}");
121+
var unknownKeys = keys.Except(KnownKeys).Except(oldStyleKeys).ToList();
122+
if (unknownKeys.Count > 0)
123+
warnings.Add($"Applies block does not support the following keys: {string.Join(", ", unknownKeys)}");
124+
107125
if (TryGetApplicabilityOverTime(dictionary, "stack", out var stackAvailability))
108126
applicableTo.Stack = stackAvailability;
109127

@@ -119,6 +137,8 @@ public class ApplicableToConverter : IYamlTypeConverter
119137
if (TryGetProjectApplicability(dictionary, out var serverless))
120138
applicableTo.Serverless = serverless;
121139

140+
if (warnings.Count > 0)
141+
applicableTo.Warnings = warnings.ToArray();
122142
return applicableTo;
123143
}
124144

tests/authoring/Applicability/AppliesToDirective.fs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,30 @@ serverless:
5555
Elasticsearch=ApplicabilityOverTime.op_Explicit "beta 9.1.0",
5656
Observability=ApplicabilityOverTime.op_Explicit "discontinued 9.2.0"
5757
)
58-
))
58+
))
59+
60+
type ``warns on old syntax`` () =
61+
static let markdown = Setup.Markdown """
62+
```{applies_to}
63+
:hosted: all
64+
```
65+
"""
66+
[<Fact>]
67+
let ``has no errors`` () = markdown |> hasNoErrors
68+
69+
[<Fact>]
70+
let ``warns on bad syntax`` () =
71+
markdown |> hasWarning "Applies block does not use valid yaml keys: :hosted"
72+
73+
type ``warns on invalid keys`` () =
74+
static let markdown = Setup.Markdown """
75+
```{applies_to}
76+
hosted: all
77+
```
78+
"""
79+
[<Fact>]
80+
let ``has no errors`` () = markdown |> hasNoErrors
81+
82+
[<Fact>]
83+
let ``warns on bad syntax`` () =
84+
markdown |> hasWarning "Applies block does not support the following keys: hosted"

0 commit comments

Comments
 (0)