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

Update support libraries #44

Merged
merged 10 commits into from
Dec 19, 2023
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
3 changes: 2 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
<!-- Package Versions -->
<ItemGroup>
<PackageVersion Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageVersion Include="MetaBrainz.Common.Json" Version="5.1.0" />
<PackageVersion Include="MetaBrainz.Common" Version="3.0.0" />
<PackageVersion Include="MetaBrainz.Common.Json" Version="6.0.1" />
</ItemGroup>

</Project>
10 changes: 9 additions & 1 deletion MetaBrainz.ListenBrainz.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,13 @@
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Brainz/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=discnumber/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=ISRC/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=listenbrainz/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=sitewide/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:Boolean x:Key="/Default/UserDictionary/Words/=mbid/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Mbid/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=mbids/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=msid/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Msid/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=sitewide/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=tracknumber/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
20 changes: 10 additions & 10 deletions MetaBrainz.ListenBrainz/Json/EnumHelper.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.Diagnostics.CodeAnalysis;

namespace MetaBrainz.ListenBrainz.Json;

internal static class EnumHelper {

[return: NotNullIfNotNull(nameof(text))]
public static StatisticsRange? ParseStatisticsRange(string? text) {
if (text == null) {
if (text is null) {
return null;
}
return text switch {
Expand All @@ -17,14 +19,12 @@ internal static class EnumHelper {
};
}

public static string ToJson(this StatisticsRange range) {
return range switch {
StatisticsRange.AllTime => "all_time",
StatisticsRange.Week => "week",
StatisticsRange.Month => "month",
StatisticsRange.Year => "year",
_ => throw new ArgumentOutOfRangeException(nameof(range), range, "Invalid statistics range specified.")
};
}
public static string ToJson(this StatisticsRange range) => range switch {
StatisticsRange.AllTime => "all_time",
StatisticsRange.Week => "week",
StatisticsRange.Month => "month",
StatisticsRange.Year => "year",
_ => throw new ArgumentOutOfRangeException(nameof(range), range, "Invalid statistics range specified.")
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ protected override ArtistCountryInfo ReadObjectContents(ref Utf8JsonReader reade
}
reader.Read();
}
if (artistCount == null) {
if (artistCount is null) {
throw new JsonException("Expected artist count not found or null.");
}
if (country == null) {
if (country is null) {
throw new JsonException("Expected country code not found or null.");
}
if (listenCount == null) {
if (listenCount is null) {
throw new JsonException("Expected listen count not found or null.");
}
return new ArtistCountryInfo(artistCount.Value, country, listenCount.Value) {
Expand Down
4 changes: 2 additions & 2 deletions MetaBrainz.ListenBrainz/Json/Readers/ArtistInfoReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ protected override ArtistInfo ReadObjectContents(ref Utf8JsonReader reader, Json
}
reader.Read();
}
if (listenCount == null) {
if (listenCount is null) {
throw new JsonException("Expected listen count not found or null.");
}
if (name == null) {
if (name is null) {
throw new JsonException("Expected artist name not found or null.");
}
return new ArtistInfo(name, listenCount.Value) {
Expand Down
4 changes: 2 additions & 2 deletions MetaBrainz.ListenBrainz/Json/Readers/ErrorInfoReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ protected override ErrorInfo ReadObjectContents(ref Utf8JsonReader reader, JsonS
}
reader.Read();
}
if (!code.HasValue) {
if (code is null) {
throw new JsonException("Expected error code not found or null.");
}
if (error == null) {
if (error is null) {
throw new JsonException("Expected error message not found or null.");
}
return new ErrorInfo(code.Value, error) {
Expand Down
4 changes: 2 additions & 2 deletions MetaBrainz.ListenBrainz/Json/Readers/FetchedListensReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ protected override FetchedListens ReadPayload(ref Utf8JsonReader reader, JsonSer
reader.Read();
}
listens = PayloadReader<FetchedListens>.VerifyPayloadContents(count, listens);
if (user == null) {
if (user is null) {
throw new JsonException("Expected user id not found or null.");
}
if (!ts.HasValue) {
if (ts is null) {
throw new JsonException("Expected latest-listen timestamp not found or null.");
}
return new FetchedListens(listens, ts.Value, user) {
Expand Down
4 changes: 2 additions & 2 deletions MetaBrainz.ListenBrainz/Json/Readers/HourlyActivityReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ protected override HourlyActivity ReadObjectContents(ref Utf8JsonReader reader,
}
reader.Read();
}
if (!hour.HasValue) {
if (hour is null) {
throw new JsonException("Expected hour not found or null.");
}
if (hour is < 0 or > 23) {
throw new JsonException($"The specified hour ({hour}) is out of range (should be 0-23).");
}
if (!listenCount.HasValue) {
if (listenCount is null) {
throw new JsonException("Expected listen count not found or null.");
}
return new HourlyActivity(hour.Value, listenCount.Value) {
Expand Down
4 changes: 2 additions & 2 deletions MetaBrainz.ListenBrainz/Json/Readers/LatestImportReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ protected override LatestImport ReadObjectContents(ref Utf8JsonReader reader, Js
}
reader.Read();
}
if (!ts.HasValue) {
if (ts is null) {
throw new JsonException("Expected latest-import timestamp not found or null.");
}
if (user == null) {
if (user is null) {
throw new JsonException("Expected user id not found or null.");
}
return new LatestImport(ts.Value, user) {
Expand Down
2 changes: 1 addition & 1 deletion MetaBrainz.ListenBrainz/Json/Readers/ListenCountReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected override ListenCount ReadPayload(ref Utf8JsonReader reader, JsonSerial
}
reader.Read();
}
if (!count.HasValue) {
if (count is null) {
throw new JsonException("Expected listen count not found or null.");
}
return new ListenCount(count.Value) {
Expand Down
10 changes: 5 additions & 5 deletions MetaBrainz.ListenBrainz/Json/Readers/ListenReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ protected override Listen ReadObjectContents(ref Utf8JsonReader reader, JsonSeri
}
reader.Read();
}
if (inserted == null) {
if (inserted is null) {
throw new JsonException("Expected inserted-at timestamp not found or null.");
}
if (msid == null) {
if (msid is null) {
throw new JsonException("Expected MessyBrainz recording id not found or null.");
}
if (track == null) {
if (track is null) {
throw new JsonException("Expected track metadata not found or null.");
}
if (user == null) {
if (user is null) {
throw new JsonException("Expected user name not found or null.");
}
if (!ts.HasValue) {
if (ts is null) {
throw new JsonException("Expected listened-at timestamp not found or null.");
}
return new Listen(inserted, msid.Value, ts.Value, track, user) {
Expand Down
17 changes: 10 additions & 7 deletions MetaBrainz.ListenBrainz/Json/Readers/ListenTimeRangeReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Text.Json;

using MetaBrainz.Common;
using MetaBrainz.Common.Json;
using MetaBrainz.Common.Json.Converters;
using MetaBrainz.ListenBrainz.Objects;
Expand All @@ -24,18 +23,22 @@ protected override ListenTimeRange ReadObjectContents(ref Utf8JsonReader reader,
try {
reader.Read();
switch (prop) {
case "from_ts":
rangeStart = UnixTime.Convert(reader.GetOptionalInt64());
case "from_ts": {
var unixTime = reader.GetOptionalInt64();
rangeStart = unixTime is null ? null : DateTimeOffset.FromUnixTimeSeconds(unixTime.Value);
break;
}
case "listen_count":
listenCount = reader.GetInt32();
break;
case "time_range":
description = reader.GetString();
break;
case "to_ts":
rangeEnd = UnixTime.Convert(reader.GetOptionalInt64());
case "to_ts": {
var unixTime = reader.GetOptionalInt64();
rangeEnd = unixTime is null ? null : DateTimeOffset.FromUnixTimeSeconds(unixTime.Value);
break;
}
default:
rest ??= new Dictionary<string, object?>();
rest[prop] = reader.GetOptionalObject(options);
Expand All @@ -47,10 +50,10 @@ protected override ListenTimeRange ReadObjectContents(ref Utf8JsonReader reader,
}
reader.Read();
}
if (description == null) {
if (description is null) {
throw new JsonException("Expected description not found or null.");
}
if (listenCount == null) {
if (listenCount is null) {
throw new JsonException("Expected listen count not found or null.");
}
return new ListenTimeRange(description, listenCount.Value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace MetaBrainz.ListenBrainz.Json.Readers;

internal sealed class MusicBrainzIdMappingsReader : ObjectReader<MusicBrainzIdMappings> {

public static readonly MusicBrainzIdMappingsReader Instance = new();

protected override MusicBrainzIdMappings ReadObjectContents(ref Utf8JsonReader reader, JsonSerializerOptions options) {
Expand Down
2 changes: 1 addition & 1 deletion MetaBrainz.ListenBrainz/Json/Readers/PayloadReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected sealed override T ReadObjectContents(ref Utf8JsonReader reader, JsonSe
protected abstract T ReadPayload(ref Utf8JsonReader reader, JsonSerializerOptions options);

protected static IReadOnlyList<TItem> VerifyPayloadContents<TItem>(int? count, IReadOnlyList<TItem>? items) {
if (!count.HasValue) {
if (count is null) {
throw new JsonException("Expected payload item count not found or null.");
}
var n = items?.Count ?? 0;
Expand Down
13 changes: 7 additions & 6 deletions MetaBrainz.ListenBrainz/Json/Readers/PlayingNowReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,26 @@ protected override PlayingNow ReadPayload(ref Utf8JsonReader reader, JsonSeriali
}
reader.Read();
}
if (!count.HasValue) {
if (count is null) {
throw new JsonException("Expected listen count not found or null.");
}
if (count > 1) {
throw new JsonException($"Too many listens reported (expected at most one; got {count}).");
}
if (count == 1 && track == null) {
if (count == 1 && track is null) {
throw new JsonException("No listen data found, but the listen count is 1.");
}
if (count == 0 && track != null) {
if (count == 0 && track is not null) {
throw new JsonException("Listen data found, but the listen count is 0.");
}
if (user == null) {
if (user is null) {
throw new JsonException("Expected user id not found or null.");
}
if (!playingNow.HasValue || playingNow.Value != true) {
if (playingNow is not true) {
throw new JsonException("Expected 'playing now' flag not found or set incorrectly.");
}
return new PlayingNow(track, user) {
return new PlayingNow(user) {
Track = track,
UnhandledProperties = rest
};
}
Expand Down
2 changes: 1 addition & 1 deletion MetaBrainz.ListenBrainz/Json/Readers/PlayingTrackReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected override PlayingTrack ReadObjectContents(ref Utf8JsonReader reader, Js
}
reader.Read();
}
if (track == null) {
if (track is null) {
throw new JsonException("Required track metadata not found.");
}
return new PlayingTrack(track) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected override RecentListens ReadPayload(ref Utf8JsonReader reader, JsonSeri
reader.Read();
}
listens = PayloadReader<RecentListens>.VerifyPayloadContents(count, listens);
if (userList == null) {
if (userList is null) {
throw new JsonException("Expected user list not found or null.");
}
return new RecentListens(listens, userList) {
Expand Down
4 changes: 2 additions & 2 deletions MetaBrainz.ListenBrainz/Json/Readers/RecordingInfoReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ protected override RecordingInfo ReadObjectContents(ref Utf8JsonReader reader, J
}
reader.Read();
}
if (listenCount == null) {
if (listenCount is null) {
throw new JsonException("Expected listen count not found or null.");
}
if (name == null) {
if (name is null) {
throw new JsonException("Expected recording name not found or null.");
}
return new RecordingInfo(name, listenCount.Value) {
Expand Down
4 changes: 2 additions & 2 deletions MetaBrainz.ListenBrainz/Json/Readers/ReleaseInfoReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ protected override ReleaseInfo ReadObjectContents(ref Utf8JsonReader reader, Jso
}
reader.Read();
}
if (listenCount == null) {
if (listenCount is null) {
throw new JsonException("Expected listen count not found or null.");
}
if (name == null) {
if (name is null) {
throw new JsonException("Expected release name not found or null.");
}
return new ReleaseInfo(name, listenCount.Value) {
Expand Down
23 changes: 13 additions & 10 deletions MetaBrainz.ListenBrainz/Json/Readers/SiteArtistStatisticsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Text.Json;

using MetaBrainz.Common;
using MetaBrainz.Common.Json;
using MetaBrainz.ListenBrainz.Interfaces;
using MetaBrainz.ListenBrainz.Objects;
Expand Down Expand Up @@ -33,11 +32,13 @@ protected override SiteArtistStatistics ReadPayload(ref Utf8JsonReader reader, J
case "count":
count = reader.GetInt32();
break;
case "from_ts":
oldestListen = UnixTime.Convert(reader.GetOptionalInt64());
case "from_ts": {
var unixTime = reader.GetOptionalInt64();
oldestListen = unixTime is null ? null : DateTimeOffset.FromUnixTimeSeconds(unixTime.Value);
break;
}
case "last_updated":
lastUpdated = UnixTime.Convert(reader.GetInt64());
lastUpdated = DateTimeOffset.FromUnixTimeSeconds(reader.GetInt64());
break;
case "offset":
offset = reader.GetInt32();
Expand All @@ -48,9 +49,11 @@ protected override SiteArtistStatistics ReadPayload(ref Utf8JsonReader reader, J
goto default; // also register it as an unhandled property
}
break;
case "to_ts":
newestListen = UnixTime.Convert(reader.GetOptionalInt64());
case "to_ts": {
var unixTime = reader.GetOptionalInt64();
newestListen = unixTime is null ? null : DateTimeOffset.FromUnixTimeSeconds(unixTime.Value);
break;
}
default:
rest ??= new Dictionary<string, object?>();
rest[prop] = reader.GetOptionalObject(options);
Expand All @@ -65,16 +68,16 @@ protected override SiteArtistStatistics ReadPayload(ref Utf8JsonReader reader, J
// LB-1013: This ALWAYS reports 1000 as count, so we can't use VerifyPayloadContents().
// artists = this.VerifyPayloadContents(count, artists);
artists ??= Array.Empty<IArtistInfo>();
if (count == null) {
if (count is null) {
throw new JsonException("Expected count not found or null.");
}
if (lastUpdated == null) {
if (lastUpdated is null) {
throw new JsonException("Expected last-updated timestamp not found or null.");
}
if (offset == null) {
if (offset is null) {
throw new JsonException("Expected offset not found or null.");
}
if (range == null) {
if (range is null) {
throw new JsonException("Expected range not found or null.");
}
return new SiteArtistStatistics(count.Value, lastUpdated.Value, offset.Value, range.Value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ protected override TokenValidationResult ReadObjectContents(ref Utf8JsonReader r
}
reader.Read();
}
if (code == null) {
if (code is null) {
throw new JsonException("Expected status code not found or null.");
}
if (message == null) {
if (message is null) {
throw new JsonException("Expected message not found or null.");
}
return new TokenValidationResult((HttpStatusCode) code.Value, message) {
Expand Down
Loading