Skip to content

Commit

Permalink
Merge pull request #30614 from vespa-engine/hakonhall/add-domainnamel…
Browse files Browse the repository at this point in the history
…eaflabel

Add DomainName.leafLabel()
  • Loading branch information
freva authored Mar 13, 2024
2 parents 37484d7 + d936dc6 commit 1a13f6f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions vespajlib/src/main/java/ai/vespa/http/DomainName.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public static String requireLabel(String label) {
return requireMatch(label, "domain name label", labelPattern);
}

public String leafLabel() {
int offset = value().lastIndexOf('.');
return offset == -1 ? value() : value().substring(0, offset);
}

}
4 changes: 4 additions & 0 deletions vespajlib/src/test/java/ai/vespa/http/DomainNameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
Expand Down Expand Up @@ -30,6 +31,9 @@ void testNames() {
assertThrows(IllegalArgumentException.class, () -> DomainName.of("foo%"));
assertThrows(IllegalArgumentException.class, () -> DomainName.of(("." + "a".repeat(32)).repeat(8).substring(1, 257)));
assertThrows(IllegalArgumentException.class, () -> DomainName.of("a".repeat(64)));

assertEquals("foo", DomainName.of("foo").leafLabel());
assertEquals("foo", DomainName.of("foo.com").leafLabel());
}

}

0 comments on commit 1a13f6f

Please sign in to comment.