Skip to content

Commit

Permalink
Enable NRT for CalDateTime and DateTimeSerializer, update CI workflow
Browse files Browse the repository at this point in the history
CI build processes now use `-p:Nullable=disable` to avoid breaking changes while we gradually migrate to NRT
  • Loading branch information
axunonb committed Nov 5, 2024
1 parent 918a4df commit 56867a1
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
run: dotnet add ./Ical.Net.Tests/Ical.Net.Tests.csproj package AltCover

- name: Build
run: dotnet build --no-restore --configuration Release -p:nowarn=1591
run: dotnet build --no-restore --configuration Release -p:Nullable=disable -p:nowarn=1591
- name: Test
run: dotnet test --no-build --configuration Release --verbosity normal /p:AltCover=true /p:AltCoverXmlReport="coverage.xml" /p:AltCoverStrongNameKey="../IcalNetStrongnameKey.snk" /p:AltCoverAssemblyExcludeFilter="Ical.Net.Tests|NUnit3.TestAdapter|AltCover" /p:AltCoverAttributeFilter="ExcludeFromCodeCoverage" /p:AltCoverLineCover="false"

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore --configuration Release -p:nowarn=1591
run: dotnet build --no-restore --configuration Release -p:Nullable=disable -p:nowarn=1591
- name: Test
run: dotnet test --no-build --configuration Release --verbosity quiet

Expand All @@ -48,7 +48,7 @@ jobs:
- name: Build and pack for publishing
run: |
dotnet restore
dotnet build --configuration Release Ical.Net/Ical.Net.csproj -p:Version=${{env.VERSION}} -p:FileVersion=${{env.VERSION}}.${{github.run_number}} -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg -p:ContinuousIntegrationBuild=true
dotnet build --configuration Release Ical.Net/Ical.Net.csproj -p:Version=${{env.VERSION}} -p:FileVersion=${{env.VERSION}}.${{github.run_number}} -p:Nullable=disable -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg -p:ContinuousIntegrationBuild=true
dotnet pack --configuration Release Ical.Net/Ical.Net.csproj -p:Version=${{env.VERSION}} -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg --no-build -p:PackageVersion=${{env.VERSION}}
- name: Store artifacts
uses: actions/upload-artifact@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ jobs:
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore --configuration Release -p:nowarn=1591
run: dotnet build --no-restore --configuration Release -p:Nullable=disable -p:nowarn=1591
- name: Test
run: dotnet test --no-build --configuration Release --verbosity normal
66 changes: 37 additions & 29 deletions Ical.Net/DataTypes/CalDateTime.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Ical.Net.Serialization.DataTypes;
#nullable enable
using Ical.Net.Serialization.DataTypes;
using Ical.Net.Utility;
using NodaTime;
using System;
Expand Down Expand Up @@ -61,7 +62,7 @@ public CalDateTime(DateTime value) : this(value, value.Kind == DateTimeKind.Utc
/// <see cref="DateTimeKind.Utc"/>. If a non-UTC time zone is specified, the underlying
/// <see cref="DateTime.Kind"/> property will be <see cref="DateTimeKind.Local"/>.
/// If no time zone is specified, the <see cref="DateTime.Kind"/> property will be left untouched.</param>
public CalDateTime(DateTime value, string tzId)
public CalDateTime(DateTime value, string? tzId)
{
Initialize(new DateOnly(value.Year, value.Month, value.Day), new TimeOnly(value.Hour, value.Minute, value.Second), value.Date.Kind, tzId, null);
}
Expand Down Expand Up @@ -107,7 +108,7 @@ public CalDateTime(int year, int month, int day, int hour, int minute, int secon
/// <param name="hour"></param>
/// <param name="minute"></param>
/// <param name="cal"></param>
public CalDateTime(int year, int month, int day, int hour, int minute, int second, string tzId, Calendar cal)
public CalDateTime(int year, int month, int day, int hour, int minute, int second, string? tzId, Calendar cal)
{
Initialize(new DateOnly(year, month, day), new TimeOnly(hour, minute, second), DateTimeKind.Unspecified, tzId, cal);
}
Expand Down Expand Up @@ -145,7 +146,7 @@ public CalDateTime(int year, int month, int day, string tzId)
/// </param>
/// <param name="date"></param>
/// <param name="time"></param>
public CalDateTime(DateOnly date, TimeOnly time, string tzId = null)
public CalDateTime(DateOnly date, TimeOnly time, string? tzId = null)
{
Initialize(date, time, DateTimeKind.Unspecified, tzId, null);
}
Expand All @@ -158,7 +159,8 @@ public CalDateTime(DateOnly date, TimeOnly time, string tzId = null)
public CalDateTime(string value)
{
var serializer = new DateTimeSerializer();
CopyFrom(serializer.Deserialize(new StringReader(value)) as ICopyable);
CopyFrom(serializer.Deserialize(new StringReader(value)) as ICopyable
?? throw new InvalidOperationException("Failure deserializing value"));
}

/// <summary>
Expand All @@ -178,12 +180,12 @@ public void SetValue(DateOnly date, TimeOnly? time, DateTimeKind kind)
_timeOnly = time;
}

private void Initialize(DateOnly date, TimeOnly? time, DateTimeKind kind, string tzId, Calendar cal)
private void Initialize(DateOnly date, TimeOnly? time, DateTimeKind kind, string? tzId, Calendar? cal)
{
_dateOnly = date;
_timeOnly = time;

if ((!string.IsNullOrWhiteSpace(tzId) && !tzId.Equals("UTC", StringComparison.OrdinalIgnoreCase))
if ((tzId != null && !string.IsNullOrWhiteSpace(tzId) && !tzId.Equals("UTC", StringComparison.OrdinalIgnoreCase))
|| (string.IsNullOrWhiteSpace(tzId) && kind == DateTimeKind.Local))
{
// Definitely local
Expand All @@ -205,7 +207,7 @@ private void Initialize(DateOnly date, TimeOnly? time, DateTimeKind kind, string
else
{
// Unspecified
TzId = string.Empty;
TzId = null;

_value = time.HasValue
? new DateTime(date.Year, date.Month, date.Day, time.Value.Hour, time.Value.Minute, time.Value.Second, kind)
Expand All @@ -216,7 +218,7 @@ private void Initialize(DateOnly date, TimeOnly? time, DateTimeKind kind, string
}

/// <inheritdoc/>
public override ICalendarObject AssociatedObject
public override ICalendarObject? AssociatedObject
{
get => base.AssociatedObject;
set
Expand Down Expand Up @@ -272,19 +274,19 @@ public override int GetHashCode()
}
}

public static bool operator <(CalDateTime left, IDateTime right)
public static bool operator <(CalDateTime? left, IDateTime? right)
=> left != null && right != null && left.AsUtc < right.AsUtc;

public static bool operator >(CalDateTime left, IDateTime right)
public static bool operator >(CalDateTime? left, IDateTime? right)
=> left != null && right != null && left.AsUtc > right.AsUtc;

public static bool operator <=(CalDateTime left, IDateTime right)
public static bool operator <=(CalDateTime? left, IDateTime? right)
=> left != null && right != null && left.AsUtc <= right.AsUtc;

public static bool operator >=(CalDateTime left, IDateTime right)
public static bool operator >=(CalDateTime? left, IDateTime? right)
=> left != null && right != null && left.AsUtc >= right.AsUtc;

public static bool operator ==(CalDateTime left, IDateTime right)
public static bool operator ==(CalDateTime? left, IDateTime? right)
{
return ReferenceEquals(left, null) || ReferenceEquals(right, null)
? ReferenceEquals(left, right)
Expand All @@ -295,13 +297,13 @@ public override int GetHashCode()
&& string.Equals(left.TzId, right.TzId, StringComparison.OrdinalIgnoreCase);
}

public static bool operator !=(CalDateTime left, IDateTime right)
public static bool operator !=(CalDateTime? left, IDateTime? right)
=> !(left == right);

public static TimeSpan operator -(CalDateTime left, IDateTime right)
public static TimeSpan? operator -(CalDateTime? left, IDateTime? right)
{
left.AssociateWith(right);
return left.AsUtc - right.AsUtc;
left?.AssociateWith(right);
return left?.AsUtc - right?.AsUtc;
}

public static IDateTime operator -(CalDateTime left, TimeSpan right)
Expand Down Expand Up @@ -438,7 +440,7 @@ public bool HasTime
}
}

private string _tzId = string.Empty;
private string? _tzId = string.Empty;

/// <summary>
/// Setting the <see cref="TzId"/> to a local time zone will set <see cref="Value"/> to <see cref="DateTimeKind.Local"/>.
Expand All @@ -447,7 +449,7 @@ public bool HasTime
/// Setting the TzId will NOT incur a UTC offset conversion.
/// To convert to another time zone, use <see cref="ToTimeZone"/>.
/// </summary>
public string TzId
public string? TzId
{
get
{
Expand Down Expand Up @@ -487,7 +489,7 @@ public string TzId
/// Gets the time zone name, if it references a time zone.
/// This is an alias for <see cref="TzId"/>.
/// </summary>
public string TimeZoneName => TzId;
public string? TimeZoneName => TzId;

/// <inheritdoc cref="DateTime.Year"/>
public int Year => Value.Year;
Expand Down Expand Up @@ -528,7 +530,7 @@ public string TzId
/// <summary>
/// Returns a representation of the <see cref="IDateTime"/> in the <paramref name="tzId"/> time zone
/// </summary>
public IDateTime ToTimeZone(string tzId)
public IDateTime ToTimeZone(string? tzId)
{
// If TzId is empty, it's a system-local datetime, so we should use the system time zone as the starting point.
var originalTzId = string.IsNullOrWhiteSpace(TzId)
Expand Down Expand Up @@ -558,7 +560,7 @@ public IDateTime ToTimeZone(string tzId)

public IDateTime Subtract(TimeSpan ts) => this - ts;

public TimeSpan Subtract(IDateTime dt) => this - dt;
public TimeSpan Subtract(IDateTime dt) => (TimeSpan)(this - dt)!;

/// <inheritdoc cref="DateTime.AddYears"/>
public IDateTime AddYears(int years)
Expand Down Expand Up @@ -658,13 +660,13 @@ public IDateTime AddTicks(long ticks)
/// Associates the current instance with the specified <see cref="IDateTime"/> object.
/// </summary>
/// <param name="dt">The <see cref="IDateTime"/> object to associate with.</param>
public void AssociateWith(IDateTime dt)
public void AssociateWith(IDateTime? dt)
{
if (AssociatedObject == null && dt.AssociatedObject != null)
if (AssociatedObject == null && dt?.AssociatedObject != null)
{
AssociatedObject = dt.AssociatedObject;
}
else if (AssociatedObject != null && dt.AssociatedObject == null)
else if (AssociatedObject != null && dt?.AssociatedObject == null && dt != null)
{
dt.AssociatedObject = AssociatedObject;
}
Expand All @@ -679,12 +681,18 @@ public void AssociateWith(IDateTime dt)
/// Zero: This instance is equal to <paramref name="dt"/>.
/// Greater than zero: This instance is greater than <paramref name="dt"/>.
/// </returns>
public int CompareTo(IDateTime dt)
public int CompareTo(IDateTime? dt)
{
if (Equals(dt))
{
return 0;
}

if (dt == null)
{
return 1;
}

if (this < dt)
{
return -1;
Expand All @@ -698,10 +706,10 @@ public int CompareTo(IDateTime dt)
public override string ToString() => ToString(null, null);

/// <inheritdoc cref="ToString()"/>
public string ToString(string format) => ToString(format, null);
public string ToString(string? format) => ToString(format, null);

/// <inheritdoc cref="ToString()"/>
public string ToString(string format, IFormatProvider formatProvider)
public string ToString(string? format, IFormatProvider? formatProvider)
{
formatProvider ??= CultureInfo.InvariantCulture;
var dateTimeOffset = AsDateTimeOffset;
Expand Down
7 changes: 4 additions & 3 deletions Ical.Net/Serialization/DataTypes/DateTimeSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Ical.Net.DataTypes;
#nullable enable
using Ical.Net.DataTypes;
using System;
using System.IO;
using System.Text;
Expand All @@ -24,7 +25,7 @@ public DateTimeSerializer(SerializationContext ctx) : base(ctx) { }

public override Type TargetType => typeof(CalDateTime);

public override string SerializeToString(object obj)
public override string? SerializeToString(object obj)
{
if (obj is not IDateTime dt)
{
Expand Down Expand Up @@ -80,7 +81,7 @@ public override string SerializeToString(object obj)
internal static readonly Regex DateOnlyMatch = new Regex(@"^((\d{4})(\d{2})(\d{2}))?$", Options, RegexDefaults.Timeout);
internal static readonly Regex FullDateTimePatternMatch = new Regex(@"^((\d{4})(\d{2})(\d{2}))T((\d{2})(\d{2})(\d{2})(Z)?)$", Options, RegexDefaults.Timeout);

public override object Deserialize(TextReader tr)
public override object? Deserialize(TextReader tr)
{
var value = tr.ReadToEnd();

Expand Down
2 changes: 1 addition & 1 deletion Ical.Net/Serialization/DataTypes/PeriodSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public override object Deserialize(TextReader tr)
}

// Only return an object if it has been deserialized correctly.
if (p.StartTime != null && p.Duration != null)
if (p.StartTime != null)
{
return p;
}
Expand Down

0 comments on commit 56867a1

Please sign in to comment.