Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support events. #10

Merged
merged 2 commits into from
Jul 4, 2024
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
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project>

<PropertyGroup>
<VersionPrefix>2.9.0</VersionPrefix>
<PackageValidationBaselineVersion>2.8.0</PackageValidationBaselineVersion>
<VersionPrefix>2.10.0</VersionPrefix>
<PackageValidationBaselineVersion>2.9.0</PackageValidationBaselineVersion>
<LangVersion>12.0</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="CodeGenCore" Version="0.3.1" />
<PackageVersion Include="Facility.CodeGen.Console" Version="2.13.0" />
<PackageVersion Include="Facility.Definition" Version="2.13.0" />
<PackageVersion Include="Facility.CodeGen.Console" Version="2.14.0" />
<PackageVersion Include="Facility.Definition" Version="2.14.0" />
<PackageVersion Include="FluentAssertions" Version="6.12.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageVersion Include="NUnit" Version="4.1.0" />
Expand Down
1 change: 1 addition & 0 deletions FacilityMarkdown.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.github\workflows\build.yaml = .github\workflows\build.yaml
Directory.Build.props = Directory.Build.props
Directory.Packages.props = Directory.Packages.props
dotnet-tools.json = dotnet-tools.json
global.json = global.json
LICENSE = LICENSE
nuget.config = nuget.config
Expand Down
4 changes: 4 additions & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## 2.10.0

* Support events.

## 2.9.0

* Add .NET 8 targets. Update dependencies.
Expand Down
9 changes: 9 additions & 0 deletions conformance/ConformanceApi.fsd
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,15 @@ service ConformanceApi
[http(from: body, type: "application/x-output")] content: bytes;
}

[http(method: GET)]
event fibonacci
{
count: int32!;
}:
{
value: int32!;
}

data Any
{
string: string;
Expand Down
4 changes: 4 additions & 0 deletions conformance/http/ConformanceApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ API for a Facility test server.
| [mirrorText](ConformanceApi/mirrorText.md) | `POST /mirrorText` | |
| [bodyTypes](ConformanceApi/bodyTypes.md) | `POST /bodyTypes` | |

| event | path | description |
| --- | --- | --- |
| [fibonacci](ConformanceApi/fibonacci.md) | ` /fibonacci` | |

| data | description |
| --- | --- |
| [Widget](ConformanceApi/Widget.md) | A widget. |
Expand Down
20 changes: 20 additions & 0 deletions conformance/http/ConformanceApi/fibonacci.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# fibonacci (event)

```
GET /fibonacci
?count={count}
--- 200 OK (server-sent events)
{
"value": (integer)
}
```

| request | type | description |
| --- | --- | --- |
| count | int32 | |

| response | type | description |
| --- | --- | --- |
| value | int32 | |

<!-- DO NOT EDIT: generated by fsdgenmd -->
4 changes: 4 additions & 0 deletions conformance/no-http/ConformanceApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ API for a Facility test server.
| [mirrorText](ConformanceApi/mirrorText.md) | |
| [bodyTypes](ConformanceApi/bodyTypes.md) | |

| event | description |
| --- | --- |
| [fibonacci](ConformanceApi/fibonacci.md) | |

| data | description |
| --- | --- |
| [Widget](ConformanceApi/Widget.md) | A widget. |
Expand Down
11 changes: 11 additions & 0 deletions conformance/no-http/ConformanceApi/fibonacci.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# fibonacci (event)

| request | type | description |
| --- | --- | --- |
| count | int32 | |

| response | type | description |
| --- | --- | --- |
| value | int32 | |

<!-- DO NOT EDIT: generated by fsdgenmd -->
5 changes: 3 additions & 2 deletions dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
"isRoot": true,
"tools": {
"facilityconformance": {
"version": "2.21.0",
"version": "2.29.0",
"commands": [
"FacilityConformance"
]
],
"rollForward": false
}
}
}
11 changes: 11 additions & 0 deletions src/Facility.CodeGen.Markdown/MarkdownGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CodeGenCore;
using Facility.Definition;
using Facility.Definition.CodeGen;
using Facility.Definition.Fsd;
using Facility.Definition.Http;

namespace Facility.CodeGen.Markdown;
Expand All @@ -13,8 +14,18 @@ public sealed class MarkdownGenerator : CodeGenerator
/// <summary>
/// Generates Markdown.
/// </summary>
/// <param name="parser">The parser.</param>
/// <param name="settings">The settings.</param>
/// <returns>The number of updated files.</returns>
public static int GenerateMarkdown(ServiceParser parser, MarkdownGeneratorSettings settings) =>
FileGenerator.GenerateFiles(parser, new MarkdownGenerator { GeneratorName = nameof(MarkdownGenerator) }, settings);

/// <summary>
/// Generates Markdown.
/// </summary>
/// <param name="settings">The settings.</param>
/// <returns>The number of updated files.</returns>
[Obsolete("Use the overload that takes a parser.")]
public static int GenerateMarkdown(MarkdownGeneratorSettings settings) =>
FileGenerator.GenerateFiles(new MarkdownGenerator { GeneratorName = nameof(MarkdownGenerator) }, settings);

Expand Down
10 changes: 9 additions & 1 deletion src/Facility.CodeGen.Markdown/MarkdownGeneratorGlobals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ public MarkdownGeneratorGlobals(MarkdownGenerator generator, ServiceInfo service
public string CodeGenCommentText { get; }

public HttpElementInfo? GetHttp(ServiceElementInfo methodInfo) =>
HttpService?.Methods.FirstOrDefault(x => x.ServiceMethod == methodInfo);
HttpService?.AllMethods.FirstOrDefault(x => x.ServiceMethod == methodInfo);

public bool IsEvent(object methodInfo) =>
methodInfo switch
{
ServiceMethodInfo serviceMethodInfo => serviceMethodInfo.Kind == ServiceMethodKind.Event,
HttpMethodInfo httpMethodInfo => httpMethodInfo.ServiceMethod.Kind == ServiceMethodKind.Event,
_ => false,
};

public ServiceTypeInfo? GetFieldType(ServiceFieldInfo field) => Service.GetFieldType(field);

Expand Down
23 changes: 20 additions & 3 deletions src/Facility.CodeGen.Markdown/template.scriban-txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ URL: [`{{ HttpService.Url }}`]({{ HttpService.Url }})
{{ end ~}}
{{ end # else ~}}
{{ end # if !Service.Methods.empty? ~}}
{{ if !Service.Events.empty? ~}}
{{ if HttpService ~}}

| event | path | description |
| --- | --- | --- |
{{ for event in HttpService.Events | WhereNotObsolete ~}}
| [{{ event.ServiceMethod.Name }}]({{ Service.Name }}/{{ event.ServiceMethod.Name }}.md) | `{{ event.Event }} {{ event.Path }}` | {{ event.ServiceMethod.Summary }} |
{{ end ~}}
{{ else # if HttpService ~}}

| event | description |
| --- | --- |
{{ for event in Service.Events | WhereNotObsolete ~}}
| [{{ event.Name }}]({{ Service.Name }}/{{ event.Name }}.md) | {{ event.Summary }} |
{{ end ~}}
{{ end # else ~}}
{{ end # if !Service.Events.empty? ~}}
{{ if !Service.Dtos.empty? ~}}

| data | description |
Expand Down Expand Up @@ -73,11 +90,11 @@ URL: [`{{ HttpService.Url }}`]({{ HttpService.Url }})
{{ WriteCodeGenComment ~}}

{{### Methods ### ~}}
{{ for method in Service.Methods | WhereNotObsolete ~}}
{{ for method in Service.AllMethods | WhereNotObsolete ~}}

==> {{ Service.Name }}/{{ method.Name }}.md

# {{ method.Name }}
# {{ method.Name }}{{ if method | IsEvent; ' (event)'; end }}
{{ WriteSummary method ~}}
{{ http = method | GetHttp ~}}
{{ if http ~}}
Expand Down Expand Up @@ -106,7 +123,7 @@ URL: [`{{ HttpService.Url }}`]({{ HttpService.Url }})
{{ end ~}}
{{ end # if !http.ResponseHeaderFields.empty? ~}}
{{ for response in http.ValidResponses ~}}
--- {{ response.StatusCode + 0 }} {{ response.StatusCode | StatusCodePhrase }}
--- {{ response.StatusCode + 0 }} {{ response.StatusCode | StatusCodePhrase }}{{ if method | IsEvent; ' (server-sent events)'; end }}
{{ if response.BodyField ~}}
({{ if (response.BodyField.ServiceField | GetFieldType).Kind == 'Boolean'; 'if '; end }}{{ response.BodyField.ServiceField.Name }})
{{ else if !response.NormalFields.empty? ~}}
Expand Down
3 changes: 3 additions & 0 deletions src/fsdgenmd/FsdGenMarkdownApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Facility.CodeGen.Console;
using Facility.CodeGen.Markdown;
using Facility.Definition.CodeGen;
using Facility.Definition.Fsd;

namespace fsdgenmd;

Expand All @@ -22,6 +23,8 @@ public sealed class FsdGenMarkdownApp : CodeGeneratorApp
" Override the default template.",
];

protected override ServiceParser CreateParser() => new FsdParser(new FsdParserSettings { SupportsEvents = true });

protected override CodeGenerator CreateGenerator() => new MarkdownGenerator();

protected override FileGeneratorSettings CreateSettings(ArgsReader args) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void GenerateExampleApiSuccess()
{
ServiceInfo service;
const string fileName = "Facility.CodeGen.Markdown.UnitTests.ConformanceApi.fsd";
var parser = new FsdParser();
var parser = CreateParser();
var stream = GetType().GetTypeInfo().Assembly.GetManifestResourceStream(fileName);
Assert.That(stream, Is.Not.Null);
using (var reader = new StreamReader(stream!))
Expand All @@ -31,7 +31,7 @@ public void DtoWithExternDataType()
const string definition = @"service TestApi { extern data Thing; data Test {
/// This is a description.
thing: Thing; } }";
var parser = new FsdParser();
var parser = CreateParser();
var service = parser.ParseDefinition(new ServiceDefinitionText("TestApi.fsd", definition));
var generator = new MarkdownGenerator { GeneratorName = nameof(MarkdownGeneratorTests) };

Expand All @@ -48,7 +48,7 @@ public void DtoWithExternEnumType()
const string definition = @"service TestApi { extern enum Kind; data Test {
/// This is a description.
kind: Kind; } }";
var parser = new FsdParser();
var parser = CreateParser();
var service = parser.ParseDefinition(new ServiceDefinitionText("TestApi.fsd", definition));
var generator = new MarkdownGenerator { GeneratorName = nameof(MarkdownGeneratorTests) };

Expand All @@ -58,4 +58,6 @@ public void DtoWithExternEnumType()
Assert.That(file.Text, Does.Contain("\"kind\": (Kind)"));
Assert.That(file.Text, Does.Contain("| kind | Kind | This is a description. |"));
}

private static FsdParser CreateParser() => new FsdParser(new FsdParserSettings { SupportsEvents = true });
}
Loading