diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7141834 --- /dev/null +++ b/.gitignore @@ -0,0 +1,49 @@ +# Created by .ignore support plugin (hsz.mobi) +### Java template +*.class + +# Package Files # +*.jar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/ + +# Mongo Explorer plugin: +.idea/mongoSettings.xml + +## File-based project format: +*.iws +*.iml + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties +### Maven template +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..1d3cb1f --- /dev/null +++ b/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + + + com.rbkmoney + parent + 1.0.0 + + + com.rbkmoney + kebab + 0.1-SNAPSHOT + + + + com.rbkmoney.thrift + libthrift + 0.9.3-3 + + + junit + junit + 4.12 + test + + + + + + + org.apache.thrift + thrift-maven-plugin + 0.9.3-1 + + thrift + java:fullcamel + + + + thrift-test-sources + generate-test-sources + + testCompile + + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/rbkmoney/kebab/Kebab.java b/src/main/java/com/rbkmoney/kebab/Kebab.java new file mode 100644 index 0000000..2d416dd --- /dev/null +++ b/src/main/java/com/rbkmoney/kebab/Kebab.java @@ -0,0 +1,22 @@ +package com.rbkmoney.kebab; + +import com.rbkmoney.kebab.thrift.ThriftElement; +import org.apache.thrift.TBase; + +import java.util.Map; + +/** + * Created by tolkonepiu on 24/01/2017. + */ +public class Kebab { + + public String toJson(T src) { + throw new UnsupportedOperationException("under contruction"); + } + + public byte[] toMsgPack(T src) { + throw new UnsupportedOperationException("under contruction"); + } + + +} diff --git a/src/main/java/com/rbkmoney/kebab/Serializer.java b/src/main/java/com/rbkmoney/kebab/Serializer.java new file mode 100644 index 0000000..39d5f7f --- /dev/null +++ b/src/main/java/com/rbkmoney/kebab/Serializer.java @@ -0,0 +1,12 @@ +package com.rbkmoney.kebab; + +import java.io.IOException; + +/** + * Created by tolkonepiu on 27/01/2017. + */ +public interface Serializer { + + void write(Writer out, T value) throws IOException; + +} diff --git a/src/main/java/com/rbkmoney/kebab/ThriftType.java b/src/main/java/com/rbkmoney/kebab/ThriftType.java new file mode 100644 index 0000000..e30998a --- /dev/null +++ b/src/main/java/com/rbkmoney/kebab/ThriftType.java @@ -0,0 +1,42 @@ +package com.rbkmoney.kebab; + +import org.apache.thrift.protocol.TType; + +import java.util.Arrays; + +/** + * Created by tolkonepiu on 12/01/2017. + */ +public enum ThriftType { + + BOOLEAN(TType.BOOL), + BYTE(TType.BYTE), + DOUBLE(TType.DOUBLE), + SHORT(TType.I16), + INTEGER(TType.I32), + LONG(TType.I64), + STRING(TType.STRING), + ENUM(TType.ENUM), + LIST(TType.LIST), + SET(TType.SET), + MAP(TType.MAP), + STRUCT(TType.STRUCT); + + int code; + + ThriftType(int code) { + this.code = code; + } + + public int getCode() { + return code; + } + + public static ThriftType findByCode(int code) { + return Arrays.stream(values()) + .filter(t -> t.getCode() == code) + .findFirst() + .orElseThrow(IllegalArgumentException::new); + } + +} diff --git a/src/main/java/com/rbkmoney/kebab/Writer.java b/src/main/java/com/rbkmoney/kebab/Writer.java new file mode 100644 index 0000000..3cc3d41 --- /dev/null +++ b/src/main/java/com/rbkmoney/kebab/Writer.java @@ -0,0 +1,51 @@ +package com.rbkmoney.kebab; + +import java.io.Closeable; +import java.io.IOException; + +/** + * Created by tolkonepiu on 26/01/2017. + */ +public interface Writer extends Closeable { + + Writer beginObject() throws IOException; + + Writer endObject() throws IOException; + + Writer beginList() throws IOException; + + Writer endList() throws IOException; + + Writer beginMap() throws IOException; + + Writer endMap() throws IOException; + + Writer beginKey() throws IOException; + + Writer endKey() throws IOException; + + Writer beginValue() throws IOException; + + Writer endValue() throws IOException; + + Writer name(String name) throws IOException; + + Writer value(boolean value) throws IOException; + + Writer value(String value) throws IOException; + + Writer value(byte value) throws IOException; + + Writer value(short value) throws IOException; + + Writer value(int value) throws IOException; + + Writer value(double value) throws IOException; + + Writer value(long value) throws IOException; + + Writer binaryValue(byte[] value) throws IOException; + + Writer nullValue() throws IOException; + +} diff --git a/src/main/java/com/rbkmoney/kebab/serializer/TBaseSerializer.java b/src/main/java/com/rbkmoney/kebab/serializer/TBaseSerializer.java new file mode 100644 index 0000000..761c571 --- /dev/null +++ b/src/main/java/com/rbkmoney/kebab/serializer/TBaseSerializer.java @@ -0,0 +1,116 @@ +package com.rbkmoney.kebab.serializer; + +import com.rbkmoney.kebab.Serializer; +import com.rbkmoney.kebab.Writer; +import com.rbkmoney.kebab.ThriftType; +import org.apache.thrift.TBase; +import org.apache.thrift.TFieldIdEnum; +import org.apache.thrift.meta_data.*; + +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Created by tolkonepiu on 27/01/2017. + */ +public class TBaseSerializer implements Serializer { + + @Override + public void write(Writer out, TBase value) throws IOException { + out.beginObject(); + + if (value == null) { + out.nullValue(); + } + + TFieldIdEnum[] tFieldIdEnums = value.getFields(); + Map fieldMetaDataMap = value.getFieldMetaData(); + + for (TFieldIdEnum tFieldIdEnum : tFieldIdEnums) { + if (value.isSet(tFieldIdEnum)) { + out.name(tFieldIdEnum.getFieldName()); + FieldMetaData fieldMetaData = fieldMetaDataMap.get(tFieldIdEnum); + write(out, value.getFieldValue(tFieldIdEnum), fieldMetaData.valueMetaData); + } + } + out.endObject(); + } + + private void write(Writer out, Object object, FieldValueMetaData fieldValueMetaData) throws IOException { + ThriftType type = ThriftType.findByCode(fieldValueMetaData.getType()); + boolean isBinary = fieldValueMetaData.isBinary(); + + if (isBinary) { + out.binaryValue((byte[]) object); + } else { + switch (type) { + case BOOLEAN: + out.value((boolean) object); + break; + case STRING: + out.value((String) object); + break; + case BYTE: + out.value((byte) object); + break; + case SHORT: + out.value((short) object); + break; + case INTEGER: + out.value((int) object); + break; + case LONG: + out.value((long) object); + break; + case LIST: + write(out, (List) object, (ListMetaData) fieldValueMetaData); + break; + case SET: + write(out, (Set) object, (SetMetaData) fieldValueMetaData); + break; + case MAP: + write(out, (Map) object, (MapMetaData) fieldValueMetaData); + break; + case STRUCT: + write(out, (TBase) object); + break; + default: + throw new IllegalStateException(String.format("Type '%s' not found", type)); + } + } + } + + private void write(Writer out, Set objectSet, SetMetaData metaData) throws IOException { + out.beginList(); + for (Object object : objectSet) { + write(out, object, metaData.getElementMetaData()); + } + out.endList(); + } + + private void write(Writer out, List objectList, ListMetaData metaData) throws IOException { + out.beginList(); + for (Object object : objectList) { + write(out, object, metaData.getElementMetaData()); + } + out.endList(); + } + + + private void write(Writer out, Map objectMap, MapMetaData metaData) throws IOException { + out.beginMap(); + for (Map.Entry entry : (Set) objectMap.entrySet()) { + out.beginKey(); + write(out, entry.getKey(), metaData.getKeyMetaData()); + out.endKey(); + out.beginValue(); + write(out, entry.getValue(), metaData.getValueMetaData()); + out.endValue(); + } + out.endMap(); + } + + +} diff --git a/src/main/java/com/rbkmoney/kebab/writer/JsonWriter.java b/src/main/java/com/rbkmoney/kebab/writer/JsonWriter.java new file mode 100644 index 0000000..d01d58b --- /dev/null +++ b/src/main/java/com/rbkmoney/kebab/writer/JsonWriter.java @@ -0,0 +1,116 @@ +package com.rbkmoney.kebab.writer; + +import com.rbkmoney.kebab.Writer; + +import java.io.IOException; + +/** + * Created by tolkonepiu on 27/01/2017. + */ +public class JsonWriter implements Writer { + @Override + public Writer beginObject() throws IOException { + return null; + } + + @Override + public Writer endObject() throws IOException { + return null; + } + + @Override + public Writer beginList() throws IOException { + return null; + } + + @Override + public Writer endList() throws IOException { + return null; + } + + @Override + public Writer beginMap() throws IOException { + return null; + } + + @Override + public Writer endMap() throws IOException { + return null; + } + + @Override + public Writer beginKey() throws IOException { + return null; + } + + @Override + public Writer endKey() throws IOException { + return null; + } + + @Override + public Writer beginValue() throws IOException { + return null; + } + + @Override + public Writer endValue() throws IOException { + return null; + } + + @Override + public Writer name(String name) throws IOException { + return null; + } + + @Override + public Writer value(boolean value) throws IOException { + return null; + } + + @Override + public Writer value(String value) throws IOException { + return null; + } + + @Override + public Writer value(byte value) throws IOException { + return null; + } + + @Override + public Writer value(short value) throws IOException { + return null; + } + + @Override + public Writer value(int value) throws IOException { + return null; + } + + @Override + public Writer value(double value) throws IOException { + return null; + } + + @Override + public Writer value(long value) throws IOException { + return null; + } + + @Override + public Writer binaryValue(byte[] value) throws IOException { + return null; + } + + @Override + public Writer nullValue() throws IOException { + return null; + } + + @Override + public void close() throws IOException { + + } + +} diff --git a/src/test/java/com/rbkmoney/kebab/KebabTest.java b/src/test/java/com/rbkmoney/kebab/KebabTest.java new file mode 100644 index 0000000..150cf16 --- /dev/null +++ b/src/test/java/com/rbkmoney/kebab/KebabTest.java @@ -0,0 +1,55 @@ +package com.rbkmoney.kebab; + +import com.rbkmoney.kebab.test.Fail; +import com.rbkmoney.kebab.test.Ids; +import com.rbkmoney.kebab.test.Status; +import com.rbkmoney.kebab.test.TestObject; +import com.rbkmoney.kebab.thrift.ThriftElement; +import org.junit.Test; + +import java.util.*; + +/** + * Created by tolkonepiu on 25/01/2017. + */ +public class KebabTest { + + @Test + public void kebabTesting() { + TestObject testObject = new TestObject(); + testObject.setDescription("kek"); + testObject.setValue(2.32); + + Ids ids = new Ids(); + ids.setBigId(2141214124L); + ids.setId(12312); + ids.setMiniId((short) 2334); + ids.setMicroId((byte) 127); + + testObject.setIds(ids); + + testObject.setData(new byte[]{4, 2}); + + testObject.setNumbers(Arrays.asList(1, 2, 3, 4, 5)); + + Set suk = new HashSet<>(Arrays.asList("kek1", "kek2")); + + testObject.setFuck(Arrays.asList(suk, suk, suk)); + + Fail fail = new Fail(); + + fail.setReasons(new HashSet<>(Arrays.asList("kek1", "kek2"))); + + Map map = new HashMap<>(); + map.put("kek1", 455); + map.put("kek2", 564); + map.put("kek3", 565); + testObject.setMaps(map); + + testObject.setStatus(Status.fail(new Fail(fail))); + + Kebab kebab = new Kebab(); + kebab.toJson(testObject); + } + +} diff --git a/src/test/thrift/test1.thrift b/src/test/thrift/test1.thrift new file mode 100644 index 0000000..029d8a9 --- /dev/null +++ b/src/test/thrift/test1.thrift @@ -0,0 +1,37 @@ +namespace java com.rbkmoney.kebab.test + +struct TestObject { + 1: required Ids ids + 2: required double value + 3: optional string description + 4: optional binary data + 5: required list numbers + 6: required Status status + 7: required list> fuck + 8: optional map maps +} + +union Status { + 1: Ok ok + 2: Fail fail + 3: Unknown unknown +} + +struct Unknown { + 1: required string description +} + +struct Fail { + 1: optional set reasons +} + +struct Ok { + +} + +struct Ids { + 1: required i8 micro_id + 2: required i16 mini_id + 3: required i32 id + 4: required i64 big_id +} \ No newline at end of file