Skip to content

Commit

Permalink
feat(core): make book mapping possible via jackson
Browse files Browse the repository at this point in the history
Signed-off-by: Jasper Lutz Severino <jasperlutzseverino@gmail.com>
  • Loading branch information
lutzseverino committed Aug 8, 2022
1 parent c6ecdb7 commit 7f43001
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 7 deletions.
13 changes: 12 additions & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
<parent>
<artifactId>discord-books</artifactId>
<groupId>com.lutzseverino.discordbooks</groupId>
<version>2.1.3</version>
<version>2.7.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<packaging>jar</packaging>

<name>Discord Books Core</name>

<artifactId>discord-books-core</artifactId>

<properties>
Expand All @@ -25,5 +26,15 @@
<artifactId>annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package com.lutzseverino.discordbooks.discord.component;

import com.fasterxml.jackson.annotation.*;
import com.lutzseverino.discordbooks.discord.component.impl.ClickableImpl;
import com.lutzseverino.discordbooks.discord.component.impl.SelectableImpl;
import org.jetbrains.annotations.Nullable;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = ClickableImpl.class, name = "clickable"),
@JsonSubTypes.Type(value = SelectableImpl.class, name = "selectable")
})
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id", scope = Actionable.class)
public interface Actionable {

@Nullable String getId();

Actionable setId(String id);

Actionable setDisabled(boolean disabled);

boolean isDisabled();

default Type getType() {
Actionable setDisabled(boolean disabled);

@JsonIgnore default Type getType() {
return Type.UNKNOWN;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package com.lutzseverino.discordbooks.discord.component;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.lutzseverino.discordbooks.discord.component.impl.ClickableImpl;
import org.jetbrains.annotations.Nullable;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = ClickableImpl.class, name = "default"),
})
public interface Clickable extends Actionable {

@Override default Type getType() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package com.lutzseverino.discordbooks.discord.component;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.lutzseverino.discordbooks.discord.component.impl.SelectableImpl;
import org.jetbrains.annotations.Nullable;

import java.util.List;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = SelectableImpl.class, name = "default"),
})
public interface Selectable extends Actionable {

@Override default Type getType() {
Expand All @@ -16,6 +23,10 @@ public interface Selectable extends Actionable {

Selectable addOptions(Option... options);

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = SelectableImpl.OptionImpl.class, name = "default"),
})
interface Option {
String getDisplay();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.lutzseverino.discordbooks.discord.component.impl;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.lutzseverino.discordbooks.discord.component.Clickable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -12,7 +14,8 @@ public class ClickableImpl implements Clickable {
private String emoji;
private boolean disabled;

public ClickableImpl(Style style) {
@JsonCreator
public ClickableImpl(@JsonProperty("style") Style style) {
this.style = style;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.lutzseverino.discordbooks.discord.component.impl;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.lutzseverino.discordbooks.discord.component.Selectable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -11,7 +13,8 @@ public class SelectableImpl implements Selectable {
private List<Option> options;
private boolean disabled;

public SelectableImpl(String id, List<Option> options) {
@JsonCreator
public SelectableImpl(@JsonProperty("id") String id, @JsonProperty("options") List<Option> options) {
this.id = id;
this.options = options;
}
Expand Down Expand Up @@ -66,7 +69,8 @@ public static class OptionImpl implements Selectable.Option {
private String emoji;
private boolean defaultOption;

public OptionImpl(String display, String value) {
@JsonCreator
public OptionImpl(@JsonProperty("display") String display, @JsonProperty("value") String value) {
this.display = display;
this.value = value;
}
Expand Down

0 comments on commit 7f43001

Please sign in to comment.