Skip to content

Commit

Permalink
Allow id to be an int in replace, upsert, insert and import cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
Westwooo committed Sep 4, 2024
1 parent 785efa6 commit 52c8228
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
11 changes: 5 additions & 6 deletions src/cli/doc_import.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use crate::cli::doc_upsert::run_kv_mutations;
use crate::cli::doc_upsert::{id_from_value, run_kv_mutations};
use crate::cli::error::serialize_error;
use crate::cli::util::convert_nu_value_to_json_value;
use crate::client::KeyValueRequest;
use crate::state::State;
use nu_command::Open;
use nu_engine::CallExt;
use std::sync::{Arc, Mutex};

use crate::cli::error::serialize_error;
use crate::cli::util::convert_nu_value_to_json_value;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Value,
};
use std::sync::{Arc, Mutex};

#[derive(Clone)]
pub struct DocImport {
Expand Down Expand Up @@ -120,7 +119,7 @@ fn run_import(
let mut content = serde_json::Map::new();
for (k, v) in val.iter() {
if k.clone() == id_column {
id = v.as_str().ok();
id = id_from_value(v, span);
}

content.insert(k.clone(), convert_nu_value_to_json_value(v, span).ok()?);
Expand Down
4 changes: 2 additions & 2 deletions src/cli/doc_remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ impl Command for DocRemove {
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
run_get(self.state.clone(), engine_state, stack, call, input)
run_remove(self.state.clone(), engine_state, stack, call, input)
}
}

fn run_get(
fn run_remove(
state: Arc<Mutex<State>>,
engine_state: &EngineState,
stack: &mut Stack,
Expand Down
17 changes: 16 additions & 1 deletion src/cli/doc_upsert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::state::State;
use crate::RemoteCluster;
use futures::stream::FuturesUnordered;
use futures::StreamExt;
use log::info;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
Expand Down Expand Up @@ -164,7 +165,7 @@ pub(crate) fn run_kv_store_ops(
let mut content = None;
for (k, v) in val.iter() {
if k.clone() == id_column {
id = v.as_str().ok();
id = id_from_value(v, span);
}
if k.clone() == content_column {
content = convert_nu_value_to_json_value(v, span).ok();
Expand Down Expand Up @@ -198,6 +199,20 @@ pub(crate) fn run_kv_store_ops(
)
}

pub fn id_from_value(v: &Value, span: Span) -> Option<String> {
match v {
Value::String { val, .. } => Some(val.clone()),
Value::Int { val, .. } => Some(val.to_string()),
_ => {
info!(
"Skipping doc with id '{}' as id is not an int or string",
convert_nu_value_to_json_value(v, span).unwrap_or("error parsing id".into())
);
None
}
}
}

pub fn run_kv_mutations(
state: Arc<Mutex<State>>,
engine_state: &EngineState,
Expand Down

0 comments on commit 52c8228

Please sign in to comment.