File: Model\ParameterValueHelpers.cs
Web Access
Project: src\src\Aspire.Dashboard\Aspire.Dashboard.csproj (Aspire.Dashboard)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using System.Text.Json;
using Aspire.Dashboard.Serialization;
using Aspire.DashboardService.Proto.V1;
 
namespace Aspire.Dashboard.Model;
 
internal static class ParameterValueHelpers
{
    /// <summary>
    /// Gets the input the set-parameter command uses for the value, which describes how the value is edited.
    /// </summary>
    public static InteractionInput? GetValueInput(CommandViewModel? setCommand) =>
        setCommand?.ArgumentInputs.FirstOrDefault(input =>
            !string.Equals(input.Name, ParameterInteractionState.SaveToUserSecretsInputName, StringComparisons.InteractionInputName));
 
    public static string GetDisplayValue(ResourceViewModel resource, string value)
    {
        var setCommand = resource.Commands.FirstOrDefault(command =>
            string.Equals(command.Name, CommandViewModel.SetParameterCommand, StringComparisons.CommandName));
 
        return GetDisplayValue(GetValueInput(setCommand)?.InputType, value);
    }
 
    /// <summary>
    /// Gets the text shown to the user for a parameter value.
    /// </summary>
    public static string GetDisplayValue(InputType? inputType, string value)
    {
        if (inputType != InputType.File)
        {
            return value;
        }
 
        // A file input stores the uploaded files as the JSON written by InputViewModel.SetFileReferences, for example:
        //   [{"Id":"0c7a2f1e","Name":"cert.pfx"},{"Id":"91be44d0","Name":"key.pem"}]
        // The upload IDs mean nothing to the user, so only the file names are shown. A value in another shape, such as
        // one set in configuration, is shown as-is.
        try
        {
            var files = JsonSerializer.Deserialize(value, DashboardJsonSerializerContext.DefaultContext.ListFileReferenceViewModel);
            var names = files?.Select(file => file?.Name).Where(name => !string.IsNullOrEmpty(name)).ToList();
            return names is { Count: > 0 } ? string.Join(", ", names) : value;
        }
        catch (JsonException)
        {
            return value;
        }
    }
}