[homekit] Add nullability to (generated and manual) bindings (#15111)

* go through the nullable ignores and add test

* Enable Nullability

* use is null

* save a few lines

Co-authored-by: TJ Lambert <tjlambert@microsoft.com>
This commit is contained in:
TJ Lambert 2022-06-06 10:05:38 -05:00 коммит произвёл GitHub
Родитель a2768dd887
Коммит 9bf39cd985
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
12 изменённых файлов: 98 добавлений и 61 удалений

Просмотреть файл

@ -1,3 +1,5 @@
#nullable enable
using System;
using ObjCRuntime;
using Foundation;
@ -17,7 +19,7 @@ namespace HomeKit {
get {
var s = _ActionSetType;
// safety in case the field does not exists / cannot be loaded / new in future iOS versions...
if (s == null)
if (s is null)
return HMActionSetType.Unknown;
if (s == HMActionSetTypesInternal.WakeUp)
return HMActionSetType.WakeUp;

Просмотреть файл

@ -1,3 +1,5 @@
#nullable enable
using System;
using ObjCRuntime;
using Foundation;

Просмотреть файл

@ -1,3 +1,5 @@
#nullable enable
using System;
using ObjCRuntime;

Просмотреть файл

@ -1,3 +1,5 @@
#nullable enable
using System;
using ObjCRuntime;

Просмотреть файл

@ -98,8 +98,10 @@ namespace HomeKit {
public partial class HMAccessorySetupManager {
#pragma warning disable CS0618 // HMChipServiceTopology and HMErrorHandler is obsolete
public virtual void AddAndSetUpAccessories (HMChipServiceTopology topology, HMErrorHandler completion) => throw new InvalidOperationException ();
public virtual Task AddAndSetUpAccessoriesAsync (HMChipServiceTopology topology) => throw new InvalidOperationException ();
#pragma warning restore CS0618 // HMChipServiceTopology and HMErrorHandler is obsolete
} /* class HMAccessorySetupManager */

Просмотреть файл

@ -1,3 +1,5 @@
#nullable enable
using System;
using ObjCRuntime;
using Foundation;
@ -24,7 +26,10 @@ namespace HomeKit {
#endif
static public NSPredicate CreatePredicateForEvaluatingTriggerOccurringBeforeSignificantEvent (HMSignificantEvent significantEvent, NSDateComponents offset)
{
return CreatePredicateForEvaluatingTriggerOccurringBeforeSignificantEvent (significantEvent.GetConstant (), offset);
var constant = significantEvent.GetConstant ();
if (constant is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (significantEvent));
return CreatePredicateForEvaluatingTriggerOccurringBeforeSignificantEvent (constant, offset);
}
#if NET
@ -45,7 +50,10 @@ namespace HomeKit {
#endif
static public NSPredicate CreatePredicateForEvaluatingTriggerOccurringAfterSignificantEvent (HMSignificantEvent significantEvent, NSDateComponents offset)
{
return CreatePredicateForEvaluatingTriggerOccurringAfterSignificantEvent (significantEvent.GetConstant (), offset);
var constant = significantEvent.GetConstant ();
if (constant is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (significantEvent));
return CreatePredicateForEvaluatingTriggerOccurringAfterSignificantEvent (constant, offset);
}
}
}

Просмотреть файл

@ -1,3 +1,5 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
@ -8,9 +10,9 @@ namespace HomeKit {
public partial class HMHome
{
public HMService [] GetServices (HMServiceType serviceTypes)
public HMService []? GetServices (HMServiceType serviceTypes)
{
var arr = new List<NSString> ();
var arr = new ServiceTypeList<NSString> ();
if ((serviceTypes & HMServiceType.LightBulb) == HMServiceType.LightBulb)
arr.Add (HMServiceType.LightBulb.GetConstant ());
@ -80,6 +82,14 @@ namespace HomeKit {
return GetServices (arr.ToArray ());
}
class ServiceTypeList<T> : List<T> {
public new void Add (T? item)
{
if (item is not null)
base.Add (item);
}
}
#if !NET
[NoTV]
[NoWatch]

Просмотреть файл

@ -1,3 +1,5 @@
#nullable enable
using System;
using System.Threading.Tasks;

Просмотреть файл

@ -39,7 +39,7 @@ namespace HomeKit {
[Protocolize]
HMHomeManagerDelegate Delegate { get; set; }
[Export ("primaryHome", ArgumentSemantic.Retain)]
[NullAllowed, Export ("primaryHome", ArgumentSemantic.Retain)]
HMHome PrimaryHome { get; }
[Export ("homes", ArgumentSemantic.Copy)]
@ -391,10 +391,10 @@ namespace HomeKit {
[Export ("properties", ArgumentSemantic.Copy)]
NSString [] Properties { get; }
[Export ("metadata", ArgumentSemantic.Retain)]
[NullAllowed, Export ("metadata", ArgumentSemantic.Retain)]
HMCharacteristicMetadata Metadata { get; }
[Export ("value", ArgumentSemantic.Copy)]
[NullAllowed, Export ("value", ArgumentSemantic.Copy)]
NSObject Value { get; }
[Export ("notificationEnabled")]
@ -416,7 +416,7 @@ namespace HomeKit {
[NoWatch]
[Async]
[Export ("updateAuthorizationData:completionHandler:")]
void UpdateAuthorizationData (NSData data, Action<NSError> completion);
void UpdateAuthorizationData ([NullAllowed] NSData data, Action<NSError> completion);
[iOS (9,0)]
[Export ("localizedDescription")]
@ -500,27 +500,27 @@ namespace HomeKit {
[BaseType (typeof (NSObject))]
partial interface HMCharacteristicMetadata {
[Export ("minimumValue")]
[NullAllowed, Export ("minimumValue")]
NSNumber MinimumValue { get; }
[Export ("maximumValue")]
[NullAllowed, Export ("maximumValue")]
NSNumber MaximumValue { get; }
[Export ("stepValue")]
[NullAllowed, Export ("stepValue")]
NSNumber StepValue { get; }
[Export ("maxLength")]
[NullAllowed, Export ("maxLength")]
NSNumber MaxLength { get; }
[Internal]
[Export ("format", ArgumentSemantic.Copy)]
[NullAllowed, Export ("format", ArgumentSemantic.Copy)]
NSString _Format { get; }
[Internal]
[Export ("units", ArgumentSemantic.Copy)]
[NullAllowed, Export ("units", ArgumentSemantic.Copy)]
NSString _Units { get; }
[Export ("manufacturerDescription")]
[NullAllowed, Export ("manufacturerDescription")]
string ManufacturerDescription { get; }
[Watch (3,0), iOS (10,0)]
@ -626,6 +626,7 @@ namespace HomeKit {
[EditorBrowsable (EditorBrowsableState.Advanced)]
[Export ("servicesWithTypes:")]
[return: NullAllowed]
HMService [] GetServices (NSString [] serviceTypes);
[NoTV]
@ -934,7 +935,7 @@ namespace HomeKit {
[Export ("name")]
string Name { get; }
[Export ("associatedServiceType")]
[NullAllowed, Export ("associatedServiceType")]
string AssociatedServiceType { get; }
[Export ("characteristics", ArgumentSemantic.Copy)]
@ -1026,13 +1027,13 @@ namespace HomeKit {
[Export ("fireDate", ArgumentSemantic.Copy)]
NSDate FireDate { get; }
[Export ("timeZone", ArgumentSemantic.Copy)]
[NullAllowed, Export ("timeZone", ArgumentSemantic.Copy)]
NSTimeZone TimeZone { get; }
[Export ("recurrence", ArgumentSemantic.Copy)]
[NullAllowed, Export ("recurrence", ArgumentSemantic.Copy)]
NSDateComponents Recurrence { get; }
[Export ("recurrenceCalendar", ArgumentSemantic.Copy)]
[NullAllowed, Export ("recurrenceCalendar", ArgumentSemantic.Copy)]
NSCalendar RecurrenceCalendar { get; }
[NoTV]
@ -1070,7 +1071,7 @@ namespace HomeKit {
[Export ("actionSets", ArgumentSemantic.Copy)]
HMActionSet [] ActionSets { get; }
[Export ("lastFireDate", ArgumentSemantic.Copy)]
[NullAllowed, Export ("lastFireDate", ArgumentSemantic.Copy)]
NSDate LastFireDate { get; }
[NoTV]

Просмотреть файл

@ -0,0 +1,36 @@
//
// Unit tests for HMCharacteristicTest
//
// Authors:
// TJ Lambert <TJ.Lambert@microsoft.com>
//
//
// Copyright 2022 Microsoft. All rights reserved.
//
#if HAS_HOMEKIT
using System;
using NUnit.Framework;
using Foundation;
using HomeKit;
using ObjCRuntime;
using Xamarin.Utils;
namespace MonoTouchFixtures.HomeKit
{
[TestFixture]
[Preserve (AllMembers = true)]
public class HMCharacteristicTest
{
[Test]
public void WriteValueNullTest ()
{
var characteristic = new HMCharacteristic ();
Assert.Throws<ArgumentNullException> (delegate { characteristic.WriteValue (null, null); }, $"WriteValue should accept a null argument for 'value'.");
}
}
}
#endif // HAS_HOMEKIT

Просмотреть файл

@ -2,25 +2,10 @@
!missing-selector! HMMutablePresenceEvent::setPresenceEventType: not bound
!missing-selector! HMMutablePresenceEvent::setPresenceUserType: not bound
# Initial result from new rule missing-null-allowed (Consolidating to common-HomeKit.ignore)
!missing-null-allowed! 'Foundation.NSCalendar HomeKit.HMTimerTrigger::get_RecurrenceCalendar()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSDate HomeKit.HMTrigger::get_LastFireDate()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSDateComponents HomeKit.HMTimerTrigger::get_Recurrence()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSNumber HomeKit.HMCharacteristicMetadata::get_MaximumValue()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSNumber HomeKit.HMCharacteristicMetadata::get_MaxLength()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSNumber HomeKit.HMCharacteristicMetadata::get_MinimumValue()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSNumber HomeKit.HMCharacteristicMetadata::get_StepValue()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSObject HomeKit.HMCharacteristic::get_Value()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSString HomeKit.HMCharacteristicMetadata::get__Format()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSString HomeKit.HMCharacteristicMetadata::get__Units()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSTimeZone HomeKit.HMTimerTrigger::get_TimeZone()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'HomeKit.HMAccessory HomeKit.HMService::get_Accessory()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'HomeKit.HMCharacteristicMetadata HomeKit.HMCharacteristic::get_Metadata()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'HomeKit.HMHome HomeKit.HMHomeManager::get_PrimaryHome()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'HomeKit.HMRoom HomeKit.HMAccessory::get_Room()' is missing an [NullAllowed] on return type
# Xcode headers and Web docs agree that these values should not be nullable
!missing-null-allowed! 'HomeKit.HMService HomeKit.HMCharacteristic::get_Service()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'HomeKit.HMService[] HomeKit.HMHome::GetServices(Foundation.NSString[])' is missing an [NullAllowed] on return type
!missing-null-allowed! 'System.String HomeKit.HMCharacteristicMetadata::get_ManufacturerDescription()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'System.String HomeKit.HMService::get_AssociatedServiceType()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'System.Void HomeKit.HMCharacteristic::UpdateAuthorizationData(Foundation.NSData,System.Action`1<Foundation.NSError>)' is missing an [NullAllowed] on parameter #0
!missing-null-allowed! 'HomeKit.HMAccessory HomeKit.HMService::get_Accessory()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'HomeKit.HMRoom HomeKit.HMAccessory::get_Room()' is missing an [NullAllowed] on return type
# Xcode headers say nullable but web docs do not. After testing, this parameter cannot be null.
!missing-null-allowed! 'System.Void HomeKit.HMCharacteristic::WriteValue(Foundation.NSObject,System.Action`1<Foundation.NSError>)' is missing an [NullAllowed] on parameter #0

Просмотреть файл

@ -2,25 +2,10 @@
!missing-selector! HMMutablePresenceEvent::setPresenceEventType: not bound
!missing-selector! HMMutablePresenceEvent::setPresenceUserType: not bound
# Initial result from new rule missing-null-allowed (Consolidating to common-HomeKit.ignore)
!missing-null-allowed! 'Foundation.NSCalendar HomeKit.HMTimerTrigger::get_RecurrenceCalendar()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSDate HomeKit.HMTrigger::get_LastFireDate()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSDateComponents HomeKit.HMTimerTrigger::get_Recurrence()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSNumber HomeKit.HMCharacteristicMetadata::get_MaximumValue()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSNumber HomeKit.HMCharacteristicMetadata::get_MaxLength()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSNumber HomeKit.HMCharacteristicMetadata::get_MinimumValue()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSNumber HomeKit.HMCharacteristicMetadata::get_StepValue()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSObject HomeKit.HMCharacteristic::get_Value()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSString HomeKit.HMCharacteristicMetadata::get__Format()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSString HomeKit.HMCharacteristicMetadata::get__Units()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSTimeZone HomeKit.HMTimerTrigger::get_TimeZone()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'HomeKit.HMAccessory HomeKit.HMService::get_Accessory()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'HomeKit.HMCharacteristicMetadata HomeKit.HMCharacteristic::get_Metadata()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'HomeKit.HMHome HomeKit.HMHomeManager::get_PrimaryHome()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'HomeKit.HMRoom HomeKit.HMAccessory::get_Room()' is missing an [NullAllowed] on return type
# Xcode headers and Web docs agree that these values should not be nullable
!missing-null-allowed! 'HomeKit.HMService HomeKit.HMCharacteristic::get_Service()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'HomeKit.HMService[] HomeKit.HMHome::GetServices(Foundation.NSString[])' is missing an [NullAllowed] on return type
!missing-null-allowed! 'System.String HomeKit.HMCharacteristicMetadata::get_ManufacturerDescription()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'System.String HomeKit.HMService::get_AssociatedServiceType()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'System.Void HomeKit.HMCharacteristic::UpdateAuthorizationData(Foundation.NSData,System.Action`1<Foundation.NSError>)' is missing an [NullAllowed] on parameter #0
!missing-null-allowed! 'HomeKit.HMAccessory HomeKit.HMService::get_Accessory()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'HomeKit.HMRoom HomeKit.HMAccessory::get_Room()' is missing an [NullAllowed] on return type
# Xcode headers say nullable but web docs do not. After testing, this parameter cannot be null.
!missing-null-allowed! 'System.Void HomeKit.HMCharacteristic::WriteValue(Foundation.NSObject,System.Action`1<Foundation.NSError>)' is missing an [NullAllowed] on parameter #0