-
Notifications
You must be signed in to change notification settings - Fork 15.3k
KAFKA-7609: Add Protocol Generator for Kafka #5893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8cd1846
KAFKA-7609: Add Protocol Generator for Kafka
cmccabe b60abe2
Move some namespaces
cmccabe a5c9bc4
Require struct versions to be set
cmccabe b7cffd2
More style fixes and etc.
cmccabe baffbf3
Finish renaming
cmccabe 3ea2671
Bump ListOffset for KIP-207
cmccabe e1b2173
Fix some javadoc, use assertTrue instead of Assert.fail in a case
cmccabe 2091b26
Fix namespace for VersionsTest, MessageGeneratorTest
cmccabe f003e4d
Some syntax cleanups
cmccabe 6386c1a
Separate ApiMessage and Message, add Optional, etc.
cmccabe aedc370
StructSpec: stylistic improvement
cmccabe 744b972
Versions: add JavaDoc
cmccabe d0957a1
Don't embed Struct into FieldSpec
cmccabe 6d8de7a
Cleanups
cmccabe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,3 +52,4 @@ docs/generated/ | |
| kafkatest.egg-info/ | ||
| systest/ | ||
| *.swp | ||
| clients/src/generated | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one or more | ||
| // contributor license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright ownership. | ||
| // The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| // (the "License"); you may not use this file except in compliance with | ||
| // the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| repositories { | ||
| mavenCentral() | ||
| } | ||
|
|
||
| dependencies { | ||
| compile "com.fasterxml.jackson.core:jackson-databind:2.9.6" | ||
| compile "com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.9.6" | ||
| } | ||
|
|
||
| // Workaround for https://github.com/gradle/gradle/issues/1271 | ||
| test.enabled=false |
81 changes: 81 additions & 0 deletions
81
buildSrc/src/main/java/org/apache/kafka/message/CodeBuffer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.kafka.message; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.Writer; | ||
| import java.util.ArrayList; | ||
|
|
||
| public class CodeBuffer { | ||
| private final ArrayList<String> lines; | ||
| private int indent; | ||
|
|
||
| public CodeBuffer() { | ||
| this.lines = new ArrayList<>(); | ||
| this.indent = 0; | ||
| } | ||
|
|
||
| public void incrementIndent() { | ||
| indent++; | ||
| } | ||
|
|
||
| public void decrementIndent() { | ||
| indent--; | ||
| if (indent < 0) { | ||
| throw new RuntimeException("Indent < 0"); | ||
| } | ||
| } | ||
|
|
||
| public void printf(String format, Object... args) { | ||
| lines.add(String.format(indentSpaces() + format, args)); | ||
| } | ||
|
|
||
| public void write(Writer writer) throws IOException { | ||
| for (String line : lines) { | ||
| writer.write(line); | ||
| } | ||
| } | ||
|
|
||
| public void write(CodeBuffer other) { | ||
| for (String line : lines) { | ||
| other.lines.add(other.indentSpaces() + line); | ||
| } | ||
| } | ||
|
|
||
| private String indentSpaces() { | ||
| StringBuilder bld = new StringBuilder(); | ||
| for (int i = 0; i < indent; i++) { | ||
| bld.append(" "); | ||
| } | ||
| return bld.toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object other) { | ||
| if (!(other instanceof CodeBuffer)) { | ||
| return false; | ||
| } | ||
| CodeBuffer o = (CodeBuffer) other; | ||
| return lines.equals(o.lines); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return lines.hashCode(); | ||
| } | ||
| } |
142 changes: 142 additions & 0 deletions
142
buildSrc/src/main/java/org/apache/kafka/message/FieldSpec.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.kafka.message; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| public final class FieldSpec { | ||
| private final StructSpec struct; | ||
|
|
||
| private final FieldType type; | ||
|
|
||
| private final boolean mapKey; | ||
|
|
||
| private final Versions nullableVersions; | ||
|
|
||
| private final String fieldDefault; | ||
|
|
||
| private final boolean ignorable; | ||
|
|
||
| private final String about; | ||
|
|
||
| @JsonCreator | ||
| public FieldSpec(@JsonProperty("name") String name, | ||
| @JsonProperty("versions") String versions, | ||
| @JsonProperty("fields") List<FieldSpec> fields, | ||
| @JsonProperty("type") String type, | ||
| @JsonProperty("mapKey") boolean mapKey, | ||
| @JsonProperty("nullableVersions") String nullableVersions, | ||
| @JsonProperty("default") String fieldDefault, | ||
| @JsonProperty("ignorable") boolean ignorable, | ||
| @JsonProperty("about") String about) { | ||
| this.struct = new StructSpec(name, versions, fields); | ||
| this.type = FieldType.parse(Objects.requireNonNull(type)); | ||
| this.mapKey = mapKey; | ||
| this.nullableVersions = Versions.parse(nullableVersions, Versions.NONE); | ||
| if (!this.nullableVersions.empty()) { | ||
| if (!this.type.canBeNullable()) { | ||
| throw new RuntimeException("Type " + this.type + " cannot be nullable."); | ||
| } | ||
| } | ||
| this.fieldDefault = fieldDefault == null ? "" : fieldDefault; | ||
| this.ignorable = ignorable; | ||
| this.about = about == null ? "" : about; | ||
| if (!this.struct.fields().isEmpty()) { | ||
| if (!this.type.isArray()) { | ||
| throw new RuntimeException("Non-array field " + name + " cannot have fields"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public StructSpec struct() { | ||
| return struct; | ||
| } | ||
|
|
||
| @JsonProperty("name") | ||
| public String name() { | ||
| return struct.name(); | ||
| } | ||
|
|
||
| String capitalizedCamelCaseName() { | ||
| return MessageGenerator.capitalizeFirst(struct.name()); | ||
| } | ||
|
|
||
| String camelCaseName() { | ||
| return MessageGenerator.lowerCaseFirst(struct.name()); | ||
| } | ||
|
|
||
| String snakeCaseName() { | ||
| return MessageGenerator.toSnakeCase(struct.name()); | ||
| } | ||
|
|
||
| @JsonProperty("versions") | ||
| public String versionsString() { | ||
| return struct.versionsString(); | ||
| } | ||
|
|
||
| @JsonProperty("fields") | ||
| public List<FieldSpec> fields() { | ||
| return struct.fields(); | ||
| } | ||
|
|
||
| @JsonProperty("type") | ||
| public String typeString() { | ||
| return type.toString(); | ||
| } | ||
|
|
||
| public FieldType type() { | ||
| return type; | ||
| } | ||
|
|
||
| @JsonProperty("mapKey") | ||
| public boolean mapKey() { | ||
|
hachikuji marked this conversation as resolved.
Outdated
|
||
| return mapKey; | ||
| } | ||
|
|
||
| public Versions nullableVersions() { | ||
| return nullableVersions; | ||
| } | ||
|
|
||
| @JsonProperty("nullableVersions") | ||
| public String nullableVersionsString() { | ||
| return nullableVersions.toString(); | ||
| } | ||
|
|
||
| @JsonProperty("default") | ||
| public String defaultString() { | ||
| return fieldDefault; | ||
| } | ||
|
|
||
| boolean hasKeys() { | ||
| return struct.hasKeys(); | ||
| } | ||
|
|
||
| @JsonProperty("ignorable") | ||
| public boolean ignorable() { | ||
| return ignorable; | ||
| } | ||
|
|
||
| @JsonProperty("about") | ||
| public String about() { | ||
| return about; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't we make this check stricter? Would we ever expect the type to be something other than
STRUCT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I refactored this code to clean it up a bit. Not all
FieldSpecsdescribe arrays-- only some of them do. We should have atoStruct()method to make it convenient, but not embed aStructSpecunconditionally into theFieldSpec.