Skip to content

Commit 2e914b3

Browse files
rjalanderlapentad
andauthored
PART 2: Generate events using jsonschema2pojo and mustache template (#60)
Changes to generate event class file using template --------- Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech> Co-authored-by: Lapenta Francesco Davide <37077655+lapentad@users.noreply.github.com>
1 parent 739fb09 commit 2e914b3

File tree

186 files changed

+21132
-212
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

186 files changed

+21132
-212
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
uses: actions/checkout@v3
1818
with:
1919
fetch-depth: 0
20+
submodules: 'true'
2021

2122
- name: Setup Java
2223
uses: actions/setup-java@v3

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "spec"]
2+
path = spec
3+
url = git@github.com:cdevents/spec.git

generator/pom.xml

Lines changed: 495 additions & 8 deletions
Large diffs are not rendered by default.

generator/src/main/java/dev/cdevents/generator/CDEventsGenerator.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ public static void main(String[] args) {
5252
}
5353
String generatorBaseDir = args[0];
5454
String sdkBaseDir = args[1];
55+
String parentBaseDir = args[2];
5556
String targetPackageDir = sdkBaseDir + File.separator + "src/main/java/dev/cdevents/events";
56-
File folder = new File(sdkBaseDir + File.separator + RESOURCES_DIR + "schema");
57-
System.out.println(folder.toPath().toAbsolutePath());
57+
File folder = new File(parentBaseDir + File.separator + "spec" + File.separator + "schemas");
5858
if (folder.isDirectory()) {
5959
File[] files = folder.listFiles((dir, name) -> name.toLowerCase().endsWith(".json"));
6060
if (files != null) {
@@ -68,7 +68,11 @@ public static void main(String[] args) {
6868
SchemaData schemaData = buildCDEventDataFromJsonSchema(file);
6969
generateClassFileFromSchemaData(mustache, schemaData, targetPackageDir);
7070
}
71+
} else {
72+
log.error("No schema files found in the specified directory {}", folder.getAbsolutePath());
7173
}
74+
} else {
75+
log.error("No schema directory found in the specified directory {}", folder.getAbsolutePath());
7276
}
7377
}
7478

@@ -94,21 +98,19 @@ private static SchemaData buildCDEventDataFromJsonSchema(File file) {
9498
try {
9599
JsonNode rootNode = objectMapper.readTree(file);
96100
JsonNode contextNode = rootNode.get("properties").get("context").get("properties");
101+
String schemaURL = rootNode.get("$id").asText();
97102

98103
String eventType = contextNode.get("type").get("enum").get(0).asText();
99104
log.info("eventType: {}", eventType);
100105
String[] type = eventType.split("\\.");
101106
String subject = type[SUBJECT_INDEX];
102107
String predicate = type[PREDICATE_INDEX];
103108
String capitalizedSubject = StringUtils.capitalize(subject);
104-
if (subject.equals("pipelinerun")) {
105-
capitalizedSubject = capitalizedSubject.substring(0, SUBSTRING_PIPELINE_INDEX)
106-
+ StringUtils.capitalize(subject.substring(SUBSTRING_PIPELINE_INDEX));
107-
}
108109
String capitalizedPredicate = StringUtils.capitalize(predicate);
109110
String version = type[VERSION_INDEX];
110111

111112
//set the Schema JsonNode required values to schemaData
113+
schemaData.setSchemaURL(schemaURL);
112114
schemaData.setSubject(subject);
113115
schemaData.setPredicate(predicate);
114116
schemaData.setCapitalizedSubject(capitalizedSubject);

generator/src/main/java/dev/cdevents/generator/SchemaData.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class SchemaData {
1717
private String upperCaseSubject;
1818
private String schemaFileName;
1919

20+
private String schemaURL;
21+
2022
private List<ContentField> contentFields;
2123

2224
private List<ContentObjectField> contentObjectFields;
@@ -153,6 +155,20 @@ public void setSchemaFileName(String schemaFileName) {
153155
this.schemaFileName = schemaFileName;
154156
}
155157

158+
/**
159+
* @return schemaURL
160+
*/
161+
public String getSchemaURL() {
162+
return schemaURL;
163+
}
164+
165+
/**
166+
* @param schemaURL
167+
*/
168+
public void setSchemaURL(String schemaURL) {
169+
this.schemaURL = schemaURL;
170+
}
171+
156172
/**
157173
* @return the Content fields of an event
158174
*/

generator/src/main/resources/template/event-template.mustache

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import java.nio.file.Files;
3434
import java.nio.file.Paths;
3535

3636

37-
public class {{capitalizedSubject}}{{capitalizedPredicate}}CDEvent extends {{capitalizedSubject}}{{capitalizedPredicate}}Event implements CDEvent {
37+
public class {{capitalizedSubject}}{{capitalizedPredicate}}CDEvent extends {{capitalizedSubject}}{{predicate}} implements CDEvent {
3838
3939
4040
/**
@@ -93,8 +93,7 @@ public class {{capitalizedSubject}}{{capitalizedPredicate}}CDEvent extends {{cap
9393

9494
@Override
9595
public String schemaURL() {
96-
String eventSchemaName = "{{schemaFileName}}".substring(0, "{{schemaFileName}}".lastIndexOf(".json"));
97-
return String.format("https://cdevents.dev/%s/schema/%s", CDEventConstants.CDEVENTS_SPEC_VERSION, eventSchemaName);
96+
return "{{schemaURL}}";
9897
}
9998

10099

pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@
196196
</plugin>
197197
</plugins>
198198
</build>
199-
200199
<profiles>
201200
<profile>
202201
<id>publication</id>

sdk/src/main/java/dev/cdevents/constants/CDEventConstants.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ private CDEventConstants() {
88
/**
99
* Event JsonSchema files location.
1010
*/
11-
public static final String SCHEMA_FOLDER = "src/main/resources/schema";
11+
public static final String SCHEMA_FOLDER = "../spec/schemas";
12+
1213
/**
1314
* Mustache generic event template file.
1415
*/

sdk/src/main/java/dev/cdevents/events/ArtifactPackagedCDEvent.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import java.nio.file.Paths;
3535

3636

37-
public class ArtifactPackagedCDEvent extends ArtifactPackagedEvent implements CDEvent {
37+
public class ArtifactPackagedCDEvent extends Artifactpackaged implements CDEvent {
3838

3939

4040
/**
@@ -86,24 +86,23 @@ public String currentCDEventType() {
8686

8787

8888
/**
89-
* @return the artifact-packaged-event.json schema URL
89+
* @return the artifactpackaged.json schema URL
9090
*/
9191

9292
@Override
9393
public String schemaURL() {
94-
String eventSchemaName = "artifact-packaged-event.json".substring(0, "artifact-packaged-event.json".lastIndexOf(".json"));
95-
return String.format("https://cdevents.dev/%s/schema/%s", CDEventConstants.CDEVENTS_SPEC_VERSION, eventSchemaName);
94+
return "https://cdevents.dev/0.1.2/schema/artifact-packaged-event";
9695
}
9796

9897

9998
/**
100-
* @return the artifact-packaged-event.json schema Json
99+
* @return the artifactpackaged.json schema Json
101100
*/
102101

103102
@Override
104103
public String eventSchema() {
105104
try {
106-
return Files.readString(Paths.get(CDEventConstants.SCHEMA_FOLDER + "/artifact-packaged-event.json"));
105+
return Files.readString(Paths.get(CDEventConstants.SCHEMA_FOLDER + "/artifactpackaged.json"));
107106
} catch (IOException e) {
108107
throw new CDEventsException("Exception while reading Event JsonSchema file ", e);
109108
}

sdk/src/main/java/dev/cdevents/events/ArtifactPublishedCDEvent.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import java.nio.file.Paths;
3535

3636

37-
public class ArtifactPublishedCDEvent extends ArtifactPublishedEvent implements CDEvent {
37+
public class ArtifactPublishedCDEvent extends Artifactpublished implements CDEvent {
3838

3939

4040
/**
@@ -85,24 +85,23 @@ public String currentCDEventType() {
8585

8686

8787
/**
88-
* @return the artifact-published-event.json schema URL
88+
* @return the artifactpublished.json schema URL
8989
*/
9090

9191
@Override
9292
public String schemaURL() {
93-
String eventSchemaName = "artifact-published-event.json".substring(0, "artifact-published-event.json".lastIndexOf(".json"));
94-
return String.format("https://cdevents.dev/%s/schema/%s", CDEventConstants.CDEVENTS_SPEC_VERSION, eventSchemaName);
93+
return "https://cdevents.dev/0.1.2/schema/artifact-published-event";
9594
}
9695

9796

9897
/**
99-
* @return the artifact-published-event.json schema Json
98+
* @return the artifactpublished.json schema Json
10099
*/
101100

102101
@Override
103102
public String eventSchema() {
104103
try {
105-
return Files.readString(Paths.get(CDEventConstants.SCHEMA_FOLDER + "/artifact-published-event.json"));
104+
return Files.readString(Paths.get(CDEventConstants.SCHEMA_FOLDER + "/artifactpublished.json"));
106105
} catch (IOException e) {
107106
throw new CDEventsException("Exception while reading Event JsonSchema file ", e);
108107
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
// Code generated by dev.cdevents.generator.CDEventsGenerator. DO NOT EDIT.
2+
3+
/*
4+
Copyright 2023 The CDEvents Authors
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
SPDX-License-Identifier: Apache-2.0
19+
*/
20+
21+
package dev.cdevents.events;
22+
23+
24+
import dev.cdevents.constants.CDEventConstants;
25+
import dev.cdevents.exception.CDEventsException;
26+
import dev.cdevents.models.CDEvent;
27+
import dev.cdevents.models.branch.created.*;
28+
29+
import java.net.URI;
30+
import java.util.Date;
31+
import java.util.UUID;
32+
import java.io.IOException;
33+
import java.nio.file.Files;
34+
import java.nio.file.Paths;
35+
36+
37+
public class BranchCreatedCDEvent extends Branchcreated implements CDEvent {
38+
39+
40+
/**
41+
* Constructor to init CDEvent and set the Subject for {@link BranchCreatedCDEvent}.
42+
*/
43+
44+
public BranchCreatedCDEvent() {
45+
initCDEvent();
46+
}
47+
48+
49+
/**
50+
* Initialize the CDEvent with the context values.
51+
*/
52+
53+
@Override
54+
public void initCDEvent() {
55+
setContext(new Context());
56+
setSubject(new Subject());
57+
setCustomData(new Object());
58+
setCustomDataContentType("application/json");
59+
Context context = getContext();
60+
context.setId(UUID.randomUUID().toString());
61+
context.setTimestamp(new Date());
62+
context.setVersion(CDEventConstants.CDEVENTS_SPEC_VERSION);
63+
getSubject().setContent(new Content());
64+
getSubject().getContent().setRepository(new Repository());
65+
getSubject().setType(CDEventConstants.SubjectType.BRANCH.getSubjectType());
66+
}
67+
68+
/**
69+
* @return the event source
70+
*/
71+
72+
@Override
73+
public String eventSource() {
74+
return getContext().getSource();
75+
}
76+
77+
78+
/**
79+
* @return the current CDEvent type
80+
*/
81+
82+
@Override
83+
public String currentCDEventType() {
84+
return getContext().getType().value();
85+
}
86+
87+
88+
/**
89+
* @return the branchcreated.json schema URL
90+
*/
91+
92+
@Override
93+
public String schemaURL() {
94+
return "https://cdevents.dev/0.1.2/schema/branch-created-event";
95+
}
96+
97+
98+
/**
99+
* @return the branchcreated.json schema Json
100+
*/
101+
102+
@Override
103+
public String eventSchema() {
104+
try {
105+
return Files.readString(Paths.get(CDEventConstants.SCHEMA_FOLDER + "/branchcreated.json"));
106+
} catch (IOException e) {
107+
throw new CDEventsException("Exception while reading Event JsonSchema file ", e);
108+
}
109+
}
110+
111+
112+
/**
113+
* @param source
114+
* Sets the {@link Context} source value
115+
*/
116+
117+
public void setSource(URI source) {
118+
getContext().setSource(source.toString());
119+
}
120+
121+
122+
/**
123+
* @param subjectId
124+
* sets the subject Id
125+
*/
126+
127+
public void setSubjectId(String subjectId) {
128+
getSubject().setId(subjectId);
129+
}
130+
131+
132+
/**
133+
* @param subjectSource
134+
* sets the subject source
135+
*/
136+
137+
public void setSubjectSource(URI subjectSource) {
138+
getSubject().setSource(subjectSource.toString());
139+
}
140+
141+
142+
143+
/**
144+
* @param id
145+
*/
146+
public void setSubjectRepositoryId(String id) {
147+
getSubject().getContent().getRepository().setId(id);
148+
}
149+
/**
150+
* @param source
151+
*/
152+
public void setSubjectRepositorySource(String source) {
153+
getSubject().getContent().getRepository().setSource(source);
154+
}
155+
156+
}

0 commit comments

Comments
 (0)