-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add date in file info; When you click calculate hash, it will be comp…
…ared with the stored hash for the file; if they are different, the hash will be highlighted in red
- Loading branch information
1 parent
b10a226
commit 46b4a1e
Showing
25 changed files
with
688 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
.../ImoutoRebirth.Common/ImoutoRebirth.Common.WebApi/NodaTime/NamingPolicyParameterFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Microsoft.OpenApi.Models; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
namespace ImoutoRebirth.Common.WebApi.NodaTime; | ||
|
||
internal class NamingPolicyParameterFilter : IParameterFilter | ||
{ | ||
private readonly NodaTimeSchemaSettings _nodaTimeSchemaSettings; | ||
|
||
public NamingPolicyParameterFilter(NodaTimeSchemaSettings nodaTimeSchemaSettings) | ||
=> _nodaTimeSchemaSettings = nodaTimeSchemaSettings; | ||
|
||
public void Apply(OpenApiParameter parameter, ParameterFilterContext context) | ||
=> parameter.Name = _nodaTimeSchemaSettings.ResolvePropertyName(parameter.Name); | ||
} |
40 changes: 40 additions & 0 deletions
40
Source/ImoutoRebirth.Common/ImoutoRebirth.Common.WebApi/NodaTime/NodaTimeSchemaSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using NodaTime; | ||
|
||
namespace ImoutoRebirth.Common.WebApi.NodaTime; | ||
|
||
public class NodaTimeSchemaSettings | ||
{ | ||
public Func<string, string> ResolvePropertyName { get; } | ||
|
||
public Func<object, string> FormatToJson { get; } | ||
|
||
public IDateTimeZoneProvider DateTimeZoneProvider { get; } | ||
|
||
public bool ShouldGenerateExamples { get; } | ||
|
||
public SchemaExamples SchemaExamples { get; } | ||
|
||
/// <param name="resolvePropertyName">Function that resolves property name by proper naming strategy.</param> | ||
/// <param name="formatToJson">Function that formats object as json text.</param> | ||
/// <param name="shouldGenerateExamples">Should the example node be generated.</param> | ||
/// <param name="schemaExamples"><see cref="SchemaExamples"/> for schema example values.</param> | ||
/// <param name="dateTimeZoneProvider"><see cref="IDateTimeZoneProvider"/> configured in Startup.</param> | ||
public NodaTimeSchemaSettings( | ||
Func<string, string> resolvePropertyName, | ||
Func<object, string> formatToJson, | ||
bool shouldGenerateExamples, | ||
SchemaExamples? schemaExamples = null, | ||
IDateTimeZoneProvider? dateTimeZoneProvider = null) | ||
{ | ||
ResolvePropertyName = resolvePropertyName; | ||
FormatToJson = formatToJson; | ||
|
||
DateTimeZoneProvider = dateTimeZoneProvider ?? DateTimeZoneProviders.Tzdb; | ||
|
||
ShouldGenerateExamples = shouldGenerateExamples; | ||
SchemaExamples = schemaExamples ?? new SchemaExamples( | ||
DateTimeZoneProvider, | ||
dateTimeUtc: null, | ||
dateTimeZone: null); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...moutoRebirth.Common/ImoutoRebirth.Common.WebApi/NodaTime/NodaTimeSchemaSettingsFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Text.Json; | ||
|
||
namespace ImoutoRebirth.Common.WebApi.NodaTime; | ||
|
||
public static class NodaTimeSchemaSettingsFactory | ||
{ | ||
public static NodaTimeSchemaSettings CreateNodaTimeSchemaSettings( | ||
this JsonSerializerOptions jsonSerializerOptions) | ||
{ | ||
return new NodaTimeSchemaSettings(ResolvePropertyName, FormatToJson, true); | ||
|
||
string ResolvePropertyName(string propertyName) | ||
=> jsonSerializerOptions.PropertyNamingPolicy?.ConvertName(propertyName) ?? propertyName; | ||
|
||
string FormatToJson(object value) | ||
=> JsonSerializer.Serialize(value, jsonSerializerOptions).Trim('\"'); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
Source/ImoutoRebirth.Common/ImoutoRebirth.Common.WebApi/NodaTime/SchemaExamples.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using NodaTime; | ||
|
||
namespace ImoutoRebirth.Common.WebApi.NodaTime; | ||
|
||
public class SchemaExamples | ||
{ | ||
public DateTimeZone DateTimeZone { get; set; } | ||
|
||
public Instant Instant { get; set; } | ||
|
||
public ZonedDateTime ZonedDateTime { get; set; } | ||
|
||
public Interval Interval { get; set; } | ||
|
||
public DateInterval DateInterval { get; set; } | ||
|
||
public Period Period { get; set; } | ||
|
||
public OffsetDate OffsetDate { get; set; } | ||
|
||
public OffsetTime OffsetTime { get; set; } | ||
|
||
public OffsetDateTime OffsetDateTime { get; set; } | ||
|
||
/// <summary> | ||
/// Creates example value by provided <see cref="DateTime"/> and <see cref="IDateTimeZoneProvider"/>. | ||
/// </summary> | ||
/// <param name="dateTimeZoneProvider">IDateTimeZoneProvider instance.</param> | ||
/// <param name="dateTimeUtc"><see cref="DateTime"/>. If not set then <see cref="DateTime.UtcNow"/> will be used.</param> | ||
/// <param name="dateTimeZone">Optional DateTimeZone name. If not set SystemDefault will be used.</param> | ||
public SchemaExamples( | ||
IDateTimeZoneProvider dateTimeZoneProvider, | ||
DateTime? dateTimeUtc = null, | ||
string? dateTimeZone = null) | ||
{ | ||
var dateTimeUtcValue = dateTimeUtc ?? DateTime.UtcNow; | ||
|
||
if (dateTimeUtcValue.Kind != DateTimeKind.Utc) | ||
throw new ArgumentException("dateTimeUtc should be UTC", nameof(dateTimeUtc)); | ||
|
||
DateTimeZone = dateTimeZone != null | ||
? dateTimeZoneProvider.GetZoneOrNull(dateTimeZone) ?? dateTimeZoneProvider.GetSystemDefault() | ||
: dateTimeZoneProvider.GetSystemDefault(); | ||
|
||
Instant = Instant.FromDateTimeUtc(dateTimeUtcValue); | ||
|
||
ZonedDateTime = Instant.InZone(DateTimeZone); | ||
|
||
Interval = new Interval(Instant, | ||
Instant.PlusTicks(TimeSpan.TicksPerDay) | ||
.PlusTicks(TimeSpan.TicksPerHour) | ||
.PlusTicks(TimeSpan.TicksPerMinute) | ||
.PlusTicks(TimeSpan.TicksPerSecond) | ||
.PlusTicks(TimeSpan.TicksPerMillisecond)); | ||
|
||
DateInterval = new DateInterval(ZonedDateTime.Date, ZonedDateTime.Date.PlusDays(1)); | ||
|
||
Period = Period.Between( | ||
ZonedDateTime.LocalDateTime, | ||
Interval.End.InZone(DateTimeZone).LocalDateTime, | ||
PeriodUnits.AllUnits); | ||
|
||
OffsetDate = new OffsetDate(ZonedDateTime.Date, ZonedDateTime.Offset); | ||
|
||
OffsetTime = new OffsetTime(ZonedDateTime.TimeOfDay, ZonedDateTime.Offset); | ||
|
||
OffsetDateTime = Instant.WithOffset(ZonedDateTime.Offset); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
Source/ImoutoRebirth.Common/ImoutoRebirth.Common.WebApi/NodaTime/Schemas.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace ImoutoRebirth.Common.WebApi.NodaTime; | ||
|
||
public class Schemas | ||
{ | ||
public required Func<OpenApiSchema> Instant { get; set; } | ||
|
||
public required Func<OpenApiSchema> LocalDate { get; set; } | ||
|
||
public required Func<OpenApiSchema> LocalTime { get; set; } | ||
|
||
public required Func<OpenApiSchema> LocalDateTime { get; set; } | ||
|
||
public required Func<OpenApiSchema> OffsetDateTime { get; set; } | ||
|
||
public required Func<OpenApiSchema> ZonedDateTime { get; set; } | ||
|
||
public required Func<OpenApiSchema> Interval { get; set; } | ||
|
||
public required Func<OpenApiSchema> DateInterval { get; set; } | ||
|
||
public required Func<OpenApiSchema> Offset { get; set; } | ||
|
||
public required Func<OpenApiSchema> Period { get; set; } | ||
|
||
public required Func<OpenApiSchema> Duration { get; set; } | ||
|
||
public required Func<OpenApiSchema> OffsetDate { get; set; } | ||
|
||
public required Func<OpenApiSchema> OffsetTime { get; set; } | ||
|
||
public required Func<OpenApiSchema> DateTimeZone { get; set; } | ||
} |
68 changes: 68 additions & 0 deletions
68
Source/ImoutoRebirth.Common/ImoutoRebirth.Common.WebApi/NodaTime/SchemasFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using Microsoft.OpenApi.Any; | ||
using Microsoft.OpenApi.Models; | ||
using NodaTime; | ||
|
||
namespace ImoutoRebirth.Common.WebApi.NodaTime; | ||
|
||
public class SchemasFactory | ||
{ | ||
private readonly NodaTimeSchemaSettings _settings; | ||
|
||
public SchemasFactory(NodaTimeSchemaSettings settings) => _settings = settings; | ||
|
||
public Schemas CreateSchemas() | ||
{ | ||
var examples = _settings.SchemaExamples; | ||
|
||
// https://xml2rfc.tools.ietf.org/public/rfc/html/rfc3339.html#anchor14 | ||
return new Schemas | ||
{ | ||
Instant = () => StringSchema(examples.Instant, "date-time"), | ||
LocalDate = () => StringSchema(examples.ZonedDateTime.Date, "date"), | ||
LocalTime = () => StringSchema(examples.ZonedDateTime.TimeOfDay), | ||
LocalDateTime = () => StringSchema(examples.ZonedDateTime.LocalDateTime), | ||
OffsetDateTime = () => StringSchema(examples.OffsetDateTime, "date-time"), | ||
ZonedDateTime = () => StringSchema(examples.ZonedDateTime), | ||
Interval = () => new OpenApiSchema | ||
{ | ||
Type = "object", | ||
Properties = new Dictionary<string, OpenApiSchema> | ||
{ | ||
{ ResolvePropertyName(nameof(Interval.Start)), StringSchema(examples.Interval.Start, "date-time") }, | ||
{ ResolvePropertyName(nameof(Interval.End)), StringSchema(examples.Interval.End, "date-time") }, | ||
}, | ||
}, | ||
DateInterval = () => new OpenApiSchema | ||
{ | ||
Type = "object", | ||
Properties = new Dictionary<string, OpenApiSchema> | ||
{ | ||
{ ResolvePropertyName(nameof(DateInterval.Start)), StringSchema(examples.DateInterval.Start, "date") }, | ||
{ ResolvePropertyName(nameof(DateInterval.End)), StringSchema(examples.DateInterval.End, "date") }, | ||
}, | ||
}, | ||
Offset = () => StringSchema(examples.ZonedDateTime.Offset), | ||
Period = () => StringSchema(examples.Period), | ||
Duration = () => StringSchema(examples.Interval.Duration), | ||
OffsetDate = () => StringSchema(examples.OffsetDate), | ||
OffsetTime = () => StringSchema(examples.OffsetTime), | ||
DateTimeZone = () => StringSchema(examples.DateTimeZone), | ||
}; | ||
} | ||
|
||
private OpenApiSchema StringSchema(object exampleObject, string? format = null) | ||
{ | ||
return new OpenApiSchema | ||
{ | ||
Type = "string", | ||
Example = _settings.ShouldGenerateExamples | ||
? new OpenApiString(FormatToJson(exampleObject)) | ||
: null, | ||
Format = format | ||
}; | ||
} | ||
|
||
private string ResolvePropertyName(string propertyName) => _settings.ResolvePropertyName(propertyName); | ||
|
||
private string FormatToJson(object value) => _settings.FormatToJson(value); | ||
} |
52 changes: 52 additions & 0 deletions
52
.../ImoutoRebirth.Common/ImoutoRebirth.Common.WebApi/NodaTime/SwaggerGenOptionsExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System.Text.Json; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using NodaTime; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
namespace ImoutoRebirth.Common.WebApi.NodaTime; | ||
|
||
public static class SwaggerGenOptionsExtensions | ||
{ | ||
public static void ConfigureForNodaTime(this SwaggerGenOptions config, JsonSerializerOptions options) | ||
{ | ||
ArgumentNullException.ThrowIfNull(config); | ||
|
||
var nodaTimeSchemaSettings = options.CreateNodaTimeSchemaSettings(); | ||
config.ConfigureForNodaTime(nodaTimeSchemaSettings); | ||
} | ||
|
||
public static void ConfigureForNodaTime(this SwaggerGenOptions config, NodaTimeSchemaSettings nodaTimeSchemaSettings) | ||
{ | ||
config.ParameterFilter<NamingPolicyParameterFilter>(nodaTimeSchemaSettings); | ||
|
||
var schemas = new SchemasFactory(nodaTimeSchemaSettings).CreateSchemas(); | ||
|
||
config.MapType<Instant> (schemas.Instant); | ||
config.MapType<LocalDate> (schemas.LocalDate); | ||
config.MapType<LocalTime> (schemas.LocalTime); | ||
config.MapType<LocalDateTime> (schemas.LocalDateTime); | ||
config.MapType<OffsetDateTime> (schemas.OffsetDateTime); | ||
config.MapType<ZonedDateTime> (schemas.ZonedDateTime); | ||
config.MapType<Interval> (schemas.Interval); | ||
config.MapType<DateInterval> (schemas.DateInterval); | ||
config.MapType<Offset> (schemas.Offset); | ||
config.MapType<Period> (schemas.Period); | ||
config.MapType<Duration> (schemas.Duration); | ||
config.MapType<OffsetDate> (schemas.OffsetDate); | ||
config.MapType<OffsetTime> (schemas.OffsetTime); | ||
config.MapType<DateTimeZone> (schemas.DateTimeZone); | ||
|
||
// Nullable structs | ||
config.MapType<Instant?> (schemas.Instant); | ||
config.MapType<LocalDate?> (schemas.LocalDate); | ||
config.MapType<LocalTime?> (schemas.LocalTime); | ||
config.MapType<LocalDateTime?> (schemas.LocalDateTime); | ||
config.MapType<OffsetDateTime?>(schemas.OffsetDateTime); | ||
config.MapType<ZonedDateTime?> (schemas.ZonedDateTime); | ||
config.MapType<Interval?> (schemas.Interval); | ||
config.MapType<Offset?> (schemas.Offset); | ||
config.MapType<Duration?> (schemas.Duration); | ||
config.MapType<OffsetDate?> (schemas.OffsetDate); | ||
config.MapType<OffsetTime?> (schemas.OffsetTime); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/Services/Tags/Model/FileMetadata.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
using ImoutoRebirth.Room.WebApi.Client; | ||
|
||
namespace ImoutoRebirth.Navigator.Services.Tags.Model; | ||
|
||
public record FileMetadata(Guid Id, string StoredMd5, DateTimeOffset AddedOn); |
Oops, something went wrong.