mirror of
https://codeberg.org/PGPainless/sop-java.git
synced 2025-09-08 17:59:43 +02:00
Add aliases to Profile
This commit is contained in:
parent
589dcacd91
commit
eb275b1638
2 changed files with 56 additions and 9 deletions
|
@ -20,11 +20,15 @@ import sop.util.UTF8Util
|
||||||
* in the IETF namespace that begins with the string `draft-` should have semantics that hew as
|
* in the IETF namespace that begins with the string `draft-` should have semantics that hew as
|
||||||
* closely as possible to the referenced Internet Draft.
|
* closely as possible to the referenced Internet Draft.
|
||||||
* @param description a free-form description of the profile.
|
* @param description a free-form description of the profile.
|
||||||
* @see <a
|
* @param aliases list of optional profile alias names
|
||||||
* href="https://www.ietf.org/archive/id/draft-dkg-openpgp-stateless-cli-05.html#name-profile">
|
* @see
|
||||||
* SOP Spec - Profile</a>
|
* [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
|
@JvmOverloads
|
||||||
constructor(
|
constructor(
|
||||||
|
@ -50,8 +54,18 @@ data class Profile(val name: String, val description: Optional<String>) {
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
override fun toString(): String =
|
override fun toString(): String = buildString {
|
||||||
if (description.isEmpty) name else "$name: ${description.get()}"
|
append(name)
|
||||||
|
if (!description.isEmpty || !aliases.isEmpty()) {
|
||||||
|
append(":")
|
||||||
|
}
|
||||||
|
if (!description.isEmpty) {
|
||||||
|
append(" ${description.get()}")
|
||||||
|
}
|
||||||
|
if (!aliases.isEmpty()) {
|
||||||
|
append(" (aliases: ${aliases.joinToString(separator = ", ")})")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
|
@ -64,9 +78,21 @@ data class Profile(val name: String, val description: Optional<String>) {
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun parse(string: String): Profile {
|
fun parse(string: String): Profile {
|
||||||
return if (string.contains(": ")) {
|
return if (string.contains(": ")) {
|
||||||
Profile(
|
val name = string.substring(0, string.indexOf(": "))
|
||||||
string.substring(0, string.indexOf(": ")),
|
var description = string.substring(string.indexOf(": ") + 2).trim()
|
||||||
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(":")) {
|
} else if (string.endsWith(":")) {
|
||||||
Profile(string.substring(0, string.length - 1))
|
Profile(string.substring(0, string.length - 1))
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -5,6 +5,9 @@
|
||||||
package sop;
|
package sop;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
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.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
@ -19,6 +22,24 @@ public class ProfileTest {
|
||||||
assertEquals("default: Use the implementers recommendations.", profile.toString());
|
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
|
@Test
|
||||||
public void toStringNameOnly() {
|
public void toStringNameOnly() {
|
||||||
Profile profile = new Profile("default");
|
Profile profile = new Profile("default");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue