Skip to content

Commit

Permalink
Improve parsing logic
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Farr <[email protected]>
  • Loading branch information
Xtansia committed May 23, 2024
1 parent ec009ce commit 3d5f9c8
Show file tree
Hide file tree
Showing 49 changed files with 1,447 additions and 786 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ gradle-app.setting

.ci/output
java-client/bin
samples/bin
samples/bin

.DS_Store
5 changes: 4 additions & 1 deletion java-codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ application {

tasks.named<JavaExec>("run") {
args = listOf(
"https://raw.githubusercontent.com/opensearch-project/opensearch-api-specification/feature/native_openapi/spec/OpenSearch.openapi.yaml",
"https://github.com/opensearch-project/opensearch-api-specification/releases/download/main/opensearch-openapi.yaml",
"$rootDir/buildSrc/formatterConfig.xml",
"${project(":java-client").projectDir}/src/generated/java/"
)
Expand Down Expand Up @@ -139,6 +139,9 @@ dependencies {
// https://search.maven.org/artifact/com.google.code.findbugs/jsr305
implementation("com.google.code.findbugs:jsr305:3.0.2")

// Apache 2.0
compileOnly("org.jetbrains:annotations:24.1.0")

// Apache 2.0
implementation("org.apache.maven.resolver:maven-resolver-api:1.9.18")
implementation("org.apache.maven.resolver:maven-resolver-supplier:1.9.18")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.diffplug.spotless.java.ImportOrderStep;
import com.diffplug.spotless.java.RemoveUnusedImportsStep;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import org.opensearch.client.codegen.exceptions.ApiSpecificationParseException;
import org.opensearch.client.codegen.exceptions.RenderException;
import org.opensearch.client.codegen.model.Namespace;
import org.opensearch.client.codegen.model.OperationGroup;
import org.opensearch.client.codegen.model.SpecTransformer;
import org.opensearch.client.codegen.openapi.OpenApiSpec;
import org.opensearch.client.codegen.openapi.OpenApiSpecification;

public class Main {
private static final OperationGroup.Matcher OPERATION_MATCHER = OperationGroup.matcher();
Expand Down Expand Up @@ -59,13 +56,16 @@ public static void main(String[] args) {
}

private static Namespace parseSpec(URI location) throws ApiSpecificationParseException {
var spec = OpenApiSpec.parse(location);
var spec = OpenApiSpecification.retrieve(location);
var transformer = new SpecTransformer(OPERATION_MATCHER);
transformer.visit(spec);
return transformer.getRoot();
}

private static void cleanDirectory(File dir) throws RenderException {
if (!dir.exists()) {
return;
}
try (Stream<Path> walker = Files.walk(dir.toPath())) {
walker.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.HashSet;
import java.util.Set;
import javax.annotation.Nonnull;
import org.opensearch.client.codegen.utils.Strings;

public class NameSanitizer {
Expand All @@ -21,7 +22,8 @@ public class NameSanitizer {
}
};

public static String wireNameToField(String wireName) {
@Nonnull
public static String wireNameToField(@Nonnull String wireName) {
var name = Strings.toCamelCase(wireName);
if (reservedWords.contains(name)) {
name += "_";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
package org.opensearch.client.codegen.model;

import java.util.Optional;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class Deprecation {
@Nullable
private final String description;
@Nullable
private final String version;

public Deprecation(String description, String version) {
public Deprecation(@Nullable String description, @Nullable String version) {
this.description = description;
this.version = version;
}

@Nonnull
public Optional<String> getDescription() {
return Optional.ofNullable(description);
}

@Nonnull
public Optional<String> getVersion() {
return Optional.ofNullable(version);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,39 @@

package org.opensearch.client.codegen.model;

import java.util.Objects;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.opensearch.client.codegen.NameSanitizer;
import org.opensearch.client.codegen.utils.Strings;

public class Field {
@Nonnull
private final String wireName;
@Nonnull
private final Type type;
private boolean required;
@Nullable
private final String description;

public Field(String wireName, Type type, boolean required, String description) {
this.wireName = wireName;
this.type = type;
public Field(@Nonnull String wireName, @Nonnull Type type, boolean required, @Nullable String description) {
this.wireName = Strings.requireNonBlank(wireName, "wireName must not be null");
this.type = Objects.requireNonNull(type, "type must not be null");
this.required = required;
this.description = description;
}

@Nonnull
public String getWireName() {
return wireName;
}

@Nonnull
public String getName() {
return NameSanitizer.wireNameToField(wireName);
}

@Nonnull
public Type getType() {
return required ? type : type.getBoxed();
}
Expand All @@ -43,6 +53,7 @@ public void setRequired(boolean required) {
this.required = required;
}

@Nullable
public String getDescription() {
return description;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.opensearch.client.codegen.openapi.OpenApiOperation;
import org.opensearch.client.codegen.utils.Either;

public class HttpPath {
@Nonnull
private final List<Part> parts;
@Nullable
private final Deprecation deprecation;
@Nullable
private final String versionAdded;

public static HttpPath from(String httpPath, OpenApiOperation operation, Map<String, Field> pathParams) {
Expand All @@ -44,11 +50,11 @@ public static HttpPath from(String httpPath, OpenApiOperation operation, Map<Str
parts.add(Part.from(isParameter, text.toString(), pathParams));
}

return new HttpPath(parts, operation.getDeprecation(), operation.getXVersionAdded().orElse(null));
return new HttpPath(parts, operation.getDeprecation().orElse(null), operation.getVersionAdded().orElse(null));
}

private HttpPath(List<Part> parts, Deprecation deprecation, String versionAdded) {
this.parts = parts;
private HttpPath(@Nonnull List<Part> parts, @Nullable Deprecation deprecation, @Nullable String versionAdded) {
this.parts = Objects.requireNonNull(parts, "parts must not be null");
this.deprecation = deprecation;
this.versionAdded = versionAdded;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.opensearch.client.codegen.JavaFormatter;
import org.opensearch.client.codegen.exceptions.RenderException;
import org.opensearch.client.codegen.utils.Strings;
Expand Down Expand Up @@ -51,7 +53,8 @@ private String getPackageNamePart() {
return name;
}

public Namespace child(String name) {
@Nonnull
public Namespace child(@Nullable String name) {
if (name == null || name.isEmpty()) {
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,43 @@

import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.opensearch.client.codegen.utils.Strings;

public class OperationGroup {
@Nullable
private final String namespace;
@Nonnull
private final String name;

public static OperationGroup from(String operationGroup) {
@Nonnull
public static OperationGroup from(@Nonnull String operationGroup) {
Strings.requireNonBlank(operationGroup, "operationGroup must not be blank");
int index = operationGroup.lastIndexOf('.');
if (index == -1) {
return new OperationGroup(null, operationGroup);
}
return new OperationGroup(operationGroup.substring(0, index), operationGroup.substring(index + 1));
}

private OperationGroup(String namespace, String name) {
private OperationGroup(@Nullable String namespace, @Nonnull String name) {
this.namespace = namespace;
this.name = name;
this.name = Strings.requireNonBlank(name, "name must not be blank");
}

public String getNamespace() {
return namespace;
@Nonnull
public Optional<String> getNamespace() {
return Optional.ofNullable(namespace);
}

@Nonnull
public String getName() {
return name;
}
Expand All @@ -53,6 +64,7 @@ public int hashCode() {
return new HashCodeBuilder(17, 37).append(namespace).append(name).toHashCode();
}

@Nonnull
public static Matcher matcher() {
return new Matcher();
}
Expand All @@ -62,12 +74,12 @@ public static class Matcher {
private final Set<OperationGroup> operations = new HashSet<>();
private final Collection<Pattern> patterns = new HashSet<>();

private Matcher() {
}
private Matcher() {}

public Matcher add(String namespace, String... operations) {
@Nonnull
public Matcher add(@Nullable String namespace, @Nullable String... operations) {
if (operations == null || operations.length == 0) {
namespaces.add(namespace);
namespaces.add(Strings.requireNonBlank(namespace, "namespace must not be blank"));
} else {
for (String operation : operations) {
add(new OperationGroup(namespace, operation));
Expand All @@ -76,18 +88,21 @@ public Matcher add(String namespace, String... operations) {
return this;
}

public Matcher add(OperationGroup operation) {
operations.add(operation);
@Nonnull
public Matcher add(@Nonnull OperationGroup operation) {
operations.add(Objects.requireNonNull(operation, "operation must not be null"));
return this;
}

public Matcher add(Pattern pattern) {
patterns.add(pattern);
@Nonnull
public Matcher add(@Nonnull Pattern pattern) {
patterns.add(Objects.requireNonNull(pattern, "pattern must not be null"));
return this;
}

public boolean matches(OperationGroup operation) {
if (namespaces.contains(operation.getNamespace())) {
public boolean matches(@Nonnull OperationGroup operation) {
Objects.requireNonNull(operation, "operation must not be null");
if (operation.getNamespace().map(namespaces::contains).orElse(false)) {
return true;
}
if (operations.contains(operation)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,38 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.lang3.tuple.Pair;
import org.opensearch.client.codegen.utils.Streams;
import org.opensearch.client.codegen.utils.Strings;

public class RequestShape extends ObjectShape {
@Nonnull
private final OperationGroup operationGroup;
@Nullable
private final String description;
@Nonnull
private final Set<String> httpMethods = new HashSet<>();
@Nonnull
private final List<HttpPath> httpPaths = new ArrayList<>();
@Nonnull
private final Map<String, Field> queryParams = new TreeMap<>();
@Nonnull
private final Map<String, Field> pathParams = new TreeMap<>();
@Nonnull
private final Map<String, Field> fields = new TreeMap<>();

public RequestShape(Namespace parent, OperationGroup operationGroup, String description) {
public RequestShape(@Nonnull Namespace parent, @Nonnull OperationGroup operationGroup, @Nullable String description) {
super(parent, requestClassName(operationGroup), operationGroup + ".Request");
this.operationGroup = operationGroup;
this.description = description;
}

@Nonnull
public OperationGroup getOperationGroup() {
return operationGroup;
}
Expand All @@ -42,6 +53,7 @@ public String getId() {
return operationGroup.getName();
}

@Nullable
public String getDescription() {
return description;
}
Expand Down Expand Up @@ -136,11 +148,15 @@ public boolean hasAnyRequiredFields() {
return fields.values().stream().anyMatch(Field::isRequired);
}

public static String requestClassName(OperationGroup operationGroup) {
@Nonnull
public static String requestClassName(@Nonnull OperationGroup operationGroup) {
Objects.requireNonNull(operationGroup, "operationGroup must not be null");
return Strings.toPascalCase(operationGroup.getName()) + "Request";
}

public static String responseClassName(OperationGroup operationGroup) {
@Nonnull
public static String responseClassName(@Nonnull OperationGroup operationGroup) {
Objects.requireNonNull(operationGroup, "operationGroup must not be null");
return Strings.toPascalCase(operationGroup.getName()) + "Response";
}
}
Loading

0 comments on commit 3d5f9c8

Please sign in to comment.