You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ni...@apache.org on 2019/06/21 07:09:53 UTC

[servicecomb-toolkit] 37/49: Support generating pojo consumer

This is an automated email from the ASF dual-hosted git repository.

ningjiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-toolkit.git

commit 1aa7e5d21af830d891532a4fdbaf2165dbbce91c
Author: kakulisen <18...@163.com>
AuthorDate: Mon Jun 3 11:26:31 2019 +0800

    Support generating pojo consumer
    
    Signed-off-by: kakulisen <18...@163.com>
---
 .../toolkit/codegen/ServiceCombCodegen.java        | 44 +++++++++++++++-------
 .../consumer/formParamsConsumer.mustache           |  2 +-
 .../ServiceComb/libraries/POJO/api.mustache        | 28 +++-----------
 .../POJO/{api.mustache => apiImpl.mustache}        |  2 +-
 .../libraries/POJO/apiInterface.mustache           | 17 ---------
 5 files changed, 37 insertions(+), 56 deletions(-)

diff --git a/code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/ServiceCombCodegen.java b/code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/ServiceCombCodegen.java
index d6b4fd2..ee1c004 100755
--- a/code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/ServiceCombCodegen.java
+++ b/code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/ServiceCombCodegen.java
@@ -18,6 +18,7 @@
 package org.apache.servicecomb.toolkit.codegen;
 
 import java.io.File;
+import java.util.List;
 import java.util.Map;
 
 import io.swagger.codegen.CliOption;
@@ -61,9 +62,11 @@ public class ServiceCombCodegen extends AbstractJavaCodegen implements CodegenCo
 
   private String apiConsumerTemplate = consumerTemplateFolder + "/apiConsumer.mustache";
 
+  private String apiConsumerTemplateForPojo = consumerTemplateFolder + "/pojo/apiConsumer.mustache";
+
   private String modelConsumerTemplate = consumerTemplateFolder + "/model.mustache";
 
-  private String pojoApiInterfaceTemplate = "apiInterface.mustache";
+  private String pojoApiImplTemplate = "apiImpl.mustache";
 
   private int modelSwitch = 1;
 
@@ -130,15 +133,20 @@ public class ServiceCombCodegen extends AbstractJavaCodegen implements CodegenCo
 
   @Override
   public String apiFilename(String templateName, String tag) {
-    if (apiConsumerTemplate.equals(templateName)) {
+    if (apiConsumerTemplate.equals(templateName) || apiConsumerTemplateForPojo.equals(templateName)) {
       String suffix = apiTemplateFiles().get(templateName);
       return apiConsumerFolder() + File.separator + toApiFilename(tag) + suffix;
     }
-    if (pojoApiInterfaceTemplate.equals(templateName)) {
-      String suffix = apiTemplateFiles().get(templateName);
-      String pojoApiInterfaceName = pojoApiInterfaceFolder() + File.separator + camelize(tag) + "Api" + suffix;
-      additionalProperties.put("pojoApiInterfaceName", camelize(tag) + "Api");
-      return pojoApiInterfaceName;
+
+    if (POJO_LIBRARY.equals(getLibrary())) {
+      if ("apiImpl.mustache".equals(templateName)) {
+        String suffix = apiTemplateFiles().get(templateName);
+        return apiFileFolder() + File.separator + additionalProperties.get("classnameImpl") + suffix;
+      }
+      if ("api.mustache".equals(templateName)) {
+        String suffix = apiTemplateFiles().get(templateName);
+        return pojoApiInterfaceFolder() + File.separator + camelize(tag) + "Api" + suffix;
+      }
     }
     return super.apiFilename(templateName, tag);
   }
@@ -152,6 +160,16 @@ public class ServiceCombCodegen extends AbstractJavaCodegen implements CodegenCo
   }
 
   @Override
+  public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
+
+    Map operations = (Map) objs.get("operations");
+    String classnameImpl = (String) operations.get("classname") + "Impl";
+    operations.put("classnameImpl", classnameImpl);
+    additionalProperties.put("classnameImpl", classnameImpl);
+    return super.postProcessOperationsWithModels(objs, allModels);
+  }
+
+  @Override
   public void processOpts() {
 
     super.processOpts();
@@ -176,7 +194,9 @@ public class ServiceCombCodegen extends AbstractJavaCodegen implements CodegenCo
     if (!POJO_LIBRARY.equals(getLibrary())) {
       return;
     }
-    apiTemplateFiles.put(pojoApiInterfaceTemplate, ".java");
+    apiTemplateFiles.put(pojoApiImplTemplate, ".java");
+    apiTemplateFiles.remove(apiConsumerTemplate);
+    apiTemplateFiles.put(apiConsumerTemplateForPojo, "Consumer.java");
     additionalProperties.put("isPOJO", true);
   }
 
@@ -265,7 +285,7 @@ public class ServiceCombCodegen extends AbstractJavaCodegen implements CodegenCo
   @Override
   public String toApiName(String name) {
     if (name.length() == 0) {
-      return "DefaultController";
+      return "DefaultApi";
     }
 
     String apiName = (String) additionalProperties.get("apiName");
@@ -273,11 +293,7 @@ public class ServiceCombCodegen extends AbstractJavaCodegen implements CodegenCo
       return apiName;
     }
 
-    if (POJO_LIBRARY.equals(getLibrary())) {
-      return initialCaps(name) + "ApiImpl";
-    }
-
-    return initialCaps(name) + "Controller";
+    return initialCaps(name) + "Api";
   }
 
   private String mainClassFolder(String projectPath) {
diff --git a/code-generator/src/main/resources/ServiceComb/consumer/formParamsConsumer.mustache b/code-generator/src/main/resources/ServiceComb/consumer/formParamsConsumer.mustache
index 5e9bb31..3aad37d 100755
--- a/code-generator/src/main/resources/ServiceComb/consumer/formParamsConsumer.mustache
+++ b/code-generator/src/main/resources/ServiceComb/consumer/formParamsConsumer.mustache
@@ -1 +1 @@
-{{#isFormParam}}{{#notFile}}{{{dataType}}} {{paramName}}{{/notFile}}{{#isFile}} MultipartFile {{baseName}}{{/isFile}}{{/isFormParam}}
\ No newline at end of file
+{{#isFormParam}}{{#notFile}}{{{dataType}}} {{paramName}}{{/notFile}}{{#isFile}} {{#isPOJO}}File{{/isPOJO}}{{^isPOJO}}MultipartFile{{/isPOJO}} {{baseName}}{{/isFile}}{{/isFormParam}}
\ No newline at end of file
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/POJO/api.mustache b/code-generator/src/main/resources/ServiceComb/libraries/POJO/api.mustache
index ea99a96..fad66ca 100755
--- a/code-generator/src/main/resources/ServiceComb/libraries/POJO/api.mustache
+++ b/code-generator/src/main/resources/ServiceComb/libraries/POJO/api.mustache
@@ -1,35 +1,17 @@
 package {{apiPackage}};
 
-import static org.springframework.http.MediaType.*;
 import java.util.List;
 import java.util.Map;
+import java.util.HashMap;
 import java.io.File;
 {{#imports}}import {{import}};
 {{/imports}}
 import {{modelPackage}}.*;
-import org.apache.servicecomb.provider.pojo.RpcSchema;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestHeader;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RequestPart;
-import org.springframework.web.multipart.MultipartFile;
 
-@RpcSchema(schemaId = "{{#camelcase}}{{classname}}{{/camelcase}}")
 {{#operations}}
-public class {{classname}} implements {{#removeImplSuffix}}{{classname}}{{/removeImplSuffix}} {
-  {{#operation}}
-
-  @Override
-  public {{>returnTypes}} {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {
-    // do something
-
-    return null;
-  }
-  {{/operation}}
+public interface {{classname}} {
+    {{#operation}}
+     public {{>returnTypes}} {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
+    {{/operation}}
 }
 {{/operations}}
\ No newline at end of file
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/POJO/api.mustache b/code-generator/src/main/resources/ServiceComb/libraries/POJO/apiImpl.mustache
similarity index 91%
copy from code-generator/src/main/resources/ServiceComb/libraries/POJO/api.mustache
copy to code-generator/src/main/resources/ServiceComb/libraries/POJO/apiImpl.mustache
index ea99a96..7e15375 100755
--- a/code-generator/src/main/resources/ServiceComb/libraries/POJO/api.mustache
+++ b/code-generator/src/main/resources/ServiceComb/libraries/POJO/apiImpl.mustache
@@ -21,7 +21,7 @@ import org.springframework.web.multipart.MultipartFile;
 
 @RpcSchema(schemaId = "{{#camelcase}}{{classname}}{{/camelcase}}")
 {{#operations}}
-public class {{classname}} implements {{#removeImplSuffix}}{{classname}}{{/removeImplSuffix}} {
+public class {{classnameImpl}} implements {{classname}} {
   {{#operation}}
 
   @Override
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/POJO/apiInterface.mustache b/code-generator/src/main/resources/ServiceComb/libraries/POJO/apiInterface.mustache
deleted file mode 100755
index 84cc2c8..0000000
--- a/code-generator/src/main/resources/ServiceComb/libraries/POJO/apiInterface.mustache
+++ /dev/null
@@ -1,17 +0,0 @@
-package {{apiPackage}};
-
-import java.util.List;
-import java.util.Map;
-import java.util.HashMap;
-import java.io.File;
-{{#imports}}import {{import}};
-{{/imports}}
-import {{modelPackage}}.*;
-
-{{#operations}}
-public interface {{#removeImplSuffix}}{{classname}}{{/removeImplSuffix}} {
-    {{#operation}}
-     public {{>returnTypes}} {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
-    {{/operation}}
-}
-{{/operations}}
\ No newline at end of file