Skip to content

Commit aa69ca0

Browse files
committed
StructSchema take a optional description and put in JSON schema
1 parent 97af248 commit aa69ca0

File tree

7 files changed

+28
-2
lines changed

7 files changed

+28
-2
lines changed

src/base/json_schema.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::schema;
22
use schemars::schema::{
3-
ArrayValidation, InstanceType, ObjectValidation, Schema, SchemaObject, SingleOrVec,
3+
ArrayValidation, InstanceType, Metadata, ObjectValidation, Schema, SchemaObject, SingleOrVec,
44
};
55

66
pub trait ToJsonSchema {
@@ -70,6 +70,10 @@ impl ToJsonSchema for schema::BasicValueType {
7070
impl ToJsonSchema for schema::StructSchema {
7171
fn to_json_schema(&self) -> SchemaObject {
7272
SchemaObject {
73+
metadata: Some(Box::new(Metadata {
74+
description: self.description.as_ref().map(|s| s.to_string()),
75+
..Default::default()
76+
})),
7377
instance_type: Some(SingleOrVec::Single(Box::new(InstanceType::Object))),
7478
object: Some(Box::new(ObjectValidation {
7579
properties: self

src/base/schema.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,16 @@ impl std::fmt::Display for BasicValueType {
6868
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
6969
pub struct StructSchema {
7070
pub fields: Arc<Vec<FieldSchema>>,
71+
72+
#[serde(default, skip_serializing_if = "Option::is_none")]
73+
pub description: Option<Arc<str>>,
7174
}
7275

7376
impl StructSchema {
7477
pub fn without_attrs(&self) -> Self {
7578
Self {
7679
fields: Arc::new(self.fields.iter().map(|f| f.without_attrs()).collect()),
80+
description: None,
7781
}
7882
}
7983
}
@@ -168,11 +172,16 @@ impl std::fmt::Display for CollectionSchema {
168172
}
169173

170174
impl CollectionSchema {
171-
pub fn new(kind: CollectionKind, fields: Vec<FieldSchema>) -> Self {
175+
pub fn new(
176+
kind: CollectionKind,
177+
fields: Vec<FieldSchema>,
178+
description: Option<Arc<str>>,
179+
) -> Self {
172180
Self {
173181
kind,
174182
row: StructSchema {
175183
fields: Arc::new(fields),
184+
description,
176185
},
177186
collectors: Default::default(),
178187
}

src/builder/analyzer.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ impl TryInto<ValueType> for &ValueTypeBuilder {
6060
pub(super) struct StructSchemaBuilder {
6161
fields: Vec<FieldSchema<ValueTypeBuilder>>,
6262
field_name_idx: HashMap<FieldName, u32>,
63+
description: Option<Arc<str>>,
6364
}
6465

6566
impl StructSchemaBuilder {
@@ -91,6 +92,7 @@ impl TryFrom<&StructSchema> for StructSchemaBuilder {
9192
let mut result = StructSchemaBuilder {
9293
fields: Vec::with_capacity(schema.fields.len()),
9394
field_name_idx: HashMap::with_capacity(schema.fields.len()),
95+
description: schema.description.clone(),
9496
};
9597
for field in schema.fields.iter() {
9698
result.add_field(FieldSchema::<ValueTypeBuilder>::from_alternative(field)?)?;
@@ -110,6 +112,7 @@ impl TryInto<StructSchema> for &StructSchemaBuilder {
110112
.map(FieldSchema::<ValueType>::from_alternative)
111113
.collect::<Result<Vec<_>>>()?,
112114
),
115+
description: self.description.clone(),
113116
})
114117
}
115118
}
@@ -284,6 +287,10 @@ fn try_make_common_struct_schemas(
284287
}
285288
Ok(StructSchema {
286289
fields: Arc::new(result_fields),
290+
description: schema1
291+
.description
292+
.clone()
293+
.or_else(|| schema2.description.clone()),
287294
})
288295
}
289296

@@ -484,6 +491,7 @@ fn analyze_struct_mapping(
484491
},
485492
StructSchema {
486493
fields: Arc::new(field_schemas),
494+
description: None,
487495
},
488496
))
489497
}
@@ -829,6 +837,7 @@ impl AnalyzerContext<'_> {
829837
} else {
830838
ValueType::Struct(StructSchema {
831839
fields: Arc::from(key_fields_schema.clone()),
840+
description: None,
832841
})
833842
};
834843
let mut value_fields_schema: Vec<FieldSchema> = vec![];

src/builder/flow_builder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ impl FlowBuilder {
563563
})
564564
.collect(),
565565
),
566+
description: None,
566567
};
567568

568569
{

src/ops/functions/split_recursively.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ impl SimpleFunctionFactoryBase for Factory {
591591
FieldSchema::new("location", make_output_type(BasicValueType::Range)),
592592
FieldSchema::new("text", make_output_type(BasicValueType::Str)),
593593
],
594+
None,
594595
))
595596
.with_attr(
596597
field_attrs::CHUNK_BASE_TEXT,

src/ops/sources/google_drive.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ impl SourceFactoryBase for Factory {
274274
}),
275275
),
276276
],
277+
None,
277278
)))
278279
}
279280

src/ops/sources/local_file.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ impl SourceFactoryBase for Factory {
112112
}),
113113
),
114114
],
115+
None,
115116
)))
116117
}
117118

0 commit comments

Comments
 (0)