Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.polyapi</groupId>
<artifactId>parent-pom</artifactId>
<version>0.15.5-SNAPSHOT</version>
<version>0.15.6-SNAPSHOT</version>
<relativePath>../parent-pom</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion library/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.polyapi</groupId>
<artifactId>parent-pom</artifactId>
<version>0.15.5-SNAPSHOT</version>
<version>0.15.6-SNAPSHOT</version>
<relativePath>../parent-pom</relativePath>
</parent>
<artifactId>library</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion parent-pom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.polyapi</groupId>
<artifactId>parent-pom</artifactId>
<version>0.15.5-SNAPSHOT</version>
<version>0.15.6-SNAPSHOT</version>
<packaging>pom</packaging>
<name>PolyAPI Java parent POM</name>
<url>https://polyapi.io</url>
Expand Down
2 changes: 1 addition & 1 deletion polyapi-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.polyapi</groupId>
<artifactId>parent-pom</artifactId>
<version>0.15.5-SNAPSHOT</version>
<version>0.15.6-SNAPSHOT</version>
<relativePath>../parent-pom</relativePath>
</parent>
<artifactId>polyapi-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,25 @@ public class GenerateSourcesMojo extends PolyApiMojo {
@Parameter(property = "overwrite", defaultValue = "false")
private Boolean overwrite;

@Parameter(property = "context")
private String context;
@Parameter(property = "contexts")
private String contexts;

@Parameter(property = "functionIds")
private String functionIds;

private PolyGenerationService polyGenerationService;

@Override
public void execute(String host, Integer port) {
log.info("Initiating generation of Poly sources.");
this.polyGenerationService = new PolyGenerationServiceImpl(getHttpClient(), getJsonParser(), host, port, getTokenProvider().getToken());
List<String> contextFilters = Arrays.stream(Optional.ofNullable(context).map(contextCsv -> contextCsv.split(",")).orElse(new String[]{""})).toList();
List<String> contextFilters = Arrays.stream(Optional.ofNullable(contexts).map(contextCsv -> contextCsv.split(",")).orElse(new String[]{""})).toList();
log.debug("Context filters: \"{}\"", join("\", \"", contextFilters));
this.polyGenerationService.generate(contextFilters, overwrite);

List<String> functionIdFilters = Arrays.stream(Optional.ofNullable(functionIds).map(functionIdsCsv -> functionIdsCsv.split(",")).orElse(new String[]{""})).toList();
log.debug("Function ID filters: \"{}\"", join("\", \"", functionIdFilters));

this.polyGenerationService.generate(contextFilters, functionIdFilters, overwrite);
log.info("Poly generation complete.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.polyapi.plugin.model.specification.Specification;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.List;

import static java.lang.String.format;
Expand All @@ -32,7 +33,7 @@ public PolyFunction deploy(String type, PolyFunction polyFunction) {
@Override
public void delete(String context, String name) {
log.info("Deleting function '{}' on context '{}'.", name, context);
List<Specification> specifications = specificationService.list(List.of())
List<Specification> specifications = specificationService.list(List.of(context), Collections.emptyList())
.stream()
.filter(spec -> spec.getName().equalsIgnoreCase(name) && spec.getContext().equalsIgnoreCase(context))
.toList();
Expand All @@ -54,7 +55,7 @@ public void delete(String context, String name) {

@Override
public void delete(String id) {
specificationService.list(List.of()).stream()
specificationService.list(Collections.emptyList(), List.of(id)).stream()
.filter(specification -> specification.getId().equals(id))
.forEach(this::delete);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ public interface SpecificationService {
/**
* Retrieve all the JSON specifications in Poly.
*
* @param context The contexts that should be used to filter the specifications.
* @param contexts The contexts that should be used to filter the specifications.
* @param functionIdFilters A list of function/specification IDs to filter by. Can be empty.
* @return String A JSON containing the specifications.
*/
List<Specification> list(List<String> context);
List<Specification> list(List<String> contexts, List<String> functionIdFilters);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import java.util.function.Predicate;

import static com.fasterxml.jackson.databind.type.TypeFactory.defaultInstance;
Expand All @@ -27,36 +28,32 @@ public SpecificationServiceImpl(HttpClient client, JsonParser jsonParser, String
}

@Override
public List<Specification> list(List<String> contextFilters) {
public List<Specification> list(List<String> contextFilters, List<String> functionIdFilters) {
log.info("Retrieving JSON specifications from PolyAPI for this user.");
List<Specification> specifications = get("specs", defaultInstance().constructCollectionType(List.class, Specification.class));
log.debug("{} specifications retrieved without filter.", specifications.size());
if (log.isDebugEnabled()) {

// Build the query parameter string for server-side filtering
StringJoiner queryParams = new StringJoiner("&");
if (contextFilters != null && !contextFilters.isEmpty()) {
queryParams.add("contexts=" + String.join(",", contextFilters));
}
if (functionIdFilters != null && !functionIdFilters.isEmpty()) {
queryParams.add("ids=" + String.join(",", functionIdFilters));
}

String path = "specs";
if (queryParams.length() > 0) {
path += "?" + queryParams.toString();
log.info("Applying server-side filters: {}", queryParams.toString());
}

// Make the API call with the constructed path
List<Specification> specifications = get(path, defaultInstance().constructCollectionType(List.class, Specification.class));

if (log.isTraceEnabled()) {
log.trace("Retrieved specifications with the following IDs: [{}]", specifications.stream().map(Specification::getId).collect(joining(", ")));
}
log.debug("Validating for duplicate context/name pairs and filtering specification contexts.");
Map<String, Specification> filteredMap = new HashMap<>();
specifications.stream()
.filter(not(IgnoredSpecification.class::isInstance))
.filter(specification -> {
String context = specification.getContext().trim().toLowerCase();
return contextFilters.isEmpty() || contextFilters.stream()
.map(String::trim)
.map(String::toLowerCase)
.anyMatch(contextFilter -> contextFilter.equalsIgnoreCase(context) || contextFilter.isEmpty() || context.startsWith(format("%s.", contextFilter)));
})
.filter(not(specification -> specification instanceof ClientFunctionSpecification clientFunctionSpecification && !clientFunctionSpecification.getLanguage().equalsIgnoreCase("java")))
.forEach(specification -> {
String key = format("%s.%s", specification.getContext(), specification.getName()).toLowerCase();
if (filteredMap.containsKey(key)) {
log.warn("Skipping {} specification '{}' in context '{}' as it clashes with {} specification with the same name and context.", specification.getType(), specification.getName(), specification.getContext(), filteredMap.get(key).getType());
} else {
log.debug("Specification key '{}' not repeated (yet).", key);
filteredMap.put(key, specification);
}
});
List<Specification> result = filteredMap.values().stream().toList();
log.info("{} specifications retrieved.", result.size());
return result;

log.info("{} specifications retrieved after server-side filtering.", specifications.size());
return specifications;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

public interface PolyGenerationService {

void generate(List<String> contextFilters, boolean overwrite);
void generate(List<String> contextFilters, List<String> functionIdFilters, boolean overwrite);
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ public PolyGenerationServiceImpl(HttpClient httpClient, JsonParser jsonParser, S
}

@Override
public void generate(List<String> contextFilters, boolean overwrite) {
var specifications = specificationService.list(contextFilters);
public void generate(List<String> contextFilters, List<String> functionIdFilters, boolean overwrite) {
// The call to list now passes both filters for server-side processing.
log.info("Applying context filters on the API call: {}", contextFilters);
log.info("Applying function ID filters on the API call: {}", functionIdFilters);
var specifications = specificationService.list(contextFilters, functionIdFilters);

var contextModel = new HashMap<String, Object>();
contextModel.put("clientId", UUID.randomUUID().toString());
contextModel.put("host", host);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.polyapi</groupId>
<artifactId>polyapi-java</artifactId>
<version>0.15.5-SNAPSHOT</version>
<version>0.15.6-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>parent-pom</module>
Expand Down