Skip to content

Commit

Permalink
Rename ImageReferenceParser to Regex
Browse files Browse the repository at this point in the history
Rename `ImageReferenceParser` to `Regex` and remove state. The regular
expressions are now used directly by the `ImageName` and
`ImageReference` classes with the values accessed directly from the
`Matcher`.

See gh-21495
  • Loading branch information
philwebb committed Jun 23, 2020
1 parent 9843888 commit f296f57
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 330 deletions.
Expand Up @@ -16,6 +16,9 @@

package org.springframework.boot.buildpack.platform.docker.type;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.util.Assert;

/**
Expand All @@ -25,11 +28,12 @@
* @author Scott Frederick
* @since 2.3.0
* @see ImageReference
* @see ImageReferenceParser
* @see #of(String)
*/
public class ImageName {

private static final Pattern PATTERN = Regex.IMAGE_NAME.compile();

private static final String DEFAULT_DOMAIN = "docker.io";

private static final String OFFICIAL_REPOSITORY_NAME = "library";
Expand All @@ -42,10 +46,10 @@ public class ImageName {

private final String string;

ImageName(String domain, String name) {
Assert.hasText(name, "Name must not be empty");
ImageName(String domain, String path) {
Assert.hasText(path, "Path must not be empty");
this.domain = getDomainOrDefault(domain);
this.name = getNameWithDefaultPath(this.domain, name);
this.name = getNameWithDefaultPath(this.domain, path);
this.string = this.domain + "/" + this.name;
}

Expand Down Expand Up @@ -128,8 +132,12 @@ private String getNameWithDefaultPath(String domain, String name) {
*/
public static ImageName of(String value) {
Assert.hasText(value, "Value must not be empty");
ImageReferenceParser parser = ImageReferenceParser.of(value);
return new ImageName(parser.getDomain(), parser.getName());
Matcher matcher = PATTERN.matcher(value);
Assert.isTrue(matcher.matches(),
() -> "Unable to parse name \"" + value + "\". "
+ "Image name must be in the form '[domainHost:port/][path/]name', "
+ "with 'path' and 'name' containing only [a-z0-9][.][_][-]");
return new ImageName(matcher.group("domain"), matcher.group("path"));
}

}
Expand Up @@ -30,13 +30,14 @@
* @author Scott Frederick
* @since 2.3.0
* @see ImageName
* @see ImageReferenceParser
*/
public final class ImageReference {

private static final String LATEST = "latest";
private static final Pattern PATTERN = Regex.IMAGE_REFERENCE.compile();

private static final Pattern JAR_VERSION_PATTERN = Pattern.compile("^(.*)(\\-\\d+)$");

private static final Pattern TRAILING_VERSION_PATTERN = Pattern.compile("^(.*)(\\-\\d+)$");
private static final String LATEST = "latest";

private final ImageName name;

Expand Down Expand Up @@ -182,7 +183,7 @@ public static ImageReference forJarFile(File jarFile) {
}
String name = filename.substring(0, firstDot);
String version = filename.substring(firstDot + 1);
Matcher matcher = TRAILING_VERSION_PATTERN.matcher(name);
Matcher matcher = JAR_VERSION_PATTERN.matcher(name);
if (matcher.matches()) {
name = matcher.group(1);
version = matcher.group(2).substring(1) + "." + version;
Expand Down Expand Up @@ -224,9 +225,13 @@ public static ImageReference random(String prefix, int randomLength) {
*/
public static ImageReference of(String value) {
Assert.hasText(value, "Value must not be null");
ImageReferenceParser parser = ImageReferenceParser.of(value);
ImageName name = new ImageName(parser.getDomain(), parser.getName());
return new ImageReference(name, parser.getTag(), parser.getDigest());
Matcher matcher = PATTERN.matcher(value);
Assert.isTrue(matcher.matches(),
() -> "Unable to parse image reference \"" + value + "\". "
+ "Image reference must be in the form '[domainHost:port/][path/]name[:tag][@digest]', "
+ "with 'path' and 'name' containing only [a-z0-9][.][_][-]");
ImageName name = new ImageName(matcher.group("domain"), matcher.group("path"));
return new ImageReference(name, matcher.group("tag"), matcher.group("digest"));
}

/**
Expand Down

This file was deleted.

0 comments on commit f296f57

Please sign in to comment.