diff --git a/src/main/java/io/github/lonamiwebs/overgram/generator/Generator.java b/src/main/java/io/github/lonamiwebs/overgram/generator/Generator.java index 0fead3d..50ff8af 100644 --- a/src/main/java/io/github/lonamiwebs/overgram/generator/Generator.java +++ b/src/main/java/io/github/lonamiwebs/overgram/generator/Generator.java @@ -77,23 +77,44 @@ public class Generator { writer.write(" {\n"); for (int i = 0; i < object.args.size(); ++i) { - if (object.args.get(i).genericDefinition || object.args.get(i).flags) { + final TLArg arg = object.args.get(i); + if (arg.genericDefinition || arg.flags) { continue; } writer.write("private "); - writer.write(object.args.get(i).javaType()); + writer.write(arg.javaType()); writer.write(" " + VARIABLE_SUFFIX + i); writer.write(";\n"); + + // 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). + writer.write("public "); + writer.write(TLObject.toCamelCase(object.name)); + writer.write(' '); + writer.write(arg.javaName()); + writer.write('('); + writer.write(arg.javaType()); + writer.write(" value) {\n"); + writer.write(VARIABLE_SUFFIX + i); + writer.write(" = value;\n"); + writer.write("return this;\n"); + writer.write("}\n"); + + // Also allow getting the values in a similar manner without input value .argument() + writer.write("public "); + writer.write(arg.javaType()); + writer.write(' '); + writer.write(arg.javaName()); + writer.write("() {\n"); + writer.write("return " + VARIABLE_SUFFIX + i); + writer.write(";\n"); + writer.write("}\n"); } writer.write("public "); writer.write(TLObject.toCamelCase(object.name)); writer.write("() {}\n"); - // 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). - // TODO - writeSerialize(writer, object); writeDeserialize(writer, object); diff --git a/src/main/java/io/github/lonamiwebs/overgram/parser/TLArg.java b/src/main/java/io/github/lonamiwebs/overgram/parser/TLArg.java index 1c2c402..2a16158 100644 --- a/src/main/java/io/github/lonamiwebs/overgram/parser/TLArg.java +++ b/src/main/java/io/github/lonamiwebs/overgram/parser/TLArg.java @@ -131,6 +131,43 @@ public class TLArg { 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() { + switch (name) { + case "lat": + return "latitude"; // not reserved but consistent + case "long": + return "longitude"; + case "static": + return "isStatic"; + case "public": + return "isPublic"; + case "private": + return "isPrivate"; + case "final": + return "last"; + default: + return toCamelCase(name); + } + } + @Override public String toString() { final StringBuilder builder = new StringBuilder();