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

Optimize Regex object usage for parameter validation #934

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions jellyfin-model/api/jellyfin-model.api
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public final class org/jellyfin/sdk/model/DeviceInfo {
public fun toString ()Ljava/lang/String;
}

public final class org/jellyfin/sdk/model/RegexValidator {
public static final field INSTANCE Lorg/jellyfin/sdk/model/RegexValidator;
public final fun matches (Ljava/lang/String;Ljava/lang/String;)Z
}

public final class org/jellyfin/sdk/model/ServerVersion : java/lang/Comparable {
public static final field Companion Lorg/jellyfin/sdk/model/ServerVersion$Companion;
public fun <init> (IIILjava/lang/Integer;)V
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.jellyfin.sdk.model


public object RegexValidator {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to introduce additional code generation that relies on existing classes. This is the main reason I didn't add the centralized regex validation myself yet.

private val patterns = mutableMapOf<String, Regex>()

public fun matches(value: String, pattern: String): Boolean {
return patterns.getOrPut(pattern) { Regex(pattern) }.matches(value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,21 @@ open class OperationBuilder(
is RegexValidation -> addCode(CodeBlock.builder().apply {
if (parameter.type.isNullable) {
addStatement(
"%M(%N·==·null·||·%M(%P).matches(%N))·{·%P·}",
"%M(%N·==·null·||·%M.matches(%N,%P))·{·%P·}",
MemberName("kotlin", "require"),
parameter.name,
MemberName("kotlin.text", "Regex"),
validation.pattern,
MemberName("org.jellyfin.sdk.model", "RegexValidator"),
parameter.name,
validation.pattern,
"Parameter \"${parameter.name}\" must match ${validation.pattern}."
)
} else {
addStatement(
"%M(%M(%P).matches(%N))·{·%P·}",
"%M(%M.matches(%N,%P))·{·%P·}",
MemberName("kotlin", "require"),
MemberName("kotlin.text", "Regex"),
validation.pattern,
MemberName("org.jellyfin.sdk.model", "RegexValidator"),
parameter.name,
validation.pattern,
"Parameter \"${parameter.name}\" must match ${validation.pattern}."
)
}
Expand Down
Loading