Skip to content

Commit

Permalink
Ensure label cannot be null in ButtonImpl (#2771)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment authored Nov 10, 2024
1 parent 466a66b commit 51f907c
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ default Button withDisabled(boolean disabled)
@CheckReturnValue
default Button withEmoji(@Nullable Emoji emoji)
{
return new ButtonImpl(getId(), getLabel(), getStyle(), getUrl(), null, isDisabled(), emoji).checkValid();
return new ButtonImpl(getId(), getLabel(), getStyle(), getUrl(), getSku(), isDisabled(), emoji).checkValid();
}

/**
Expand Down Expand Up @@ -217,7 +217,7 @@ default Button withLabel(@Nonnull String label)
@CheckReturnValue
default Button withId(@Nonnull String id)
{
return new ButtonImpl(id, getLabel(), getStyle(), null, null, isDisabled(), getEmoji()).checkValid();
return new ButtonImpl(id, getLabel(), getStyle(), getUrl(), getSku(), isDisabled(), getEmoji()).checkValid();
}

/**
Expand All @@ -240,7 +240,7 @@ default Button withId(@Nonnull String id)
@CheckReturnValue
default Button withUrl(@Nonnull String url)
{
return new ButtonImpl(null, getLabel(), getStyle(), url, null, isDisabled(), getEmoji()).checkValid();
return new ButtonImpl(getId(), getLabel(), getStyle(), url, getSku(), isDisabled(), getEmoji()).checkValid();
}

/**
Expand All @@ -258,7 +258,7 @@ default Button withUrl(@Nonnull String url)
@CheckReturnValue
default Button withSku(@Nonnull SkuSnowflake sku)
{
return new ButtonImpl(null, "", getStyle(), null, sku, isDisabled(), null).checkValid();
return new ButtonImpl(getId(), getLabel(), getStyle(), getUrl(), sku, isDisabled(), getEmoji()).checkValid();
}

/**
Expand Down Expand Up @@ -345,7 +345,7 @@ static Button primary(@Nonnull String id, @Nonnull String label)
@Nonnull
static Button primary(@Nonnull String id, @Nonnull Emoji emoji)
{
return new ButtonImpl(id, "", ButtonStyle.PRIMARY, false, emoji).checkValid();
return new ButtonImpl(id, null, ButtonStyle.PRIMARY, false, emoji).checkValid();
}

/**
Expand Down Expand Up @@ -399,7 +399,7 @@ static Button secondary(@Nonnull String id, @Nonnull String label)
@Nonnull
static Button secondary(@Nonnull String id, @Nonnull Emoji emoji)
{
return new ButtonImpl(id, "", ButtonStyle.SECONDARY, false, emoji).checkValid();
return new ButtonImpl(id, null, ButtonStyle.SECONDARY, false, emoji).checkValid();
}

/**
Expand Down Expand Up @@ -453,7 +453,7 @@ static Button success(@Nonnull String id, @Nonnull String label)
@Nonnull
static Button success(@Nonnull String id, @Nonnull Emoji emoji)
{
return new ButtonImpl(id, "", ButtonStyle.SUCCESS, false, emoji).checkValid();
return new ButtonImpl(id, null, ButtonStyle.SUCCESS, false, emoji).checkValid();
}

/**
Expand Down Expand Up @@ -507,7 +507,7 @@ static Button danger(@Nonnull String id, @Nonnull String label)
@Nonnull
static Button danger(@Nonnull String id, @Nonnull Emoji emoji)
{
return new ButtonImpl(id, "", ButtonStyle.DANGER, false, emoji).checkValid();
return new ButtonImpl(id, null, ButtonStyle.DANGER, false, emoji).checkValid();
}

/**
Expand Down Expand Up @@ -567,7 +567,7 @@ static Button link(@Nonnull String url, @Nonnull String label)
@Nonnull
static Button link(@Nonnull String url, @Nonnull Emoji emoji)
{
return new ButtonImpl(null, "", ButtonStyle.LINK, url, null, false, emoji).checkValid();
return new ButtonImpl(null, null, ButtonStyle.LINK, url, null, false, emoji).checkValid();
}

/**
Expand All @@ -589,7 +589,7 @@ static Button link(@Nonnull String url, @Nonnull Emoji emoji)
@Nonnull
static Button premium(@Nonnull SkuSnowflake sku)
{
return new ButtonImpl(null, "", ButtonStyle.PREMIUM, null, sku, false, null).checkValid();
return new ButtonImpl(null, null, ButtonStyle.PREMIUM, null, sku, false, null).checkValid();
}

/**
Expand Down Expand Up @@ -661,7 +661,7 @@ static Button of(@Nonnull ButtonStyle style, @Nonnull String idOrUrl, @Nonnull E
Checks.check(style != ButtonStyle.PREMIUM, "Premium buttons don't support emojis");
if (style == ButtonStyle.LINK)
return link(idOrUrl, emoji);
return new ButtonImpl(idOrUrl, "", style, false, emoji).checkValid();
return new ButtonImpl(idOrUrl, null, style, false, emoji).checkValid();
}

/**
Expand Down Expand Up @@ -697,10 +697,16 @@ static Button of(@Nonnull ButtonStyle style, @Nonnull String idOrUrl, @Nonnull E
@Nonnull
static Button of(@Nonnull ButtonStyle style, @Nonnull String idOrUrlOrSku, @Nullable String label, @Nullable Emoji emoji)
{
if (style == ButtonStyle.LINK)
Checks.notNull(style, "ButtonStyle");

switch (style)
{
case LINK:
return new ButtonImpl(null, label, style, idOrUrlOrSku, null, false, emoji).checkValid();
if (style == ButtonStyle.PREMIUM)
case PREMIUM:
return new ButtonImpl(null, label, style, null, SkuSnowflake.fromId(idOrUrlOrSku), false, emoji).checkValid();
return new ButtonImpl(idOrUrlOrSku, label, style, null, null, false, emoji).checkValid();
default:
return new ButtonImpl(idOrUrlOrSku, label, style, null, null, false, emoji).checkValid();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public ButtonImpl(String id, String label, ButtonStyle style, boolean disabled,
public ButtonImpl(String id, String label, ButtonStyle style, String url, SkuSnowflake sku, boolean disabled, Emoji emoji)
{
this.id = id;
this.label = label;
this.label = label == null ? "" : label;
this.style = style;
this.url = url; // max length 512
this.url = url;
this.sku = sku;
this.disabled = disabled;
this.emoji = (EmojiUnion) emoji;
Expand All @@ -71,6 +71,7 @@ public ButtonImpl(String id, String label, ButtonStyle style, String url, SkuSno
public ButtonImpl checkValid()
{
Checks.notNull(style, "Style");
Checks.notLonger(label, LABEL_MAX_LENGTH, "Label");
Checks.check(style != ButtonStyle.UNKNOWN, "Cannot make button with unknown style!");

switch (style)
Expand All @@ -81,32 +82,27 @@ public ButtonImpl checkValid()
case DANGER:
Checks.check(url == null, "Cannot set an URL on action buttons");
Checks.check(sku == null, "Cannot set an SKU on action buttons");
Checks.check(emoji != null || (label != null && !label.isEmpty()), "Action buttons must have either an emoji or label");
Checks.check(emoji != null || !label.isEmpty(), "Action buttons must have either an emoji or label");
Checks.notEmpty(id, "Id");
Checks.notLonger(id, ID_MAX_LENGTH, "Id");
break;
case LINK:
Checks.check(id == null, "Cannot set an ID on link buttons");
Checks.check(url != null, "You must set an URL on link buttons");
Checks.check(sku == null, "Cannot set an SKU on link buttons");
Checks.check(emoji != null || (label != null && !label.isEmpty()), "Link buttons must have either an emoji or label");
Checks.check(emoji != null || !label.isEmpty(), "Link buttons must have either an emoji or label");
Checks.notEmpty(url, "URL");
Checks.notLonger(url, URL_MAX_LENGTH, "URL");
break;
case PREMIUM:
Checks.check(id == null, "Cannot set an ID on premium buttons");
Checks.check(url == null, "Cannot set an URL on premium buttons");
Checks.check(emoji == null, "Cannot set an emoji on premium buttons");
Checks.check(label == null || label.isEmpty(), "Cannot set a label on premium buttons");
Checks.check(label.isEmpty(), "Cannot set a label on premium buttons");
Checks.notNull(sku, "SKU");
break;
}

if (label != null)
{
Checks.notLonger(label, LABEL_MAX_LENGTH, "Label");
}

return this;
}

Expand Down
Loading

0 comments on commit 51f907c

Please sign in to comment.