Skip to content

Commit

Permalink
feat: implement tryfrom for api types
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinkys committed Jun 5, 2024
1 parent 04be0cb commit a3eae38
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 122 deletions.
88 changes: 50 additions & 38 deletions crates/indexify_internal_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,21 @@ pub struct ExtractionGraph {
pub extraction_policies: Vec<ExtractionPolicy>,
}

impl From<ExtractionGraph> for indexify_coordinator::ExtractionGraph {
fn from(value: ExtractionGraph) -> Self {
Self {
impl TryFrom<ExtractionGraph> for indexify_coordinator::ExtractionGraph {
type Error = anyhow::Error;

fn try_from(value: ExtractionGraph) -> Result<Self> {
let mut extraction_policies = vec![];
for policy in value.extraction_policies {
let policy = indexify_coordinator::ExtractionPolicy::try_from(policy)?;
extraction_policies.push(policy);
}
Ok(Self {
id: value.id,
name: value.name,
namespace: value.namespace,
extraction_policies: value
.extraction_policies
.into_iter()
.map(|p| p.into())
.collect(),
}
extraction_policies,
})
}
}

Expand Down Expand Up @@ -478,21 +481,23 @@ impl Display for Task {
}
}

impl From<Task> for indexify_coordinator::Task {
fn from(value: Task) -> Self {
impl TryFrom<Task> for indexify_coordinator::Task {
type Error = anyhow::Error;

fn try_from(value: Task) -> Result<Self> {
let outcome: indexify_coordinator::TaskOutcome = value.outcome.into();
indexify_coordinator::Task {
Ok(indexify_coordinator::Task {
id: value.id,
extractor: value.extractor,
namespace: value.namespace,
content_metadata: Some(value.content_metadata.into()),
content_metadata: Some(value.content_metadata.try_into()?),
input_params: value.input_params.to_string(),
extraction_policy_id: value.extraction_policy_id,
extraction_graph_name: value.extraction_graph_name,
output_index_mapping: value.output_index_table_mapping,
outcome: outcome as i32,
index_tables: value.index_tables,
}
})
}
}

Expand Down Expand Up @@ -637,13 +642,15 @@ pub struct ExtractionPolicy {
pub content_source: ExtractionPolicyContentSource,
}

impl From<ExtractionPolicy> for indexify_coordinator::ExtractionPolicy {
fn from(value: ExtractionPolicy) -> Self {
impl TryFrom<ExtractionPolicy> for indexify_coordinator::ExtractionPolicy {
type Error = anyhow::Error;

fn try_from(value: ExtractionPolicy) -> Result<Self> {
let filters = utils::convert_map_serde_to_prost_json(value.filters)
.map_err(|e| anyhow!("unable to convert to protobuff JSON value: {e:?}"))
.unwrap();

Self {
Ok(Self {
id: value.id,
extractor: value.extractor,
name: value.name,
Expand All @@ -652,7 +659,7 @@ impl From<ExtractionPolicy> for indexify_coordinator::ExtractionPolicy {
content_source: value.content_source.into(),
graph_name: value.graph_name,
output_table_mapping: value.output_table_mapping,
}
})
}
}

Expand Down Expand Up @@ -918,13 +925,15 @@ impl ContentMetadata {
}
}

impl From<ContentMetadata> for indexify_coordinator::ContentMetadata {
fn from(value: ContentMetadata) -> Self {
impl TryFrom<ContentMetadata> for indexify_coordinator::ContentMetadata {
type Error = anyhow::Error;

fn try_from(value: ContentMetadata) -> Result<Self> {
let labels = utils::convert_map_serde_to_prost_json(value.labels)
.map_err(|e| anyhow!("unable to convert to protobuff JSON value: {e:?}"))
.unwrap();

Self {
Ok(Self {
id: value.id.id, // don't expose the version on the task
parent_id: value.parent_id.map(|id| id.id).unwrap_or_default(), /* don't expose the
* version on the
Expand All @@ -941,15 +950,15 @@ impl From<ContentMetadata> for indexify_coordinator::ContentMetadata {
hash: value.hash,
extraction_policy_ids: value.extraction_policy_ids,
extraction_graph_names: value.extraction_graph_names,
}
})
}
}

impl From<indexify_coordinator::ContentMetadata> for ContentMetadata {
fn from(value: indexify_coordinator::ContentMetadata) -> Self {
let labels = utils::convert_map_prost_to_serde_json(value.labels)
.map_err(|e| anyhow!("unable to convert to serde JSON value: {e:?}"))
.unwrap();
impl TryFrom<indexify_coordinator::ContentMetadata> for ContentMetadata {
type Error = anyhow::Error;

fn try_from(value: indexify_coordinator::ContentMetadata) -> Result<Self> {
let labels = utils::convert_map_prost_to_serde_json(value.labels)?;

let root_content_id = if value.root_content_id.is_empty() {
None
Expand All @@ -961,7 +970,7 @@ impl From<indexify_coordinator::ContentMetadata> for ContentMetadata {
} else {
Some(ContentMetadataId::new(&value.parent_id))
};
Self {
Ok(Self {
id: ContentMetadataId {
id: value.id,
version: 1,
Expand All @@ -981,7 +990,7 @@ impl From<indexify_coordinator::ContentMetadata> for ContentMetadata {
hash: value.hash,
extraction_policy_ids: value.extraction_policy_ids,
extraction_graph_names: value.extraction_graph_names,
}
})
}
}

Expand Down Expand Up @@ -1069,16 +1078,19 @@ pub struct Namespace {
pub extraction_graphs: Vec<ExtractionGraph>,
}

impl From<Namespace> for indexify_coordinator::Namespace {
fn from(value: Namespace) -> Self {
indexify_coordinator::Namespace {
name: value.name,
extraction_graphs: value
.extraction_graphs
.into_iter()
.map(|g| g.into())
.collect(),
impl TryFrom<Namespace> for indexify_coordinator::Namespace {
type Error = anyhow::Error;

fn try_from(value: Namespace) -> Result<Self> {
let mut extraction_graphs = vec![];
for graph in value.extraction_graphs {
extraction_graphs.push(graph.try_into()?)
}

Ok(indexify_coordinator::Namespace {
name: value.name,
extraction_graphs,
})
}
}

Expand Down
94 changes: 55 additions & 39 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,22 @@ pub struct ExtractionGraph {
pub extraction_policies: Vec<ExtractionPolicy>,
}

impl From<indexify_coordinator::ExtractionGraph> for ExtractionGraph {
fn from(value: indexify_coordinator::ExtractionGraph) -> Self {
Self {
impl TryFrom<indexify_coordinator::ExtractionGraph> for ExtractionGraph {
type Error = anyhow::Error;

fn try_from(value: indexify_coordinator::ExtractionGraph) -> Result<Self> {
let mut extraction_policies = vec![];
for policy in value.extraction_policies {
let policy: ExtractionPolicy = policy.try_into()?;
extraction_policies.push(policy);
}

Ok(Self {
id: value.namespace.clone(),
namespace: value.namespace,
name: value.name,
extraction_policies: value
.extraction_policies
.into_iter()
.map(Into::into)
.collect(),
}
extraction_policies,
})
}
}

Expand All @@ -51,21 +55,20 @@ pub struct ExtractionPolicy {
pub graph_name: String,
}

impl From<indexify_coordinator::ExtractionPolicy> for ExtractionPolicy {
fn from(value: indexify_coordinator::ExtractionPolicy) -> Self {
let filters_eq = internal_api::utils::convert_map_prost_to_serde_json(value.filters)
.map_err(|e| anyhow!("unable to convert filters to serde JSON: {e:?}",))
.unwrap();
impl TryFrom<indexify_coordinator::ExtractionPolicy> for ExtractionPolicy {
type Error = anyhow::Error;

Self {
fn try_from(value: indexify_coordinator::ExtractionPolicy) -> Result<Self> {
let filters_eq = internal_api::utils::convert_map_prost_to_serde_json(value.filters)?;
Ok(Self {
id: value.id,
extractor: value.extractor,
name: value.name,
filters_eq: Some(filters_eq),
input_params: Some(serde_json::from_str(&value.input_params).unwrap()),
content_source: Some(value.content_source),
graph_name: value.graph_name,
}
})
}
}

Expand All @@ -75,16 +78,23 @@ pub struct DataNamespace {
pub extraction_graphs: Vec<ExtractionGraph>,
}

impl From<indexify_coordinator::Namespace> for DataNamespace {
fn from(value: indexify_coordinator::Namespace) -> Self {
Self {
impl TryFrom<indexify_coordinator::Namespace> for DataNamespace {
type Error = anyhow::Error;

fn try_from(value: indexify_coordinator::Namespace) -> Result<Self> {
let extraction_graphs = {
let mut graphs = vec![];
for graph in value.extraction_graphs {
let graph: ExtractionGraph = graph.try_into()?;
graphs.push(graph);
}
graphs
};

Ok(Self {
name: value.name,
extraction_graphs: value
.extraction_graphs
.into_iter()
.map(Into::into)
.collect(),
}
extraction_graphs,
})
}
}

Expand Down Expand Up @@ -421,13 +431,13 @@ pub struct ContentMetadata {
pub hash: String,
}

impl From<indexify_coordinator::ContentMetadata> for ContentMetadata {
fn from(value: indexify_coordinator::ContentMetadata) -> Self {
let labels = internal_api::utils::convert_map_prost_to_serde_json(value.labels)
.map_err(|e| anyhow!("unable to convert labels to serde JSON: {e:?}",))
.unwrap();
impl TryFrom<indexify_coordinator::ContentMetadata> for ContentMetadata {
type Error = anyhow::Error;

Self {
fn try_from(value: indexify_coordinator::ContentMetadata) -> Result<Self> {
let labels = internal_api::utils::convert_map_prost_to_serde_json(value.labels)?;

Ok(Self {
id: value.id,
parent_id: value.parent_id,
root_content_id: value.root_content_id,
Expand All @@ -441,7 +451,7 @@ impl From<indexify_coordinator::ContentMetadata> for ContentMetadata {
size: value.size_bytes,
hash: value.hash,
extraction_graph_names: value.extraction_graph_names,
}
})
}
}

Expand Down Expand Up @@ -478,21 +488,27 @@ pub struct Task {
pub index_tables: Vec<String>,
}

impl From<indexify_coordinator::Task> for Task {
fn from(value: indexify_coordinator::Task) -> Self {
Self {
impl TryFrom<indexify_coordinator::Task> for Task {
type Error = anyhow::Error;

fn try_from(value: indexify_coordinator::Task) -> Result<Self> {
let content_metadata = if let Some(metadata) = value.content_metadata {
metadata.try_into()?
} else {
ContentMetadata::default()
};

Ok(Self {
id: value.id,
extractor: value.extractor,
extraction_policy_id: value.extraction_policy_id,
output_index_table_mapping: value.output_index_mapping,
namespace: value.namespace,
content_metadata: value
.content_metadata
.map_or_else(Default::default, Into::into), // EGTODO: Is this correct?
content_metadata,
input_params: serde_json::Value::String(value.input_params),
outcome: value.outcome, // EGTODO: Is it correct to just return i32 for value outcome?
index_tables: value.index_tables,
}
})
}
}

Expand Down
17 changes: 11 additions & 6 deletions src/coordinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Coordinator {
) -> Result<Vec<indexify_coordinator::ContentMetadata>> {
let mut content_meta_list = Vec::new();
for content in content_list {
let content: indexify_coordinator::ContentMetadata = content.into();
let content: indexify_coordinator::ContentMetadata = content.try_into()?;
content_meta_list.push(content.clone());
}
Ok(content_meta_list)
Expand All @@ -73,8 +73,13 @@ impl Coordinator {
pub fn external_content_metadata_to_internal(
&self,
content_list: Vec<indexify_coordinator::ContentMetadata>,
) -> Vec<internal_api::ContentMetadata> {
content_list.into_iter().map(|v| v.into()).collect()
) -> Result<Vec<internal_api::ContentMetadata>> {
let mut contents = vec![];
for content in content_list {
let content: internal_api::ContentMetadata = content.try_into()?;
contents.push(content.clone());
}
Ok(contents)
}

pub async fn list_content(
Expand Down Expand Up @@ -183,7 +188,7 @@ impl Coordinator {
.await?;
let tasks = tasks
.into_iter()
.map(|task| -> Result<indexify_coordinator::Task> { Ok(task.into()) })
.map(|task| -> Result<indexify_coordinator::Task> { Ok(task.try_into()?) })
.collect::<Result<Vec<_>>>()?;
Ok(tasks)
}
Expand All @@ -207,7 +212,7 @@ impl Coordinator {
.await?;
let tasks = tasks
.into_iter()
.map(|task| -> Result<indexify_coordinator::Task> { Ok(task.into()) })
.map(|task| -> Result<indexify_coordinator::Task> { Ok(task.try_into()?) })
.collect::<Result<Vec<_>>>()?;
Ok(tasks)
}
Expand Down Expand Up @@ -316,7 +321,7 @@ impl Coordinator {

pub async fn get_task(&self, task_id: &str) -> Result<indexify_coordinator::Task> {
let task = self.shared_state.task_with_id(task_id).await?;
Ok(task.into())
Ok(task.try_into()?)
}

pub async fn get_task_and_root_content(
Expand Down
Loading

0 comments on commit a3eae38

Please sign in to comment.