public void StringAndByteArray() { var cm = new ConverterManager(); // empty // No default byte[]-->Wrapper conversion. var fromBytes = cm.GetConverter<byte[], Wrapper, Attribute>(); Assert.Null(fromBytes); // Add a string-->Wrapper conversion cm.AddConverter<string, Wrapper>(str => new Wrapper { Value = str }); var fromString = cm.GetConverter<string, Wrapper, Attribute>(); Wrapper obj1 = fromString("abc", null); Assert.Equal("abc", obj1.Value); // Now we can get a byte-->string , composed from a default (byte[]-->string) + supplied (string-->Wrapper) byte[] bytes = Encoding.UTF8.GetBytes("abc"); fromBytes = cm.GetConverter<byte[], Wrapper, Attribute>(); Assert.NotNull(fromBytes); Wrapper obj2 = fromBytes(bytes, null); Assert.Equal("abc", obj2.Value); // Now override the default. Uppercase the string so we know it used our custom converter. cm.AddConverter<byte[], string>(b => Encoding.UTF8.GetString(b).ToUpper()); fromBytes = cm.GetConverter<byte[], Wrapper, Attribute>(); Wrapper obj3 = fromBytes(bytes, null); Assert.Equal("ABC", obj3.Value); }
public void CatchAll() { var cm = new ConverterManager(); // empty cm.AddConverter<object, Wrapper>(x => new Wrapper { Value = x.ToString() }); var func = cm.GetConverter<int, Wrapper, Attribute>(); var x1 = func(123, null); Assert.Equal("123", x1.Value); }
public void ExactMatchOverride() { var cm = new ConverterManager(); // empty cm.AddConverter<string, string>(x => "*" + x + "*"); var func = cm.GetConverter<string, string, Attribute>(); var x1 = func("x", null); Assert.Equal("*x*", x1); }
public void CatchAll() { var cm = new ConverterManager(); // empty cm.AddConverter <object, Wrapper>(x => new Wrapper { Value = x.ToString() }); var func = cm.GetConverter <int, Wrapper, Attribute>(); var x1 = func(123, null, context); Assert.Equal("123", x1.Value); }
public void ObjectToJArray_ChainConverter() { var jobjString = "{ \"a\": 2 }"; var obj = JObject.Parse(jobjString); var cm = new ConverterManager(); cm.AddConverter <string, IEnumerable <JObject>, Attribute>((str, attr) => new List <JObject>() { JObject.Parse(str), JObject.Parse(str) }); var jarray = new JArray(obj, obj); TestDefaultConverter(jobjString, jarray, cm); }
public void UseValueBindingContext() { var cm = new ConverterManager(); // empty Guid instance = Guid.NewGuid(); var testContext = new ValueBindingContext(new FunctionBindingContext(instance, CancellationToken.None, null), CancellationToken.None); FuncAsyncConverter converter = (object obj, Attribute attr, ValueBindingContext ctx) => { Assert.Same(ctx, testContext); var result = JObject.FromObject(obj); result["$"] = ctx.FunctionInstanceId; return(Task.FromResult <object>(result)); }; cm.AddConverter <object, JObject, Attribute>(converter); cm.AddConverter <JObject, Wrapper>(str => new Wrapper { Value = str.ToString() }); // Expected: // Other --> JObject, // JObject --> string , (builtin) // string --> Wrapper var func = cm.GetSyncConverter <Other, Wrapper, Attribute>(); var value = new Other { Value2 = "abc" }; Wrapper x1 = func(value, null, testContext); // strip whitespace string val = Regex.Replace(x1.Value, @"\s", ""); string expected = String.Format("{{\"Value2\":\"abc\",\"$\":\"{0}\"}}", instance); Assert.Equal(expected, val); }
public void TestConvertFakeEntity() { var cm = new ConverterManager(); // Derived<ITableEntity> --> IFakeEntity [automatic] // JObject --> IFakeEntity // Poco --> IFakeEntity cm.AddConverter <JObject, IFakeEntity, Attribute>(typeof(TestConverterFakeEntity)); cm.AddConverter <OpenType, IFakeEntity, Attribute>(typeof(TestConverterFakeEntity <>)); { var converter = cm.GetSyncConverter <IFakeEntity, IFakeEntity, Attribute>(); var src = new MyFakeEntity { Property = "123" }; var dest = converter(src, null, null); Assert.Same(src, dest); // should be exact same instance - no conversion } { var converter = cm.GetSyncConverter <JObject, IFakeEntity, Attribute>(); JObject obj = new JObject(); obj["Property1"] = "456"; var dest = converter(obj, null, null); Assert.Equal("456", dest.Property); } { var converter = cm.GetSyncConverter <PocoEntity, IFakeEntity, Attribute>(); var src = new PocoEntity { Property2 = "789" }; var dest = converter(src, null, null); Assert.Equal("789", dest.Property); } }
static void Postfix() { var oreMass = Db.Get().MiscStatusItems.OreMass; var oreTemp = Db.Get().MiscStatusItems.OreTemp; // ORE MASS ConverterManager.AddConverter( oreMass.Id, data => ((GameObject)data).GetComponent <PrimaryElement>().Mass, (original, masses) => oreMass.Name.Replace("{Mass}", GameUtil.GetFormattedMass(masses.Sum())) + ConverterManager.sumSuffix); // ORE TEMP ConverterManager.AddConverter( oreTemp.Id, data => ((GameObject)data).GetComponent <PrimaryElement>().Temperature, (original, temps) => oreTemp.Name.Replace("{Temp}", GameUtil.GetFormattedTemperature(temps.Average())) + ConverterManager.avgSuffix, new() { (x => x, Options.Opts.TemperatureBandWidth) }); }
public void String2TDoesNotEnableJObject() { var cm = new ConverterManager(); // empty cm.AddConverter <string, Wrapper>(str => new Wrapper { Value = str }); var objSrc = new Other { Value2 = "abc" }; // Json Serialize: (Other --> string) // custom (string -->Wrapper) var func = cm.GetSyncConverter <Other, Wrapper, Attribute>(); Assert.Null(func); }
public void OpenTypeSimpleConcreteConverter() { Assert.Equal(0, _counter); var cm = new ConverterManager(); // Register a converter builder. // Builder runs once; converter runs each time. // Uses open type to match. cm.AddConverter <TypeWrapperIsString, int, Attribute>(new ConverterInstanceMethod()); var converter = cm.GetSyncConverter <string, int, Attribute>(); Assert.Equal(12, converter("12", new TestAttribute(null), null)); Assert.Equal(34, converter("34", new TestAttribute(null), null)); Assert.Equal(0, _counter); // passed in instantiated object; counter never incremented. // 'char' as src parameter doesn't match the type predicate. Assert.Null(cm.GetSyncConverter <char, int, Attribute>()); }
public void OpenTypeConverterWithConcreteTypes() { Assert.Equal(0, _counter); var cm = new ConverterManager(); // Register a converter builder. // Builder runs once; converter runs each time. // Uses open type to match. cm.AddConverter <TypeWrapperIsString, int, Attribute>(typeof(TypeConverterWithConcreteTypes), this); var converter = cm.GetSyncConverter <string, int, Attribute>(); Assert.Equal(12, converter("12", new TestAttribute(null), null)); Assert.Equal(34, converter("34", new TestAttribute(null), null)); Assert.Equal(1, _counter); // converterBuilder is only called once. // 'char' as src parameter doesn't match the type predicate. Assert.Null(cm.GetSyncConverter <char, int, Attribute>()); }
public void OpenTypeConverterWithOneGenericArg() { var cm = new ConverterManager(); // Register a converter builder. // Builder runs once; converter runs each time. // Uses open type to match. // Also test the IEnumerable<OpenType> pattern. cm.AddConverter <OpenType, IEnumerable <OpenType>, Attribute>(typeof(TypeConverterWithOneGenericArg <>)); var attr = new TestAttribute(null); { var converter = cm.GetConverter <int, IEnumerable <int>, Attribute>(); Assert.Equal(new int[] { 1, 1, 1 }, converter(1, attr, null)); } { var converter = cm.GetConverter <string, IEnumerable <string>, Attribute>(); Assert.Equal(new string[] { "a", "a", "a" }, converter("a", attr, null)); } }
public void JsonSerialization() { var cm = new ConverterManager(); // empty cm.AddConverter <string, Wrapper>(str => new Wrapper { Value = str }); var objSrc = new Other { Value2 = "abc" }; // Json Serialize: (Other --> string) // custom (string -->Wrapper) var func = cm.GetConverter <Other, Wrapper, Attribute>(); Wrapper obj2 = func(objSrc, null); string json = obj2.Value; var objSrc2 = JsonConvert.DeserializeObject <Other>(json); Assert.Equal(objSrc.Value2, objSrc2.Value2); }
public void AttributeOverloads() { var cm = new ConverterManager(); // empty cm.AddConverter<Wrapper, string, TestAttribute>((x, attr) => string.Format("[t1:{0}-{1}]", x.Value, attr.Flag)); cm.AddConverter<Wrapper, string, TestAttribute2>((x, attr) => string.Format("[t2:{0}-{1}]", x.Value, attr.Flag)); // Since converter was registered for a specific attribute, it must be queried by that attribute. var funcMiss = cm.GetConverter<Wrapper, string, Attribute>(); Assert.Null(funcMiss); // Each attribute type has its own conversion function var func1 = cm.GetConverter<Wrapper, string, TestAttribute>(); Assert.NotNull(func1); var x1 = func1(new Wrapper { Value = "x" } , new TestAttribute("y")); Assert.Equal("[t1:x-y]", x1); var func2 = cm.GetConverter<Wrapper, string, TestAttribute2>(); Assert.NotNull(func2); var x2 = func2(new Wrapper { Value = "x" }, new TestAttribute2("y")); Assert.Equal("[t2:x-y]", x2); }
public void AttributeOverloads2() { var cm = new ConverterManager(); // empty cm.AddConverter<Wrapper, string, TestAttribute>((x, attr) => string.Format("[t1:{0}-{1}]", x.Value, attr.Flag)); cm.AddConverter<Wrapper, string>(x => string.Format("[common:{0}]", x.Value)); // This has an exact match on attribute and gives the specific function we registered. var func1 = cm.GetConverter<Wrapper, string, TestAttribute>(); Assert.NotNull(func1); var x1 = func1(new Wrapper { Value = "x" }, new TestAttribute("y")); Assert.Equal("[t1:x-y]", x1); // Nothing registered for this attribute, so we return the converter that didn't require any attribute. var func2 = cm.GetConverter<Wrapper, string, TestAttribute2>(); Assert.NotNull(func2); var x2 = func2(new Wrapper { Value = "x" }, new TestAttribute2("y")); Assert.Equal("[common:x]", x2); }
public void JsonSerialization() { var cm = new ConverterManager(); // empty cm.AddConverter<string, Wrapper>(str => new Wrapper { Value = str }); var objSrc = new Other { Value2 = "abc" }; // Json Serialize: (Other --> string) // custom (string -->Wrapper) var func = cm.GetConverter<Other, Wrapper, Attribute>(); Wrapper obj2 = func(objSrc, null); string json = obj2.Value; var objSrc2 = JsonConvert.DeserializeObject<Other>(json); Assert.Equal(objSrc.Value2, objSrc2.Value2); }
/// <summary> /// Register a new converter function that applies for all attributes. /// If TSource is object, then this converter is applied to any attempt to convert to TDestination. /// </summary> /// <typeparam name="TSource">Source type.</typeparam> /// <typeparam name="TDestination">Destination type.</typeparam> /// <param name="converterManager"></param> /// <param name="converter">A function to convert from the source to the destination type.</param> public static void AddConverter <TSource, TDestination>(this ConverterManager converterManager, Func <TSource, TDestination> converter) { Func <TSource, Attribute, TDestination> func = (src, attr) => converter(src); converterManager.AddConverter(func); }