Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ private static void ProcessAppliesToDirective(AppliesToDirective appliesToDirect
{
var applicableTo = YamlSerialization.Deserialize<ApplicableTo>(yaml);
appliesToDirective.AppliesTo = applicableTo;
if (appliesToDirective.AppliesTo.Warnings is null)
return;
foreach (var warning in appliesToDirective.AppliesTo.Warnings)
appliesToDirective.EmitWarning(warning);
applicableTo.Warnings = null;
}
catch (Exception e)
{
Expand Down
20 changes: 20 additions & 0 deletions src/Elastic.Markdown/Myst/FrontMatter/ApplicableTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public record ApplicableTo
[YamlMember(Alias = "product")]
public ApplicabilityOverTime? Product { get; set; }

public string[]? Warnings { get; set; }

public static ApplicableTo All { get; } = new()
{
Stack = ApplicabilityOverTime.GenerallyAvailable,
Expand Down Expand Up @@ -87,6 +89,11 @@ public record ServerlessProjectApplicability

public class ApplicableToConverter : IYamlTypeConverter
{
private static readonly string[] KnownKeys =
["stack", "deployment", "serverless", "product", "ece",
"eck", "ess", "self", "elasticsearch", "observability","security"
];

public bool Accepts(Type type) => type == typeof(ApplicableTo);

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


var applicableTo = new ApplicableTo();
var warnings = new List<string>();

var keys = dictionary.Keys.OfType<string>().ToArray();
var oldStyleKeys = keys.Where(k => k.StartsWith(":")).ToList();
if (oldStyleKeys.Count > 0)
warnings.Add($"Applies block does not use valid yaml keys: {string.Join(", ", oldStyleKeys)}");
var unknownKeys = keys.Except(KnownKeys).Except(oldStyleKeys).ToList();
if (unknownKeys.Count > 0)
warnings.Add($"Applies block does not support the following keys: {string.Join(", ", unknownKeys)}");

if (TryGetApplicabilityOverTime(dictionary, "stack", out var stackAvailability))
applicableTo.Stack = stackAvailability;

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

if (warnings.Count > 0)
applicableTo.Warnings = warnings.ToArray();
return applicableTo;
}

Expand Down
28 changes: 27 additions & 1 deletion tests/authoring/Applicability/AppliesToDirective.fs
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,30 @@ serverless:
Elasticsearch=ApplicabilityOverTime.op_Explicit "beta 9.1.0",
Observability=ApplicabilityOverTime.op_Explicit "discontinued 9.2.0"
)
))
))

type ``warns on old syntax`` () =
static let markdown = Setup.Markdown """
```{applies_to}
:hosted: all
```
"""
[<Fact>]
let ``has no errors`` () = markdown |> hasNoErrors

[<Fact>]
let ``warns on bad syntax`` () =
markdown |> hasWarning "Applies block does not use valid yaml keys: :hosted"

type ``warns on invalid keys`` () =
static let markdown = Setup.Markdown """
```{applies_to}
hosted: all
```
"""
[<Fact>]
let ``has no errors`` () = markdown |> hasNoErrors

[<Fact>]
let ``warns on bad syntax`` () =
markdown |> hasWarning "Applies block does not support the following keys: hosted"
Loading