Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2018 The original authors.
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.sundr.swagger.language;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.sundr.utils.Maps;
import io.sundr.utils.Strings;
import io.swagger.codegen.v3.CliOption;
import io.swagger.codegen.v3.CodegenModel;
import io.swagger.codegen.v3.CodegenOperation;
import io.swagger.codegen.v3.CodegenParameter;
import io.swagger.codegen.v3.CodegenProperty;
import io.swagger.codegen.v3.CodegenType;
import io.swagger.codegen.v3.SupportingFile;
import io.swagger.codegen.v3.generators.java.JavaClientCodegen;
import io.swagger.v3.oas.models.Operation;
public class JavaFluentCodegen extends JavaClientCodegen {
private static final Logger LOGGER = LoggerFactory.getLogger(JavaFluentCodegen.class);
public static final String GENERATE_BUILDERS = "generateBuilders";
public static final String EDITABLE_ENABLED = "editableEnabled";
public static final String VALIDATION_ENABLED = "validationEnabled";
public static final String LAZY_MAP_INIT_ENABLED = "lazyMapInitEnabled";
public static final String GENERATE_BUILDER_PACKAGE = "generateBuilderPackage";
public static final String BUILDER_PACKAGE = "builderPackage";
public static final String BUILDER_ARTIFACT = "builderArtifact";
public static final String BUILDER_GROUP_ID = "builderGroupId";
public static final String BUILDER_ARTIFACT_ID = "builderArtifactId";
public static final String BUILDER_VERSION = "builderVersion";
public static final String BUILDER_CLASSIFIER = "builderClassifier";
public static final String HAS_BUILDER_ARTIFACT = "hasBuilderArtifact";
public static final String HAS_BUILDER_CLASSIFIER = "hasBuilderClassifier";
public static final String PACKAGE_MAPPINGS = "package-mappings";
protected boolean generateBuilders = true;
protected boolean editableEnabled = true;
protected boolean validationEnabled = false;
protected boolean lazyMapInitEnabled = true;
protected boolean generateBuilderPackage = false;
protected String builderPackage = null;
protected String builderArtifact = null;
protected String builderGroupId = null;
protected String builderArtifactId = null;
protected String builderVersion = null;
protected String builderClassifier = null;
protected Map packageMappings = null;
public JavaFluentCodegen() {
super();
outputFolder = "generated-code" + File.separator + "java";
embeddedTemplateDir = templateDir = "handlebars/JavaFluent";
invokerPackage = "io.swagger.client";
artifactId = "swagger-java-client";
apiPackage = "io.sundr.client.api";
modelPackage = "io.sundr.client.model";
cliOptions.add(CliOption.newBoolean(GENERATE_BUILDERS, "Whether to generate builders for model objects."));
cliOptions.add(CliOption.newBoolean(EDITABLE_ENABLED, "Flag to specify if editable classes should to be generated."));
cliOptions.add(CliOption.newBoolean(VALIDATION_ENABLED,
"Flag to specify if validation code should be part of the generated builders."));
cliOptions.add(CliOption.newBoolean(LAZY_MAP_INIT_ENABLED, "Flag to specify if maps should be lazily initialized."));
cliOptions.add(CliOption.newBoolean(BUILDER_PACKAGE, "The package that contains the builder helper classes."));
cliOptions.add(CliOption.newBoolean(GENERATE_BUILDER_PACKAGE,
"Flag that specifies wether the builder package should be generated or not."));
cliOptions.add(CliOption.newBoolean(BUILDER_ARTIFACT,
"The maven artifact that contains the builder utility classes. The format is ::(:)"));
cliOptions.add(CliOption.newBoolean(PACKAGE_MAPPINGS, "Mappings for model/api package names."));
supportedLibraries.put("okhttp-jackson", "HTTP client: OkHttp 2.7.5. JSON processing: Jackson 2.8.9.");
}
@Override
public void processOpts() {
super.processOpts();
if (additionalProperties.containsKey(GENERATE_BUILDERS)) {
this.setGenerateBuilders(Boolean.valueOf(additionalProperties.get(GENERATE_BUILDERS).toString()));
}
additionalProperties.put(GENERATE_BUILDERS, generateBuilders);
if (additionalProperties.containsKey(EDITABLE_ENABLED)) {
this.setEditableEnabled(Boolean.valueOf(additionalProperties.get(EDITABLE_ENABLED).toString()));
}
additionalProperties.put(EDITABLE_ENABLED, editableEnabled);
if (additionalProperties.containsKey(VALIDATION_ENABLED)) {
this.setValidationEnabled(Boolean.valueOf(additionalProperties.get(VALIDATION_ENABLED).toString()));
}
additionalProperties.put(VALIDATION_ENABLED, validationEnabled);
if (additionalProperties.containsKey(LAZY_MAP_INIT_ENABLED)) {
this.setLazyMapInitEnabled(Boolean.valueOf(additionalProperties.get(LAZY_MAP_INIT_ENABLED).toString()));
}
additionalProperties.put(LAZY_MAP_INIT_ENABLED, lazyMapInitEnabled);
if (additionalProperties.containsKey(GENERATE_BUILDER_PACKAGE)) {
this.setGenerateBuilderPackage(Boolean.valueOf(additionalProperties.get(GENERATE_BUILDER_PACKAGE).toString()));
}
additionalProperties.put(GENERATE_BUILDER_PACKAGE, generateBuilderPackage);
if (additionalProperties.containsKey(BUILDER_PACKAGE)) {
this.setBuilderPackage(additionalProperties.get(BUILDER_PACKAGE).toString());
} else if (!additionalProperties.containsKey(BUILDER_ARTIFACT)) {
//When builder package does not exist, we need to generate one, relative to the model.
this.setGenerateBuilderPackage(true);
this.setBuilderPackage(modelPackage + ".builder");
}
additionalProperties.put(BUILDER_PACKAGE, builderPackage);
additionalProperties.put(GENERATE_BUILDER_PACKAGE, generateBuilderPackage);
if (additionalProperties.containsKey(BUILDER_ARTIFACT)) {
this.setBuilderArtifact(additionalProperties.get(BUILDER_ARTIFACT).toString());
this.setBuilderGroupId(ArtifactUtil.groupId(builderArtifact));
this.setBuilderArtifactId(ArtifactUtil.artifactId(builderArtifact));
this.setBuilderVersion(ArtifactUtil.version(builderArtifact));
ArtifactUtil.classifier(builderArtifact).ifPresent(c -> this.setBuilderClassifier(c));
}
additionalProperties.put(BUILDER_ARTIFACT, builderArtifact);
if (additionalProperties.containsKey(BUILDER_GROUP_ID)) {
this.setBuilderGroupId(additionalProperties.get(BUILDER_GROUP_ID).toString());
}
additionalProperties.put(BUILDER_GROUP_ID, builderGroupId);
if (additionalProperties.containsKey(BUILDER_ARTIFACT_ID)) {
this.setBuilderArtifactId(additionalProperties.get(BUILDER_ARTIFACT_ID).toString());
}
additionalProperties.put(BUILDER_ARTIFACT_ID, builderArtifactId);
if (additionalProperties.containsKey(BUILDER_VERSION)) {
this.setBuilderVersion(additionalProperties.get(BUILDER_VERSION).toString());
}
additionalProperties.put(BUILDER_VERSION, builderVersion);
if (additionalProperties.containsKey(BUILDER_CLASSIFIER)) {
this.setBuilderClassifier(additionalProperties.get(BUILDER_CLASSIFIER).toString());
}
additionalProperties.put(BUILDER_CLASSIFIER, builderClassifier);
additionalProperties.put(HAS_BUILDER_ARTIFACT, !Strings.isNullOrEmpty(builderGroupId)
&& !Strings.isNullOrEmpty(builderArtifactId) && !Strings.isNullOrEmpty(builderVersion));
additionalProperties.put(HAS_BUILDER_CLASSIFIER, !Strings.isNullOrEmpty(builderClassifier));
if (additionalProperties.containsKey(PACKAGE_MAPPINGS)) {
this.setPackageMappings(Maps.create(String.valueOf(additionalProperties.get(PACKAGE_MAPPINGS).toString())));
}
additionalProperties.put(PACKAGE_MAPPINGS, packageMappings);
final String invokerFolder = (sourceFolder + File.separator + invokerPackage).replace(".", File.separator);
if ("okhttp-jackson".equals(getLibrary())) {
supportingFiles.add(new SupportingFile("JSON.mustache", invokerFolder, "JSON.java"));
supportingFiles.add(new SupportingFile("ApiResponse.mustache", invokerFolder, "ApiResponse.java"));
supportingFiles.add(new SupportingFile("ApiCallback.mustache", invokerFolder, "ApiCallback.java"));
supportingFiles.add(new SupportingFile("ProgressRequestBody.mustache", invokerFolder, "ProgressRequestBody.java"));
supportingFiles.add(new SupportingFile("ProgressResponseBody.mustache", invokerFolder, "ProgressResponseBody.java"));
supportingFiles.add(new SupportingFile("GzipRequestInterceptor.mustache", invokerFolder, "GzipRequestInterceptor.java"));
additionalProperties.put("jackson", "true");
}
}
@Override
public CodegenType getTag() {
return CodegenType.CLIENT;
}
@Override
public String getName() {
return "java-fluent";
}
@Override
public String getHelp() {
return "Generates a Java fluent model.";
}
@Override
public String getDefaultTemplateDir() {
return "JavaFluent";
}
//
// Customizations
//
private final Map importMappings = new HashMap<>();
private final Map classMappings = new HashMap<>();
private final List