< Summary

Information
Class: NexusLabs.Needlr.AgentFramework.Generators.GraphDiscoveryHelper
Assembly: NexusLabs.Needlr.AgentFramework.Generators
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr.AgentFramework.Generators/GraphDiscoveryHelper.cs
Line coverage
93%
Covered lines: 83
Uncovered lines: 6
Coverable lines: 89
Total lines: 194
Line coverage: 93.2%
Branch coverage
85%
Covered branches: 67
Total branches: 78
Branch coverage: 85.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetGraphEdgeEntries(...)92.85%282896.77%
GetGraphEntryPointEntries(...)68.75%171684.21%
GetGraphNodeEntries(...)87.5%161694.73%
GetGraphReducerEntries(...)88.88%181895%

File(s)

/home/runner/work/needlr/needlr/src/NexusLabs.Needlr.AgentFramework.Generators/GraphDiscoveryHelper.cs

#LineLine coverage
 1// Copyright (c) NexusLabs. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System.Collections.Immutable;
 5using System.Threading;
 6
 7using Microsoft.CodeAnalysis;
 8
 9namespace NexusLabs.Needlr.AgentFramework.Generators;
 10
 11internal static class GraphDiscoveryHelper
 12{
 13    public static ImmutableArray<GraphEdgeEntry> GetGraphEdgeEntries(
 14        GeneratorAttributeSyntaxContext context,
 15        CancellationToken cancellationToken)
 16    {
 1917        var typeSymbol = context.TargetSymbol as INamedTypeSymbol;
 1918        if (typeSymbol is null || !AgentDiscoveryHelper.IsAccessibleFromGeneratedCode(typeSymbol))
 019            return ImmutableArray<GraphEdgeEntry>.Empty;
 20
 1921        var sourceTypeName = AgentDiscoveryHelper.GetFullyQualifiedName(typeSymbol);
 1922        var entries = ImmutableArray.CreateBuilder<GraphEdgeEntry>();
 23
 8424        foreach (var attr in context.Attributes)
 25        {
 2326            if (attr.ConstructorArguments.Length < 2)
 27                continue;
 28
 2329            var graphName = attr.ConstructorArguments[0].Value as string;
 2330            if (string.IsNullOrWhiteSpace(graphName))
 31                continue;
 32
 2333            var typeArg = attr.ConstructorArguments[1];
 2334            if (typeArg.Kind != TypedConstantKind.Type || typeArg.Value is not INamedTypeSymbol targetTypeSymbol)
 35                continue;
 36
 2337            var targetTypeName = AgentDiscoveryHelper.GetFullyQualifiedName(targetTypeSymbol);
 38
 2339            string? condition = null;
 2340            var isRequired = true;
 2341            int? nodeRoutingMode = null;
 42
 6443            foreach (var named in attr.NamedArguments)
 44            {
 945                if (named.Key == "Condition" && named.Value.Value is string conditionValue)
 46                {
 647                    condition = conditionValue;
 48                }
 349                else if (named.Key == "IsRequired" && named.Value.Value is bool isReqValue)
 50                {
 251                    isRequired = isReqValue;
 52                }
 153                else if (named.Key == "NodeRoutingMode" && named.Value.Value is int routeVal)
 54                {
 55                    // Presence in NamedArguments means the user explicitly set it,
 56                    // equivalent to HasNodeRoutingMode = true at runtime.
 157                    nodeRoutingMode = routeVal;
 58                }
 59            }
 60
 2361            entries.Add(new GraphEdgeEntry(
 2362                sourceTypeName,
 2363                typeSymbol.Name,
 2364                graphName!,
 2365                targetTypeName,
 2366                condition,
 2367                isRequired,
 2368                nodeRoutingMode));
 69        }
 70
 1971        return entries.ToImmutable();
 72    }
 73
 74    public static ImmutableArray<GraphEntryPointEntry> GetGraphEntryPointEntries(
 75        GeneratorAttributeSyntaxContext context,
 76        CancellationToken cancellationToken)
 77    {
 2178        var typeSymbol = context.TargetSymbol as INamedTypeSymbol;
 2179        if (typeSymbol is null || !AgentDiscoveryHelper.IsAccessibleFromGeneratedCode(typeSymbol))
 080            return ImmutableArray<GraphEntryPointEntry>.Empty;
 81
 2182        var agentTypeName = AgentDiscoveryHelper.GetFullyQualifiedName(typeSymbol);
 2183        var entries = ImmutableArray.CreateBuilder<GraphEntryPointEntry>();
 84
 8485        foreach (var attr in context.Attributes)
 86        {
 2187            if (attr.ConstructorArguments.Length < 1)
 88                continue;
 89
 2190            var graphName = attr.ConstructorArguments[0].Value as string;
 2191            if (string.IsNullOrWhiteSpace(graphName))
 92                continue;
 93
 2194            var routingMode = 0;
 95
 4296            foreach (var named in attr.NamedArguments)
 97            {
 098                if (named.Key == "RoutingMode" && named.Value.Value is int routeVal)
 99                {
 0100                    routingMode = routeVal;
 101                }
 102            }
 103
 21104            entries.Add(new GraphEntryPointEntry(
 21105                agentTypeName,
 21106                typeSymbol.Name,
 21107                graphName!,
 21108                routingMode));
 109        }
 110
 21111        return entries.ToImmutable();
 112    }
 113
 114    public static ImmutableArray<GraphNodeEntry> GetGraphNodeEntries(
 115        GeneratorAttributeSyntaxContext context,
 116        CancellationToken cancellationToken)
 117    {
 2118        var typeSymbol = context.TargetSymbol as INamedTypeSymbol;
 2119        if (typeSymbol is null || !AgentDiscoveryHelper.IsAccessibleFromGeneratedCode(typeSymbol))
 0120            return ImmutableArray<GraphNodeEntry>.Empty;
 121
 2122        var agentTypeName = AgentDiscoveryHelper.GetFullyQualifiedName(typeSymbol);
 2123        var entries = ImmutableArray.CreateBuilder<GraphNodeEntry>();
 124
 8125        foreach (var attr in context.Attributes)
 126        {
 2127            if (attr.ConstructorArguments.Length < 1)
 128                continue;
 129
 2130            var graphName = attr.ConstructorArguments[0].Value as string;
 2131            if (string.IsNullOrWhiteSpace(graphName))
 132                continue;
 133
 2134            var joinMode = 0;
 135
 8136            foreach (var named in attr.NamedArguments)
 137            {
 2138                if (named.Key == "JoinMode" && named.Value.Value is int joinVal)
 139                {
 2140                    joinMode = joinVal;
 141                }
 142            }
 143
 2144            entries.Add(new GraphNodeEntry(
 2145                agentTypeName,
 2146                typeSymbol.Name,
 2147                graphName!,
 2148                joinMode));
 149        }
 150
 2151        return entries.ToImmutable();
 152    }
 153
 154    public static ImmutableArray<GraphReducerEntry> GetGraphReducerEntries(
 155        GeneratorAttributeSyntaxContext context,
 156        CancellationToken cancellationToken)
 157    {
 3158        var typeSymbol = context.TargetSymbol as INamedTypeSymbol;
 3159        if (typeSymbol is null || !AgentDiscoveryHelper.IsAccessibleFromGeneratedCode(typeSymbol))
 0160            return ImmutableArray<GraphReducerEntry>.Empty;
 161
 3162        var agentTypeName = AgentDiscoveryHelper.GetFullyQualifiedName(typeSymbol);
 3163        var entries = ImmutableArray.CreateBuilder<GraphReducerEntry>();
 164
 12165        foreach (var attr in context.Attributes)
 166        {
 3167            if (attr.ConstructorArguments.Length < 1)
 168                continue;
 169
 3170            var graphName = attr.ConstructorArguments[0].Value as string;
 3171            if (string.IsNullOrWhiteSpace(graphName))
 172                continue;
 173
 3174            string reducerMethod = "Reduce";
 175
 10176            foreach (var named in attr.NamedArguments)
 177            {
 2178                if (named.Key == "ReducerMethod" && named.Value.Value is string methodVal
 2179                    && !string.IsNullOrWhiteSpace(methodVal))
 180                {
 2181                    reducerMethod = methodVal;
 182                }
 183            }
 184
 3185            entries.Add(new GraphReducerEntry(
 3186                agentTypeName,
 3187                typeSymbol.Name,
 3188                graphName!,
 3189                reducerMethod));
 190        }
 191
 3192        return entries.ToImmutable();
 193    }
 194}