Skip to content

Commit

Permalink
Clean up JSON deserialization
Browse files Browse the repository at this point in the history
Use modern System.Text.Json advancements like KebabCaseLower naming policy.

Properly share the JsonSerializerOptions lol whoops.

Source gen it (cuz why not).
  • Loading branch information
PJB3005 committed Dec 21, 2023
1 parent 1eb7c84 commit 200af6b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 89 deletions.
11 changes: 6 additions & 5 deletions SS14.Changelog/Controllers/WebhookController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,22 @@ public async Task<IActionResult> PostHook()
switch (eventType)
{
case "push":
HandlePush(Deserialize<GHPushEvent>(ms));
HandlePush(DeserializeGitHub<GHPushEvent>(ms));
break;

case "pull_request":
HandlePullRequest(Deserialize<GHPullRequestEvent>(ms));
HandlePullRequest(DeserializeGitHub<GHPullRequestEvent>(ms));
break;
}

return Ok();
}

private static T Deserialize<T>(MemoryStream stream)
private static T DeserializeGitHub<T>(MemoryStream stream)
{
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web);
return JsonSerializer.Deserialize<T>(stream.GetBuffer().AsSpan(0, (int) stream.Length), options)!;
return JsonSerializer.Deserialize<T>(
stream.GetBuffer().AsSpan(0, (int) stream.Length),
GitHubSerializationContext.Default.Options)!;
}

private void HandlePush(GHPushEvent eventData)
Expand Down
101 changes: 17 additions & 84 deletions SS14.Changelog/GitHubData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,94 +4,27 @@

namespace SS14.Changelog
{
// System.Text.Json doesn't handle kebab_case.
// Great.

public sealed record GHPullRequestEvent
{
[JsonConstructor]
public GHPullRequestEvent(int number, string action, GHPullRequest pullRequest)
{
Number = number;
Action = action;
PullRequest = pullRequest;
}
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.KebabCaseLower)]
[JsonSerializable(typeof(GHPullRequestEvent))]
[JsonSerializable(typeof(GHPushEvent))]
public sealed partial class GitHubSerializationContext : JsonSerializerContext;

public int Number { get; }
public string Action { get; }
[JsonPropertyName("pull_request")]
public GHPullRequest PullRequest { get; }
}
public sealed record GHPullRequestEvent(int Number, string Action, GHPullRequest PullRequest);

public sealed record GHPullRequest
{
[JsonConstructor]
public GHPullRequest(bool merged, string body, GHUser user, DateTimeOffset? mergedAt, GHPullRequestBase @base, int number, string url)
{
Merged = merged;
Body = body;
User = user;
MergedAt = mergedAt;
Base = @base;
Number = number;
Url = url;
}
public sealed record GHPullRequest(
bool Merged,
string Body,
GHUser User,
DateTimeOffset? MergedAt,
GHPullRequestBase Base,
int Number,
string Url);

public bool Merged { get; }
public string Body { get; }
public GHUser User { get; }
[JsonPropertyName("merged_at")]
public DateTimeOffset? MergedAt { get; }
public GHPullRequestBase Base { get; }
public int Number { get; }
public string Url { get; }
}
public sealed record GHPullRequestBase(string Ref);

public sealed record GHPullRequestBase
{
public string Ref { get; }
public sealed record GHUser(string Login);

[JsonConstructor]
public GHPullRequestBase(string @ref)
{
Ref = @ref;
}
}
public sealed record GHPushEvent(ImmutableArray<GHPushedCommit> Commits, string Ref);

public sealed record GHUser
{
[JsonConstructor]
public GHUser(string login)
{
Login = login;
}

public string Login { get; }
}

public sealed record GHPushEvent
{
[JsonConstructor]
public GHPushEvent(ImmutableArray<GHPushedCommit> commits, string @ref)
{
Commits = commits;
Ref = @ref;
}

public ImmutableArray<GHPushedCommit> Commits { get; }
public string Ref { get; }
}

public sealed record GHPushedCommit
{
[JsonConstructor]
public GHPushedCommit(ImmutableArray<string> added, ImmutableArray<string> modified)
{
Added = added;
Modified = modified;
}

public ImmutableArray<string> Added { get; }
public ImmutableArray<string> Modified { get; }
}
public sealed record GHPushedCommit(ImmutableArray<string> Added, ImmutableArray<string> Modified);
}

0 comments on commit 200af6b

Please sign in to comment.