示例#1
0
        /// <summary>
        /// Creates an instance of <see cref="ParameterSymbol"/> using
        /// the provided Json Data.
        /// </summary>
        /// <param name="jObject">JSON to initialize the symbol with.</param>
        /// <param name="defaultOverride"></param>
        public ParameterSymbol(JObject jObject, string defaultOverride)
            : base(jObject, defaultOverride)
        {
            DefaultIfOptionWithoutValue = jObject.ToString(nameof(DefaultIfOptionWithoutValue));
            DisplayName = jObject.ToString(nameof(DisplayName)) ?? string.Empty;
            Description = jObject.ToString(nameof(Description)) ?? string.Empty;

            var choicesAndDescriptions = new Dictionary <string, ParameterChoice>();

            if (DataType == "choice")
            {
                IsTag   = false;
                TagName = jObject.ToString(nameof(TagName));

                foreach (JObject choiceObject in jObject.Items <JObject>(nameof(Choices)))
                {
                    string choiceName = choiceObject.ToString("choice");
                    var    choice     = new ParameterChoice(
                        choiceObject.ToString("displayName") ?? string.Empty,
                        choiceObject.ToString("description") ?? string.Empty);

                    choicesAndDescriptions.Add(choiceName, choice);
                }
            }
            else if (DataType == "bool" && string.IsNullOrEmpty(DefaultIfOptionWithoutValue))
            {
                // bool flags are considered true if they're provided without a value.
                DefaultIfOptionWithoutValue = "true";
            }

            Choices                 = choicesAndDescriptions;
            AllowMultipleValues     = jObject.ToBool(nameof(AllowMultipleValues));
            EnableQuotelessLiterals = jObject.ToBool(nameof(EnableQuotelessLiterals));
        }
示例#2
0
        private static IReadOnlyList <ITemplateParameter> LocalizeParameters(ITemplateInfo template, ILocalizationLocator?localizationInfo)
        {
            //we would like to copy the parameters to format supported for serialization as we cannot be sure that ITemplateInfo supports serialization in needed format.
            List <ITemplateParameter> localizedParameters = new List <ITemplateParameter>();

            foreach (ITemplateParameter parameter in template.Parameters)
            {
                IParameterSymbolLocalizationModel?   localization     = null;
                Dictionary <string, ParameterChoice>?localizedChoices = null;
                if (localizationInfo != null)
                {
                    if (!localizationInfo.ParameterSymbols.TryGetValue(parameter.Name, out localization))
                    {
                        // There is no localization for this symbol. Use the symbol as is.
                        localizedParameters.Add(parameter);
                        continue;
                    }
                    if (parameter.IsChoice() && parameter.Choices != null)
                    {
                        localizedChoices = new Dictionary <string, ParameterChoice>();
                        foreach (KeyValuePair <string, ParameterChoice> templateChoice in parameter.Choices)
                        {
                            ParameterChoice localizedChoice = new ParameterChoice(
                                templateChoice.Value.DisplayName,
                                templateChoice.Value.Description);

                            if (localization.Choices.TryGetValue(templateChoice.Key, out ParameterChoiceLocalizationModel locModel))
                            {
                                localizedChoice.Localize(locModel);
                            }
                            localizedChoices.Add(templateChoice.Key, localizedChoice);
                        }
                    }
                }

                TemplateParameter localizedParameter = new TemplateParameter(
                    name: parameter.Name,
                    displayName: localization?.DisplayName ?? parameter.DisplayName,
                    description: localization?.Description ?? parameter.Description,
                    defaultValue: parameter.DefaultValue,
                    defaultIfOptionWithoutValue: parameter.DefaultIfOptionWithoutValue,
                    datatype: parameter.DataType,
                    priority: parameter.Priority,
                    type: parameter.Type,
                    allowMultipleValues: parameter.AllowMultipleValues,
                    choices: localizedChoices ?? parameter.Choices);

                localizedParameters.Add(localizedParameter);
            }
            return(localizedParameters);
        }
示例#3
0
        private IReadOnlyDictionary <string, ICacheTag> LocalizeCacheTags(ITemplateInfo template, ILocalizationLocator localizationInfo)
        {
            if (localizationInfo == null || localizationInfo.ParameterSymbols == null)
            {
                return(template.Tags);
            }

            IReadOnlyDictionary <string, ICacheTag> templateTags = template.Tags;
            IReadOnlyDictionary <string, IParameterSymbolLocalizationModel> localizedParameterSymbols = localizationInfo.ParameterSymbols;

            Dictionary <string, ICacheTag> localizedCacheTags = new Dictionary <string, ICacheTag>();

            foreach (KeyValuePair <string, ICacheTag> templateTag in templateTags)
            {
                if (!localizedParameterSymbols.TryGetValue(templateTag.Key, out IParameterSymbolLocalizationModel localizationForTag))
                {
                    // There is no localization for this symbol. Use the symbol as is.
                    localizedCacheTags.Add(templateTag.Key, templateTag.Value);
                    continue;
                }

                // There is localization. Create a localized instance, starting with the choices.
                var localizedChoices = new Dictionary <string, ParameterChoice>();

                foreach (KeyValuePair <string, ParameterChoice> templateChoice in templateTag.Value.Choices)
                {
                    ParameterChoice localizedChoice = new ParameterChoice(
                        templateChoice.Value.DisplayName,
                        templateChoice.Value.Description);

                    if (localizationForTag.Choices.TryGetValue(templateChoice.Key, out ParameterChoiceLocalizationModel locModel))
                    {
                        localizedChoice.Localize(locModel);
                    }

                    localizedChoices.Add(templateChoice.Key, localizedChoice);
                }

                ICacheTag localizedTag = new CacheTag(
                    localizationForTag.DisplayName ?? templateTag.Value.DisplayName,
                    localizationForTag.Description ?? templateTag.Value.Description,
                    localizedChoices,
                    templateTag.Value.DefaultValue,
                    (templateTag.Value as IAllowDefaultIfOptionWithoutValue)?.DefaultIfOptionWithoutValue);

                localizedCacheTags.Add(templateTag.Key, localizedTag);
            }

            return(localizedCacheTags);
        }
        internal static ISymbolModel FromJObject(JObject jObject, IParameterSymbolLocalizationModel localization, string defaultOverride)
        {
            ParameterSymbol symbol = FromJObject <ParameterSymbol>(jObject, localization, defaultOverride);

            symbol.DefaultIfOptionWithoutValue = jObject.ToString(nameof(DefaultIfOptionWithoutValue));
            symbol.DisplayName = localization?.DisplayName ?? jObject.ToString(nameof(DisplayName)) ?? string.Empty;
            symbol.Description = localization?.Description ?? jObject.ToString(nameof(Description)) ?? string.Empty;

            var choicesAndDescriptions = new Dictionary <string, ParameterChoice>();

            if (symbol.DataType == "choice")
            {
                symbol.IsTag   = jObject.ToBool(nameof(IsTag), true);
                symbol.TagName = jObject.ToString(nameof(TagName));

                foreach (JObject choiceObject in jObject.Items <JObject>(nameof(Choices)))
                {
                    string choiceName = choiceObject.ToString("choice");
                    var    choice     = new ParameterChoice(
                        choiceObject.ToString("displayName") ?? string.Empty,
                        choiceObject.ToString("description") ?? string.Empty);

                    if (localization != null &&
                        localization.Choices.TryGetValue(choiceName, out ParameterChoiceLocalizationModel choiceLocalization))
                    {
                        choice.Localize(choiceLocalization);
                    }

                    choicesAndDescriptions.Add(choiceName, choice);
                }
            }
            else if (symbol.DataType == "bool" && string.IsNullOrEmpty(symbol.DefaultIfOptionWithoutValue))
            {
                // bool flags are considred true if they're provided without a value.
                symbol.DefaultIfOptionWithoutValue = "true";
            }

            symbol.Choices = choicesAndDescriptions;

            return(symbol);
        }