|
14 | 14 |
|
15 | 15 | import com.algorand.sdkutils.generators.Utils;
|
16 | 16 | import com.algorand.sdkutils.listeners.Publisher.Events;
|
| 17 | +import com.algorand.sdkutils.utils.QueryDef; |
17 | 18 | import com.algorand.sdkutils.utils.StructDef;
|
18 | 19 | import com.algorand.sdkutils.utils.Tools;
|
19 | 20 | import com.algorand.sdkutils.utils.TypeDef;
|
@@ -81,20 +82,18 @@ public JavaGenerator(
|
81 | 82 | public void onEvent(Events event) {
|
82 | 83 | switch(event) {
|
83 | 84 | case END_QUERY:
|
84 |
| - javaQueryWriter.finalize(); |
| 85 | + javaQueryWriter.finish(); |
85 | 86 | break;
|
86 | 87 | default:
|
87 | 88 | throw new RuntimeException("Unimplemented event! " + event);
|
88 | 89 | }
|
89 | 90 | }
|
90 | 91 |
|
91 | 92 | @Override
|
92 |
| - public void onEvent(Events event, String [] notes) { |
| 93 | + public void onEvent(Events event, QueryDef query) { |
93 | 94 | switch(event) {
|
94 | 95 | case NEW_QUERY:
|
95 |
| - javaQueryWriter = new JavaQueryWriter( |
96 |
| - notes[0], notes[1], notes[2], |
97 |
| - notes[3], notes[4], this); |
| 96 | + javaQueryWriter = new JavaQueryWriter(query, this); |
98 | 97 | break;
|
99 | 98 | default:
|
100 | 99 | throw new RuntimeException("Unimplemented event for note! " + event);
|
@@ -434,47 +433,41 @@ final class JavaQueryWriter {
|
434 | 433 |
|
435 | 434 | TreeMap<String, Set<String>> imports;
|
436 | 435 |
|
437 |
| - public JavaQueryWriter( |
438 |
| - String methodName, |
439 |
| - String returnType, |
440 |
| - String path, |
441 |
| - String desc, |
442 |
| - String httpMethod, |
443 |
| - JavaGenerator javaGenerator) { |
444 |
| - |
445 |
| - this.className = Tools.getCamelCase(methodName, true); |
446 |
| - decls = new StringBuilder(); |
447 |
| - builders = new StringBuilder(); |
448 |
| - constructorHeader = new StringBuilder(); |
449 |
| - constructorBody = new StringBuilder(); |
450 |
| - requestMethod = new StringBuilder(); |
451 |
| - constructorComments = new ArrayList<String>(); |
452 |
| - |
453 |
| - generatedPathsEntryBody = new StringBuilder(); |
454 |
| - this.httpMethod = httpMethod; |
455 |
| - |
456 |
| - requestMethod.append(getQueryResponseMethod(returnType)); |
457 |
| - requestMethod.append(" protected QueryData getRequestString() {\n"); |
458 |
| - pAdded = false; |
459 |
| - addFormatMsgpack = false; |
460 |
| - |
461 |
| - this.path = path; |
462 |
| - discAndPath = desc + "\n" + path; |
| 436 | + public JavaQueryWriter(QueryDef query, JavaGenerator javaGenerator) { |
| 437 | + |
| 438 | + this.className = Tools.getCamelCase(query.name, true); |
| 439 | + this.decls = new StringBuilder(); |
| 440 | + this.builders = new StringBuilder(); |
| 441 | + this.constructorHeader = new StringBuilder(); |
| 442 | + this.constructorBody = new StringBuilder(); |
| 443 | + this.requestMethod = new StringBuilder(); |
| 444 | + this.constructorComments = new ArrayList<String>(); |
| 445 | + |
| 446 | + this.generatedPathsEntryBody = new StringBuilder(); |
| 447 | + this.httpMethod = query.method; |
| 448 | + |
| 449 | + this.requestMethod.append(getQueryResponseMethod(query.returnType)); |
| 450 | + this.requestMethod.append(" protected QueryData getRequestString() {\n"); |
| 451 | + this.pAdded = false; |
| 452 | + this.addFormatMsgpack = false; |
| 453 | + |
| 454 | + this.path = query.path; |
| 455 | + this.discAndPath = query.description + "\n" + query.path; |
463 | 456 |
|
464 | 457 | this.javaGen = javaGenerator;
|
465 | 458 |
|
466 |
| - imports = new TreeMap<String, Set<String>>(); |
467 |
| - Tools.addImport(imports, "com.algorand.algosdk.v2.client.common.Client"); |
468 |
| - Tools.addImport(imports, "com.algorand.algosdk.v2.client.common.HttpMethod"); |
469 |
| - Tools.addImport(imports, "com.algorand.algosdk.v2.client.common.Query"); |
470 |
| - Tools.addImport(imports, "com.algorand.algosdk.v2.client.common.QueryData"); |
471 |
| - Tools.addImport(imports, "com.algorand.algosdk.v2.client.common.Response"); |
472 |
| - if (needsClassImport(returnType.toLowerCase())) { |
473 |
| - Tools.addImport(imports, javaGen.modelPackage + "." + returnType); |
| 459 | + this.imports = new TreeMap<String, Set<String>>(); |
| 460 | + Tools.addImport(this.imports, "com.algorand.algosdk.v2.client.common.Client"); |
| 461 | + Tools.addImport(this.imports, "com.algorand.algosdk.v2.client.common.HttpMethod"); |
| 462 | + Tools.addImport(this.imports, "com.algorand.algosdk.v2.client.common.Query"); |
| 463 | + Tools.addImport(this.imports, "com.algorand.algosdk.v2.client.common.QueryData"); |
| 464 | + Tools.addImport(this.imports, "com.algorand.algosdk.v2.client.common.Response"); |
| 465 | + if (needsClassImport(query.returnType.toLowerCase())) { |
| 466 | + Tools.addImport(this.imports, this.javaGen.modelPackage + "." + query.returnType); |
474 | 467 | }
|
475 | 468 |
|
476 |
| - javaGen.generatedPathsEntries.append(Tools.formatComment(discAndPath, TAB, true)); |
477 |
| - javaGen.generatedPathsEntries.append(" public " + className + " " + methodName + "("); |
| 469 | + this.javaGen.generatedPathsEntries.append(Tools.formatComment(this.discAndPath, this.TAB, true)); |
| 470 | + this.javaGen.generatedPathsEntries.append(" public " + this.className + " " + query.name + "("); |
478 | 471 | }
|
479 | 472 |
|
480 | 473 | public void addQueryProperty(TypeDef propType, boolean inQuery, boolean inPath, boolean inBody) {
|
@@ -568,7 +561,7 @@ public void addQueryProperty(TypeDef propType, boolean inQuery, boolean inPath,
|
568 | 561 | }
|
569 | 562 | }
|
570 | 563 |
|
571 |
| - public void finalize() { |
| 564 | + public void finish() { |
572 | 565 |
|
573 | 566 | javaGen.generatedPathsImports.append("import " + javaGen.queryPackage + "." + className + ";\n");
|
574 | 567 |
|
@@ -621,7 +614,7 @@ public void finalize() {
|
621 | 614 | sb.append(Tools.formatComment(discAndPath, "", true));
|
622 | 615 | sb.append("public class " + className + " extends Query {\n\n");
|
623 | 616 | sb.append(queryParamsCode);
|
624 |
| - sb.append("\n}"); |
| 617 | + sb.append("\n}\n"); |
625 | 618 |
|
626 | 619 | BufferedWriter bw = JavaGenerator.newFile(className, javaGen.queryFilesDirectory);
|
627 | 620 | JavaGenerator.append(bw, "package " + javaGen.queryPackage + ";\n\n");
|
@@ -651,12 +644,31 @@ static ArrayList<String> getPathInserts(String path) {
|
651 | 644 |
|
652 | 645 | static String getQueryResponseMethod(String returnType) {
|
653 | 646 | String ret =
|
| 647 | + " /**\n" + |
| 648 | + " * Execute the query.\n" + |
| 649 | + " * @return the query response object.\n" + |
| 650 | + " * @throws Exception\n" + |
| 651 | + " */\n" + |
| 652 | + " @Override\n" + |
| 653 | + " public Response<" + returnType + "> execute() throws Exception {\n" + |
| 654 | + " Response<" + returnType + "> resp = baseExecute();\n" + |
| 655 | + " resp.setValueType(" + returnType + ".class);\n" + |
| 656 | + " return resp;\n" + |
| 657 | + " }\n\n" + |
| 658 | + " /**\n" + |
| 659 | + " * Execute the query with custom headers, there must be an equal number of keys and values\n" + |
| 660 | + " * or else an error will be generated.\n" + |
| 661 | + " * @param headers an array of header keys\n" + |
| 662 | + " * @param values an array of header values\n" + |
| 663 | + " * @return the query response object.\n" + |
| 664 | + " * @throws Exception\n" + |
| 665 | + " */\n" + |
654 | 666 | " @Override\n" +
|
655 |
| - " public Response<" + returnType + "> execute() throws Exception {\n" + |
656 |
| - " Response<" + returnType + "> resp = baseExecute();\n" + |
657 |
| - " resp.setValueType(" + returnType + ".class);\n" + |
658 |
| - " return resp;\n" + |
659 |
| - " }\n\n"; |
| 667 | + " public Response<" + returnType + "> execute(String[] headers, String[] values) throws Exception {\n" + |
| 668 | + " Response<" + returnType + "> resp = baseExecute(headers, values);\n" + |
| 669 | + " resp.setValueType(" + returnType + ".class);\n" + |
| 670 | + " return resp;\n" + |
| 671 | + " }\n\n"; |
660 | 672 | return ret;
|
661 | 673 | }
|
662 | 674 |
|
@@ -754,7 +766,7 @@ public void close () {
|
754 | 766 |
|
755 | 767 | if (!modelPropertyAdded) {
|
756 | 768 | // No file should be generated
|
757 |
| - // There are some alias types in one spec file, which |
| 769 | + // There are some alias types in one spec file, which |
758 | 770 | // are in contradiction with real types in the other spec file.
|
759 | 771 | // We don't want the alias type file to corrupt the real type object.
|
760 | 772 | return;
|
|
0 commit comments