From 67abbb0529c63539c3f2cd1eac2a94b34ebaf91a Mon Sep 17 00:00:00 2001 From: David Driscoll Date: Sun, 3 Jul 2022 00:55:27 -0400 Subject: [PATCH] Updated nodatime configuration --- .../Extensions/ISchemaBuilderExtensions.cs | 515 ------------------ 1 file changed, 515 deletions(-) diff --git a/src/HotChocolate/Extensions/ISchemaBuilderExtensions.cs b/src/HotChocolate/Extensions/ISchemaBuilderExtensions.cs index f51be88dd..579fff362 100644 --- a/src/HotChocolate/Extensions/ISchemaBuilderExtensions.cs +++ b/src/HotChocolate/Extensions/ISchemaBuilderExtensions.cs @@ -56,519 +56,4 @@ public static ISchemaBuilder AddNodaTime(this ISchemaBuilder schemaBuilder) .AddType(new PeriodType(PeriodPattern.Roundtrip, PeriodPattern.NormalizingIso)) .AddType(); } - - /// - /// Represents a fixed (and calendar-independent) length of time. - /// - public class DurationType : StringToStructBaseType - { - private readonly IPattern[] _allowedPatterns; - private readonly IPattern _serializationPattern; - - /// - /// Initializes a new instance of . - /// - public DurationType() : this(DurationPattern.Roundtrip) - { - } - - /// - /// Initializes a new instance of . - /// - public DurationType(params IPattern[] allowedPatterns) : base("Duration") - { - _allowedPatterns = allowedPatterns; - _serializationPattern = allowedPatterns[0]; - Description = "Represents a fixed (and calendar-independent) length of time."; - } - - /// - protected override string Serialize(Duration runtimeValue) - { - return _serializationPattern - .Format(runtimeValue); - } - - /// - protected override bool TryDeserialize( - string resultValue, - [NotNullWhen(true)] out Duration? runtimeValue - ) - { - return _allowedPatterns.TryParse(resultValue, out runtimeValue); - } - } - - /// - /// Represents an instant on the global timeline, with nanosecond resolution. - /// - public class InstantType : StringToStructBaseType - { - private readonly IPattern[] _allowedPatterns; - private readonly IPattern _serializationPattern; - - /// - /// Initializes a new instance of . - /// - public InstantType() : this(InstantPattern.ExtendedIso) - { - } - - /// - /// Initializes a new instance of . - /// - public InstantType(params IPattern[] allowedPatterns) : base("Instant") - { - _allowedPatterns = allowedPatterns; - _serializationPattern = allowedPatterns[0]; - Description = "Represents an instant on the global timeline, with nanosecond resolution."; - } - - /// - protected override string Serialize(Instant runtimeValue) - { - return _serializationPattern - .Format(runtimeValue); - } - - /// - protected override bool TryDeserialize( - string resultValue, - [NotNullWhen(true)] out Instant? runtimeValue - ) - { - return _allowedPatterns.TryParse(resultValue, out runtimeValue); - } - } - - - /// - /// A date and time in a particular calendar system. - /// - public class LocalDateTimeType : StringToStructBaseType - { - private readonly IPattern[] _allowedPatterns; - private readonly IPattern _serializationPattern; - - /// - /// Initializes a new instance of . - /// - public LocalDateTimeType() : this(LocalDateTimePattern.ExtendedIso) - { - } - - /// - /// Initializes a new instance of . - /// - public LocalDateTimeType(params IPattern[] allowedPatterns) : base("LocalDateTime") - { - _allowedPatterns = allowedPatterns; - _serializationPattern = allowedPatterns[0]; - Description = "A date and time in a particular calendar system."; - } - - /// - protected override string Serialize(LocalDateTime runtimeValue) - { - return _serializationPattern - .Format(runtimeValue); - } - - /// - protected override bool TryDeserialize( - string resultValue, - [NotNullWhen(true)] out LocalDateTime? runtimeValue - ) - { - return _allowedPatterns.TryParse(resultValue, out runtimeValue); - } - } - - /// - /// LocalDate is an immutable struct representing a date within the calendar, - /// with no reference to a particular time zone or time of day. - /// - public class LocalDateType : StringToStructBaseType - { - private readonly IPattern[] _allowedPatterns; - private readonly IPattern _serializationPattern; - - /// - /// Initializes a new instance of . - /// - public LocalDateType() : this(LocalDatePattern.Iso) - { - } - - /// - /// Initializes a new instance of . - /// - public LocalDateType(params IPattern[] allowedPatterns) : base("LocalDate") - { - _allowedPatterns = allowedPatterns; - _serializationPattern = allowedPatterns[0]; - Description = - "LocalDate is an immutable struct representing a date within the calendar, with no reference to a particular time zone or time of day."; - } - - /// - protected override string Serialize(LocalDate runtimeValue) - { - return _serializationPattern - .Format(runtimeValue); - } - - /// - protected override bool TryDeserialize( - string resultValue, - [NotNullWhen(true)] out LocalDate? runtimeValue - ) - { - return _allowedPatterns.TryParse(resultValue, out runtimeValue); - } - } - - /// - /// LocalTime is an immutable struct representing a time of day, - /// with no reference to a particular calendar, time zone or date. - /// - public class LocalTimeType : StringToStructBaseType - { - private readonly IPattern[] _allowedPatterns; - private readonly IPattern _serializationPattern; - - /// - /// Initializes a new instance of . - /// - public LocalTimeType() : this(LocalTimePattern.ExtendedIso) - { - } - - /// - /// Initializes a new instance of . - /// - public LocalTimeType(params IPattern[] allowedPatterns) : base("LocalTime") - { - _allowedPatterns = allowedPatterns; - _serializationPattern = allowedPatterns[0]; - Description = "LocalTime is an immutable struct representing a time of day, with no reference to a particular calendar, time zone or date."; - } - - /// - protected override string Serialize(LocalTime runtimeValue) - { - return _serializationPattern - .Format(runtimeValue); - } - - /// - protected override bool TryDeserialize( - string resultValue, - [NotNullWhen(true)] out LocalTime? runtimeValue - ) - { - return _allowedPatterns.TryParse(resultValue, out runtimeValue); - } - } - - /// - /// A local date and time in a particular calendar system, combined with an offset from UTC. - /// - public class OffsetDateTimeType : StringToStructBaseType - { - private readonly IPattern[] _allowedPatterns; - private readonly IPattern _serializationPattern; - - /// - /// Initializes a new instance of . - /// - public OffsetDateTimeType() : this(OffsetDateTimePattern.ExtendedIso) - { - // Backwards compatibility with the original code's behavior - _serializationPattern = OffsetDateTimePattern.GeneralIso; - _allowedPatterns = new IPattern[] { OffsetDateTimePattern.ExtendedIso }; - } - - /// - /// Initializes a new instance of . - /// - public OffsetDateTimeType(params IPattern[] allowedPatterns) : base("OffsetDateTime") - { - _allowedPatterns = allowedPatterns; - _serializationPattern = _allowedPatterns[0]; - Description = "A local date and time in a particular calendar system, combined with an offset from UTC."; - } - - /// - protected override string Serialize(OffsetDateTime runtimeValue) - { - return _serializationPattern - .Format(runtimeValue); - } - - /// - protected override bool TryDeserialize( - string resultValue, - [NotNullWhen(true)] out OffsetDateTime? runtimeValue - ) - { - return _allowedPatterns.TryParse(resultValue, out runtimeValue); - } - } - - /// - /// A combination of a LocalDate and an Offset, - /// to represent a date at a specific offset from UTC but - /// without any time-of-day information. - /// - public class OffsetDateType : StringToStructBaseType - { - private readonly IPattern[] _allowedPatterns; - private readonly IPattern _serializationPattern; - - /// - /// Initializes a new instance of . - /// - public OffsetDateType() : this(OffsetDatePattern.GeneralIso) - { - } - - /// - /// Initializes a new instance of . - /// - public OffsetDateType(params IPattern[] allowedPatterns) : base("OffsetDate") - { - _allowedPatterns = allowedPatterns; - _serializationPattern = allowedPatterns[0]; - Description = - "A combination of a LocalDate and an Offset, to represent a date at a specific offset from UTC but without any time-of-day information."; - } - - /// - protected override string Serialize(OffsetDate runtimeValue) - { - return _serializationPattern - .Format(runtimeValue); - } - - /// - protected override bool TryDeserialize( - string resultValue, - [NotNullWhen(true)] out OffsetDate? runtimeValue - ) - { - return _allowedPatterns.TryParse(resultValue, out runtimeValue); - } - } - - /// - /// A combination of a LocalTime and an Offset, to represent a time-of-day at a specific offset - /// from UTC but without any date information. - /// - public class OffsetTimeType : StringToStructBaseType - { - private readonly IPattern[] _allowedPatterns; - private readonly IPattern _serializationPattern; - - /// - /// Initializes a new instance of . - /// - public OffsetTimeType() : this(OffsetTimePattern.GeneralIso) - { - } - - /// - /// Initializes a new instance of . - /// - public OffsetTimeType(params IPattern[] allowedPatterns) : base("OffsetTime") - { - _allowedPatterns = allowedPatterns; - _serializationPattern = _allowedPatterns[0]; - - Description = - "A combination of a LocalTime and an Offset, to represent a time-of-day at a specific offset from UTC but without any date information."; - } - - /// - protected override string Serialize(OffsetTime runtimeValue) - { - return _serializationPattern - .Format(runtimeValue); - } - - /// - protected override bool TryDeserialize( - string resultValue, - [NotNullWhen(true)] out OffsetTime? runtimeValue - ) - { - return _allowedPatterns.TryParse(resultValue, out runtimeValue); - } - } - - /// - /// An offset from UTC in seconds. - /// A positive value means that the local time is ahead of UTC (e.g. for Europe); - /// a negative value means that the local time is behind UTC (e.g. for America). - /// - public class OffsetType : StringToStructBaseType - { - private readonly IPattern[] _allowedPatterns; - private readonly IPattern _serializationPattern; - - /// - /// Initializes a new instance of . - /// - public OffsetType() : this(OffsetPattern.GeneralInvariantWithZ) - { - } - - /// - /// Initializes a new instance of . - /// - public OffsetType(params IPattern[] allowedPatterns) : base("Offset") - { - _allowedPatterns = allowedPatterns; - _serializationPattern = allowedPatterns[0]; - Description = - "An offset from UTC in seconds. A positive value means that the local time is ahead of UTC (e.g. for Europe); a negative value means that the local time is behind UTC (e.g. for America)."; - } - - /// - protected override string Serialize(Offset runtimeValue) - { - return _serializationPattern - .Format(runtimeValue); - } - - /// - protected override bool TryDeserialize( - string resultValue, - [NotNullWhen(true)] out Offset? runtimeValue - ) - { - return _allowedPatterns.TryParse(resultValue, out runtimeValue); - } - } - - /// - /// Represents a period of time expressed in human chronological terms: - /// hours, days, weeks, months and so on. - /// - public class PeriodType : StringToClassBaseType - { - private readonly IPattern[] _allowedPatterns; - private readonly IPattern _serializationPattern; - - /// - /// Initializes a new instance of . - /// - public PeriodType() : this(PeriodPattern.Roundtrip) - { - } - - /// - /// Initializes a new instance of . - /// - public PeriodType(params IPattern[] allowedPatterns) : base("Period") - { - _allowedPatterns = allowedPatterns; - _serializationPattern = allowedPatterns[0]; - Description = "Represents a period of time expressed in human chronological terms: hours, days, weeks, months and so on."; - } - - /// - protected override string Serialize(Period runtimeValue) - { - return _serializationPattern.Format(runtimeValue); - } - - /// - protected override bool TryDeserialize( - string resultValue, - [NotNullWhen(true)] out Period? runtimeValue - ) - { - return _allowedPatterns.TryParse(resultValue, out runtimeValue); - } - } -} - -internal static class PatternExtensions -{ - public static bool TryParse( - this IPattern pattern, - string text, - [NotNullWhen(true)] out NodaTimeType? output - ) - where NodaTimeType : struct - { - var result = pattern.Parse(text); - - if (result.Success) - { - output = result.Value; - return true; - } - - output = null; - return false; - } - - public static bool TryParse( - this IPattern pattern, - string text, - [NotNullWhen(true)] out NodaTimeType? output - ) - where NodaTimeType : class - { - var result = pattern.Parse(text); - - if (result.Success) - { - output = result.Value; - return true; - } - - output = null; - return false; - } - - public static bool TryParse( - this IPattern[] patterns, - string text, - [NotNullWhen(true)] out NodaTimeType? output - ) - where NodaTimeType : struct - { - foreach (var pattern in patterns) - { - if (pattern.TryParse(text, out output)) - { - return true; - } - } - - output = default; - return false; - } - - public static bool TryParse( - this IPattern[] patterns, - string text, - [NotNullWhen(true)] out NodaTimeType? output - ) - where NodaTimeType : class - { - foreach (var pattern in patterns) - { - if (pattern.TryParse(text, out output)) - { - return true; - } - } - - output = default; - return false; - } }