< Summary

Information
Class: MyNet.Metadata.TypeMetadataExtensions
Assembly: MyNet.Metadata
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Metadata/Extensions/TypeMetadataExtensions.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 5
Uncovered lines: 0
Coverable lines: 5
Total lines: 49
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
WithFeature(...)100%22100%
TryGetFeature(...)100%22100%
GetFeatureOrDefault(...)100%22100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Metadata/Extensions/TypeMetadataExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="TypeMetadataExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Diagnostics.CodeAnalysis;
 9using System.Linq;
 10
 11#pragma warning disable IDE0130
 12namespace MyNet.Metadata;
 13#pragma warning restore IDE0130
 14
 15/// <summary>
 16/// Query extensions for <see cref="TypeMetadata"/> (runtime consumption, not fluent authoring).
 17/// </summary>
 18public static class TypeMetadataExtensions
 19{
 20    extension(TypeMetadata metadata)
 21    {
 22        /// <summary>
 23        /// Gets the names of properties that have a feature of the specified type.
 24        /// </summary>
 25        public string[] WithFeature<TFeature>(Func<TFeature, bool>? predicate = null)
 26            where TFeature : class
 1702827            => [.. metadata.Properties.Where(p => p.Value.TryGetFeature<TFeature>(out var feature) && (predicate is null
 1702828                .Select(p => p.Key)];
 29
 30        /// <summary>
 31        /// Tries to get a feature for the specified property.
 32        /// </summary>
 33        public bool TryGetFeature<TFeature>(string propertyName, [NotNullWhen(true)] out TFeature? feature)
 34            where TFeature : class
 35        {
 3936            feature = null;
 37
 3938            return metadata.Properties.TryGetValue(propertyName, out var property) && property.TryGetFeature(out feature
 39        }
 40
 41        /// <summary>
 42        /// Gets a feature for the specified property, or <c>null</c> when missing.
 43        /// </summary>
 44        public TFeature? GetFeatureOrDefault<TFeature>(string propertyName)
 45            where TFeature : class
 3046            => metadata.TryGetFeature<TFeature>(propertyName, out var feature) ? feature : null;
 47    }
 48}
 49