Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow id to be an int in doc upsert #393

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading