Skip to content

Commit

Permalink
- refactored schema processor
Browse files Browse the repository at this point in the history
- implemented jobs->foreach
  • Loading branch information
jan-schutte committed Oct 3, 2023
1 parent a9f53c4 commit f0e1972
Show file tree
Hide file tree
Showing 25 changed files with 270 additions and 202 deletions.
4 changes: 2 additions & 2 deletions src/Nox.Cli.Abstractions/Nox.Cli.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Nox.Secrets" Version="7.0.14" />
<PackageReference Include="Nox.Solution" Version="7.0.15" />
<PackageReference Include="Nox.Secrets" Version="7.0.49" />
<PackageReference Include="Nox.Solution" Version="7.0.49" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.32.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.0.2" />
</ItemGroup>
</Project>
8 changes: 4 additions & 4 deletions src/Nox.Cli.Authentication/Nox.Cli.Authentication.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

<ItemGroup>
<PackageReference Include="Azure.Extensions.AspNetCore.DataProtection.Keys" Version="1.2.2" />
<PackageReference Include="Azure.Identity" Version="1.9.0" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Abstractions" Version="7.0.9" />
<PackageReference Include="Azure.Identity" Version="1.10.1" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Abstractions" Version="7.0.11" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.55.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.32.1" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.56.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Nox.Cli.Caching/Nox.Cli.Caching.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<PackageReference Include="Nox.Utilities" Version="1.0.7" />
<PackageReference Include="RestSharp" Version="110.2.0" />
<PackageReference Include="Spectre.Console" Version="0.47.0" />
<PackageReference Include="YamlDotNet" Version="13.3.1" />
<PackageReference Include="YamlDotNet" Version="13.4.0" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/Nox.Cli.Configuration/Nox.Cli.Configuration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="11.6.0" />
<PackageReference Include="FluentValidation" Version="11.7.1" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Nox.Cli.Helpers/Nox.Cli.Helpers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="Spectre.Console" Version="0.47.0" />
<PackageReference Include="YamlDotNet" Version="13.3.1" />
<PackageReference Include="YamlDotNet" Version="13.4.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.9.0" />
<PackageReference Include="Azure.Identity" Version="1.10.1" />
<PackageReference Include="Azure.ResourceManager" Version="1.7.0" />
<PackageReference Include="Azure.ResourceManager.Dns" Version="1.0.1" />
<PackageReference Include="Azure.ResourceManager.KeyVault" Version="1.1.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.9.0" />
<PackageReference Include="Microsoft.Graph" Version="5.20.0" />
<PackageReference Include="Azure.Identity" Version="1.10.1" />
<PackageReference Include="Microsoft.Graph" Version="5.28.0" />
<PackageReference Include="RestSharp" Version="110.2.0" />
</ItemGroup>

Expand Down
178 changes: 82 additions & 96 deletions src/Nox.Cli.Plugins/Nox.Cli.Plugin.Console/ConsolePromptSchema_v1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,14 @@ public async Task<IDictionary<string, object>> ProcessAsync(INoxWorkflowContext
WriteIndented = false,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
serializeOptions.Converters.Add(new JsonSchemaTypeConverter());

var jsonSchema = JsonSerializer.Deserialize<JsonSchema.JsonSchema>(json, serializeOptions);
var jsonSchemaRaw = JsonSerializer.Deserialize<JsonSchema.JsonSchemaRaw>(json, serializeOptions);

if (jsonSchema != null)
if (jsonSchemaRaw != null)
{
await ProcessSchema(jsonSchema);
var processor = new JsonSchemaProcessor();
var processedSchema = processor.Process(jsonSchemaRaw);
await ProcessSchema(processedSchema);

foreach(var (key,value) in _responses)
{
Expand Down Expand Up @@ -290,10 +291,10 @@ public Task EndAsync()
private async Task ProcessSchema(JsonSchema.JsonSchema schema, string rootKey = "", string key = "")
{
var newKey = $"{rootKey}.{key}".TrimStart('.');

var prefix = $"[grey]{newKey.PadRight(40, '.').EscapeMarkup()}[/] ";
var yamlSpacing = new string(' ', newKey.Count(d => d == '.') * 2);

var yamlSpacingPostfix = "";

if (_isArrayStart)
Expand All @@ -302,7 +303,7 @@ private async Task ProcessSchema(JsonSchema.JsonSchema schema, string rootKey =
}
else
{
if (rootKey.EndsWith(']')) yamlSpacing += " ";
if (rootKey.EndsWith(']')) yamlSpacing += " ";
}

if (!string.IsNullOrWhiteSpace(key) && _includedPrompts != null && !_includedPrompts.Any(f => newKey.StartsWith(f, StringComparison.OrdinalIgnoreCase)))
Expand All @@ -314,16 +315,19 @@ private async Task ProcessSchema(JsonSchema.JsonSchema schema, string rootKey =
{
_yaml.AppendLine($"{yamlSpacing}{yamlSpacingPostfix}{key}: {_defaults[newKey]}");
_responses[newKey] = _defaults[newKey];
} else if (_defaults.Any(d => d.Key.StartsWith(newKey, StringComparison.CurrentCultureIgnoreCase)))
}
else if (_defaults.Any(d => d.Key.StartsWith($"{newKey}.", StringComparison.CurrentCultureIgnoreCase)) ||
_defaults.Any(d => d.Key.StartsWith($"{newKey}[", StringComparison.CurrentCultureIgnoreCase)))
{
//The key is not in included prompts, but there are defaults for the sub-keys
ProcessDefaults(newKey, yamlSpacing, yamlSpacingPostfix);
}
return;
}

return;
}

if (!string.IsNullOrWhiteSpace(key) && _excludedPrompts != null && _excludedPrompts.Any(f => newKey.StartsWith(f, StringComparison.OrdinalIgnoreCase)))
if (!string.IsNullOrWhiteSpace(key) && _excludedPrompts != null && _excludedPrompts.Any(f => newKey.Equals(f, StringComparison.OrdinalIgnoreCase)))
{
if (_defaults != null && _defaults.Any(d => newKey.Equals(d.Key, StringComparison.OrdinalIgnoreCase)))
{
Expand All @@ -340,7 +344,7 @@ private async Task ProcessSchema(JsonSchema.JsonSchema schema, string rootKey =
AnsiConsole.MarkupLine($"[yellow]{schema.Description.EscapeMarkup()}[/]");
}

if (_optionalPrompts != null && _optionalPrompts.TryGetValue(newKey, out var optionalPrompt) && schema.AnyOf == null)
if (_optionalPrompts != null && _optionalPrompts.TryGetValue(newKey, out var optionalPrompt))
{
if (AnsiConsole.Prompt(
new TextPrompt<char>($"[grey]{yamlSpacing}[/] [bold]{optionalPrompt}[/]?")
Expand All @@ -354,94 +358,72 @@ private async Task ProcessSchema(JsonSchema.JsonSchema schema, string rootKey =
return;
}


var message = (schema.Description ?? newKey).EscapeMarkup();
var prompt = $"{prefix}[bold]{message}[/]:";
var isRequired = schema.Required != null && schema.Required.Contains(newKey);

if (schema.JsonSchemaType == null)
{
if (schema.AnyOf != null)
{
await ProcessSchema(schema.AnyOf[0], rootKey, key);
}
}
else
switch (schema.SchemaType!.DataType)
{
switch (schema.JsonSchemaType.Type)
{
case SchemaType.Boolean:
_isArrayStart = false;
PromptBoolean(prompt, rootKey, key, yamlSpacing + yamlSpacingPostfix);
break;
case SchemaType.Integer:
_isArrayStart = false;
PromptInteger(prompt, rootKey, key, yamlSpacing + yamlSpacingPostfix, isRequired);
break;
case SchemaType.String:
_isArrayStart = false;
if (schema.Enum != null)
{
PromptEnum(prompt, rootKey, key, yamlSpacing + yamlSpacingPostfix, schema.Enum);
}
else
{
PromptString(prompt, rootKey, key, yamlSpacing + yamlSpacingPostfix, isRequired);
}
break;
case SchemaType.Object:
if (!key.EndsWith(']'))
{
_yaml.AppendLine("");
AppendKey(yamlSpacing, key);
}
foreach (var prop in schema.Properties!)
{
await ProcessSchema(prop.Value, newKey, prop.Key);
}
break;
case SchemaType.Array:
_isArrayStart = false;
if (schema.Items != null)
{
if (schema.Items.AnyOf != null)
{
_yaml.AppendLine("");
AppendKey(yamlSpacing, key);

var index = 0;
do
{
_isArrayStart = true;
await ProcessSchema(schema.Items.AnyOf[0], rootKey, $"{key}[{index}]");
_yaml.AppendLine("");
AnsiConsole.WriteLine();
index++;
} while (
AnsiConsole.Prompt(
new TextPrompt<char>($"[grey]{yamlSpacing}[/] [bold]Add another[/]?")
.DefaultValueStyle(Style.Parse("mediumpurple3_1"))
.ChoicesStyle(Style.Parse("mediumpurple3_1"))
.PromptStyle(Style.Parse("seagreen1"))
.DefaultValue('n')
.AddChoice('y')
.AddChoice('n')
) == 'y');
} else if (schema.Items.Enum != null)
{
schema.Items.JsonSchemaType!.Type = SchemaType.EnumList;
await ProcessSchema(schema.Items, rootKey, key);
}
}
break;
case SchemaType.EnumList:
_isArrayStart = false;
PromptMultipleEnum(prompt, rootKey, key, yamlSpacing + yamlSpacingPostfix, schema.Enum!);
break;
}
case SchemaDataType.Boolean:
_isArrayStart = false;
PromptBoolean(prompt, rootKey, key, yamlSpacing + yamlSpacingPostfix);
break;
case SchemaDataType.Integer:
_isArrayStart = false;
PromptInteger(prompt, rootKey, key, yamlSpacing + yamlSpacingPostfix, schema.IsRequired);
break;
case SchemaDataType.String:
_isArrayStart = false;
PromptString(prompt, rootKey, key, yamlSpacing + yamlSpacingPostfix, schema.IsRequired);
break;
case SchemaDataType.Object:
if (!key.EndsWith(']'))
{
_yaml.AppendLine("");
AppendKey(yamlSpacing, key);
}

foreach (var prop in schema.Properties!)
{
await ProcessSchema(prop.Value, newKey, prop.Key);
}

break;
case SchemaDataType.Array:
_isArrayStart = false;
_yaml.AppendLine("");
AppendKey(yamlSpacing, key);

var index = 0;
do
{
_isArrayStart = true;
await ProcessSchema(schema.Item!, rootKey, $"{key}[{index}]");
_yaml.AppendLine("");
AnsiConsole.WriteLine();
index++;
} while (
AnsiConsole.Prompt(
new TextPrompt<char>($"[grey]{yamlSpacing}[/] [bold]Add another[/]?")
.DefaultValueStyle(Style.Parse("mediumpurple3_1"))
.ChoicesStyle(Style.Parse("mediumpurple3_1"))
.PromptStyle(Style.Parse("seagreen1"))
.DefaultValue('n')
.AddChoice('y')
.AddChoice('n')
) == 'y');

break;
case SchemaDataType.Enum:
PromptEnum(prompt, rootKey, key, yamlSpacing + yamlSpacingPostfix, schema.SchemaType.Enum!);
break;
case SchemaDataType.EnumList:
_isArrayStart = false;
PromptMultipleEnum(prompt, rootKey, key, yamlSpacing + yamlSpacingPostfix, schema.SchemaType.Enum!);
break;
}
}

private void PromptBoolean(string prompt, string rootKey, string key, string yamlPrefix)
{
var newKey = $"{rootKey}.{key}".TrimStart('.');
Expand All @@ -463,11 +445,15 @@ private void PromptBoolean(string prompt, string rootKey, string key, string yam
private void PromptInteger(string prompt, string rootKey, string key, string yamlPrefix, bool isRequired)
{
var newKey = $"{rootKey}.{key}".TrimStart('.');
var spectrePrompt = new TextPrompt<int>(prompt)
var spectrePrompt = new TextPrompt<int?>(prompt)
.PromptStyle(Style.Parse("seagreen1"))
.DefaultValueStyle(Style.Parse("mediumpurple3_1"));

spectrePrompt.AllowEmpty = !isRequired;

if (!isRequired)
{
spectrePrompt.AllowEmpty();
}


var defaultValue = GetDefault<int>(newKey);

Expand Down
29 changes: 7 additions & 22 deletions src/Nox.Cli.Plugins/Nox.Cli.Plugin.Console/JsonSchema/JsonSchema.cs
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
using System.Text.Json.Serialization;

namespace Nox.Cli.Plugin.Console.JsonSchema;


internal class JsonSchema
public class JsonSchema
{
public string? Title { get; set; } = string.Empty;
public string? Description { get; set; } = string.Empty;

[JsonConverter(typeof(JsonSchemaTypeConverter))]
[JsonPropertyName("type")]
public JsonSchemaType? JsonSchemaType { get; set; }

public object? Default { get; set; } = null;

public List<string>? Required { get; set; } = new();

public Dictionary<string, JsonSchema>? Properties { get; set; } = null!;

public JsonSchema? Items { get; set; } = null!;

public JsonSchema[]? AnyOf { get; set; }

public List<string>? Enum { get; set; }
}

public JsonSchemaType? SchemaType { get; set; }
public Dictionary<string, JsonSchema>? Properties { get; set; }

public JsonSchema? Item { get; set; }

public bool IsRequired { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Text.Json.Serialization;

namespace Nox.Cli.Plugin.Console.JsonSchema;

public class JsonSchemaOneOfElement
{
[JsonPropertyName("type")]
public string? TypeName { get; set; }

public List<string>? Enum { get; set; }
}
Loading

0 comments on commit f0e1972

Please sign in to comment.