public void ConfigDocumentContainsSection() { ConfigDocument document = new ConfigDocument(file); Assert.IsTrue(document.ContainsSection("section1")); Assert.IsTrue(document.ContainsSection("section2")); }
/// <summary> /// 支付金额 /// </summary> /// <returns></returns> private static void InitListPayMoney() { try { XmlNodeList xmlNodeList = ConfigDocument.SelectNodes("/configuration/payMoneys/payMoney"); if (xmlNodeList == null || xmlNodeList.Count == 0) { return; } List <PayMoney> list = new List <PayMoney>(); foreach (XmlNode xmlNode in xmlNodeList) { PayMoney tmpPayMoney = new PayMoney(); string tmpValue = xmlNode.Attributes["value"].Value; Double doubleValue = 0; if (double.TryParse(tmpValue, out doubleValue)) { tmpPayMoney.value = doubleValue; tmpPayMoney.IsDefault = xmlNode.Attributes["isDefault"].Value == "1"; tmpPayMoney.payKey = xmlNode.Attributes["payKey"].Value; list.Add(tmpPayMoney); } } list = list.OrderBy(A => A.value).ToList(); PayMoneys = list; return; } catch { return; } }
/// <summary> /// 支付方式 /// </summary> /// <returns></returns> private static void InitListPayType() { try { XmlNodeList xmlNodeList = ConfigDocument.SelectNodes("/configuration/payTypes/payType"); if (xmlNodeList == null || xmlNodeList.Count == 0) { return; } List <PayType> list = new List <PayType>(); foreach (XmlNode xmlNode in xmlNodeList) { PayType payType = new PayType(); payType.Key = xmlNode.Attributes["key"].Value; payType.Name = xmlNode.Attributes["name"].Value; payType.IsOpen = xmlNode.Attributes["isOpen"].Value == "1"; payType.IsDefault = xmlNode.Attributes["isDefault"].Value == "1"; list.Add(payType); } PayTypes = list; return; } catch { return; } }
public void CanParseManyEmptyMultiLineObjectsAtRoot() { const string input = "myType MyObject1\n" + "{\n" + "}" + "myType MyObject2\n" + "{\n" + "}"; ConfigDocument doc = DocumentParser.Document.Parse(input); Assert.Equal(2, doc.Children.Count()); for (int i = 1; i <= 2; i++) { string expectedName = $"MyObject{i}"; ConfigElement element = doc.Children.ElementAtOrDefault(i - 1); Assert.NotNull(element); Assert.IsType <ObjectElement>(element); var obj = (ObjectElement)element; Assert.Equal("myType", obj.Heading.Type); Assert.Equal(expectedName, obj.Heading.Name); } }
public void ShouldInclude() { //Arrange var srcDoc = ConfigDocument.Load("{/*include:123*/\"foo\": \"bar\"}"); var includeDoc = ConfigDocument.Load("{\"baz\":\"quux\"}"); _output.WriteLine("Source:"); _output.WriteLine(srcDoc.Serialize(true)); _output.WriteLine(""); _output.WriteLine("Include:"); _output.WriteLine(includeDoc.Serialize(true)); _output.WriteLine(""); var includes = srcDoc.GetIncludes().ToArray(); //Act includes.First().Resolve(includeDoc); _output.WriteLine("Result:"); _output.WriteLine(srcDoc.Serialize(true)); //Assert Assert.Single(includes); Assert.Equal("{\"foo\":\"bar\",\"baz\":\"quux\"}", srcDoc.Serialize(false)); }
public void ConfigDocumentAddSection() { ConfigDocument document = new ConfigDocument(file); Assert.IsFalse(document.ContainsSection("section3")); document.AddSection("section3"); Assert.IsTrue(document.ContainsSection("section3")); }
private ConfigManager() { var task = ReadFile(); task.Wait(); configDocument = JsonConvert.DeserializeObject <ConfigDocument>(task.Result); }
public static void Main(string[] args) { Console.WriteLine("FILE\n===="); Console.WriteLine(String.Join("\n", file)); ConfigDocument document = new ConfigDocument(file); Console.WriteLine("\n\nDOCUMENT\n========"); Console.WriteLine(String.Join("\n", document.ReadAllLines())); Console.ReadKey(); }
public void throws_when_unset_multivalued_variable() { var path = Path.GetTempFileName(); File.WriteAllText(path, @"[foo] bar = hello bar = world"); var doc = ConfigDocument.FromFile(path); Assert.Throws <NotSupportedException>(() => doc.Unset("foo", null, "bar")); }
public async Task <ConfigInfo> LoadInclude(string id) { var str = await File.ReadAllTextAsync(Path.Combine(IncludePath, id + ".json")); var config = ConfigDocument.Load(str); return(new ConfigInfo { Secrets = _secretsAnalyzer.GetSecrets(config).ToArray(), Content = JsonPrettify(str) }); }
public void ShouldApplySecrets() { //Arrange var secretProvider = new DefaultSecretsProvider("[{ \"key\": \"some-secret\", \"value\": \"some-val\" }]"); var config = ConfigDocument.Load("{\"secret\":\"[secret:some-secret]\"}"); //Act config.ApplySecrets(secretProvider); //Assert Assert.Equal("{\"secret\":\"some-val\"}", config.Serialize(false)); }
/// <summary> /// 支付配置 /// </summary> /// <returns></returns> private static void InitListPaySetting() { try { XmlNodeList xmlNodeList = ConfigDocument.SelectNodes("/configuration/paySettings/add"); if (xmlNodeList == null || xmlNodeList.Count == 0) { return; } foreach (XmlNode xmlNode in xmlNodeList) { String attributeKey = xmlNode.Attributes["key"].Value.ToLower(); switch (attributeKey) { case "pid": pid = xmlNode.Attributes["value"].Value; break; case "key": key = xmlNode.Attributes["value"].Value; break; case "sitename": sitename = xmlNode.Attributes["value"].Value; break; case "payurl": payurl = xmlNode.Attributes["value"].Value; break; case "queryurl": queryurl = xmlNode.Attributes["value"].Value; break; case "notifyurl": notifyurl = xmlNode.Attributes["value"].Value; break; case "returnurl": returnurl = xmlNode.Attributes["value"].Value; break; default: break; } } } catch { } }
public void can_add_multi_valued_variable() { var path = Path.GetTempFileName(); File.WriteAllText(path, @"[foo] bar = hello # should come after this [other] # but definitely before this"); ConfigDocument .FromFile(path) .Add("foo", default, "bar", "bye")
public async Task ShouldUseBaseValueWhenConflict() { //Arrange var doc = ConfigDocument.Load(SrcConfig); var includeProvider = new TestIncludeProvider(IncludeSimple); //Act await doc.ApplyIncludes(includeProvider); var res = TestModel.Create(doc); //Assert Assert.Equal("from-base-val", res.IncludeConflict); }
public async Task ShouldResolveIncludes() { //Arrange var doc = ConfigDocument.Load(SrcConfig); var includeProvider = new TestIncludeProvider(IncludeSimple); //Act await doc.ApplyIncludes(includeProvider); var res = TestModel.Create(doc); //Assert Assert.Equal("from-include-val", res.Included); }
public void ShouldOverride() { //Arrange var originDoc = ConfigDocument.Load(SrcConfig); var overrideDoc = ConfigDocument.Load(OverrideConfig); var overrides = overrideDoc.CreateOverrides(); //Act originDoc.ApplyOverrides(overrides); var res = TestModel.Create(originDoc); //Assert Assert.Equal("NewVal", res.ParamForOverride); }
public void ShouldOverrideSubElements() { //Arrange var originDoc = ConfigDocument.Load(SrcConfig); var overrideDoc = ConfigDocument.Load(OverrideConfig); var overrides = overrideDoc.CreateOverrides(); //Act originDoc.ApplyOverrides(overrides); var res = TestModel.Create(originDoc); //Assert Assert.Equal("NewBarVal", res.InnerObject.Bar); }
public void unset_variable_removes_empty_section() { var path = Path.GetTempFileName(); File.WriteAllText(path, @"[foo] bar = true"); ConfigDocument .FromFile(path) .Unset("foo", null, "bar") .Save(); var saved = ConfigDocument.FromFile(path); Assert.Empty(saved.Lines); }
public void can_unset_non_existent_variable() { var path = Path.GetTempFileName(); File.WriteAllText(path, @"[foo] bar = true"); ConfigDocument .FromFile(path) .Unset("foo", null, "baz") .Save(); var saved = ConfigDocument.FromFile(path); Assert.Single(saved.Variables); }
public void can_load_document() { var path = Path.GetTempFileName(); File.WriteAllText(path, @"# sample [foo] # section bar = baz # variable ; done "); var doc = ConfigDocument.FromFile(path); Assert.Single(doc.Sections); Assert.Single(doc.Variables); Assert.Equal(2, doc.Comments.Count()); }
public override bool Execute() { ConfigDocument = XDocument.Load(new StringReader(File.ReadAllText(ConfigFilePath))); if (!String.IsNullOrEmpty(JobSystemConnectionString)) { var elem = ConfigDocument.Descendants("connectionStrings").Elements().Where(e => e.Attribute("name").Value == "JobSystem").Single(); elem.Attribute("connectionString").Value = JobSystemConnectionString; } if (!String.IsNullOrEmpty(UseRemoteConnection)) { var elem = ConfigDocument.Descendants("appSettings").Elements().Where(e => e.Attribute("key").Value == "UseRemoteConfiguration").Single(); elem.Attribute("value").Value = UseRemoteConnection; } ConfigDocument.Save(ConfigFilePath); return(true); }
public void can_set_new_variable_new_section() { var path = Path.GetTempFileName(); var doc = ConfigDocument.FromFile(path); doc.Set("foo", "bar", "baz", "true") .Save(); var saved = ConfigDocument.FromFile(path); Assert.Single(saved); Assert.Equal("foo", saved.Single().Section); Assert.Equal("bar", saved.Single().Subsection); Assert.Equal("baz", saved.Single().Variable); Assert.Equal("true", saved.Single().RawValue); }
public ParseExampleTemplateFixture() { Log.LogLevel = Log.Level.Debug; TemplateText = File.ReadAllText("TestTemplate.txt"); IResult <ConfigDocument> result = DocumentParser.Document.TryParse(TemplateText); if (!result.WasSuccessful) { Debug.WriteLine("Failed to parse example template config: " + result); } else { Config = result.Value; } }
public void CanParseSingleEmptySingleLineObjectAtRoot() { const string input = "myType MyObject {}"; ConfigDocument doc = DocumentParser.Document.Parse(input); Assert.Single(doc.Children); ConfigElement element = doc.Children.FirstOrDefault(); Assert.NotNull(element); Assert.IsType <ObjectElement>(element); var obj = (ObjectElement)element; Assert.Equal("myType", obj.Heading.Type); Assert.Equal("MyObject", obj.Heading.Name); }
public void does_not_set_variable_not_matching_regex() { var path = Path.GetTempFileName(); File.WriteAllText(path, @"[foo] bar = hi baz = bye"); ConfigDocument .FromFile(path) .Set("foo", null, "baz", "hi", ValueMatcher.From("blah")) .Save(); var saved = ConfigDocument.FromFile(path); Assert.Equal("bye", saved.Where(x => x.Variable == "baz").First().RawValue); }
public void can_unset_variable() { var path = Path.GetTempFileName(); File.WriteAllText(path, @"[foo] bar = true baz = false"); ConfigDocument .FromFile(path) .Unset("foo", null, "bar") .Save(); var saved = ConfigDocument.FromFile(path); Assert.Empty(saved.Variables.Where(v => v.Variable == "bar")); }
public void can_get_variables_by_regex() { var path = Path.GetTempFileName(); File.WriteAllText(path, @"[foo] hello = first hell = second bar = none elle = third "); var doc = ConfigDocument.FromFile(path); Assert.Equal(3, doc.GetAll("el").Count()); Assert.Single(doc.GetAll("el", "on")); Assert.Equal(2, doc.GetAll("!hel").Count()); Assert.Single(doc.GetAll("!hel", "!on")); }
public void set_variable_null_reads_as_null() { var path = Path.GetTempFileName(); var doc = ConfigDocument.FromFile(path); doc.Set("foo", "bar", "baz", "value") .Save() .Set("foo", "bar", "baz", null) .Save(); var saved = ConfigDocument.FromFile(path); Assert.Single(saved); Assert.Equal("foo", saved.Single().Section); Assert.Equal("bar", saved.Single().Subsection); Assert.Equal("baz", saved.Single().Variable); Assert.Null(saved.Single().RawValue); }
public async Task <ConfigDocument> LoadConfigCore(string id) { var originStr = await File.ReadAllTextAsync(Path.Combine(ConfigsPath, id + ".json")); var originDoc = ConfigDocument.Load(originStr); await originDoc.ApplyIncludes(new DefaultIncludeProvider(IncludePath)); var overridePath = Path.Combine(OverridesPath, id + ".json"); var overridingStr = File.Exists(overridePath) ? await File.ReadAllTextAsync(overridePath) : null; if (!string.IsNullOrWhiteSpace(overridingStr)) { var overrideDoc = ConfigDocument.Load(overridingStr); originDoc.ApplyOverride(overrideDoc); } return(originDoc); }
public void can_set_new_variable_existing_section() { var path = Path.GetTempFileName(); File.WriteAllText(path, @"[foo] bar = true"); ConfigDocument .FromFile(path) .Set("foo", null, "baz", "false") .Save(); var saved = ConfigDocument.FromFile(path); Assert.Single(saved.Skip(1)); Assert.Equal("foo", saved.Skip(1).Single().Section); Assert.Equal("baz", saved.Skip(1).Single().Variable); Assert.Equal("false", saved.Skip(1).Single().RawValue); }
public void can_set_many_variables_section() { var path = Path.GetTempFileName(); ConfigDocument .FromFile(path) .Set("foo", null, "bar", "false") .Save() .Set("foo", null, "baz", "true") .Save() .Set("foo", null, "weak") .Save(); var saved = ConfigDocument.FromFile(path); Assert.Single(saved.Sections); Assert.Equal(3, saved.Variables.Count()); Assert.Equal("weak", saved.Variables.Last().Variable); }
public void can_set_all_variables_matching_regex() { var path = Path.GetTempFileName(); File.WriteAllText(path, @"[foo] source = github.com/kzu source = microsoft.com/kzu source = github.com/vga source = microsoft.com/vga"); ConfigDocument .FromFile(path) .SetAll("foo", null, "source", "none", ValueMatcher.From("github\\.com")) .Save(); var saved = ConfigDocument.FromFile(path); Assert.Equal(2, saved.Where(x => x.RawValue == "none").Count()); }
public void can_replace_existing_variable() { var path = Path.GetTempFileName(); File.WriteAllText(path, @"[foo] bar = true # with a comment ; some # stuff """); ConfigDocument .FromFile(path) .Set("foo", null, "bar", "false") .Save(); var saved = ConfigDocument.FromFile(path); Assert.Single(saved); Assert.Equal("foo", saved.Single().Section); Assert.Equal("bar", saved.Single().Variable); Assert.Equal("false", saved.Single().RawValue); }
public void ConfigDocumentGetSections() { ConfigDocument document = new ConfigDocument(); List<ConfigSection> sections = document.Sections; for (int i = 0; i < sections.Count; i++) { switch (i) { case 0: Assert.AreEqual("section1", sections[i].Name); break; case 1: Assert.AreEqual("section2", sections[i].Name); break; default: Assert.Fail("Too many sections!"); break; } } }
public void ConfigDocumentGetSetting() { ConfigDocument document = new ConfigDocument(file); Assert.AreEqual("value1", document.GetValue("section1", "key1")); }