From 31e462e9c5c437a82c9b1713882ac2e7787277e0 Mon Sep 17 00:00:00 2001 From: Dung Ta Van Date: Fri, 8 Oct 2021 17:51:21 +0700 Subject: [PATCH] update binding --- binding/EzyArrayBinding.cs | 9 ++++ binding/EzyBinding.cs | 37 +++++++++++-- binding/EzyConverter.cs | 73 ++++++++++++++++++++++---- binding/EzyDateTimeConverter.cs | 50 ++++++++++++------ binding/EzyObjectBinding.cs | 9 ++++ binding/EzyReflectionArrayConverter.cs | 2 +- binding/EzyReflectionMapConverter.cs | 2 +- binding/EzyValue.cs | 1 + builder/EzyArrayBuilder.cs | 7 +++ builder/EzyObjectBuilder.cs | 7 +++ entity/EzyArray.cs | 11 +++- entity/EzyObject.cs | 14 ++++- entity/EzyRoObject.cs | 2 +- util/EzyObjectToMap.cs | 4 +- 14 files changed, 190 insertions(+), 38 deletions(-) create mode 100644 binding/EzyArrayBinding.cs create mode 100644 binding/EzyObjectBinding.cs diff --git a/binding/EzyArrayBinding.cs b/binding/EzyArrayBinding.cs new file mode 100644 index 0000000..2c12f2d --- /dev/null +++ b/binding/EzyArrayBinding.cs @@ -0,0 +1,9 @@ +using System; + +namespace com.tvd12.ezyfoxserver.client.binding +{ + [AttributeUsage(AttributeTargets.Class)] + public class EzyObjectBinding : System.Attribute + { + } +} diff --git a/binding/EzyBinding.cs b/binding/EzyBinding.cs index 5113fb9..09a9079 100644 --- a/binding/EzyBinding.cs +++ b/binding/EzyBinding.cs @@ -85,14 +85,14 @@ namespace com.tvd12.ezyfoxserver.client.binding /// public class EzyBinding { - private readonly Ezymarshaller marshaller; + private readonly EzyMarshaller marshaller; private readonly EzyUnmarshaller unmarshaller; public EzyBinding( IDictionary writerByInType, IDictionary readerByOutType) { - this.marshaller = new Ezymarshaller(writerByInType); + this.marshaller = new EzyMarshaller(writerByInType); this.unmarshaller = new EzyUnmarshaller(readerByOutType); } @@ -106,6 +106,16 @@ public T marshall(object input) return marshaller.marshall(input); } + public List marshallToList(object input) + { + return marshaller.marshall(input).toList(); + } + + public Dictionary marshallToDict(object input) + { + return marshaller.marshall(input).toDict(); + } + public T unmarshall(object input) { return unmarshaller.unmarshall(input); @@ -153,17 +163,36 @@ public EzyBindingBuilder addReflectionArrayConverter() return addConverter(new EzyReflectionArrayConverter()); } + public EzyBindingBuilder addReflectionConverter() + { + Type type = typeof(T); + object[] attributes = type.GetCustomAttributes(false); + foreach (object attr in attributes) + { + if (attr.GetType() == typeof(EzyObjectBinding)) + { + return addConverter(new EzyReflectionMapConverter()); + } + if (attr.GetType() == typeof(EzyArrayBinding)) + { + return addConverter(new EzyReflectionArrayConverter()); + } + + } + return this; + } + public EzyBinding build() { return new EzyBinding(writerByInType, readerByOutType); } } - public class Ezymarshaller + public class EzyMarshaller { private readonly IDictionary writerByInType; - public Ezymarshaller(IDictionary writerByInType) + public EzyMarshaller(IDictionary writerByInType) { this.writerByInType = writerByInType; } diff --git a/binding/EzyConverter.cs b/binding/EzyConverter.cs index 64b87b4..4cc9486 100644 --- a/binding/EzyConverter.cs +++ b/binding/EzyConverter.cs @@ -1,5 +1,7 @@ using System; +using System.Collections; using com.tvd12.ezyfoxserver.client.entity; +using com.tvd12.ezyfoxserver.client.factory; namespace com.tvd12.ezyfoxserver.client.binding { @@ -12,7 +14,7 @@ public interface IEzyReader public interface IEzyWriter { - object write(object input, Ezymarshaller marshaller); + object write(object input, EzyMarshaller marshaller); Type getInType(); } @@ -49,12 +51,12 @@ public Type getOutType() public abstract class EzyObjectToMap : IEzyWriter { - public object write(object input, Ezymarshaller marshaller) + public object write(object input, EzyMarshaller marshaller) { return objectToMap((T)input, marshaller); } - protected abstract EzyObject objectToMap(T obj, Ezymarshaller marshaller); + protected abstract EzyObject objectToMap(T obj, EzyMarshaller marshaller); public Type getInType() { @@ -64,12 +66,12 @@ public Type getInType() public abstract class EzyobjectToArray : IEzyWriter { - public object write(object input, Ezymarshaller marshaller) + public object write(object input, EzyMarshaller marshaller) { return objectToArray((T)input, marshaller); } - protected abstract EzyArray objectToArray(T obj, Ezymarshaller marshaller); + protected abstract EzyArray objectToArray(T obj, EzyMarshaller marshaller); public Type getInType() { @@ -81,21 +83,59 @@ public interface IEzyConverter: IEzyReader, IEzyWriter { } + public abstract class EzyDataConverter : IEzyConverter + { + public object read(object input, EzyUnmarshaller unmarshaller) + { + return valueToData(input, unmarshaller); + } + + public object write(object input, EzyMarshaller marshaller) + { + return dataToValue((T)input, marshaller); + } + + protected abstract T valueToData(object value, EzyUnmarshaller unmarshaller); + + protected abstract object dataToValue(T data, EzyMarshaller marshaller); + + public Type getInType() + { + return typeof(T); + } + + public Type getOutType() + { + return typeof(T); + } + } + public abstract class EzyMapConverter : IEzyConverter { public object read(object input, EzyUnmarshaller unmarshaller) { - return mapToObject((EzyObject)input, unmarshaller); + EzyObject map = null; + if (input is IDictionary) + { + map = EzyEntityFactory.newObjectBuilder() + .appendRawDict((IDictionary)input) + .build(); + } + else + { + map = (EzyObject)input; + } + return mapToObject(map, unmarshaller); } - public object write(object input, Ezymarshaller marshaller) + public object write(object input, EzyMarshaller marshaller) { return objectToMap((T)input, marshaller); } protected abstract T mapToObject(EzyObject map, EzyUnmarshaller unmarshaller); - protected abstract EzyObject objectToMap(T obj, Ezymarshaller marshaller); + protected abstract EzyObject objectToMap(T obj, EzyMarshaller marshaller); public Type getInType() { @@ -112,17 +152,28 @@ public abstract class EzyArrayConverter : IEzyConverter { public object read(object input, EzyUnmarshaller unmarshaller) { - return arrayToObject((EzyArray)input, unmarshaller); + EzyArray array = null; + if (input is IList) + { + array = EzyEntityFactory.newArrayBuilder() + .appendRawList((IList)input) + .build(); + } + else + { + array = (EzyArray)input; + } + return arrayToObject(array, unmarshaller); } - public object write(object input, Ezymarshaller marshaller) + public object write(object input, EzyMarshaller marshaller) { return objectToArray((T)input, marshaller); } protected abstract T arrayToObject(EzyArray array, EzyUnmarshaller unmarshaller); - protected abstract EzyArray objectToArray(T obj, Ezymarshaller marshaller); + protected abstract EzyArray objectToArray(T obj, EzyMarshaller marshaller); public Type getInType() { diff --git a/binding/EzyDateTimeConverter.cs b/binding/EzyDateTimeConverter.cs index d5bc16d..8342cf0 100644 --- a/binding/EzyDateTimeConverter.cs +++ b/binding/EzyDateTimeConverter.cs @@ -1,27 +1,47 @@ using System; +using System.Globalization; namespace com.tvd12.ezyfoxserver.client.binding { - public class EzyDateTimeConverter : IEzyConverter + public class EzyDateTimeConverter : EzyDataConverter { - public object read(object input, EzyUnmarshaller unmarshaller) + protected override DateTime valueToData( + object value, + EzyUnmarshaller unmarshaller) { - return new DateTime(1970, 1, 1).AddMilliseconds((long)input); + if (value is Int64) + { + return new DateTime(1970, 1, 1).AddMilliseconds((long)value); + } + if (value is Int32) + { + return new DateTime(1970, 1, 1).AddMilliseconds((Int32)value); + } + if (value is string) + { + try + { + return DateTime.ParseExact( + (string)value, + "yyyy-MM-dd'T'HH:mm:ss:fff", + CultureInfo.InvariantCulture); + } + catch (Exception e) + { + return DateTime.ParseExact( + (string)value, + "yyyy-MM-dd'T'HH:mm:ss.fff", + CultureInfo.InvariantCulture); + } + } + return (DateTime)value; } - public object write(object input, Ezymarshaller marshaller) + protected override object dataToValue( + DateTime data, + EzyMarshaller marshaller) { - return ((DateTime)input).Millisecond; - } - - public Type getInType() - { - return typeof(DateTime); - } - - public Type getOutType() - { - return typeof(DateTime); + return data.Millisecond; } } } diff --git a/binding/EzyObjectBinding.cs b/binding/EzyObjectBinding.cs new file mode 100644 index 0000000..d7f264b --- /dev/null +++ b/binding/EzyObjectBinding.cs @@ -0,0 +1,9 @@ +using System; + +namespace com.tvd12.ezyfoxserver.client.binding +{ + [AttributeUsage(AttributeTargets.Class)] + public class EzyArrayBinding : System.Attribute + { + } +} diff --git a/binding/EzyReflectionArrayConverter.cs b/binding/EzyReflectionArrayConverter.cs index c928a33..371698e 100644 --- a/binding/EzyReflectionArrayConverter.cs +++ b/binding/EzyReflectionArrayConverter.cs @@ -70,7 +70,7 @@ protected override T arrayToObject(EzyArray array, EzyUnmarshaller unmarshaller) return obj; } - protected override EzyArray objectToArray(T obj, Ezymarshaller marshaller) + protected override EzyArray objectToArray(T obj, EzyMarshaller marshaller) { int count = 0; SortedDictionary valueByIndex = new SortedDictionary(); diff --git a/binding/EzyReflectionMapConverter.cs b/binding/EzyReflectionMapConverter.cs index df8b418..6fc4e2a 100644 --- a/binding/EzyReflectionMapConverter.cs +++ b/binding/EzyReflectionMapConverter.cs @@ -81,7 +81,7 @@ protected override T mapToObject(EzyObject map, EzyUnmarshaller unmarshaller) return obj; } - protected override EzyObject objectToMap(T obj, Ezymarshaller marshaller) + protected override EzyObject objectToMap(T obj, EzyMarshaller marshaller) { EzyObject map = EzyEntityFactory.newObject(); foreach (PropertyInfo property in objectType.GetProperties()) diff --git a/binding/EzyValue.cs b/binding/EzyValue.cs index 785be66..4ca4354 100644 --- a/binding/EzyValue.cs +++ b/binding/EzyValue.cs @@ -1,4 +1,5 @@ using System; + namespace com.tvd12.ezyfoxserver.client.binding { [AttributeUsage(AttributeTargets.Property)] diff --git a/builder/EzyArrayBuilder.cs b/builder/EzyArrayBuilder.cs index c7b1924..c3e28e0 100644 --- a/builder/EzyArrayBuilder.cs +++ b/builder/EzyArrayBuilder.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using com.tvd12.ezyfoxserver.client.io; using com.tvd12.ezyfoxserver.client.entity; @@ -41,6 +42,12 @@ public EzyArrayBuilder append(params T[] values) return this; } + public EzyArrayBuilder appendRawList(IList values) + { + product.addRawList(values); + return this; + } + public EzyArrayBuilder appendAll(IList values) { product.addAll(values); diff --git a/builder/EzyObjectBuilder.cs b/builder/EzyObjectBuilder.cs index 0831e8d..429b8ae 100644 --- a/builder/EzyObjectBuilder.cs +++ b/builder/EzyObjectBuilder.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using com.tvd12.ezyfoxserver.client.io; using com.tvd12.ezyfoxserver.client.entity; @@ -26,6 +27,12 @@ public EzyObjectBuilder append(Object key, Object value) return this; } + public EzyObjectBuilder appendRawDict(IDictionary dict) + { + product.putRawDict(dict); + return this; + } + public EzyObjectBuilder append(IDictionary dict) { product.putAll(dict); diff --git a/entity/EzyArray.cs b/entity/EzyArray.cs index 5ea8b35..1ca2cb4 100644 --- a/entity/EzyArray.cs +++ b/entity/EzyArray.cs @@ -1,5 +1,6 @@ using System; using System.Text; +using System.Collections; using System.Collections.Generic; using com.tvd12.ezyfoxserver.client.io; using com.tvd12.ezyfoxserver.client.util; @@ -38,7 +39,15 @@ public void add(EzyBuilder builder) list.Add(t); } - public void addAll(ICollection values) + public void addRawList(IList values) + { + foreach (Object value in values) + { + list.Add(inputTransformer.transform(value)); + } + } + + public void addAll(IList values) { foreach (T value in values) { diff --git a/entity/EzyObject.cs b/entity/EzyObject.cs index fb61301..38cc331 100644 --- a/entity/EzyObject.cs +++ b/entity/EzyObject.cs @@ -1,5 +1,6 @@ using System; using System.Text; +using System.Collections; using System.Collections.Generic; using com.tvd12.ezyfoxserver.client.io; using com.tvd12.ezyfoxserver.client.util; @@ -32,6 +33,15 @@ public void put(Object key, Object value) = inputTransformer.transform(value); } + public void putRawDict(IDictionary dict) + { + foreach (Object key in dict.Keys) + { + dictionary[inputTransformer.transform(key)] + = inputTransformer.transform(dict[key]); + } + } + public void putAll(IDictionary dict) { foreach (K key in dict.Keys) @@ -90,10 +100,10 @@ public ICollection values() return dictionary.Values; } - public IDictionary toDict() + public Dictionary toDict() { EzyObjectToMap objectToMap = EzyObjectToMap.getInstance(); - IDictionary map = objectToMap.toMap(this); + Dictionary map = objectToMap.toMap(this); return map; } diff --git a/entity/EzyRoObject.cs b/entity/EzyRoObject.cs index 170ef4d..da07131 100644 --- a/entity/EzyRoObject.cs +++ b/entity/EzyRoObject.cs @@ -23,6 +23,6 @@ public interface EzyRoObject : EzyData ICollection values(); - IDictionary toDict(); + Dictionary toDict(); } } diff --git a/util/EzyObjectToMap.cs b/util/EzyObjectToMap.cs index 34d19e5..7fe847e 100644 --- a/util/EzyObjectToMap.cs +++ b/util/EzyObjectToMap.cs @@ -14,9 +14,9 @@ public static EzyObjectToMap getInstance() return INSTANCE; } - public IDictionary toMap(EzyObject obj) + public Dictionary toMap(EzyObject obj) { - IDictionary answer = new Dictionary(); + Dictionary answer = new Dictionary(); foreach (Object key in obj.keys()) { Object value = obj.get(key);