mirror of
https://github.com/valitydev/geck.git
synced 2024-11-06 01:35:22 +00:00
BJ-76: Thrift serializer (#1)
* BJ-76: init commit * BJ-76: Small changes * BJ-76: Remove redundant objects
This commit is contained in:
parent
d20de47d49
commit
704b487e82
49
.gitignore
vendored
Normal file
49
.gitignore
vendored
Normal file
@ -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
|
54
pom.xml
Normal file
54
pom.xml
Normal file
@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>com.rbkmoney</groupId>
|
||||
<artifactId>parent</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
|
||||
<groupId>com.rbkmoney</groupId>
|
||||
<artifactId>kebab</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.rbkmoney.thrift</groupId>
|
||||
<artifactId>libthrift</artifactId>
|
||||
<version>0.9.3-3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.thrift</groupId>
|
||||
<artifactId>thrift-maven-plugin</artifactId>
|
||||
<version>0.9.3-1</version>
|
||||
<configuration>
|
||||
<thriftExecutable>thrift</thriftExecutable>
|
||||
<generator>java:fullcamel</generator>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>thrift-test-sources</id>
|
||||
<phase>generate-test-sources</phase>
|
||||
<goals>
|
||||
<goal>testCompile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
22
src/main/java/com/rbkmoney/kebab/Kebab.java
Normal file
22
src/main/java/com/rbkmoney/kebab/Kebab.java
Normal file
@ -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 <T extends TBase> {
|
||||
|
||||
public String toJson(T src) {
|
||||
throw new UnsupportedOperationException("under contruction");
|
||||
}
|
||||
|
||||
public byte[] toMsgPack(T src) {
|
||||
throw new UnsupportedOperationException("under contruction");
|
||||
}
|
||||
|
||||
|
||||
}
|
12
src/main/java/com/rbkmoney/kebab/Serializer.java
Normal file
12
src/main/java/com/rbkmoney/kebab/Serializer.java
Normal file
@ -0,0 +1,12 @@
|
||||
package com.rbkmoney.kebab;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by tolkonepiu on 27/01/2017.
|
||||
*/
|
||||
public interface Serializer<T> {
|
||||
|
||||
void write(Writer out, T value) throws IOException;
|
||||
|
||||
}
|
42
src/main/java/com/rbkmoney/kebab/ThriftType.java
Normal file
42
src/main/java/com/rbkmoney/kebab/ThriftType.java
Normal file
@ -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);
|
||||
}
|
||||
|
||||
}
|
51
src/main/java/com/rbkmoney/kebab/Writer.java
Normal file
51
src/main/java/com/rbkmoney/kebab/Writer.java
Normal file
@ -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;
|
||||
|
||||
}
|
116
src/main/java/com/rbkmoney/kebab/serializer/TBaseSerializer.java
Normal file
116
src/main/java/com/rbkmoney/kebab/serializer/TBaseSerializer.java
Normal file
@ -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<TBase> {
|
||||
|
||||
@Override
|
||||
public void write(Writer out, TBase value) throws IOException {
|
||||
out.beginObject();
|
||||
|
||||
if (value == null) {
|
||||
out.nullValue();
|
||||
}
|
||||
|
||||
TFieldIdEnum[] tFieldIdEnums = value.getFields();
|
||||
Map<TFieldIdEnum, FieldMetaData> 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<Map.Entry>) objectMap.entrySet()) {
|
||||
out.beginKey();
|
||||
write(out, entry.getKey(), metaData.getKeyMetaData());
|
||||
out.endKey();
|
||||
out.beginValue();
|
||||
write(out, entry.getValue(), metaData.getValueMetaData());
|
||||
out.endValue();
|
||||
}
|
||||
out.endMap();
|
||||
}
|
||||
|
||||
|
||||
}
|
116
src/main/java/com/rbkmoney/kebab/writer/JsonWriter.java
Normal file
116
src/main/java/com/rbkmoney/kebab/writer/JsonWriter.java
Normal file
@ -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 {
|
||||
|
||||
}
|
||||
|
||||
}
|
55
src/test/java/com/rbkmoney/kebab/KebabTest.java
Normal file
55
src/test/java/com/rbkmoney/kebab/KebabTest.java
Normal file
@ -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<String> 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<String, Integer> 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);
|
||||
}
|
||||
|
||||
}
|
37
src/test/thrift/test1.thrift
Normal file
37
src/test/thrift/test1.thrift
Normal file
@ -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<i32> numbers
|
||||
6: required Status status
|
||||
7: required list<set<string>> fuck
|
||||
8: optional map<string, i32> maps
|
||||
}
|
||||
|
||||
union Status {
|
||||
1: Ok ok
|
||||
2: Fail fail
|
||||
3: Unknown unknown
|
||||
}
|
||||
|
||||
struct Unknown {
|
||||
1: required string description
|
||||
}
|
||||
|
||||
struct Fail {
|
||||
1: optional set<string> reasons
|
||||
}
|
||||
|
||||
struct Ok {
|
||||
|
||||
}
|
||||
|
||||
struct Ids {
|
||||
1: required i8 micro_id
|
||||
2: required i16 mini_id
|
||||
3: required i32 id
|
||||
4: required i64 big_id
|
||||
}
|
Loading…
Reference in New Issue
Block a user