Skip to content

Commit a1c57eb

Browse files
authored
Merge pull request #69 from sasajo/main
Rename the 'context' option in Java to be 'contexts'.
2 parents 722c5d8 + 64fed62 commit a1c57eb

File tree

11 files changed

+55
-44
lines changed

11 files changed

+55
-44
lines changed

commons/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>io.polyapi</groupId>
66
<artifactId>parent-pom</artifactId>
7-
<version>0.15.5-SNAPSHOT</version>
7+
<version>0.15.6-SNAPSHOT</version>
88
<relativePath>../parent-pom</relativePath>
99
</parent>
1010

library/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>io.polyapi</groupId>
66
<artifactId>parent-pom</artifactId>
7-
<version>0.15.5-SNAPSHOT</version>
7+
<version>0.15.6-SNAPSHOT</version>
88
<relativePath>../parent-pom</relativePath>
99
</parent>
1010
<artifactId>library</artifactId>

parent-pom/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>io.polyapi</groupId>
55
<artifactId>parent-pom</artifactId>
6-
<version>0.15.5-SNAPSHOT</version>
6+
<version>0.15.6-SNAPSHOT</version>
77
<packaging>pom</packaging>
88
<name>PolyAPI Java parent POM</name>
99
<url>https://polyapi.io</url>

polyapi-maven-plugin/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>io.polyapi</groupId>
66
<artifactId>parent-pom</artifactId>
7-
<version>0.15.5-SNAPSHOT</version>
7+
<version>0.15.6-SNAPSHOT</version>
88
<relativePath>../parent-pom</relativePath>
99
</parent>
1010
<artifactId>polyapi-maven-plugin</artifactId>

polyapi-maven-plugin/src/main/java/io/polyapi/plugin/mojo/GenerateSourcesMojo.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,25 @@ public class GenerateSourcesMojo extends PolyApiMojo {
2222
@Parameter(property = "overwrite", defaultValue = "false")
2323
private Boolean overwrite;
2424

25-
@Parameter(property = "context")
26-
private String context;
25+
@Parameter(property = "contexts")
26+
private String contexts;
27+
28+
@Parameter(property = "functionIds")
29+
private String functionIds;
30+
2731
private PolyGenerationService polyGenerationService;
2832

2933
@Override
3034
public void execute(String host, Integer port) {
3135
log.info("Initiating generation of Poly sources.");
3236
this.polyGenerationService = new PolyGenerationServiceImpl(getHttpClient(), getJsonParser(), host, port, getTokenProvider().getToken());
33-
List<String> contextFilters = Arrays.stream(Optional.ofNullable(context).map(contextCsv -> contextCsv.split(",")).orElse(new String[]{""})).toList();
37+
List<String> contextFilters = Arrays.stream(Optional.ofNullable(contexts).map(contextCsv -> contextCsv.split(",")).orElse(new String[]{""})).toList();
3438
log.debug("Context filters: \"{}\"", join("\", \"", contextFilters));
35-
this.polyGenerationService.generate(contextFilters, overwrite);
39+
40+
List<String> functionIdFilters = Arrays.stream(Optional.ofNullable(functionIds).map(functionIdsCsv -> functionIdsCsv.split(",")).orElse(new String[]{""})).toList();
41+
log.debug("Function ID filters: \"{}\"", join("\", \"", functionIdFilters));
42+
43+
this.polyGenerationService.generate(contextFilters, functionIdFilters, overwrite);
3644
log.info("Poly generation complete.");
3745
}
3846
}

polyapi-maven-plugin/src/main/java/io/polyapi/plugin/service/PolyFunctionServiceImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.polyapi.plugin.model.specification.Specification;
99
import lombok.extern.slf4j.Slf4j;
1010

11+
import java.util.Collections;
1112
import java.util.List;
1213

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

5556
@Override
5657
public void delete(String id) {
57-
specificationService.list(List.of()).stream()
58+
specificationService.list(Collections.emptyList(), List.of(id)).stream()
5859
.filter(specification -> specification.getId().equals(id))
5960
.forEach(this::delete);
6061
}

polyapi-maven-plugin/src/main/java/io/polyapi/plugin/service/SpecificationService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ public interface SpecificationService {
1212
/**
1313
* Retrieve all the JSON specifications in Poly.
1414
*
15-
* @param context The contexts that should be used to filter the specifications.
15+
* @param contexts The contexts that should be used to filter the specifications.
16+
* @param functionIdFilters A list of function/specification IDs to filter by. Can be empty.
1617
* @return String A JSON containing the specifications.
1718
*/
18-
List<Specification> list(List<String> context);
19+
List<Specification> list(List<String> contexts, List<String> functionIdFilters);
1920
}

polyapi-maven-plugin/src/main/java/io/polyapi/plugin/service/SpecificationServiceImpl.java

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.HashMap;
1313
import java.util.List;
1414
import java.util.Map;
15+
import java.util.StringJoiner;
1516
import java.util.function.Predicate;
1617

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

2930
@Override
30-
public List<Specification> list(List<String> contextFilters) {
31+
public List<Specification> list(List<String> contextFilters, List<String> functionIdFilters) {
3132
log.info("Retrieving JSON specifications from PolyAPI for this user.");
32-
List<Specification> specifications = get("specs", defaultInstance().constructCollectionType(List.class, Specification.class));
33-
log.debug("{} specifications retrieved without filter.", specifications.size());
34-
if (log.isDebugEnabled()) {
33+
34+
// Build the query parameter string for server-side filtering
35+
StringJoiner queryParams = new StringJoiner("&");
36+
if (contextFilters != null && !contextFilters.isEmpty()) {
37+
queryParams.add("contexts=" + String.join(",", contextFilters));
38+
}
39+
if (functionIdFilters != null && !functionIdFilters.isEmpty()) {
40+
queryParams.add("ids=" + String.join(",", functionIdFilters));
41+
}
42+
43+
String path = "specs";
44+
if (queryParams.length() > 0) {
45+
path += "?" + queryParams.toString();
46+
log.info("Applying server-side filters: {}", queryParams.toString());
47+
}
48+
49+
// Make the API call with the constructed path
50+
List<Specification> specifications = get(path, defaultInstance().constructCollectionType(List.class, Specification.class));
51+
52+
if (log.isTraceEnabled()) {
3553
log.trace("Retrieved specifications with the following IDs: [{}]", specifications.stream().map(Specification::getId).collect(joining(", ")));
3654
}
37-
log.debug("Validating for duplicate context/name pairs and filtering specification contexts.");
38-
Map<String, Specification> filteredMap = new HashMap<>();
39-
specifications.stream()
40-
.filter(not(IgnoredSpecification.class::isInstance))
41-
.filter(specification -> {
42-
String context = specification.getContext().trim().toLowerCase();
43-
return contextFilters.isEmpty() || contextFilters.stream()
44-
.map(String::trim)
45-
.map(String::toLowerCase)
46-
.anyMatch(contextFilter -> contextFilter.equalsIgnoreCase(context) || contextFilter.isEmpty() || context.startsWith(format("%s.", contextFilter)));
47-
})
48-
.filter(not(specification -> specification instanceof ClientFunctionSpecification clientFunctionSpecification && !clientFunctionSpecification.getLanguage().equalsIgnoreCase("java")))
49-
.forEach(specification -> {
50-
String key = format("%s.%s", specification.getContext(), specification.getName()).toLowerCase();
51-
if (filteredMap.containsKey(key)) {
52-
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());
53-
} else {
54-
log.debug("Specification key '{}' not repeated (yet).", key);
55-
filteredMap.put(key, specification);
56-
}
57-
});
58-
List<Specification> result = filteredMap.values().stream().toList();
59-
log.info("{} specifications retrieved.", result.size());
60-
return result;
55+
56+
log.info("{} specifications retrieved after server-side filtering.", specifications.size());
57+
return specifications;
6158
}
6259
}

polyapi-maven-plugin/src/main/java/io/polyapi/plugin/service/generation/PolyGenerationService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
public interface PolyGenerationService {
66

7-
void generate(List<String> contextFilters, boolean overwrite);
7+
void generate(List<String> contextFilters, List<String> functionIdFilters, boolean overwrite);
88
}

polyapi-maven-plugin/src/main/java/io/polyapi/plugin/service/generation/PolyGenerationServiceImpl.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,12 @@ public PolyGenerationServiceImpl(HttpClient httpClient, JsonParser jsonParser, S
5050
}
5151

5252
@Override
53-
public void generate(List<String> contextFilters, boolean overwrite) {
54-
var specifications = specificationService.list(contextFilters);
53+
public void generate(List<String> contextFilters, List<String> functionIdFilters, boolean overwrite) {
54+
// The call to list now passes both filters for server-side processing.
55+
log.info("Applying context filters on the API call: {}", contextFilters);
56+
log.info("Applying function ID filters on the API call: {}", functionIdFilters);
57+
var specifications = specificationService.list(contextFilters, functionIdFilters);
58+
5559
var contextModel = new HashMap<String, Object>();
5660
contextModel.put("clientId", UUID.randomUUID().toString());
5761
contextModel.put("host", host);

0 commit comments

Comments
 (0)