-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
KeyVaultSettingValue.cs
89 lines (76 loc) · 3.4 KB
/
KeyVaultSettingValue.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace Azure.Security.KeyVault.Administration
{
/// <summary>
/// An account setting value.
/// </summary>
public readonly struct KeyVaultSettingValue
{
internal KeyVaultSettingValue(string value, KeyVaultSettingType? settingType)
{
// Service may not response with settingType for boolean (originally only supported value type).
if (settingType == null || settingType == KeyVaultSettingType.Boolean)
{
if (!bool.TryParse(value, out bool result))
{
throw InvalidSettingType(KeyVaultSettingType.Boolean);
}
ValueBool = result;
SettingType = KeyVaultSettingType.Boolean;
}
else
{
ValueString = value;
SettingType = settingType;
}
}
internal KeyVaultSettingValue(bool value)
{
ValueBool = value;
SettingType = KeyVaultSettingType.Boolean;
}
/// <summary>
/// Gets the type specifier of the value.
/// </summary>
internal KeyVaultSettingType? SettingType { get; }
/// <summary>
/// Gets the boolean value of this account setting if <see cref="Type"/> is <see cref="KeyVaultSettingType.Boolean"/>.
/// </summary>
/// <returns>A boolean value if <see cref="Type"/> is <see cref="KeyVaultSettingType.Boolean"/>.</returns>
/// <exception cref="InvalidOperationException">The <see cref="Type"/> is not <see cref="KeyVaultSettingType.Boolean"/>, or the value cannot be normalized as a Boolean.</exception>
public bool AsBoolean() => CheckType(KeyVaultSettingType.Boolean, ValueBool);
/// <summary>
/// Gets the raw string value of this account setting.
/// </summary>
/// <returns>The raw string value of this account setting.</returns>
public override string ToString()
{
if (SettingType == KeyVaultSettingType.Boolean)
{
// bool.ToString() returns "True" or "False", but the service wants "true" or "false".
return ValueBool.Value ? "true" : "false";
}
// Though a value is required, ToString() should never return null.
return ValueString ?? string.Empty;
}
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private bool? ValueBool { get; }
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string ValueString { get; }
private T CheckType<T>(KeyVaultSettingType expectedType, T? value) where T : struct
{
if (SettingType != expectedType)
{
throw new InvalidOperationException($"Cannot get setting as {expectedType}. Setting type is {SettingType}.");
}
return value.Value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static InvalidOperationException InvalidSettingType(KeyVaultSettingType expectedType) =>
new($"Cannot normalize the setting as {expectedType}. Use '{nameof(KeyVaultSetting)}.{nameof(KeyVaultSetting.Value)}.ToString()' for a textual representation of the setting.");
}
}