Skip to content

Commit 659f00b

Browse files
author
Alex Davies-Moore
committed
Moved around the deps in prep for a spring boot demo
1 parent b479272 commit 659f00b

16 files changed

Lines changed: 397 additions & 69 deletions

File tree

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,24 @@
11
package com.flit.protoc;
22

3-
import com.flit.protoc.gen.server.spring.SpringGenerator;
43
import com.google.protobuf.compiler.PluginProtos;
5-
import com.flit.protoc.gen.Generator;
6-
import com.flit.protoc.gen.GeneratorException;
7-
8-
import java.util.Map;
9-
10-
import static com.flit.protoc.Parameter.PARAM_TARGET;
11-
import static com.flit.protoc.Parameter.PARAM_TYPE;
124

5+
/**
6+
* Main entry point to the plugin which handles the parsing of the input from the main protoc process
7+
* and hands off to a {@link Plugin} instance.
8+
*/
139
public class Main {
1410

1511
public static void main(String[] args) throws Exception {
1612

17-
PluginProtos.CodeGeneratorRequest request = PluginProtos.CodeGeneratorRequest.newBuilder().mergeFrom(System.in).build();
18-
19-
if (!request.hasParameter()) {
20-
PluginProtos.CodeGeneratorResponse
13+
Plugin plugin = new Plugin(
14+
PluginProtos.CodeGeneratorRequest
2115
.newBuilder()
22-
.setError("Usage: --flit_out=target=server,type=[spring]:<PATH> or --flit_out=target=client,type=[js]:<PATH>")
16+
.mergeFrom(System.in)
2317
.build()
24-
.writeTo(System.out);
25-
26-
return;
27-
}
28-
29-
Map<String, Parameter> params = Parameter.of(request.getParameter());
30-
31-
try {
32-
PluginProtos.CodeGeneratorResponse.Builder builder = PluginProtos.CodeGeneratorResponse.newBuilder();
18+
);
3319

34-
resolveGenerator(params).generate(request, params).forEach(builder::addFile);
35-
36-
builder.build().writeTo(System.out);
37-
} catch (GeneratorException e) {
38-
PluginProtos.CodeGeneratorResponse
39-
.newBuilder()
40-
.setError(e.getMessage())
41-
.build()
42-
.writeTo(System.out);
43-
}
20+
plugin.process().writeTo(System.out);
4421
}
4522

46-
private static Generator resolveGenerator(Map<String, Parameter> params) {
4723

48-
if (!params.containsKey(PARAM_TARGET)) {
49-
throw new GeneratorException("No argument specified for target");
50-
}
51-
52-
switch (params.get(PARAM_TARGET).getValue()) {
53-
case "server":
54-
switch (params.get(PARAM_TYPE).getValue()) {
55-
case "boot":
56-
case "spring":
57-
return new SpringGenerator();
58-
default:
59-
throw new GeneratorException("Unknown type: " + params.get(PARAM_TYPE).getValue());
60-
}
61-
default:
62-
throw new GeneratorException("Unknown target type: " + params.get(PARAM_TARGET).getValue());
63-
}
64-
65-
}
6624
}

plugin/src/main/java/com/flit/protoc/Parameter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public static Map<String, Parameter> of(String value) {
3333

3434
return Arrays.stream(value.split(","))
3535
.map(p -> p.split("=", 2))
36+
.filter(s -> s.length == 2 && !s[0].trim().isEmpty() && !s[1].trim().isEmpty())
3637
.map(Parameter::new)
3738
.collect(Collectors.toMap(Parameter::getKey, Function.identity()));
3839
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.flit.protoc;
2+
3+
import com.flit.protoc.gen.Generator;
4+
import com.flit.protoc.gen.GeneratorException;
5+
import com.flit.protoc.gen.server.spring.SpringGenerator;
6+
import com.google.protobuf.compiler.PluginProtos;
7+
8+
import java.util.Map;
9+
10+
import static com.flit.protoc.Parameter.PARAM_TARGET;
11+
import static com.flit.protoc.Parameter.PARAM_TYPE;
12+
13+
public class Plugin {
14+
15+
private final PluginProtos.CodeGeneratorRequest request;
16+
17+
public Plugin(PluginProtos.CodeGeneratorRequest request) {
18+
this.request = request;
19+
}
20+
21+
public PluginProtos.CodeGeneratorResponse process() {
22+
if (!request.hasParameter()) {
23+
return PluginProtos.CodeGeneratorResponse
24+
.newBuilder()
25+
.setError("Usage: --flit_out=target=server,type=[spring]:<PATH>")
26+
.build();
27+
}
28+
29+
Map<String, Parameter> params = Parameter.of(request.getParameter());
30+
31+
try {
32+
PluginProtos.CodeGeneratorResponse.Builder builder = PluginProtos.CodeGeneratorResponse.newBuilder();
33+
34+
resolveGenerator(params).generate(request, params).forEach(builder::addFile);
35+
36+
return builder.build();
37+
} catch (GeneratorException e) {
38+
return PluginProtos.CodeGeneratorResponse
39+
.newBuilder()
40+
.setError(e.getMessage())
41+
.build();
42+
}
43+
}
44+
45+
private Generator resolveGenerator(Map<String, Parameter> params) {
46+
47+
if (!params.containsKey(PARAM_TARGET)) {
48+
throw new GeneratorException("No argument specified for target");
49+
}
50+
51+
if (!params.containsKey(PARAM_TYPE)) {
52+
throw new GeneratorException("No argument specified for type");
53+
}
54+
55+
switch (params.get(PARAM_TARGET).getValue()) {
56+
case "server":
57+
switch (params.get(PARAM_TYPE).getValue()) {
58+
case "boot":
59+
case "spring":
60+
return new SpringGenerator();
61+
default:
62+
throw new GeneratorException("Unknown server type: " + params.get(PARAM_TYPE).getValue());
63+
}
64+
default:
65+
throw new GeneratorException("Unknown target type: " + params.get(PARAM_TARGET).getValue());
66+
}
67+
68+
}
69+
}

plugin/src/main/java/com/flit/protoc/gen/Generator.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
public interface Generator {
1313

1414
/**
15+
* This is the main entry point to code generation: the implementation is decided in the {@link com.flit.protoc.Plugin}
16+
* class from the given parameters.
1517
*
16-
* @param request
17-
* @param params
18-
* @return
18+
* @param request The inbound protoc request
19+
* @param params The plugin parameters
20+
*
21+
* @return The list of files to be added to the output.
1922
*/
2023
List<PluginProtos.CodeGeneratorResponse.File> generate(PluginProtos.CodeGeneratorRequest request, Map<String, Parameter> params);
2124
}

plugin/src/main/java/com/flit/protoc/gen/server/spring/BaseGenerator.java

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,46 @@ abstract class BaseGenerator {
1717
DescriptorProtos.FileDescriptorProto proto;
1818

1919
BaseGenerator(DescriptorProtos.FileDescriptorProto proto, DescriptorProtos.ServiceDescriptorProto s) {
20-
String baseClassName = proto.getOptions().getJavaOuterClassname();
21-
22-
if (baseClassName == null || baseClassName.isEmpty()) {
23-
baseClassName = proto.getName().substring(0, proto.getName().lastIndexOf('.'));
24-
}
25-
26-
char[] className = baseClassName.toCharArray();
27-
if (!Character.isUpperCase(className[0])) {
28-
className[0] = Character.toUpperCase(className[0]);
20+
this.clazz = proto.getOptions().getJavaOuterClassname();
21+
22+
if (this.clazz == null || this.clazz.isEmpty()) {
23+
24+
char[] classname = proto.getName().substring(0, proto.getName().lastIndexOf('.')).toCharArray();
25+
StringBuilder sb = new StringBuilder();
26+
27+
char previous = '_';
28+
for (char c : classname) {
29+
if (c == '_') {
30+
previous = c;
31+
continue;
32+
}
33+
34+
if (previous == '_') {
35+
sb.append(Character.toUpperCase(c));
36+
} else {
37+
sb.append(c);
38+
}
39+
40+
previous = c;
41+
}
42+
43+
this.clazz = sb.toString();
44+
45+
// check to see if there are any messages with this same class name as per java proto specs
46+
// note that we also check the services too as the protoc compiler does that as well
47+
proto.getMessageTypeList().forEach(m -> {
48+
if (m.getName().equals(this.clazz)) {
49+
this.clazz += "OuterClass";
50+
}
51+
});
52+
53+
proto.getServiceList().forEach(m -> {
54+
if (m.getName().equals(this.clazz)) {
55+
this.clazz += "OuterClass";
56+
}
57+
});
2958
}
3059

31-
this.clazz = new String(className);
3260
this.javaPackage = proto.getOptions().getJavaPackage();
3361
this.proto = proto;
3462
this.service = s;

plugin/src/main/shell/protoc-gen-flit

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# The quiet flag is required otherwise the tooling will dump output to stdout which will break the
1616
# plugin output and generation will fail.
1717
# -----------------------------------------------------------------------------
18+
DIR=$(dirname $(readlink -f "$0"))
1819

19-
JAR=$(ls -c ./protoc-plugin-*-all.jar | head -1)
20+
JAR=$(ls -c ${DIR}/plugin-*-all.jar | head -1)
2021
java ${FLIT_JAVA_OPTS} -jar $JAR $@
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package com.flit.protoc;
2+
3+
import com.google.protobuf.compiler.PluginProtos;
4+
import org.junit.Test;
5+
6+
import static junit.framework.TestCase.assertTrue;
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertFalse;
9+
10+
public class PluginTest {
11+
12+
@Test
13+
public void test_NoParameters() {
14+
Plugin plugin = new Plugin(PluginProtos.CodeGeneratorRequest.newBuilder().build());
15+
PluginProtos.CodeGeneratorResponse response = plugin.process();
16+
17+
assertTrue("Expected an error for no parameters", response.hasError());
18+
assertEquals(
19+
"Incorrect error message",
20+
"Usage: --flit_out=target=server,type=[spring]:<PATH>",
21+
response.getError()
22+
);
23+
}
24+
25+
@Test
26+
public void test_NoTargetSpecified() {
27+
Plugin plugin = new Plugin(PluginProtos.CodeGeneratorRequest.newBuilder().setParameter("unknown=unknown").build());
28+
PluginProtos.CodeGeneratorResponse response = plugin.process();
29+
30+
assertTrue("Expected an error for unknown target type", response.hasError());
31+
assertEquals(
32+
"Incorrect error message",
33+
"No argument specified for target",
34+
response.getError()
35+
);
36+
}
37+
38+
@Test
39+
public void test_UnknownTargetType() {
40+
Plugin plugin = new Plugin(PluginProtos.CodeGeneratorRequest.newBuilder().setParameter("target=unknown,type=boot").build());
41+
PluginProtos.CodeGeneratorResponse response = plugin.process();
42+
43+
assertTrue("Expected an error for unknown target type", response.hasError());
44+
assertEquals(
45+
"Incorrect error message",
46+
"Unknown target type: unknown",
47+
response.getError()
48+
);
49+
}
50+
51+
@Test
52+
public void test_EmptyTargetType() {
53+
Plugin plugin = new Plugin(PluginProtos.CodeGeneratorRequest.newBuilder().setParameter("target=").build());
54+
PluginProtos.CodeGeneratorResponse response = plugin.process();
55+
56+
assertTrue("Expected an error for unknown target type", response.hasError());
57+
assertEquals(
58+
"Incorrect error message",
59+
"No argument specified for target",
60+
response.getError()
61+
);
62+
}
63+
64+
@Test
65+
public void test_MissingTargetType() {
66+
Plugin plugin = new Plugin(PluginProtos.CodeGeneratorRequest.newBuilder().setParameter("target=server").build());
67+
PluginProtos.CodeGeneratorResponse response = plugin.process();
68+
69+
assertTrue("Expected an error for unknown server type", response.hasError());
70+
assertEquals(
71+
"Incorrect error message",
72+
"No argument specified for type",
73+
response.getError()
74+
);
75+
}
76+
77+
@Test
78+
public void test_UnknownServerType() {
79+
Plugin plugin = new Plugin(PluginProtos.CodeGeneratorRequest.newBuilder().setParameter("target=server,type=unknown").build());
80+
PluginProtos.CodeGeneratorResponse response = plugin.process();
81+
82+
assertTrue("Expected an error for unknown server type", response.hasError());
83+
assertEquals(
84+
"Incorrect error message",
85+
"Unknown server type: unknown",
86+
response.getError()
87+
);
88+
}
89+
90+
@Test
91+
public void test_MissingServerType() {
92+
Plugin plugin = new Plugin(PluginProtos.CodeGeneratorRequest.newBuilder().setParameter("target=server,type=").build());
93+
PluginProtos.CodeGeneratorResponse response = plugin.process();
94+
95+
assertTrue("Expected an error for unknown server type", response.hasError());
96+
assertEquals(
97+
"Incorrect error message",
98+
"No argument specified for type",
99+
response.getError()
100+
);
101+
}
102+
103+
@Test
104+
public void test_EmptyProtoList() {
105+
Plugin plugin = new Plugin(PluginProtos.CodeGeneratorRequest.newBuilder().setParameter("target=server,type=boot").build());
106+
PluginProtos.CodeGeneratorResponse response = plugin.process();
107+
108+
assertFalse("No error expected for empty file list", response.hasError());
109+
assertEquals("Expected no files generated", 0, response.getFileCount());
110+
}
111+
112+
}

runtime/core/build.gradle

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
group 'com.flit'
6+
7+
sourceCompatibility = 1.8
8+
9+
repositories {
10+
mavenCentral()
11+
}
12+
13+
dependencies {
14+
15+
compileOnly 'org.projectlombok:lombok:1.18.0'
16+
17+
testCompile 'junit:junit:4.12'
18+
}

runtime/spring/src/main/java/com/flit/ErrorCode.java renamed to runtime/core/src/main/java/com/flit/runtime/ErrorCode.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.flit;
1+
package com.flit.runtime;
22

33
/**
44
* Provides the mapping of error code (string status) to the corresponding HTTP status code.
@@ -113,4 +113,12 @@ public enum ErrorCode {
113113
this.errorCode = errorCode;
114114
this.httpStatus = httpStatus;
115115
}
116+
117+
public String getErrorCode() {
118+
return errorCode;
119+
}
120+
121+
public int getHttpStatus() {
122+
return httpStatus;
123+
}
116124
}

0 commit comments

Comments
 (0)