generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathTransformerManager.cs
More file actions
143 lines (129 loc) · 5.87 KB
/
TransformerManager.cs
File metadata and controls
143 lines (129 loc) · 5.87 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
using System.Collections.Concurrent;
using AWS.Lambda.Powertools.Parameters.Transform;
namespace AWS.Lambda.Powertools.Parameters.Internal.Transform;
/// <summary>
/// The TransformerManager class to manage transformers.
/// </summary>
internal class TransformerManager : ITransformerManager
{
/// <summary>
/// Thread-safe lazy initialization of the TransformerManager singleton instance.
/// Uses LazyThreadSafetyMode.ExecutionAndPublication to ensure only one instance
/// is created even under concurrent access from multiple threads.
/// </summary>
private static readonly Lazy<ITransformerManager> _lazyInstance =
new Lazy<ITransformerManager>(() => new TransformerManager(), LazyThreadSafetyMode.ExecutionAndPublication);
/// <summary>
/// The JsonTransformer instance.
/// </summary>
private readonly ITransformer _jsonTransformer;
/// <summary>
/// The Base64Transformer instance.
/// </summary>
private readonly ITransformer _base64Transformer;
/// <summary>
/// Gets the TransformerManager instance in a thread-safe manner.
/// </summary>
internal static ITransformerManager Instance => _lazyInstance.Value;
/// <summary>
/// Gets the list of transformer instances.
/// </summary>
private readonly ConcurrentDictionary<string, ITransformer> _transformers = new(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// TransformerManager constructor.
/// </summary>
internal TransformerManager()
{
_jsonTransformer = new JsonTransformer();
_base64Transformer = new Base64Transformer();
}
/// <summary>
/// Gets an instance of transformer for the provided transformation type.
/// </summary>
/// <param name="transformation">Type of the transformation.</param>
/// <returns>The transformer instance</returns>
/// <exception cref="NotSupportedException"></exception>
public ITransformer GetTransformer(Transformation transformation)
{
var transformer = TryGetTransformer(transformation, string.Empty);
if (transformer is null)
throw new NotSupportedException("'Transformation.Auto' requires additional 'key' parameter");
return transformer;
}
/// <summary>
/// Gets an instance of transformer for the provided transformation type and parameter key.
/// </summary>
/// <param name="transformation">Type of the transformation.</param>
/// <param name="key">Parameter key, it's required for Transformation.Auto</param>
/// <returns>The transformer instance</returns>
public ITransformer? TryGetTransformer(Transformation transformation, string key)
{
return transformation switch
{
Transformation.Json => _transformers.GetOrAdd("json", _jsonTransformer),
Transformation.Base64 => _transformers.GetOrAdd("base64", _base64Transformer),
Transformation.Auto when key.EndsWith(".json", StringComparison.CurrentCultureIgnoreCase) =>
_transformers.GetOrAdd("json", _jsonTransformer),
Transformation.Auto when key.EndsWith(".binary", StringComparison.CurrentCultureIgnoreCase) =>
_transformers.GetOrAdd("base64", _base64Transformer),
Transformation.Auto when key.EndsWith(".base64", StringComparison.CurrentCultureIgnoreCase) =>
_transformers.GetOrAdd("base64", _base64Transformer),
_ => null
};
}
/// <summary>
/// Gets an instance of transformer for the provided transformer name.
/// </summary>
/// <param name="transformerName">The unique name for the transformer</param>
/// <returns>The transformer instance</returns>
/// <exception cref="KeyNotFoundException"></exception>
public ITransformer GetTransformer(string transformerName)
{
var transformer = TryGetTransformer(transformerName);
if (transformer is null)
throw new KeyNotFoundException($"Transformer with name '{transformerName}' not found.");
return transformer;
}
/// <summary>
/// Gets an instance of transformer for the provided transformer name.
/// </summary>
/// <param name="transformerName">The unique name for the transformer</param>
/// <returns>The transformer instance</returns>
/// <exception cref="ArgumentException"></exception>
public ITransformer? TryGetTransformer(string transformerName)
{
if (string.IsNullOrWhiteSpace(transformerName))
throw new ArgumentException("transformerName is required.");
_transformers.TryGetValue(transformerName, out var transformer);
return transformer;
}
/// <summary>
/// Add an instance of a transformer by a unique name
/// </summary>
/// <param name="transformerName">name of the transformer</param>
/// <param name="transformer">the transformer instance</param>
/// <exception cref="ArgumentException"></exception>
public void AddTransformer(string transformerName, ITransformer transformer)
{
if (string.IsNullOrWhiteSpace(transformerName))
throw new ArgumentException("transformerName is required.");
if (_transformers.ContainsKey(transformerName))
_transformers[transformerName] = transformer;
else
_transformers.TryAdd(transformerName, transformer);
}
}