/// <summary>
        /// Read shopify theme settings from 'config' folder
        /// </summary>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public IDictionary GetSettings(string defaultValue = null)
        {
            return(_cacheManager.Get(GetCacheKey("GetSettings", defaultValue), "LiquidThemeRegion", () =>
            {
                DefaultableDictionary retVal = new DefaultableDictionary(defaultValue);
                //Read first settings from global theme
                var resultSettings = InnerGetSettings(_globalThemeBlobProvider, "");
                //Then load from current theme
                var currentThemeSettings = InnerGetSettings(_themeBlobProvider, CurrentThemePath);
                if (currentThemeSettings != null)
                {
                    if (resultSettings == null) // if there is no default settings, use just current theme
                    {
                        resultSettings = currentThemeSettings;
                    }
                    else
                    {
                        resultSettings.Merge(currentThemeSettings, new JsonMergeSettings {
                            MergeArrayHandling = MergeArrayHandling.Merge
                        });
                    }
                }


                if (resultSettings != null)
                {
                    var dict = resultSettings.ToObject <Dictionary <string, object> >().ToDictionary(x => x.Key, x => x.Value);
                    retVal = new DefaultableDictionary(dict, defaultValue);
                }

                return retVal;
            }));
        }
        /// <summary>
        /// Read shopify theme settings from 'config' folder
        /// </summary>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public DefaultableDictionary GetSettings(string defaultValue = null)
        {
            return(_cacheManager.Get(GetCacheKey("GetSettings", defaultValue), "LiquidThemeRegion", () =>
            {
                DefaultableDictionary retVal = new DefaultableDictionary(defaultValue);

                var resultSettings = InnerGetSettings(GlobalThemeLocalPath);
                if (GlobalThemeLocalPath != CurrentThemeLocalPath)
                {
                    var currentThemeSettings = InnerGetSettings(CurrentThemeLocalPath);
                    if (currentThemeSettings != null)
                    {
                        resultSettings.Merge(currentThemeSettings, new JsonMergeSettings {
                            MergeArrayHandling = MergeArrayHandling.Merge
                        });
                    }
                }

                if (resultSettings != null)
                {
                    var dict = resultSettings.ToObject <Dictionary <string, object> >().ToDictionary(x => x.Key, x => x.Value);
                    retVal = new DefaultableDictionary(dict, defaultValue);
                }

                return retVal;
            }));
        }
        /// <summary>
        /// Read shopify theme settings from 'config' folder
        /// </summary>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public IDictionary GetSettings(string defaultValue = null)
        {
            var cacheKey = CacheKey.With(GetType(), "GetSettings", CurrentThemeSettingPath, defaultValue);

            return(_memoryCache.GetOrCreate(cacheKey, (cacheItem) =>
            {
                cacheItem.AddExpirationToken(new CompositeChangeToken(new[] { ThemeEngineCacheRegion.CreateChangeToken(), _themeBlobProvider.Watch(CurrentThemeSettingPath) }));
                var retVal = new DefaultableDictionary(defaultValue);
                //Load all data from current theme config
                var resultSettings = InnerGetAllSettings(_themeBlobProvider, CurrentThemeSettingPath);
                if (resultSettings != null)
                {
                    //Get actual preset from merged config
                    var currentPreset = resultSettings.GetValue("current");
                    if (currentPreset is JValue)
                    {
                        var currentPresetName = ((JValue)currentPreset).Value.ToString();
                        if (!(resultSettings.GetValue("presets") is JObject presets) || !presets.Children().Any())
                        {
                            throw new StorefrontException("Setting presets not defined");
                        }

                        IList <JProperty> allPresets = presets.Children().Cast <JProperty>().ToList();
                        resultSettings = allPresets.FirstOrDefault(p => p.Name == currentPresetName).Value as JObject;
                        if (resultSettings == null)
                        {
                            throw new StorefrontException($"Setting preset with name '{currentPresetName}' not found");
                        }
                    }
        public void when_creating_a_dictionary_with_a_default_value_it_returns_the_default_value()
        {
            var dictionary = new DefaultableDictionary<string, string>(new Dictionary<string, string>(), "default");
            var value = dictionary["test"];

            Assert.AreEqual("default", value);
        }
 public void Values_IncludesDefault()
 {
     var dd = new DefaultableDictionary<int, string>(dictionary, "not found");
     Assert.Contains("one", dd.Values.ToList());
     Assert.Contains("two", dd.Values.ToList());
     Assert.Contains("three", dd.Values.ToList());
     Assert.Contains("not found", dd.Values.ToList());
 }
 public void TryGetValue_WhenValueNotFound_ReturnsDefault()
 {
     var dd = new DefaultableDictionary<int, string>(dictionary, "not found");
     string value;
     bool result = dd.TryGetValue(0, out value);
     Assert.IsTrue(result);
     Assert.AreEqual("not found", value);
 }
 public void can_create_instance_with_only_a_default_value()
 {
     var dd = new DefaultableDictionary<int, string>("not found");
     string value;
     bool result = dd.TryGetValue(1, out value);
     Assert.IsTrue(result);
     Assert.AreEqual(value, "not found");
 }
Example #8
0
        /// <summary>
        /// Read shopify theme settings from 'config' folder
        /// </summary>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public IDictionary GetSettings(string defaultValue = null)
        {
            return(_cacheManager.Get(GetCacheKey("GetSettings", defaultValue), "LiquidThemeRegion", () =>
            {
                var retVal = new DefaultableDictionary(defaultValue);

                //Load all data from current theme config
                var currentThemeSettings = InnerGetAllSettings(_themeBlobProvider, CurrentThemePath);

                //Load all data from global theme config
                var resultSettings = InnerGetAllSettings(_globalThemeBlobProvider, string.Empty);

                if (currentThemeSettings != null)
                {
                    //Merge two configs
                    resultSettings.Merge(currentThemeSettings, new JsonMergeSettings {
                        MergeArrayHandling = MergeArrayHandling.Merge
                    });
                }

                //Get actual preset from merged config
                var currentPreset = resultSettings.GetValue("current");
                if (currentPreset is JValue)
                {
                    string currentPresetName = ((JValue)currentPreset).Value.ToString();
                    var presets = resultSettings.GetValue("presets") as JObject;
                    if (presets == null || !presets.Children().Any())
                    {
                        throw new StorefrontException("Setting presets not defined");
                    }

                    IList <JProperty> allPresets = presets.Children().Cast <JProperty>().ToList();
                    resultSettings = allPresets.FirstOrDefault(p => p.Name == currentPresetName).Value as JObject;
                    if (resultSettings == null)
                    {
                        throw new StorefrontException($"Setting preset with name '{currentPresetName}' not found");
                    }
                }
                if (currentPreset is JObject)
                {
                    resultSettings = (JObject)currentPreset;
                }

                var dict = resultSettings.ToObject <Dictionary <string, object> >().ToDictionary(x => x.Key, x => x.Value);
                retVal = new DefaultableDictionary(dict, defaultValue);

                return retVal;
            }));
        }
 public void WhenKeyNotFound_ReturnsExpectedDefault()
 {
     var dd = new DefaultableDictionary<int, string>(dictionary, "not found");
     Assert.AreEqual("not found", dd[0]);
 }
        public void when_trying_to_retrieve_an_entry_with_a_null_key_the_implementation_throws()
        {
            var dictionary = new DefaultableDictionary<string, string>(new Dictionary<string, string>(), "default");

            var value = dictionary[null];
        }