Skip to content

Commit

Permalink
Add oneof object support
Browse files Browse the repository at this point in the history
  • Loading branch information
HoKim98 committed Sep 25, 2023
1 parent 8ecfb13 commit eaa37a6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 55 deletions.
30 changes: 23 additions & 7 deletions dash/api/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ mod _impl_jsonschema_for_model_field_kind_spec {
#[serde(default)]
children: &'a Vec<String>,
#[serde(default)]
dynamic: &'a bool,
kind: &'a super::ModelFieldKindObjectSpec,
},
ObjectArray {
#[serde(default)]
Expand Down Expand Up @@ -277,8 +277,8 @@ mod _impl_jsonschema_for_model_field_kind_spec {
super::ModelFieldKindNativeSpec::StringArray {} => {
ModelFieldKindSpec::StringArray {}
}
super::ModelFieldKindNativeSpec::Object { children, dynamic } => {
ModelFieldKindSpec::Object { children, dynamic }
super::ModelFieldKindNativeSpec::Object { children, kind } => {
ModelFieldKindSpec::Object { children, kind }
}
super::ModelFieldKindNativeSpec::ObjectArray { children } => {
ModelFieldKindSpec::ObjectArray { children }
Expand Down Expand Up @@ -347,7 +347,7 @@ mod _impl_jsonschema_for_model_field_kind_spec {
#[serde(default)]
children: Vec<String>,
#[serde(default)]
dynamic: bool,
kind: super::ModelFieldKindObjectSpec,
},
ObjectArray {
#[serde(default)]
Expand Down Expand Up @@ -415,10 +415,10 @@ mod _impl_jsonschema_for_model_field_kind_spec {
Self::Native(super::ModelFieldKindNativeSpec::Uuid {})
}
// BEGIN aggregation types
ModelFieldKindSpec::Object { children, dynamic } => {
ModelFieldKindSpec::Object { children, kind } => {
Self::Native(super::ModelFieldKindNativeSpec::Object {
children,
dynamic,
kind,
})
}
ModelFieldKindSpec::ObjectArray { children } => {
Expand Down Expand Up @@ -538,7 +538,7 @@ pub enum ModelFieldKindNativeSpec {
#[serde(default)]
children: Vec<String>,
#[serde(default)]
dynamic: bool,
kind: ModelFieldKindObjectSpec,
},
ObjectArray {
#[serde(default)]
Expand Down Expand Up @@ -638,6 +638,22 @@ impl Default for ModelFieldKindStringSpec {
}
}

#[derive(
Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
)]
#[serde(rename_all = "camelCase")]
pub enum ModelFieldKindObjectSpec {
Dynamic,
OneOfStatic,
Static,
}

impl Default for ModelFieldKindObjectSpec {
fn default() -> Self {
Self::Static {}
}
}

#[derive(
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
)]
Expand Down
38 changes: 26 additions & 12 deletions dash/controller/src/validator/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use std::{
use anyhow::{bail, Result};
use dash_api::model::{
ModelCustomResourceDefinitionRefSpec, ModelFieldAttributeSpec, ModelFieldKindExtendedSpec,
ModelFieldKindNativeSpec, ModelFieldKindSpec, ModelFieldKindStringSpec, ModelFieldNativeSpec,
ModelFieldSpec, ModelFieldsNativeSpec, ModelFieldsSpec, ModelSpec,
ModelFieldKindNativeSpec, ModelFieldKindObjectSpec, ModelFieldKindSpec,
ModelFieldKindStringSpec, ModelFieldNativeSpec, ModelFieldSpec, ModelFieldsNativeSpec,
ModelFieldsSpec, ModelSpec,
};
use dash_provider::{imp::assert_contains, name, storage::KubernetesStorageClient};
use inflector::Inflector;
Expand All @@ -29,7 +30,7 @@ impl<'namespace, 'kube> ModelValidator<'namespace, 'kube> {
name: "/".into(),
kind: ModelFieldKindNativeSpec::Object {
children: Default::default(),
dynamic: true,
kind: ModelFieldKindObjectSpec::Dynamic,
},
attribute: ModelFieldAttributeSpec { optional: true },
}]),
Expand Down Expand Up @@ -198,8 +199,24 @@ impl ModelFieldsParser {
self.parse_json_property_aggregation(&name, prop.properties.as_ref())?;

Some(ModelFieldKindNativeSpec::Object {
kind: match prop.one_of.as_ref() {
Some(one_of) => {
let one_of = one_of
.iter()
.filter_map(|one_of| one_of.required.as_ref())
.flatten()
.cloned()
.collect::<BTreeSet<_>>();
if children == one_of {
ModelFieldKindObjectSpec::OneOfStatic
} else {
warn!("partial oneOf property is not supported");
ModelFieldKindObjectSpec::Static
}
}
None => ModelFieldKindObjectSpec::Static,
},
children: children.into_iter().collect(),
dynamic: false,
})
}
Some("array") => {
Expand Down Expand Up @@ -245,7 +262,7 @@ impl ModelFieldsParser {
"/metadata/labels/".into(),
"/metadata/name/".into(),
],
dynamic: false,
kind: ModelFieldKindObjectSpec::Static,
},
attribute: ModelFieldAttributeSpec { optional: false },
};
Expand All @@ -256,7 +273,7 @@ impl ModelFieldsParser {
name: name.to_string(),
kind: ModelFieldKindNativeSpec::Object {
children: vec![],
dynamic: true,
kind: ModelFieldKindObjectSpec::Dynamic,
},
attribute: ModelFieldAttributeSpec { optional: false },
};
Expand All @@ -278,7 +295,7 @@ impl ModelFieldsParser {
name: name.to_string(),
kind: ModelFieldKindNativeSpec::Object {
children: vec![],
dynamic: true,
kind: ModelFieldKindObjectSpec::Dynamic,
},
attribute: ModelFieldAttributeSpec { optional: false },
};
Expand Down Expand Up @@ -374,10 +391,7 @@ impl ModelFieldsParser {
ModelFieldKindNativeSpec::Uuid {} => {}
// BEGIN aggregation types
ModelFieldKindNativeSpec::StringArray {} => {}
ModelFieldKindNativeSpec::Object {
children,
dynamic: _,
}
ModelFieldKindNativeSpec::Object { children, kind: _ }
| ModelFieldKindNativeSpec::ObjectArray { children } => {
*children = children
.iter()
Expand Down Expand Up @@ -457,7 +471,7 @@ impl ModelFieldsParser {
name: name.to_string(),
kind: ModelFieldKindNativeSpec::Object {
children: children.into_iter().collect(),
dynamic: false,
kind: ModelFieldKindObjectSpec::Static,
},
attribute: ModelFieldAttributeSpec { optional: false },
};
Expand Down
42 changes: 16 additions & 26 deletions dash/provider/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use anyhow::{anyhow, bail, Error, Result};
use async_recursion::async_recursion;
use chrono::{DateTime, Utc};
use dash_api::model::{
Integer, ModelFieldDateTimeDefaultType, ModelFieldKindNativeSpec, ModelFieldNativeSpec,
ModelFieldSpec, ModelFieldsNativeSpec, ModelFieldsSpec, Number,
Integer, ModelFieldDateTimeDefaultType, ModelFieldKindNativeSpec, ModelFieldKindObjectSpec,
ModelFieldNativeSpec, ModelFieldSpec, ModelFieldsNativeSpec, ModelFieldsSpec, Number,
};
use inflector::Inflector;
use regex::Regex;
Expand Down Expand Up @@ -349,10 +349,7 @@ impl InputTemplate {
}
value => assert_optional(&name, &value, &base_field.parsed, optional),
},
ModelFieldKindNativeSpec::Object {
children: _,
dynamic,
} => match value {
ModelFieldKindNativeSpec::Object { children: _, kind } => match value {
Value::String(ref_name) => {
let input = InputFieldValue {
name,
Expand All @@ -362,19 +359,20 @@ impl InputTemplate {
};
self.update_field_value_impl(storage, input, optional).await
}
Value::Object(children) => {
if *dynamic {
Value::Object(children) => match kind {
ModelFieldKindObjectSpec::Dynamic => {
*field = Value::Object(children);
Ok(())
} else {
}
ModelFieldKindObjectSpec::OneOfStatic | ModelFieldKindObjectSpec::Static => {
for (child, value) in children.into_iter() {
let child = InputField::sub_object(&name, &child, value);
self.update_field_value_impl(storage, child, optional)
.await?;
}
Ok(())
}
}
},
value => assert_optional(&name, &value, &base_field.parsed, optional),
},
ModelFieldKindNativeSpec::ObjectArray { .. } => match value {
Expand Down Expand Up @@ -578,10 +576,7 @@ impl InputTemplate {
}
Ok(())
}
ModelFieldKindNativeSpec::Object {
children,
dynamic: _,
} => {
ModelFieldKindNativeSpec::Object { children, kind: _ } => {
if field.is_null() {
*field = Value::Object(Default::default());
}
Expand Down Expand Up @@ -795,22 +790,20 @@ impl<'a> ItemTemplate<'a> {
}
value => assert_optional(&name, &value, base_field, optional),
},
ModelFieldKindNativeSpec::Object {
children: _,
dynamic,
} => match value {
Value::Object(children) => {
if *dynamic {
ModelFieldKindNativeSpec::Object { children: _, kind } => match value {
Value::Object(children) => match kind {
ModelFieldKindObjectSpec::Dynamic => {
*field = Value::Object(children);
Ok(())
} else {
}
ModelFieldKindObjectSpec::OneOfStatic | ModelFieldKindObjectSpec::Static => {
for (child, value) in children.into_iter() {
let child = InputField::sub_object(&name, &child, value);
self.update_field_value_impl(child, optional)?;
}
Ok(())
}
}
},
value => assert_optional(&name, &value, base_field, optional),
},
ModelFieldKindNativeSpec::ObjectArray { .. } => match value {
Expand Down Expand Up @@ -1018,10 +1011,7 @@ impl<'a> ItemTemplate<'a> {
}
Ok(())
}
ModelFieldKindNativeSpec::Object {
children,
dynamic: _,
} => {
ModelFieldKindNativeSpec::Object { children, kind: _ } => {
if field.is_null() {
*field = Value::Object(Default::default());
}
Expand Down
21 changes: 11 additions & 10 deletions dash/provider/src/storage/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use chrono::{NaiveDateTime, Utc};
use dash_api::{
model::{
ModelCrd, ModelFieldDateTimeDefaultType, ModelFieldKindNativeSpec,
ModelFieldKindStringSpec, ModelFieldNativeSpec, ModelFieldsNativeSpec, ModelState,
ModelFieldKindObjectSpec, ModelFieldKindStringSpec, ModelFieldNativeSpec,
ModelFieldsNativeSpec, ModelState,
},
storage::db::{
ModelStorageDatabaseBorrowedSpec, ModelStorageDatabaseOwnedSpec, ModelStorageDatabaseSpec,
Expand Down Expand Up @@ -601,16 +602,16 @@ fn convert_field_to_column(
column.json();
Ok(Some(column))
}
ModelFieldKindNativeSpec::Object {
children: _,
dynamic,
} => {
ModelFieldKindNativeSpec::Object { children: _, kind } => {
// attribute: type
if *dynamic {
column.json();
Ok(Some(column))
} else {
Ok(None)
match kind {
ModelFieldKindObjectSpec::Dynamic => {
column.json();
Ok(Some(column))
}
ModelFieldKindObjectSpec::OneOfStatic | ModelFieldKindObjectSpec::Static => {
Ok(None)
}
}
}
ModelFieldKindNativeSpec::ObjectArray { children: _ } => {
Expand Down

0 comments on commit eaa37a6

Please sign in to comment.