This is an automated email from the ASF dual-hosted git repository.
shuber pushed a commit to branch unomi-3-dev
in repository https://gitbox.apache.org/repos/asf/unomi.git
The following commit(s) were added to refs/heads/unomi-3-dev by this push:
new df7bfe364 UNOMI-879 Enhance UnomiCrudCommand to support inline JSON
data
df7bfe364 is described below
commit df7bfe364e9bddf3371abb31b9c7a2fe82364648
Author: Serge Huber <[email protected]>
AuthorDate: Thu Jan 15 18:37:17 2026 +0100
UNOMI-879 Enhance UnomiCrudCommand to support inline JSON data
- Added a new option `--data` for providing inline JSON string properties
for create/update operations.
- Implemented a `parseProperties` method to handle JSON parsing from either
a file or inline data.
- Updated error messages to require either `--file` or `--data` for create
and update operations, improving user feedback.
---
.../unomi/shell/dev/actions/UnomiCrudCommand.java | 35 ++++++++++++++++++----
1 file changed, 29 insertions(+), 6 deletions(-)
diff --git
a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/actions/UnomiCrudCommand.java
b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/actions/UnomiCrudCommand.java
index 78a69dd4d..43c70c9cc 100644
---
a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/actions/UnomiCrudCommand.java
+++
b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/actions/UnomiCrudCommand.java
@@ -63,6 +63,9 @@ public class UnomiCrudCommand implements Action {
@Option(name = "-f", aliases = "--file", description = "JSON file
containing object properties (for create/update)")
private String file;
+ @Option(name = "-d", aliases = "--data", description = "Inline JSON string
containing object properties (for create/update)")
+ private String data;
+
@Option(name = "--csv", description = "Output list in CSV format")
private boolean csv;
@@ -74,6 +77,22 @@ public class UnomiCrudCommand implements Action {
LOGGER.debug("UnomiCrudCommand init");
}
+ /**
+ * Parse JSON properties from either a file or inline data.
+ * @return Map of properties, or null if neither file nor data is provided
+ * @throws Exception if there's an error parsing the JSON
+ */
+ private Map<String, Object> parseProperties() throws Exception {
+ if (data != null && !data.trim().isEmpty()) {
+ // Use inline data if provided
+ return OBJECT_MAPPER.readValue(data, Map.class);
+ } else if (file != null && !file.trim().isEmpty()) {
+ // Fall back to file if no inline data
+ return OBJECT_MAPPER.readValue(Files.readString(Paths.get(file)),
Map.class);
+ }
+ return null;
+ }
+
@Override
public Object execute() throws Exception {
// Get all registered CrudCommand implementations
@@ -86,11 +105,11 @@ public class UnomiCrudCommand implements Action {
try {
switch (operation.toLowerCase()) {
case "create":
- if (file == null) {
- System.err.println("--file option is
required for create operation");
+ Map<String, Object> createProps =
parseProperties();
+ if (createProps == null) {
+ System.err.println("Either --file or
--data option is required for create operation");
return null;
}
- Map<String, Object> createProps =
OBJECT_MAPPER.readValue(Files.readString(Paths.get(file)), Map.class);
String newId = cmd.create(createProps);
System.out.println("Created " + type + " with
ID: " + newId);
break;
@@ -109,11 +128,15 @@ public class UnomiCrudCommand implements Action {
break;
case "update":
- if (id == null || file == null) {
- System.err.println("ID and --file options
are required for update operation");
+ if (id == null) {
+ System.err.println("ID is required for
update operation");
+ return null;
+ }
+ Map<String, Object> updateProps =
parseProperties();
+ if (updateProps == null) {
+ System.err.println("Either --file or
--data option is required for update operation");
return null;
}
- Map<String, Object> updateProps =
OBJECT_MAPPER.readValue(Files.readString(Paths.get(file)), Map.class);
cmd.update(id, updateProps);
System.out.println("Updated " + type + " with
ID: " + id);
break;