-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix display empty string when json cell is null (#132)
* Fix display empty when json cell is null * Decrease output cells complexity * Imp coverage
- Loading branch information
1 parent
d2104a6
commit e2bdb8f
Showing
6 changed files
with
158 additions
and
50 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
src/main/java/com/michelin/kafkactl/model/format/AgoFormat.java
This file contains 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,34 @@ | ||
package com.michelin.kafkactl.model.format; | ||
|
||
import static io.micronaut.core.util.StringUtils.EMPTY_STRING; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.util.StdDateFormat; | ||
import java.text.ParseException; | ||
import java.util.Date; | ||
import lombok.AllArgsConstructor; | ||
import org.ocpsoft.prettytime.PrettyTime; | ||
|
||
/** | ||
* Ago format. | ||
*/ | ||
@AllArgsConstructor | ||
public class AgoFormat implements OutputFormatStrategy { | ||
private String jsonPointer; | ||
|
||
@Override | ||
public String display(JsonNode node) { | ||
String output; | ||
JsonNode cell = node.at(this.jsonPointer); | ||
|
||
try { | ||
StdDateFormat sdf = new StdDateFormat(); | ||
Date d = sdf.parse(cell.asText()); | ||
output = new PrettyTime().format(d); | ||
} catch (ParseException e) { | ||
output = EMPTY_STRING; | ||
} | ||
|
||
return output; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/michelin/kafkactl/model/format/DefaultFormat.java
This file contains 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,33 @@ | ||
package com.michelin.kafkactl.model.format; | ||
|
||
import static io.micronaut.core.util.StringUtils.EMPTY_STRING; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.node.JsonNodeType; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
|
||
/** | ||
* Default format. | ||
*/ | ||
@AllArgsConstructor | ||
public class DefaultFormat implements OutputFormatStrategy { | ||
private String jsonPointer; | ||
|
||
@Override | ||
public String display(JsonNode node) { | ||
String output; | ||
JsonNode cell = node.at(this.jsonPointer); | ||
|
||
if (cell.isArray()) { | ||
List<String> children = new ArrayList<>(); | ||
cell.elements().forEachRemaining(jsonNode -> children.add(jsonNode.asText())); | ||
output = String.join(",", children); | ||
} else { | ||
output = cell.getNodeType().equals(JsonNodeType.NULL) ? EMPTY_STRING : cell.asText(); | ||
} | ||
|
||
return output; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/michelin/kafkactl/model/format/OutputFormatStrategy.java
This file contains 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,10 @@ | ||
package com.michelin.kafkactl.model.format; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
/** | ||
* Output format strategy. | ||
*/ | ||
public interface OutputFormatStrategy { | ||
String display(JsonNode node); | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/michelin/kafkactl/model/format/PeriodFormat.java
This file contains 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,36 @@ | ||
package com.michelin.kafkactl.model.format; | ||
|
||
import static io.micronaut.core.util.StringUtils.EMPTY_STRING; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import java.util.concurrent.TimeUnit; | ||
import lombok.AllArgsConstructor; | ||
|
||
/** | ||
* Period format. | ||
*/ | ||
@AllArgsConstructor | ||
public class PeriodFormat implements OutputFormatStrategy { | ||
private String jsonPointer; | ||
|
||
@Override | ||
public String display(JsonNode node) { | ||
String output; | ||
JsonNode cell = node.at(this.jsonPointer); | ||
|
||
try { | ||
long ms = Long.parseLong(cell.asText()); | ||
long days = TimeUnit.MILLISECONDS.toDays(ms); | ||
long hours = TimeUnit.MILLISECONDS.toHours(ms - TimeUnit.DAYS.toMillis(days)); | ||
long minutes = TimeUnit.MILLISECONDS.toMinutes( | ||
ms - TimeUnit.DAYS.toMillis(days) - TimeUnit.HOURS.toMillis(hours)); | ||
output = days > 0 ? (days + "d") : ""; | ||
output += hours > 0 ? (hours + "h") : ""; | ||
output += minutes > 0 ? (minutes + "m") : ""; | ||
} catch (NumberFormatException e) { | ||
output = EMPTY_STRING; | ||
} | ||
|
||
return output; | ||
} | ||
} |
This file contains 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 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