diff options
Diffstat (limited to 'graph-inventory/fluent-builder-maven-plugin/src/main/java')
3 files changed, 79 insertions, 33 deletions
diff --git a/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/FluentGenerator.java b/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/FluentGenerator.java index f9c664389b..23a1a812df 100644 --- a/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/FluentGenerator.java +++ b/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/FluentGenerator.java @@ -8,6 +8,7 @@ import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.regex.Matcher; import java.util.stream.Collectors; import javax.lang.model.element.Modifier; import org.apache.commons.lang3.ArrayUtils; @@ -240,19 +241,17 @@ public class FluentGenerator { } protected TypeSpec createTypes() { - Pair<String, String> path = splitClasspath(this.nameClass); - ClassName nameType = ClassName.get(path.getLeft(), path.getRight()); - - List<FieldSpec> params = - doc.values().stream().filter(item -> item.getType().equals("singular")) - .sorted(Comparator - .comparing(item -> item.getName())) - .map(item -> FieldSpec - .builder(nameType, - CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_UNDERSCORE, item.getName()), - Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL) - .initializer("new $T($S)", nameType, item.getName()).build()) - .collect(Collectors.toList()); + List<FieldSpec> params = doc.values().stream().filter(item -> item.getType().equals("singular")) + .sorted(Comparator.comparing(item -> item.getName())).map(item -> { + ClassName nameType = + ClassName.get(CLASSPATH, CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, item.getName())) + .nestedClass("Info"); + FieldSpec field = FieldSpec + .builder(nameType, CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_UNDERSCORE, item.getName()), + Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL) + .initializer("new $T()", nameType).build(); + return field; + }).collect(Collectors.toList()); TypeSpec type = TypeSpec.classBuilder("Types").addModifiers(Modifier.PUBLIC, Modifier.FINAL, Modifier.STATIC) .addFields(params).build(); @@ -276,30 +275,71 @@ public class FluentGenerator { .build()); ClassName superInterface; + String name; if (oType.getType().equals("plural")) { - classFields.add(FieldSpec.builder(String.class, "name") - .addModifiers(Modifier.PRIVATE, Modifier.FINAL, Modifier.STATIC) - .initializer("$S", oType.getAdditionalName()).build()); - superInterface = ClassName.get(this.pluralBuilderClass, "Info"); + Pair<String, String> path = splitClasspath(this.pluralBuilderClass); + superInterface = ClassName.get(path.getLeft(), path.getRight()); + name = oType.getAdditionalName(); } else if (oType.getType().equals("singular")) { - classFields.add(FieldSpec.builder(String.class, "name") - .addModifiers(Modifier.PRIVATE, Modifier.FINAL, Modifier.STATIC).initializer("$S", oType.getName()) - .build()); - superInterface = ClassName.get(this.singularBuilderClass, "Info"); + Pair<String, String> path = splitClasspath(this.singularBuilderClass); + superInterface = ClassName.get(path.getLeft(), path.getRight()); + name = oType.getName(); } else { - superInterface = ClassName.get(this.topLevelBuilderClass, "Info"); + Pair<String, String> path = splitClasspath(this.topLevelBuilderClass); + superInterface = ClassName.get(path.getLeft(), path.getRight()); + name = oType.getName(); } + superInterface = superInterface.nestedClass("Info"); methods.add(MethodSpec.methodBuilder("getPaths").returns(ParameterizedTypeName.get(List.class, String.class)) .addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addStatement("return Info.paths").build()); methods.add(MethodSpec.methodBuilder("getPartialUri").returns(String.class).addModifiers(Modifier.PUBLIC) .addAnnotation(Override.class).addStatement("return Info.partialUri").build()); if (!oType.getType().equals("top level")) { + classFields.add(FieldSpec.builder(String.class, "name") + .addModifiers(Modifier.PRIVATE, Modifier.FINAL, Modifier.STATIC).initializer("$S", name).build()); + classFields.add(FieldSpec.builder(ClassName.get("", "UriParams"), "uriParams") + .addModifiers(Modifier.PRIVATE, Modifier.FINAL, Modifier.STATIC) + .initializer("new $T()", ClassName.get("", "UriParams")).build()); methods.add(MethodSpec.methodBuilder("getName").returns(String.class).addModifiers(Modifier.PUBLIC) .addAnnotation(Override.class).addStatement("return Info.name").build()); + + methods.add(MethodSpec.methodBuilder("getUriParams").returns(ClassName.get("", "UriParams")) + .addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addStatement("return Info.uriParams") + .build()); + } + TypeSpec.Builder returnTypeSpec = TypeSpec.classBuilder("Info").addModifiers(Modifier.PUBLIC, Modifier.STATIC) + .addSuperinterface(superInterface).addFields(classFields).addMethods(methods); + if (!oType.getType().equals("top level")) { + returnTypeSpec.addType(createUriParamsClass(superInterface, oType)); + } + return returnTypeSpec.build(); + + } + + protected TypeSpec createUriParamsClass(ClassName parent, ObjectType oType) { + + List<FieldSpec> classFields = new ArrayList<>(); + Matcher params = Patterns.urlTemplatePattern.matcher(oType.getPartialUri()); + + while (params.find()) { + String value; + String name; + + if (params.group(2) != null) { + name = params.group(2); + } else { + name = params.group(1); + } + value = params.group(1); + + name = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, name); + + classFields.add(FieldSpec.builder(String.class, name, Modifier.PUBLIC, Modifier.FINAL) + .initializer("$S", value).build()); } - return TypeSpec.classBuilder("Info").addModifiers(Modifier.PUBLIC, Modifier.STATIC) - .addSuperinterface(superInterface).addFields(classFields).addMethods(methods).build(); + return TypeSpec.classBuilder("UriParams").addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL) + .addFields(classFields).addSuperinterface(parent.nestedClass("UriParams")).build(); } protected String makeValidJavaVariable(String name) { diff --git a/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/Patterns.java b/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/Patterns.java new file mode 100644 index 0000000000..8be22e1dcc --- /dev/null +++ b/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/Patterns.java @@ -0,0 +1,11 @@ +package org.onap.graphinventory.generate; + +import java.util.regex.Pattern; + +public class Patterns { + + public static final Pattern pluralPattern = Pattern.compile(".*(?<partial>/(?<name>[^{]*$))"); + public static final Pattern singularPattern = Pattern.compile(".*(?<partial>/(?<name>[^/{}]*)/\\{.*$)"); + public static final Pattern topLevelPattern = Pattern.compile("^/([^/]+)/.*"); + public static final Pattern urlTemplatePattern = Pattern.compile("\\{([^}.]+(?:\\.([^}]+))?)\\}"); +} diff --git a/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/SwaggerConverter.java b/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/SwaggerConverter.java index b3ced65390..42305488bc 100644 --- a/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/SwaggerConverter.java +++ b/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/SwaggerConverter.java @@ -7,7 +7,6 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.regex.Matcher; -import java.util.regex.Pattern; import java.util.stream.Collectors; import org.apache.maven.plugin.logging.Log; import com.fasterxml.jackson.core.JsonProcessingException; @@ -33,10 +32,6 @@ public class SwaggerConverter { .filter(item -> !item.getKey().endsWith("/relationship-list/relationship")) .collect(Collectors.toMap(item -> item.getKey(), item -> item.getValue())); - Pattern pluralPattern = Pattern.compile(".*(?<partial>/(?<name>[^{]*$))"); - Pattern singularPattern = Pattern.compile(".*(?<partial>/(?<name>[^/{}]*)/\\{.*$)"); - Pattern topLevelPattern = Pattern.compile("^/([^/]+)/.*"); - Pattern urlTemplatePattern = Pattern.compile("\\{([^}]+)\\}"); Matcher pluralMatcher; Matcher singularMatcher; Matcher topLevelMatcher; @@ -44,9 +39,9 @@ public class SwaggerConverter { Map<String, ObjectType> output = new HashMap<>(); for (Map.Entry<String, Path> entry : paths.entrySet()) { - pluralMatcher = pluralPattern.matcher(entry.getKey()); - singularMatcher = singularPattern.matcher(entry.getKey()); - topLevelMatcher = topLevelPattern.matcher(entry.getKey()); + pluralMatcher = Patterns.pluralPattern.matcher(entry.getKey()); + singularMatcher = Patterns.singularPattern.matcher(entry.getKey()); + topLevelMatcher = Patterns.topLevelPattern.matcher(entry.getKey()); ObjectType item; if (pluralMatcher.matches()) { if (!output.containsKey(pluralMatcher.group("name"))) { @@ -158,7 +153,7 @@ public class SwaggerConverter { } if (!item.getValue().getFields().isEmpty()) { - Matcher templates = urlTemplatePattern.matcher(item.getValue().getPartialUri()); + Matcher templates = Patterns.urlTemplatePattern.matcher(item.getValue().getPartialUri()); List<String> localFields = new ArrayList<>(); while (templates.find()) { localFields.add(templates.group(1)); |