Skip to content

Commit

Permalink
update binding
Browse files Browse the repository at this point in the history
  • Loading branch information
tvd12 committed Oct 8, 2021
1 parent 4b97f63 commit 31e462e
Show file tree
Hide file tree
Showing 14 changed files with 190 additions and 38 deletions.
9 changes: 9 additions & 0 deletions binding/EzyArrayBinding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace com.tvd12.ezyfoxserver.client.binding
{
[AttributeUsage(AttributeTargets.Class)]
public class EzyObjectBinding : System.Attribute
{
}
}
37 changes: 33 additions & 4 deletions binding/EzyBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ namespace com.tvd12.ezyfoxserver.client.binding
/// </example>
public class EzyBinding
{
private readonly Ezymarshaller marshaller;
private readonly EzyMarshaller marshaller;
private readonly EzyUnmarshaller unmarshaller;

public EzyBinding(
IDictionary<Type, IEzyWriter> writerByInType,
IDictionary<Type, IEzyReader> readerByOutType)
{
this.marshaller = new Ezymarshaller(writerByInType);
this.marshaller = new EzyMarshaller(writerByInType);
this.unmarshaller = new EzyUnmarshaller(readerByOutType);
}

Expand All @@ -106,6 +106,16 @@ public T marshall<T>(object input)
return marshaller.marshall<T>(input);
}

public List<object> marshallToList(object input)
{
return marshaller.marshall<EzyArray>(input).toList<object>();
}

public Dictionary<object, object> marshallToDict(object input)
{
return marshaller.marshall<EzyObject>(input).toDict<object, object>();
}

public T unmarshall<T>(object input)
{
return unmarshaller.unmarshall<T>(input);
Expand Down Expand Up @@ -153,17 +163,36 @@ public EzyBindingBuilder addReflectionArrayConverter<T>()
return addConverter(new EzyReflectionArrayConverter<T>());
}

public EzyBindingBuilder addReflectionConverter<T>()
{
Type type = typeof(T);
object[] attributes = type.GetCustomAttributes(false);
foreach (object attr in attributes)
{
if (attr.GetType() == typeof(EzyObjectBinding))
{
return addConverter(new EzyReflectionMapConverter<T>());
}
if (attr.GetType() == typeof(EzyArrayBinding))
{
return addConverter(new EzyReflectionArrayConverter<T>());
}

}
return this;
}

public EzyBinding build()
{
return new EzyBinding(writerByInType, readerByOutType);
}
}

public class Ezymarshaller
public class EzyMarshaller
{
private readonly IDictionary<Type, IEzyWriter> writerByInType;

public Ezymarshaller(IDictionary<Type, IEzyWriter> writerByInType)
public EzyMarshaller(IDictionary<Type, IEzyWriter> writerByInType)
{
this.writerByInType = writerByInType;
}
Expand Down
73 changes: 62 additions & 11 deletions binding/EzyConverter.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -12,7 +14,7 @@ public interface IEzyReader

public interface IEzyWriter
{
object write(object input, Ezymarshaller marshaller);
object write(object input, EzyMarshaller marshaller);

Type getInType();
}
Expand Down Expand Up @@ -49,12 +51,12 @@ public Type getOutType()

public abstract class EzyObjectToMap<T> : 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()
{
Expand All @@ -64,12 +66,12 @@ public Type getInType()

public abstract class EzyobjectToArray<T> : 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()
{
Expand All @@ -81,21 +83,59 @@ public interface IEzyConverter: IEzyReader, IEzyWriter
{
}

public abstract class EzyDataConverter<T> : 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<T> : 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()
{
Expand All @@ -112,17 +152,28 @@ public abstract class EzyArrayConverter<T> : 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()
{
Expand Down
50 changes: 35 additions & 15 deletions binding/EzyDateTimeConverter.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,47 @@
using System;
using System.Globalization;

namespace com.tvd12.ezyfoxserver.client.binding
{
public class EzyDateTimeConverter : IEzyConverter
public class EzyDateTimeConverter : EzyDataConverter<DateTime>
{
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;
}
}
}
9 changes: 9 additions & 0 deletions binding/EzyObjectBinding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace com.tvd12.ezyfoxserver.client.binding
{
[AttributeUsage(AttributeTargets.Class)]
public class EzyArrayBinding : System.Attribute
{
}
}
2 changes: 1 addition & 1 deletion binding/EzyReflectionArrayConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, object> valueByIndex = new SortedDictionary<int, object>();
Expand Down
2 changes: 1 addition & 1 deletion binding/EzyReflectionMapConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
1 change: 1 addition & 0 deletions binding/EzyValue.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;

namespace com.tvd12.ezyfoxserver.client.binding
{
[AttributeUsage(AttributeTargets.Property)]
Expand Down
7 changes: 7 additions & 0 deletions builder/EzyArrayBuilder.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -41,6 +42,12 @@ public EzyArrayBuilder append<T>(params T[] values)
return this;
}

public EzyArrayBuilder appendRawList(IList values)
{
product.addRawList(values);
return this;
}

public EzyArrayBuilder appendAll<T>(IList<T> values)
{
product.addAll<T>(values);
Expand Down
7 changes: 7 additions & 0 deletions builder/EzyObjectBuilder.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<K, V>(IDictionary<K, V> dict)
{
product.putAll<K, V>(dict);
Expand Down
11 changes: 10 additions & 1 deletion entity/EzyArray.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -38,7 +39,15 @@ public void add<T>(EzyBuilder<T> builder)
list.Add(t);
}

public void addAll<T>(ICollection<T> values)
public void addRawList(IList values)
{
foreach (Object value in values)
{
list.Add(inputTransformer.transform(value));
}
}

public void addAll<T>(IList<T> values)
{
foreach (T value in values)
{
Expand Down
Loading

0 comments on commit 31e462e

Please sign in to comment.