Add aliases to Profile

This commit is contained in:
Paul Schaub 2025-05-27 18:11:54 +02:00
parent 4ef5444e78
commit e5cb58468b
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 56 additions and 9 deletions

View file

@ -20,11 +20,15 @@ import sop.util.UTF8Util
* in the IETF namespace that begins with the string `draft-` should have semantics that hew as
* closely as possible to the referenced Internet Draft.
* @param description a free-form description of the profile.
* @see <a
* href="https://www.ietf.org/archive/id/draft-dkg-openpgp-stateless-cli-05.html#name-profile">
* SOP Spec - Profile</a>
* @param aliases list of optional profile alias names
* @see
* [SOP Spec - Profile](https://www.ietf.org/archive/id/draft-dkg-openpgp-stateless-cli-05.html#name-profile)
*/
data class Profile(val name: String, val description: Optional<String>) {
data class Profile(
val name: String,
val description: Optional<String>,
val aliases: List<String> = listOf()
) {
@JvmOverloads
constructor(
@ -50,8 +54,18 @@ data class Profile(val name: String, val description: Optional<String>) {
*
* @return string
*/
override fun toString(): String =
if (description.isEmpty) name else "$name: ${description.get()}"
override fun toString(): String = buildString {
append(name)
if (!description.isEmpty || !aliases.isEmpty()) {
append(":")
}
if (!description.isEmpty) {
append(" ${description.get()}")
}
if (!aliases.isEmpty()) {
append(" (aliases: ${aliases.joinToString(separator = ", ")})")
}
}
companion object {
@ -64,9 +78,21 @@ data class Profile(val name: String, val description: Optional<String>) {
@JvmStatic
fun parse(string: String): Profile {
return if (string.contains(": ")) {
Profile(
string.substring(0, string.indexOf(": ")),
string.substring(string.indexOf(": ") + 2).trim())
val name = string.substring(0, string.indexOf(": "))
var description = string.substring(string.indexOf(": ") + 2).trim()
if (description.contains("(aliases: ")) {
val aliases =
description.substring(
description.indexOf("(aliases: ") + 10, description.indexOf(")"))
description = description.substring(0, description.indexOf("(aliases: ")).trim()
Profile(name, Optional.of(description), aliases.split(", ").toList())
} else {
if (description.isNotBlank()) {
Profile(name, Optional.of(description))
} else {
Profile(name)
}
}
} else if (string.endsWith(":")) {
Profile(string.substring(0, string.length - 1))
} else {

View file

@ -5,6 +5,9 @@
package sop;
import org.junit.jupiter.api.Test;
import sop.util.Optional;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
@ -19,6 +22,24 @@ public class ProfileTest {
assertEquals("default: Use the implementers recommendations.", profile.toString());
}
@Test
public void withAliasesToString() {
Profile profile = new Profile(
"Foo",
Optional.of("Something something"),
Arrays.asList("Bar", "Baz"));
assertEquals("Foo: Something something (aliases: Bar, Baz)", profile.toString());
}
@Test
public void parseWithAliases() {
Profile profile = Profile.parse("Foo: Something something (aliases: Bar, Baz)");
assertEquals("Foo", profile.getName());
assertEquals("Something something", profile.getDescription().get());
assertEquals(Arrays.asList("Bar", "Baz"), profile.getAliases());
}
@Test
public void toStringNameOnly() {
Profile profile = new Profile("default");