-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathTypedComponentMapping.cs
More file actions
59 lines (55 loc) · 2.94 KB
/
TypedComponentMapping.cs
File metadata and controls
59 lines (55 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#nullable disable
namespace Microsoft.ComponentDetection.Contracts.BcdeModels;
using System;
using System.Collections.Generic;
using Microsoft.ComponentDetection.Contracts.TypedComponent;
/// <summary>
/// Provides a shared mapping between component type discriminator strings and their corresponding concrete TypedComponent types.
/// This mapping is used by both Newtonsoft.Json and System.Text.Json converters for polymorphic serialization.
/// </summary>
internal static class TypedComponentMapping
{
/// <summary>
/// Gets the dictionary mapping type discriminator strings to their corresponding concrete types.
/// The keys are case-insensitive to handle variations in JSON serialization.
/// </summary>
public static IReadOnlyDictionary<string, Type> TypeDiscriminatorToType { get; } = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase)
{
{ nameof(ComponentType.Other), typeof(OtherComponent) },
{ nameof(ComponentType.NuGet), typeof(NuGetComponent) },
{ nameof(ComponentType.Npm), typeof(NpmComponent) },
{ nameof(ComponentType.Maven), typeof(MavenComponent) },
{ nameof(ComponentType.Git), typeof(GitComponent) },
{ nameof(ComponentType.RubyGems), typeof(RubyGemsComponent) },
{ nameof(ComponentType.Cargo), typeof(CargoComponent) },
{ nameof(ComponentType.Conan), typeof(ConanComponent) },
{ nameof(ComponentType.Pip), typeof(PipComponent) },
{ nameof(ComponentType.Go), typeof(GoComponent) },
{ nameof(ComponentType.DockerImage), typeof(DockerImageComponent) },
{ nameof(ComponentType.Pod), typeof(PodComponent) },
{ nameof(ComponentType.Linux), typeof(LinuxComponent) },
{ nameof(ComponentType.Conda), typeof(CondaComponent) },
{ nameof(ComponentType.ContainerImageReference), typeof(ContainerImageReferenceComponent) },
{ "DockerReference", typeof(ContainerImageReferenceComponent) },
{ nameof(ComponentType.Vcpkg), typeof(VcpkgComponent) },
{ nameof(ComponentType.Spdx), typeof(SpdxComponent) },
{ nameof(ComponentType.DotNet), typeof(DotNetComponent) },
{ nameof(ComponentType.Swift), typeof(SwiftComponent) },
{ nameof(ComponentType.CppSdk), typeof(CppSdkComponent) },
};
/// <summary>
/// Tries to get the concrete type for a given type discriminator string.
/// </summary>
/// <param name="typeDiscriminator">The type discriminator string from JSON.</param>
/// <param name="targetType">When successful, contains the concrete type; otherwise null.</param>
/// <returns>True if the type discriminator was recognized; otherwise false.</returns>
public static bool TryGetType(string typeDiscriminator, out Type targetType)
{
if (string.IsNullOrWhiteSpace(typeDiscriminator))
{
targetType = null;
return false;
}
return TypeDiscriminatorToType.TryGetValue(typeDiscriminator, out targetType);
}
}