Extract JsonRpcNamespace

This commit is contained in:
AsamK 2021-11-08 14:57:38 +01:00
parent e7a4bc5ef1
commit b81a83eec5
2 changed files with 43 additions and 36 deletions

View file

@ -8,7 +8,6 @@ import org.asamk.signal.OutputType;
import org.asamk.signal.OutputWriter; import org.asamk.signal.OutputWriter;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.util.Util;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -29,39 +28,4 @@ public interface JsonRpcLocalCommand extends JsonRpcCommand<Map<String, Object>>
default List<OutputType> getSupportedOutputTypes() { default List<OutputType> getSupportedOutputTypes() {
return List.of(OutputType.PLAIN_TEXT, OutputType.JSON); 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");
}
}
} }

View file

@ -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");
}
}