mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 18:40:39 +00:00
Extract JsonRpcNamespace
This commit is contained in:
parent
e7a4bc5ef1
commit
b81a83eec5
2 changed files with 43 additions and 36 deletions
|
@ -8,7 +8,6 @@ import org.asamk.signal.OutputType;
|
|||
import org.asamk.signal.OutputWriter;
|
||||
import org.asamk.signal.commands.exceptions.CommandException;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.util.Util;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -29,39 +28,4 @@ public interface JsonRpcLocalCommand extends JsonRpcCommand<Map<String, Object>>
|
|||
default List<OutputType> getSupportedOutputTypes() {
|
||||
return List.of(OutputType.PLAIN_TEXT, OutputType.JSON);
|
||||
}
|
||||
|
||||
/**
|
||||
* Namespace implementation, that has plural handling for list arguments and converts camel case keys to dashed strings
|
||||
*/
|
||||
final class JsonRpcNamespace extends Namespace {
|
||||
|
||||
public JsonRpcNamespace(final Map<String, Object> attrs) {
|
||||
super(attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T get(String dest) {
|
||||
final T value = super.get(dest);
|
||||
if (value != null) {
|
||||
return value;
|
||||
}
|
||||
|
||||
final var camelCaseString = Util.dashSeparatedToCamelCaseString(dest);
|
||||
return super.get(camelCaseString);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E> List<E> getList(final String dest) {
|
||||
try {
|
||||
final List<E> value = super.getList(dest);
|
||||
if (value != null) {
|
||||
return value;
|
||||
}
|
||||
} catch (ClassCastException e) {
|
||||
return List.of(this.<E>get(dest));
|
||||
}
|
||||
|
||||
return super.getList(dest + "s");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
package org.asamk.signal.commands;
|
||||
|
||||
import net.sourceforge.argparse4j.inf.Namespace;
|
||||
|
||||
import org.asamk.signal.util.Util;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Namespace implementation, that has plural handling for list arguments and converts camel case keys to dashed strings
|
||||
*/
|
||||
final class JsonRpcNamespace extends Namespace {
|
||||
|
||||
public JsonRpcNamespace(final Map<String, Object> attrs) {
|
||||
super(attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T get(String dest) {
|
||||
final T value = super.get(dest);
|
||||
if (value != null) {
|
||||
return value;
|
||||
}
|
||||
|
||||
final var camelCaseString = Util.dashSeparatedToCamelCaseString(dest);
|
||||
return super.get(camelCaseString);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E> List<E> getList(final String dest) {
|
||||
try {
|
||||
final List<E> value = super.getList(dest);
|
||||
if (value != null) {
|
||||
return value;
|
||||
}
|
||||
} catch (ClassCastException e) {
|
||||
return List.of(this.<E>get(dest));
|
||||
}
|
||||
|
||||
return super.getList(dest + "s");
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue