• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

alunacjones / LSL.VariableReplacer / 8

01 Apr 2025 03:04PM UTC coverage: 97.581% (-2.4%) from 100.0%
8

push

appveyor

web-flow
Merge pull request #1 from alunacjones/feature/partials

Feature/partials

121 of 124 relevant lines covered (97.58%)

29.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.11
/LSL.VariableReplacer/VariableReplacerConfiguration.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4

5
namespace LSL.VariableReplacer;
6

7
/// <summary>
8
/// The variable replacer configuration
9
/// </summary>
10
public sealed class VariableReplacerConfiguration : ICanAddVariables<VariableReplacerConfiguration>
11
{
12
    internal VariableReplacerConfiguration() {}
34✔
13
    
14
    internal VariableReplacerConfiguration(
1✔
15
        IDictionary<string, object> variables,
1✔
16
        ITransformer transformer,
1✔
17
        Func<string, string> variableNotFound,
1✔
18
        Func<object, string> valueFormatter,
1✔
19
        Action<IDictionary<string, object>, string, object> addToDictionaryDelelgate)
1✔
20
    {
21
        Variables = variables;
1✔
22
        Transformer = transformer;
1✔
23
        VariableNotFound = variableNotFound;
1✔
24
        ValueFormatter = valueFormatter;
1✔
25
        AddToDictionaryDelelgate = addToDictionaryDelelgate;
1✔
26
    }
1✔
27

28
    internal IDictionary<string, object> Variables { get; } = new Dictionary<string, object>();
208✔
29
    internal ITransformer Transformer { get; private set; } = new RegexTransformer();
49✔
30
    internal Func<string, string> VariableNotFound { get; private set; } = variableName => $"NOTFOUND:{variableName}";
×
31
    internal Func<object, string> ValueFormatter { get; private set; } = value => $"{value}";
×
32
    internal Action<IDictionary<string, object>, string, object> AddToDictionaryDelelgate = (dictionary, name, value) => dictionary.Add(name, value);
×
33

34
    /// <inheritdoc/>
35
    public VariableReplacerConfiguration AddVariable(string name, object value)
36
    {
37
        Guard.IsNotNull(name, nameof(name));
142✔
38

39
        AddToDictionaryDelelgate(Variables, name, value);
141✔
40
        return this;
140✔
41
    }
42

43
    /// <summary>
44
    /// Use a custom transformer
45
    /// </summary>
46
    /// <remarks>
47
    /// The default transformer uses a <see cref="System.Text.RegularExpressions.Regex"/>
48
    /// That matches variables of the form <c>$(VariableName)</c>
49
    /// </remarks>
50
    /// <param name="transformer">A custom transformer</param>
51
    /// <returns></returns>
52
    public VariableReplacerConfiguration WithTransformer(ITransformer transformer)
53
    {
54
        Guard.IsNotNull(transformer, nameof(transformer));
3✔
55

56
        Transformer = transformer;
3✔
57
        return this;
3✔
58
    }
59

60
    /// <summary>
61
    /// Customise the pattern used for the default Regex-based transformer
62
    /// </summary>
63
    /// <param name="variablePlaceholderPrefix"></param>
64
    /// <param name="variablePlaceholderSuffix"></param>
65
    /// <param name="commandProcessor">
66
    /// A delegate that accepts a <c>command</c> and a <c>value</c> paramter.
67
    /// Based on what the provided command is it can then return a modified version
68
    /// of the value if required.
69
    /// </param>
70
    /// <returns></returns>
71
    public VariableReplacerConfiguration WithDefaultTransformer(
72
        string variablePlaceholderPrefix = "$(",
73
        string variablePlaceholderSuffix = ")",
74
        CommandProcessingDelegate commandProcessor = null) =>
75
        WithTransformer(new RegexTransformer(variablePlaceholderPrefix, variablePlaceholderSuffix, commandProcessor));
3✔
76

77
    /// <summary>
78
    /// Provide a custom replacement string
79
    /// for a variable placeholder if it is not found
80
    /// </summary>
81
    /// <remarks>
82
    /// An exception can be thrown instead of returning a string
83
    /// </remarks>
84
    /// <param name="whenVariableNotFound"></param>
85
    /// <returns></returns>
86
    public VariableReplacerConfiguration WhenVariableNotFound(Func<string, string> whenVariableNotFound)
87
    {
88
        Guard.IsNotNull(whenVariableNotFound, nameof(whenVariableNotFound));
1✔
89

90
        VariableNotFound = whenVariableNotFound;
1✔
91
        return this;
1✔
92
    }
93

94
    /// <summary>
95
    /// Throw an exception if a variable is not found
96
    /// </summary>
97
    /// <returns></returns>
98
    /// <exception cref="ArgumentException"></exception>
99
    public VariableReplacerConfiguration ThrowIfVariableNotFound() =>
100
        WhenVariableNotFound(variableName => throw new ArgumentException($"Variable '{variableName}' not found"));
2✔
101

102
    /// <summary>
103
    /// Optional formatter of a variable value
104
    /// </summary>
105
    /// <remarks>
106
    /// The default formatter will use <see cref="object.ToString"><c>ToString</c></see>
107
    /// to format all objects. <see langword="null"><c>null</c></see>
108
    /// will result in an empty string
109
    /// </remarks>
110
    /// <param name="formatter"></param>
111
    /// <returns></returns>
112
    public VariableReplacerConfiguration WithValueFormatter(Func<object, string> formatter)
113
    {
114
        Guard.IsNotNull(formatter, nameof(formatter));
1✔
115

116
        ValueFormatter = formatter;
1✔
117
        return this;
1✔
118
    }
119

120
    /// <inheritdoc/>
121
    public VariableReplacerConfiguration WithAddToDictionaryDelelgate(Action<IDictionary<string, object>, string, object> action)
122
    {
123
        Guard.IsNotNull(action, nameof(action));
2✔
124

125
        AddToDictionaryDelelgate = action;
2✔
126
        return this;
2✔
127
    }
128

129
    /// <summary>
130
    /// Uses replace variable behaviour when
131
    /// adding to the variables dictionary
132
    /// </summary>
133
    /// <returns></returns>
134
    public VariableReplacerConfiguration WithReplaceVariableBehaviour() =>
135
        WithAddToDictionaryDelelgate((dicionary, name, value) => dicionary[name] = value);
5✔
136

137
    internal VariableReplacerConfiguration Clone() =>
138
        new(CopyDictionary(Variables), Transformer, VariableNotFound, ValueFormatter, AddToDictionaryDelelgate);
1✔
139

140
    private static IDictionary<string, object> CopyDictionary(IDictionary<string, object> source) =>
141
        source.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
7✔
142
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc