Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor thrift type #73

Merged
merged 11 commits into from
Nov 8, 2016
4 changes: 2 additions & 2 deletions thrifty-compiler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ sourceSets {
srcDir 'src/main/java'

// Include the generated test classes in the regular build
srcDir 'build/src/gen/java'
srcDir 'build/generated-src/gen/java'
}
}
}
Expand All @@ -57,7 +57,7 @@ task compileTestCase(type: Exec) {
dependsOn jarTask

executable 'java'
args('-jar', jarTask.archivePath.absolutePath, "--parcelable" , "--out=$projectDir/build/src/gen/java", "$projectDir/TestThrift.thrift")
args('-jar', jarTask.archivePath.absolutePath, "--use-java-style-names", "--out=$projectDir/build/generated-src/gen/java", "$projectDir/TestThrift.thrift")
}

afterEvaluate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public ThriftyCompiler setOutputDirectory(File directory) {
}

public void compile() throws IOException {
Loader loader = new Loader(fieldNamingPolicy);
Loader loader = new Loader();
for (String thriftFile : thriftFiles) {
loader.addThriftFile(thriftFile);
}
Expand All @@ -192,7 +192,7 @@ public void compile() throws IOException {
return;
}

ThriftyCodeGenerator gen = new ThriftyCodeGenerator(schema);
ThriftyCodeGenerator gen = new ThriftyCodeGenerator(schema, fieldNamingPolicy);
if (listTypeName != null) {
gen = gen.withListType(listTypeName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@
*/
package com.microsoft.thrifty.gen;

import com.microsoft.thrifty.schema.BuiltinThriftType;
import com.microsoft.thrifty.schema.Constant;
import com.microsoft.thrifty.schema.EnumMember;
import com.microsoft.thrifty.schema.EnumType;
import com.microsoft.thrifty.schema.ListType;
import com.microsoft.thrifty.schema.MapType;
import com.microsoft.thrifty.schema.NamespaceScope;
import com.microsoft.thrifty.schema.Schema;
import com.microsoft.thrifty.schema.ServiceType;
import com.microsoft.thrifty.schema.SetType;
import com.microsoft.thrifty.schema.StructType;
import com.microsoft.thrifty.schema.ThriftType;
import com.microsoft.thrifty.schema.TypedefType;
import com.microsoft.thrifty.schema.parser.ConstValueElement;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.NameAllocator;
Expand Down Expand Up @@ -64,15 +72,15 @@ public Void visitBuiltin(ThriftType builtinType) {
}

@Override
public Void visitEnum(ThriftType userType) {
public Void visitEnum(EnumType userType) {
CodeBlock item = renderConstValue(initializer, allocator, scope, tt, value);

initializer.addStatement("$L = $L", name, item);
return null;
}

@Override
public Void visitList(ThriftType.ListType listType) {
public Void visitList(ListType listType) {
List<ConstValueElement> list = (List<ConstValueElement>) value.value();
ThriftType elementType = listType.elementType().getTrueType();
TypeName elementTypeName = typeResolver.getJavaClass(elementType);
Expand All @@ -83,7 +91,7 @@ public Void visitList(ThriftType.ListType listType) {
}

@Override
public Void visitSet(ThriftType.SetType setType) {
public Void visitSet(SetType setType) {
List<ConstValueElement> set = (List<ConstValueElement>) value.value();
ThriftType elementType = setType.elementType().getTrueType();
TypeName elementTypeName = typeResolver.getJavaClass(elementType);
Expand Down Expand Up @@ -112,7 +120,7 @@ private void generateSingleElementCollection(
}

@Override
public Void visitMap(ThriftType.MapType mapType) {
public Void visitMap(MapType mapType) {
Map<ConstValueElement, ConstValueElement> map =
(Map<ConstValueElement, ConstValueElement>) value.value();
ThriftType keyType = mapType.keyType().getTrueType();
Expand Down Expand Up @@ -140,13 +148,23 @@ public Void visitMap(ThriftType.MapType mapType) {
}

@Override
public Void visitUserType(ThriftType userType) {
public Void visitStruct(StructType userType) {
// TODO: this
throw new UnsupportedOperationException("struct-type default values are not yet implemented");
}

@Override
public Void visitTypedef(ThriftType.TypedefType typedefType) {
public Void visitTypedef(TypedefType typedefType) {
throw new AssertionError("Should not be possible!");
}

@Override
public Void visitService(ServiceType serviceType) {
throw new AssertionError("Should not be possible!");
}

@Override
public Void visitVoid(BuiltinThriftType voidType) {
throw new AssertionError("Should not be possible!");
}
});
Expand Down Expand Up @@ -182,7 +200,7 @@ private class ConstRenderingVisitor implements ThriftType.Visitor<CodeBlock> {
}

@Override
public CodeBlock visitBool() {
public CodeBlock visitBool(BuiltinThriftType boolType) {
String name;
if (value.isIdentifier()
&& ("true".equals(value.getAsString()) || "false".equals(value.getAsString()))) {
Expand All @@ -197,7 +215,7 @@ public CodeBlock visitBool() {
}

@Override
public CodeBlock visitByte() {
public CodeBlock visitByte(BuiltinThriftType byteType) {
if (value.isInt()) {
return CodeBlock.builder().add("(byte) $L", value.getAsInt()).build();
} else {
Expand All @@ -206,7 +224,7 @@ public CodeBlock visitByte() {
}

@Override
public CodeBlock visitI16() {
public CodeBlock visitI16(BuiltinThriftType i16Type) {
if (value.isInt()) {
return CodeBlock.builder().add("(short) $L", value.getAsInt()).build();
} else {
Expand All @@ -215,7 +233,7 @@ public CodeBlock visitI16() {
}

@Override
public CodeBlock visitI32() {
public CodeBlock visitI32(BuiltinThriftType i32Type) {
if (value.isInt()) {
return CodeBlock.builder().add("$L", value.getAsInt()).build();
} else {
Expand All @@ -224,7 +242,7 @@ public CodeBlock visitI32() {
}

@Override
public CodeBlock visitI64() {
public CodeBlock visitI64(BuiltinThriftType i64Type) {
if (value.isInt()) {
return CodeBlock.builder().add("$L", value.getAsLong()).build();
} else {
Expand All @@ -233,7 +251,7 @@ public CodeBlock visitI64() {
}

@Override
public CodeBlock visitDouble() {
public CodeBlock visitDouble(BuiltinThriftType doubleType) {
if (value.isInt() || value.isDouble()) {
return CodeBlock.builder().add("(double) $L", value.getAsDouble()).build();
} else {
Expand All @@ -242,7 +260,7 @@ public CodeBlock visitDouble() {
}

@Override
public CodeBlock visitString() {
public CodeBlock visitString(BuiltinThriftType stringType) {
if (value.isString()) {
return CodeBlock.builder().add("$S", value.getAsString()).build();
} else {
Expand All @@ -251,26 +269,19 @@ public CodeBlock visitString() {
}

@Override
public CodeBlock visitBinary() {
public CodeBlock visitBinary(BuiltinThriftType binaryType) {
throw new UnsupportedOperationException("Binary literals are not supported");
}

@Override
public CodeBlock visitVoid() {
public CodeBlock visitVoid(BuiltinThriftType voidType) {
throw new AssertionError("Void literals are meaningless, what are you even doing");
}

@Override
public CodeBlock visitEnum(final ThriftType tt) {
EnumType enumType;
try {
enumType = schema.findEnumByType(tt);
} catch (NoSuchElementException e) {
throw new AssertionError("Missing enum type: " + tt.name());
}

public CodeBlock visitEnum(EnumType enumType) {
// TODO(ben): Figure out how to handle const references
EnumType.Member member;
EnumMember member;
try {
if (value.kind() == ConstValueElement.Kind.INTEGER) {
member = enumType.findMemberById(value.getAsInt());
Expand All @@ -294,12 +305,12 @@ public CodeBlock visitEnum(final ThriftType tt) {
}

return CodeBlock.builder()
.add("$T.$L", typeResolver.getJavaClass(tt), member.name())
.add("$T.$L", typeResolver.getJavaClass(enumType), member.name())
.build();
}

@Override
public CodeBlock visitList(ThriftType.ListType listType) {
public CodeBlock visitList(ListType listType) {
if (value.isList()) {
if (value.getAsList().isEmpty()) {
TypeName elementType = typeResolver.getJavaClass(listType.elementType());
Expand All @@ -314,7 +325,7 @@ public CodeBlock visitList(ThriftType.ListType listType) {
}

@Override
public CodeBlock visitSet(ThriftType.SetType setType) {
public CodeBlock visitSet(SetType setType) {
if (value.isList()) { // not a typo; ConstantValueElement.Kind.LIST covers lists and sets.
if (value.getAsList().isEmpty()) {
TypeName elementType = typeResolver.getJavaClass(setType.elementType());
Expand All @@ -329,7 +340,7 @@ public CodeBlock visitSet(ThriftType.SetType setType) {
}

@Override
public CodeBlock visitMap(ThriftType.MapType mapType) {
public CodeBlock visitMap(MapType mapType) {
if (value.isMap()) {
if (value.getAsMap().isEmpty()) {
TypeName keyType = typeResolver.getJavaClass(mapType.keyType());
Expand All @@ -354,13 +365,18 @@ private CodeBlock visitCollection(
}

@Override
public CodeBlock visitUserType(ThriftType userType) {
public CodeBlock visitStruct(StructType userType) {
throw new IllegalStateException("nested structs not implemented");
}

@Override
public CodeBlock visitTypedef(ThriftType.TypedefType typedefType) {
return null;
public CodeBlock visitTypedef(TypedefType typedefType) {
return typedefType.oldType().accept(this);
}

@Override
public CodeBlock visitService(ServiceType serviceType) {
throw new IllegalStateException("constants cannot be services");
}

private CodeBlock constantOrError(String error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Thrifty
*
* Copyright (c) Microsoft Corporation
*
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
* WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE,
* FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
*
* See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
*/
package com.microsoft.thrifty.gen;

import com.microsoft.thrifty.schema.Field;
import com.microsoft.thrifty.schema.FieldNamingPolicy;

import java.util.LinkedHashMap;
import java.util.Map;

class FieldNamer {
private final Map<Field, String> nameCache = new LinkedHashMap<>();
private final FieldNamingPolicy namingPolicy;

FieldNamer(FieldNamingPolicy namingPolicy) {
this.namingPolicy = namingPolicy;
}

public String getName(Field field) {
String name = nameCache.get(field);
if (name == null) {
name = namingPolicy.apply(field.name());
nameCache.put(field, name);
}
return name;
}
}