Skip to content

Commit e88805f

Browse files
author
Guy Davenport
committed
updated String Utils, metadata and tests
1 parent fe47c1a commit e88805f

File tree

13 files changed

+210
-41
lines changed

13 files changed

+210
-41
lines changed

java/buildSrc/src/main/groovy/brapi.schema-tools.java-conventions.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
}
44

55
group = 'org.brapi'
6-
version = '0.12.0-SNAPSHOT'
6+
version = '0.13.0-SNAPSHOT'
77
sourceCompatibility = JavaVersion.VERSION_21
88
targetCompatibility = JavaVersion.VERSION_21
99

java/cli/src/main/java/org/brapi/schematools/cli/GenerateSubCommand.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.brapi.schematools.core.ontmodel.metadata.OntModelGeneratorMetadata;
1818
import org.brapi.schematools.core.ontmodel.options.OntModelGeneratorOptions;
1919
import org.brapi.schematools.core.openapi.OpenAPIGenerator;
20+
import org.brapi.schematools.core.openapi.metadata.OpenAPIGeneratorMetadata;
2021
import org.brapi.schematools.core.openapi.options.OpenAPIGeneratorOptions;
2122
import org.brapi.schematools.core.response.Response;
2223
import org.brapi.schematools.core.xlsx.XSSFWorkbookGenerator;
@@ -79,13 +80,16 @@ public void run() {
7980
case OPEN_API -> {
8081
OpenAPIGeneratorOptions options = optionsPath != null ?
8182
OpenAPIGeneratorOptions.load(optionsPath) : OpenAPIGeneratorOptions.load() ;
82-
generateOpenAPISpecification(options);
83+
OpenAPIGeneratorMetadata metadata = metadataPath != null ?
84+
OpenAPIGeneratorMetadata.load(metadataPath) : OpenAPIGeneratorMetadata.load() ;
85+
generateOpenAPISpecification(options, metadata);
8386
}
8487
case GRAPHQL -> {
8588
GraphQLGeneratorOptions options = optionsPath != null ?
8689
GraphQLGeneratorOptions.load(optionsPath) : GraphQLGeneratorOptions.load();
87-
GraphQLGeneratorMetadata metadata ;
88-
generateGraphQLSchema(options);
90+
GraphQLGeneratorMetadata metadata = metadataPath != null ?
91+
GraphQLGeneratorMetadata.load(metadataPath) : GraphQLGeneratorMetadata.load() ;
92+
generateGraphQLSchema(options, metadata);
8993
}
9094
case OWL -> {
9195
OntModelGeneratorOptions options = optionsPath != null ?
@@ -119,10 +123,10 @@ public void run() {
119123
}
120124
}
121125

122-
private void generateGraphQLSchema(GraphQLGeneratorOptions options) {
126+
private void generateGraphQLSchema(GraphQLGeneratorOptions options, GraphQLGeneratorMetadata metadata) {
123127
GraphQLGenerator graphQLGenerator = new GraphQLGenerator(options);
124128

125-
Response<GraphQLSchema> response = graphQLGenerator.generate(schemaDirectory);
129+
Response<GraphQLSchema> response = graphQLGenerator.generate(schemaDirectory, metadata);
126130

127131
response.onSuccessDoWithResult(this::outputIDLSchema).onFailDoWithResponse(this::printGraphQLSchemaErrors);
128132
}
@@ -169,10 +173,10 @@ private void printGraphQLSchemaErrors(Response<GraphQLSchema> response) {
169173
}
170174
}
171175

172-
private void generateOpenAPISpecification(OpenAPIGeneratorOptions options) {
176+
private void generateOpenAPISpecification(OpenAPIGeneratorOptions options, OpenAPIGeneratorMetadata metadata) {
173177
OpenAPIGenerator openAPIGenerator = new OpenAPIGenerator(options);
174178

175-
Response<List<OpenAPI>> response = openAPIGenerator.generate(schemaDirectory, componentsDirectory);
179+
Response<List<OpenAPI>> response = openAPIGenerator.generate(schemaDirectory, componentsDirectory, metadata);
176180

177181
response.onSuccessDoWithResult(this::outputOpenAPISpecification).onFailDoWithResponse(this::printOpenAPISpecificationErrors);
178182
}

java/core/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies {
1515
implementation 'org.apache.poi:poi:5.2.5'
1616
implementation 'org.apache.poi:poi-ooxml:5.2.5'
1717
implementation 'commons-beanutils:commons-beanutils:1.9.4'
18+
implementation 'org.atteo:evo-inflector:1.3'
1819
}
1920

2021
publishing {

java/core/src/main/java/org/brapi/schematools/core/graphql/metadata/GraphQLGeneratorMetadata.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,52 @@
33
import lombok.Getter;
44
import lombok.Setter;
55
import org.brapi.schematools.core.metadata.Metadata;
6+
import org.brapi.schematools.core.utils.ConfigurationUtils;
7+
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.nio.file.Path;
611

712
/**
813
* Provides metadata for the GraphQL generation
914
*/
1015
@Getter
1116
@Setter
1217
public class GraphQLGeneratorMetadata implements Metadata {
18+
private String title ;
19+
private String version ;
20+
21+
/**
22+
* Load the metadata from a metadata file in YAML or Json. The metadata file may have missing
23+
* (defined) values, in these cases the default values are loaded. See {@link #load()}
24+
* @param metadataFile The path to the metadata file in YAML or Json.
25+
* @return The metadata loaded from the YAML or Json file.
26+
* @throws IOException if the metadata file can not be found or is incorrectly formatted.
27+
*/
28+
public static GraphQLGeneratorMetadata load(Path metadataFile) throws IOException {
29+
return ConfigurationUtils.load(metadataFile, GraphQLGeneratorMetadata.class) ;
30+
}
31+
32+
/**
33+
* Load the default metadata
34+
* @return The default metadata
35+
*/
36+
public static GraphQLGeneratorMetadata load() {
37+
try {
38+
return ConfigurationUtils.load("graphql-metadata.yaml", GraphQLGeneratorMetadata.class) ;
39+
} catch (IOException e) {
40+
throw new RuntimeException(e);
41+
}
42+
}
43+
44+
/**
45+
* Load the metadata from an metadata input stream in YAML or Json. The metadata file may have missing
46+
* (defined) values, in these cases the default values are loaded. See {@link #load()}
47+
* @param inputStream The input stream in YAML or Json.
48+
* @return The metadata loaded from input stream.
49+
* @throws IOException if the input stream is not valid or the content is incorrectly formatted.
50+
*/
51+
public static GraphQLGeneratorMetadata load(InputStream inputStream) throws IOException {
52+
return ConfigurationUtils.load(inputStream, GraphQLGeneratorMetadata.class) ;
53+
}
1354
}

java/core/src/main/java/org/brapi/schematools/core/openapi/OpenAPIGenerator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ private Response<OpenAPI> generate(String title, Collection<BrAPIClass> types) {
132132
Info info = new Info();
133133

134134
info.setTitle(title);
135-
info.setVersion(metadata.getVersion() != null ?metadata.getVersion() : "0.0.0");
135+
info.setTitle(metadata.getTitle() != null ? metadata.getTitle() : title);
136+
info.setVersion(metadata.getVersion() != null ? metadata.getVersion() : "0.0.0");
136137

137138
openAPI.setInfo(info);
138139

java/core/src/main/java/org/brapi/schematools/core/openapi/metadata/OpenAPIGeneratorMetadata.java

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,59 @@
33
import lombok.Getter;
44
import lombok.Setter;
55
import org.brapi.schematools.core.metadata.Metadata;
6+
import org.brapi.schematools.core.utils.ConfigurationUtils;
7+
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.nio.file.Path;
611

712
/**
813
* Provides metadata for the OpenAPI generation
914
*/
1015
@Getter
1116
@Setter
1217
public class OpenAPIGeneratorMetadata implements Metadata {
13-
String version ;
14-
SingleGetMetadata singleGet = new SingleGetMetadata() ;
15-
ListGetMetadata listGet = new ListGetMetadata() ;
16-
PostMetadata post = new PostMetadata() ;
17-
PutMetadata put = new PutMetadata() ;
18-
DeleteMetadata delete = new DeleteMetadata() ;
19-
SearchMetadata search = new SearchMetadata() ;
18+
private String title ;
19+
private String version ;
20+
21+
private SingleGetMetadata singleGet = new SingleGetMetadata() ;
22+
private ListGetMetadata listGet = new ListGetMetadata() ;
23+
private PostMetadata post = new PostMetadata() ;
24+
private PutMetadata put = new PutMetadata() ;
25+
private DeleteMetadata delete = new DeleteMetadata() ;
26+
private SearchMetadata search = new SearchMetadata() ;
27+
28+
/**
29+
* Load the metadata from a metadata file in YAML or Json. The metadata file may have missing
30+
* (defined) values, in these cases the default values are loaded. See {@link #load()}
31+
* @param metadataFile The path to the metadata file in YAML or Json.
32+
* @return The metadata loaded from the YAML or Json file.
33+
* @throws IOException if the metadata file can not be found or is incorrectly formatted.
34+
*/
35+
public static OpenAPIGeneratorMetadata load(Path metadataFile) throws IOException {
36+
return ConfigurationUtils.load(metadataFile, OpenAPIGeneratorMetadata.class) ;
37+
}
38+
39+
/**
40+
* Load the default metadata
41+
* @return The default metadata
42+
*/
43+
public static OpenAPIGeneratorMetadata load() {
44+
try {
45+
return ConfigurationUtils.load("openapi-metadata.yaml", OpenAPIGeneratorMetadata.class) ;
46+
} catch (IOException e) {
47+
throw new RuntimeException(e);
48+
}
49+
}
50+
51+
/**
52+
* Load the metadata from an metadata input stream in YAML or Json. The metadata file may have missing
53+
* (defined) values, in these cases the default values are loaded. See {@link #load()}
54+
* @param inputStream The input stream in YAML or Json.
55+
* @return The metadata loaded from input stream.
56+
* @throws IOException if the input stream is not valid or the content is incorrectly formatted.
57+
*/
58+
public static OpenAPIGeneratorMetadata load(InputStream inputStream) throws IOException {
59+
return ConfigurationUtils.load(inputStream, OpenAPIGeneratorMetadata.class) ;
60+
}
2061
}

java/core/src/main/java/org/brapi/schematools/core/utils/StringUtils.java

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import graphql.com.google.common.collect.ImmutableList;
44
import graphql.com.google.common.collect.ImmutableSet;
5+
import org.atteo.evo.inflector.English;
56

67
import java.util.List;
78
import java.util.Set;
@@ -16,12 +17,11 @@ public class StringUtils {
1617
public static String capitalise(String value) {
1718
return value.substring(0, 1).toUpperCase() + value.substring(1);
1819
}
19-
private static final Set<String> unpluralisables = ImmutableSet.of(
20-
"equipment", "information", "rice", "money", "species", "series",
21-
"fish", "sheep", "deer");
20+
private static final Set<String> unpluralisables = ImmutableSet.of("germplasm");
2221

2322
private static final List<Replacer> singularisations = ImmutableList.of(
2423
replace("(.*)people$").with("$1person"),
24+
replace("(.*)People$").with("$1Person"),
2525
replace("oxen$").with("ox"),
2626
replace("children$").with("child"),
2727
replace("feet$").with("foot"),
@@ -46,22 +46,10 @@ public static String capitalise(String value) {
4646
);
4747

4848
private static final List<Replacer> pluralisations = ImmutableList.of(
49+
replace("(.*)matrix$").with("$1matrices"),
50+
replace("(.*)Matrix$").with("$1Matrices"),
4951
replace("(.*)person$").with("$1people"),
50-
replace("ox$").with("oxen"),
51-
replace("child$").with("children"),
52-
replace("foot$").with("feet"),
53-
replace("tooth$").with("teeth"),
54-
replace("goose$").with("geese"),
55-
replace("(.*)fe?$").with("$1ves"),
56-
replace("(.*)man$").with("$1men"),
57-
replace("(.+[aeiou]y)$").with("$1s"),
58-
replace("(.+[^aeiou])y$").with("$1ies"),
59-
replace("(.+z)$").with("$1zes"),
60-
replace("([m|l])ouse$").with("$1ice"),
61-
replace("(.+)(e|i)x$").with("$1ices"),
62-
replace("(octop|vir)us$").with("$1i"),
63-
replace("(.+(s|x|sh|ch))$").with("$1es"),
64-
replace("(.+)").with("$1s")
52+
replace("(.*)Person$").with("$1People")
6553
);
6654

6755
/**
@@ -71,6 +59,7 @@ public static String capitalise(String value) {
7159
* @return singular form of a plural noun
7260
*/
7361
public static String toSingular(String value) {
62+
7463
if (unpluralisables.contains(value.toLowerCase())) {
7564
return value;
7665
}
@@ -81,7 +70,7 @@ public static String toSingular(String value) {
8170
}
8271
}
8372

84-
return value;
73+
return value ;
8574
}
8675

8776
/**
@@ -101,7 +90,7 @@ public static String toPlural(String value) {
10190
}
10291
}
10392

104-
return value;
93+
return English.plural(value);
10594
}
10695

10796
/**
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
title: BrAPI
2+
version: 0.0.0
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
title: BrAPI
2+
version: 0.0.0
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.brapi.schematools.core.graphql.metadata;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class GraphQLGeneratorMetadataTest {
8+
9+
void load() {
10+
GraphQLGeneratorMetadata metadata = GraphQLGeneratorMetadata.load();
11+
12+
checkMetadata(metadata);
13+
}
14+
15+
@Test
16+
void loadJson() {
17+
}
18+
19+
@Test
20+
void loadYaml() {
21+
}
22+
23+
private void checkMetadata(GraphQLGeneratorMetadata metadata) {
24+
assertNotNull(metadata);
25+
26+
assertEquals("BrAPI", metadata.getTitle());
27+
assertEquals("0.0.0", metadata.getVersion());
28+
}
29+
}

0 commit comments

Comments
 (0)