Fix some name inconsistencies

This commit is contained in:
Lonami Exo 2018-07-23 23:36:33 +02:00
parent f8f80c1659
commit 11755fb266
5 changed files with 45 additions and 35 deletions

View File

@ -2,6 +2,7 @@ package io.github.lonamiwebs.overgram.generator;
import io.github.lonamiwebs.overgram.parser.TLArg; import io.github.lonamiwebs.overgram.parser.TLArg;
import io.github.lonamiwebs.overgram.parser.TLObject; import io.github.lonamiwebs.overgram.parser.TLObject;
import io.github.lonamiwebs.overgram.utils.Utils;
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
@ -37,7 +38,7 @@ public class Generator {
} }
for (final String name : nsNames.getValue()) { for (final String name : nsNames.getValue()) {
writer.write("public static abstract class "); writer.write("public static abstract class ");
writer.write(TLObject.toCamelCase(name)); writer.write(Utils.toCamelCase(name, true));
writer.write(" extends TLObject {}\n"); writer.write(" extends TLObject {}\n");
} }
if (!nsNames.getKey().isEmpty()) { if (!nsNames.getKey().isEmpty()) {
@ -67,11 +68,11 @@ public class Generator {
} }
for (final TLObject object : nsObjects.getValue()) { for (final TLObject object : nsObjects.getValue()) {
writer.write("public static class "); writer.write("public static class ");
writer.write(TLObject.toCamelCase(object.name)); writer.write(Utils.toCamelCase(object.name, true));
writer.write(" extends "); writer.write(" extends ");
if (extendsName == null) { if (extendsName == null) {
writer.write("Abstract."); writer.write("Abstract.");
writer.write(TLObject.toCamelCase(object.type)); writer.write(Utils.toCamelCase(object.type, true));
} else { } else {
writer.write(extendsName); writer.write(extendsName);
} }
@ -91,7 +92,7 @@ public class Generator {
// Use the builder pattern because Java is stupid and has no optional named arguments. // Use the builder pattern because Java is stupid and has no optional named arguments.
// Objects will be created with nothing set and the arguments set with .argument(value). // Objects will be created with nothing set and the arguments set with .argument(value).
writer.write("public "); writer.write("public ");
writer.write(TLObject.toCamelCase(object.name)); writer.write(Utils.toCamelCase(object.name, true));
writer.write(' '); writer.write(' ');
writer.write(arg.javaName()); writer.write(arg.javaName());
writer.write('('); writer.write('(');
@ -114,7 +115,7 @@ public class Generator {
} }
writer.write("public "); writer.write("public ");
writer.write(TLObject.toCamelCase(object.name)); writer.write(Utils.toCamelCase(object.name, true));
writer.write("() {}\n"); writer.write("() {}\n");
writeSerialize(writer, object); writeSerialize(writer, object);
@ -135,7 +136,7 @@ public class Generator {
writer.write("case " + object.code); writer.write("case " + object.code);
writer.write(":\n"); writer.write(":\n");
writer.write("return new Types."); writer.write("return new Types.");
writer.write(TLObject.toCamelCase(object.fullname)); writer.write(Utils.toCamelCase(object.fullname, true));
writer.write("();\n"); writer.write("();\n");
} }

View File

@ -1,5 +1,7 @@
package io.github.lonamiwebs.overgram.parser; package io.github.lonamiwebs.overgram.parser;
import io.github.lonamiwebs.overgram.utils.Utils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -113,7 +115,7 @@ public class TLArg {
break; break;
default: default:
builder.append("Abstract."); builder.append("Abstract.");
builder.append(TLObject.toCamelCase(type)); builder.append(Utils.toCamelCase(type, true));
break; break;
} }
builder.append('<'); builder.append('<');
@ -132,24 +134,6 @@ public class TLArg {
return types.size() == 1 && types.get(0).equals("true") ? "" : " != null"; return types.size() == 1 && types.get(0).equals("true") ? "" : " != null";
} }
public static String toCamelCase(final String string) {
final StringBuilder result = new StringBuilder(string.length());
result.append(Character.toLowerCase(string.charAt(0)));
boolean upperNext = false;
for (int i = 1; i < string.length(); ++i) {
final char c = string.charAt(i);
if (c == '_') {
upperNext = true;
} else if (upperNext) {
upperNext = false;
result.append(Character.toUpperCase(c));
} else {
result.append(Character.toLowerCase(c));
}
}
return result.toString();
}
public String javaName() { public String javaName() {
switch (name) { switch (name) {
case "lat": case "lat":
@ -165,7 +149,7 @@ public class TLArg {
case "final": case "final":
return "last"; return "last";
default: default:
return toCamelCase(name); return Utils.toCamelCase(name, false);
} }
} }

View File

@ -28,13 +28,6 @@ public class TLObject {
} }
} }
public static String toCamelCase(final String string) {
final int dot = string.lastIndexOf(".");
return string.substring(0, dot + 1)
+ string.substring(dot + 1, dot + 2).toUpperCase()
+ string.substring(dot + 2);
}
public static TLObject fromString(String string) { public static TLObject fromString(String string) {
String[] tmp; String[] tmp;
String name; String name;

View File

@ -0,0 +1,31 @@
package io.github.lonamiwebs.overgram.utils;
public class Utils {
public static String toCamelCase(final String string, final boolean firstUpper) {
final StringBuilder result = new StringBuilder(string.length());
final int dot = string.lastIndexOf(".");
if (dot != -1) {
result.append(string, 0, dot + 1);
}
if (firstUpper) {
result.append(Character.toUpperCase(string.charAt(dot + 1)));
} else {
result.append(Character.toLowerCase(string.charAt(dot + 1)));
}
boolean upperNext = false;
for (int i = dot + 2; i < string.length(); ++i) {
final char c = string.charAt(i);
if (c == '_') {
upperNext = true;
} else if (upperNext) {
upperNext = false;
result.append(Character.toUpperCase(c));
} else {
result.append(c);
}
}
return result.toString();
}
}

View File

@ -3,7 +3,7 @@
//double ? = Double; //double ? = Double;
//string ? = String; //string ? = String;
dummyHttpWait = HttpWait; //dummyHttpWait = HttpWait;
//vector {t:Type} # [ t ] = Vector t; //vector {t:Type} # [ t ] = Vector t;
@ -63,7 +63,8 @@ msgs_all_info#8cc0d131 msg_ids:Vector<long> info:string = MsgsAllInfo;
msg_detailed_info#276d3ec6 msg_id:long answer_msg_id:long bytes:int status:int = MsgDetailedInfo; msg_detailed_info#276d3ec6 msg_id:long answer_msg_id:long bytes:int status:int = MsgDetailedInfo;
msg_new_detailed_info#809db6df answer_msg_id:long bytes:int status:int = MsgDetailedInfo; msg_new_detailed_info#809db6df answer_msg_id:long bytes:int status:int = MsgDetailedInfo;
rsa_public_key n:string e:bytes = RSAPublicKey; // TODO infer ID
//rsa_public_key n:string e:bytes = RSAPublicKey;
---functions--- ---functions---