Cfg-NET ReadMe Apples and Bananas

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using Cfg.Net;
					
public class Program
{
	public static void Main()
	{
		
		// LOAD VALID XML
		var cfg = new Cfg();
		cfg.Load(GetXml());
		Console.WriteLine("There are {0} errors in the XML configuration.",cfg.Errors().Length);
		Console.WriteLine("---");
		
		// LOAD VALID JSON
		cfg.Load(GetJson());
		Console.WriteLine("There are {0} errors in the JSON configuration.",cfg.Errors().Length);
		Console.WriteLine("---");
		
		// LOAD SHOWING REQUIRED CONSTRAINT
		cfg.Load(GetXmlWithoutFruit());
		Console.WriteLine("There is {0} error in the fruitless XML configuration.",cfg.Errors().Length);
		foreach(var error in cfg.Errors()){
			Console.WriteLine(error);
		}
		Console.WriteLine("---");

		// LOAD SHOWING UNIQUE CONSTRAINT
		cfg.Load(GetXmlWithAnExtraApple());
		Console.WriteLine("There is {0} error in the too many apples XML configuration.",cfg.Errors().Length);
		foreach(var error in cfg.Errors()){
			Console.WriteLine(error);
		}
		Console.WriteLine("---");
		
		// LOAD SHOWING DOMAIN CONSTRAINT
		cfg.Load(GetXmlWithPinkBanana());
		Console.WriteLine("There is {0} error in the pink banana XML configuration.",cfg.Errors().Length);
		foreach(var error in cfg.Errors()){
			Console.WriteLine(error);
		}
		Console.WriteLine("---");

		// LOOP WITHOUT A CARE IN THE WORLD
		cfg.Load(GetXml());
			
		foreach (var fruit in cfg.Fruit) {
			foreach (var color in fruit.Colors) {
				Console.WriteLine("{0} {1}", color.Name, fruit.Name);
			}
		}
		
	}
	
	class Cfg : CfgNode {
		[Cfg]
		public int Id {get; set; }
		[Cfg(required=true)] // THERE MUST BE SOME FRUIT!
		public List<Fruit> Fruit { get; set; }
	}
	
	class Fruit : CfgNode {
		[Cfg(unique=true)] // THE FRUIT MUST BE UNIQUE!
		public string Name { get; set; }
		[Cfg]
		public List<Color> Colors {get; set;}
	}
	
	class Color : CfgNode {
		[Cfg(domain="red,yellow,green,blue,purple,orange")]
		public string Name {get; set;}
	}
	
	
	private static string GetXml(){
		return @"
			<cfg id='1'>
				<fruit>
					<add name='apple'>
						<colors>
							<add name='red' />
							<add name='yellow' />
							<add name='green' />
						</colors>
					</add>
					<add name='banana'>
						<colors>
							<add name='yellow' />
						</colors>
					</add>
				</fruit>
			</cfg>
			";
	}

	private static string GetXmlWithoutFruit(){
		return "<cfg></cfg>";
	}

	private static string GetXmlWithAnExtraApple(){
		return @"
			<cfg id='1'>
				<fruit>
					<add name='apple'>
						<colors>
							<add name='red' />
							<add name='yellow' />
							<add name='green' />
						</colors>
					</add>
					<add name='apple'>
						<colors>
							<add name='green' />
						</colors>
					</add>
				</fruit>
			</cfg>
			";
	}
	
	private static string GetXmlWithPinkBanana(){
		return @"
			<cfg id='2'>
				<fruit>
					<add name='banana'>
						<colors>
							<add name='pink' />
						</colors>
					</add>
				</fruit>
			</cfg>
			";
	}

	private static string GetJson(){
		return @"
			{
				""id"": 1,
				""fruit"": [
					{ 
						""name"":""apple"",
						""colors"": [
							{""name"":""red""},
							{""name"":""yellow""},
							{""name"":""green""}
						]
					},
					{
						""name"":""banana"",
						""colors"": [
							{""name"":""yellow""}
						]
					}
				]
			}
		";
	}
	
}
;