Skip to content
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

add ability to format JSON in publish window #56

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,4 @@ Check the settings.yaml in the <user.home>/.kafkaesque directory for cluster ind
* check.for.updates.enabled: configures if KafkaEsque checks github-releases for a newer version on startup</span>.
* <span style="color:gray">default: true</span>
* check.for.updates.duration.between.hours: configures how many hours a check of the latest version on github is valid before it is checked again</span>.
* <span style="color:gray">default: 24</span>
* <span style="color:gray">default: 24</span>
8 changes: 5 additions & 3 deletions src/main/java/at/esque/kafka/JsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.google.common.base.Strings;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Message;
import com.google.protobuf.MessageOrBuilder;
import com.google.protobuf.Struct;
import com.google.protobuf.util.JsonFormat;
import io.netty.util.internal.StringUtil;
import javafx.scene.control.TreeItem;
import org.apache.kafka.common.header.Header;
import org.apache.kafka.common.header.internals.RecordHeader;
Expand Down Expand Up @@ -150,14 +152,14 @@ public static String formatJson(JsonNode jsonNode) {

public static ValidationResult validate(String jsonInString) {
if (jsonInString == null) {
return new ValidationResult(true);
return new ValidationResult(true, true);
}

try {
objectMapper.readTree(jsonInString);
return new ValidationResult(true);
return new ValidationResult(true, jsonInString.isBlank());
} catch (IOException e) {
return new ValidationResult(false, e.getMessage());
return new ValidationResult(false, jsonInString.isBlank(), e.getMessage());
}
}

Expand Down
44 changes: 39 additions & 5 deletions src/main/java/at/esque/kafka/PublisherController.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.*;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.stage.Stage;
import javafx.stage.Window;
Expand All @@ -37,7 +34,6 @@


public class PublisherController {

@FXML
private ComboBox<Integer> partitionCombobox;
@FXML
Expand All @@ -55,8 +51,12 @@ public class PublisherController {
@FXML
private CheckBox validateIsJsonKeyBox;
@FXML
public Button jsonKeyFormatButton;
@FXML
private CheckBox validateIsJsonValueBox;
@FXML
public Button jsonValueFormatButton;
@FXML
private TableView<Header> headerTableView;
@FXML
private TableColumn<Header, String> headerKeyColumn;
Expand Down Expand Up @@ -91,6 +91,8 @@ private void setupControls(ObservableList<Integer> partitions, TopicMessageTypeC
partitionCombobox.getSelectionModel().select(Integer.valueOf(-1));
keyTextArea.disableProperty().bind(nullKeyToggle.selectedProperty());
valueTextArea.disableProperty().bind(nullMessageToggle.selectedProperty());
jsonKeyFormatButton.setVisible(false);
jsonValueFormatButton.setVisible(false);

if (kafkaMessage != null) {
keyTextArea.setText(kafkaMessage.getKey());
Expand Down Expand Up @@ -142,6 +144,8 @@ private void setupControls(ObservableList<Integer> partitions, TopicMessageTypeC
}
}
}

handleTextChange();
}


Expand Down Expand Up @@ -232,4 +236,34 @@ private ObservableList<String> findTypesForTopic(String topicName, RestService s
.collect(Collectors.toList());
return FXCollections.observableList(collect);
}

private void handleTextChange() {
jsonKeyFormatButton.setVisible(jsonIsValidAndNotBlank(JsonUtils.validate(keyTextArea.getText())));
jsonValueFormatButton.setVisible(jsonIsValidAndNotBlank(JsonUtils.validate(valueTextArea.getText())));

keyTextArea.textProperty().addListener((observable, oldValue, newValue) ->
jsonKeyFormatButton.setVisible(jsonIsValidAndNotBlank(JsonUtils.validate(newValue)))
);
valueTextArea.textProperty().addListener((observable, oldValue, newValue) ->
jsonValueFormatButton.setVisible(jsonIsValidAndNotBlank(JsonUtils.validate(newValue)))
);
}

private boolean jsonIsValidAndNotBlank(ValidationResult validationResult) {
return validationResult.isValid() && !validationResult.isBlank();
}

@FXML
public void jsonKeyFormatClick(ActionEvent actionEvent) {
if (JsonUtils.validate(keyTextArea.getText()).isValid()) {
keyTextArea.setText(JsonUtils.formatJson(keyTextArea.getText()));
}
}

@FXML
public void jsonValueFormatClick(ActionEvent actionEvent) {
if (JsonUtils.validate(valueTextArea.getText()).isValid()) {
valueTextArea.setText(JsonUtils.formatJson(valueTextArea.getText()));
}
}
}
11 changes: 9 additions & 2 deletions src/main/java/at/esque/kafka/ValidationResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
public class ValidationResult {

private final boolean valid;
private final boolean isBlank;

private final String validationError;

public ValidationResult(boolean valid) {
public ValidationResult(boolean valid, boolean isBlank) {
this.valid = valid;
this.validationError = null;
this.isBlank = isBlank;
}

public ValidationResult(boolean valid, String validationError) {
public ValidationResult(boolean valid, boolean isBlank, String validationError) {
this.valid = valid;
this.validationError = validationError;
this.isBlank = isBlank;
}

public boolean isValid() {
Expand All @@ -23,4 +26,8 @@ public boolean isValid() {
public String getValidationError() {
return validationError;
}

public boolean isBlank() {
return isBlank;
}
}
5 changes: 5 additions & 0 deletions src/main/resources/fxml/publishMessage.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<?import javafx.scene.layout.VBox?>
<?import org.kordamp.ikonli.javafx.FontIcon?>
<?import java.lang.String?>
<?import javafx.scene.layout.Region?>
<BorderPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" prefHeight="554.0" prefWidth="631.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="at.esque.kafka.PublisherController">
<bottom>
<ToolBar minWidth="40.0" nodeOrientation="RIGHT_TO_LEFT" prefHeight="40.0" prefWidth="200.0" BorderPane.alignment="CENTER">
Expand Down Expand Up @@ -51,6 +52,8 @@
<Label alignment="CENTER" maxWidth="1.7976931348623157E308" text="Key Validation" textAlignment="CENTER" />
<CheckBox fx:id="validateIsJsonKeyBox" mnemonicParsing="false" text="Validate is JSON" />
<CheckBox fx:id="nullKeyToggle" alignment="CENTER" mnemonicParsing="false" text="Send null"/>
<Region VBox.vgrow="ALWAYS" />
<Button fx:id="jsonKeyFormatButton" minHeight="-Infinity" alignment="BOTTOM_CENTER" mnemonicParsing="false" onAction="#jsonKeyFormatClick" text="Format JSON"/>
</children>
<padding>
<Insets left="10.0" right="10.0" />
Expand All @@ -74,6 +77,8 @@
<Cursor fx:constant="HAND" />
</cursor>
</CheckBox>
<Region VBox.vgrow="ALWAYS" />
<Button fx:id="jsonValueFormatButton" minHeight="-Infinity" alignment="BOTTOM_CENTER" mnemonicParsing="false" onAction="#jsonValueFormatClick" text="Format as Json"/>
</children>
<padding>
<Insets left="10.0" right="10.0" />
Expand Down