Skip to content

Commit

Permalink
convert JObject to Dictionary<string, string> for secrets processing
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Oct 28, 2020
1 parent ea0e87b commit b192378
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
31 changes: 19 additions & 12 deletions src/Mobile.BuildTools/Generators/Secrets/SecretsClassGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,31 +109,38 @@ protected override void ExecuteInternal()
File.WriteAllText(intermediateFile, secretsClass);
}

internal JObject GetMergedSecrets()
internal IDictionary<string, string> GetMergedSecrets()
{
JObject secrets = null;
JObject jObject = null;
foreach (var file in SecretsFileLocations)
{
CreateOrMerge(file, ref secrets);
CreateOrMerge(file, ref jObject);
}

if (secrets is null)
if (jObject is null)
{
throw new Exception("An unexpected error occurred. Could not locate any secrets.");
}

var secrets = jObject.ToObject<Dictionary<string, string>>();

if (Build?.Configuration?.Environment != null)
{
var settings = Build.Configuration.Environment;
UpdateVariables(settings.Defaults, ref secrets);
var defaultSettings = settings.Defaults ?? new Dictionary<string, string>();
if (settings.Configuration != null && settings.Configuration.ContainsKey(Build.BuildConfiguration))
UpdateVariables(settings.Configuration[Build.BuildConfiguration], ref secrets);
{
foreach ((var key, var value) in settings.Configuration[Build.BuildConfiguration])
defaultSettings[key] = value;
}

UpdateVariables(defaultSettings, ref secrets);
}

return secrets;
}

private static void UpdateVariables(IDictionary<string, string> settings, ref JObject output)
private static void UpdateVariables(IDictionary<string, string> settings, ref Dictionary<string, string> output)
{
if (settings is null)
return;
Expand Down Expand Up @@ -164,7 +171,7 @@ internal void CreateOrMerge(string jsonFilePath, ref JObject secrets)
}
}

internal CodeWriter GenerateClass(SecretsConfig secretsConfig, JObject secrets)
internal CodeWriter GenerateClass(SecretsConfig secretsConfig, IDictionary<string, string> secrets)
{
var writer = new CodeWriter();
writer.AppendLine("// ------------------------------------------------------------------------------");
Expand Down Expand Up @@ -201,7 +208,7 @@ internal CodeWriter GenerateClass(SecretsConfig secretsConfig, JObject secrets)
return writer;
}

private void ProcessSecret(KeyValuePair<string, JToken> secret, SecretsConfig secretsConfig, ref CodeWriter writer)
private void ProcessSecret(KeyValuePair<string, string> secret, SecretsConfig secretsConfig, ref CodeWriter writer)
{
if (!secretsConfig.HasKey(secret.Key, out var valueConfig))
{
Expand All @@ -227,7 +234,7 @@ private void ProcessSecret(KeyValuePair<string, JToken> secret, SecretsConfig se
writer.AppendLine(standardOutput, safeOutput);
}

internal string ProcessSecret(KeyValuePair<string, JToken> secret, SecretsConfig secretsConfig, bool saveOutput, bool safeOutput = false)
internal string ProcessSecret(KeyValuePair<string, string> secret, SecretsConfig secretsConfig, bool saveOutput, bool safeOutput = false)
{
if (!secretsConfig.HasKey(secret.Key, out var valueConfig) && !saveOutput)
{
Expand All @@ -249,7 +256,7 @@ internal string ProcessSecret(KeyValuePair<string, JToken> secret, SecretsConfig
return PropertyBuilder(secret, mapping.Type, mapping.Handler, valueConfig.IsArray, safeOutput);
}

internal ValueConfig GenerateValueConfig(KeyValuePair<string, JToken> secret, SecretsConfig config)
internal ValueConfig GenerateValueConfig(KeyValuePair<string, string> secret, SecretsConfig config)
{
var value = secret.Value.ToString();
var valueArray = Regex.Split(value, $"(?<!\\\\){config.Delimiter}").Select(x => x.Replace($"\\{config.Delimiter}", config.Delimiter));
Expand Down Expand Up @@ -301,7 +308,7 @@ internal string GetNamespace(string relativeNamespace)
return $"{BaseNamespace}.{relativeNamespace}";
}

internal string PropertyBuilder(KeyValuePair<string, JToken> secret, Type type, IValueHandler valueHandler, bool isArray, bool safeOutput)
internal string PropertyBuilder(KeyValuePair<string, string> secret, Type type, IValueHandler valueHandler, bool isArray, bool safeOutput)
{
var output = string.Empty;
var typeDeclaration = type.GetStandardTypeName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Mobile.BuildTools.Build;
using Mobile.BuildTools.Generators.Secrets;
using Mobile.BuildTools.Models.Secrets;
using Newtonsoft.Json.Linq;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -44,7 +43,7 @@ private SecretsClassGenerator GetGenerator(IBuildConfiguration config) =>
public void ProcessSecretHidesValueForSafeOutput(bool firstRun)
{
var value = "TestValue";
var pair = new KeyValuePair<string, JToken>(TestKey, value);
var pair = new KeyValuePair<string, string>(TestKey, value);
var generator = GetGenerator();
var config = new SecretsConfig()
{
Expand All @@ -61,8 +60,8 @@ public void ProcessSecretHidesValueForSafeOutput(bool firstRun)
public void ProcessSecretGeneratesBooleanValue(bool firstRun)
{
var key = "TestKey";
var value = true;
var pair = new KeyValuePair<string, JToken>(key, value);
var value = bool.TrueString;
var pair = new KeyValuePair<string, string>(key, value);
var generator = GetGenerator();
var config = new SecretsConfig()
{
Expand All @@ -79,8 +78,8 @@ public void ProcessSecretGeneratesBooleanValue(bool firstRun)
public void ProcessSecretGeneratesIntegerValue(bool firstRun)
{
var key = "TestKey";
var value = 2;
var pair = new KeyValuePair<string, JToken>(key, value);
var value = "2";
var pair = new KeyValuePair<string, string>(key, value);
var generator = GetGenerator();
var config = new SecretsConfig()
{
Expand All @@ -97,8 +96,8 @@ public void ProcessSecretGeneratesIntegerValue(bool firstRun)
public void ProcessSecretGeneratesDoubleValue(bool firstRun)
{
var key = "TestKey";
var value = 2.2;
var pair = new KeyValuePair<string, JToken>(key, value);
var value = "2.2";
var pair = new KeyValuePair<string, string>(key, value);
var generator = GetGenerator();
var config = new SecretsConfig()
{
Expand All @@ -116,7 +115,7 @@ public void ProcessSecretGeneratesStringValue(bool firstRun)
{
var key = "TestKey";
var value = "TestValue";
var pair = new KeyValuePair<string, JToken>(key, value);
var pair = new KeyValuePair<string, string>(key, value);
var generator = GetGenerator();
var config = new SecretsConfig()
{
Expand Down Expand Up @@ -206,7 +205,7 @@ public void GeneratesCorrectNamespace_WithSpecifiedBaseNamespace(string relative
[InlineData(false, true, PropertyType.TimeSpan, "static readonly System.TimeSpan[]", "6:12;4:20", "new System.TimeSpan[] { System.TimeSpan.Parse(\"6:12\"), System.TimeSpan.Parse(\"4:20\") }")]
public void ProcessSecretValue(bool firstRun, bool isArray, PropertyType type, string typeDeclaration, string value, string valueDeclaration)
{
var pair = new KeyValuePair<string, JToken>(TestKey, value);
var pair = new KeyValuePair<string, string>(TestKey, value);
var generator = GetGenerator();
var valueConfig = new ValueConfig
{
Expand Down

0 comments on commit b192378

Please sign in to comment.