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

Initial Value Fix #249

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
37 changes: 28 additions & 9 deletions core/src/schema/content/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use std::hash::{Hash, Hasher};
use serde::{de::IntoDeserializer, Deserialize, Serialize};
use serde_json::Value;

use crate::graph::Value as SynthValue;

mod r#bool;
pub use self::r#bool::BoolContent;

Expand Down Expand Up @@ -239,6 +241,22 @@ macro_rules! content {
}
}

use crate::graph::Value as SynthValue;
impl From<SynthValue> for Content {
fn from(value: SynthValue) -> Self {
/* match value {
Value::Null(_) => {}
Value::Bool(_) => {}
Value::Number(_) => {}
Value::String(_) => {}
Value::DateTime(_) => {}
Value::Object(_) => {}
Value::Array(_) => {}
}*/
todo!()
}
}

content! {
labels: ContentLabels,
variants: {
Expand Down Expand Up @@ -435,22 +453,22 @@ impl std::fmt::Display for Content {
}
}

impl<'r> From<&'r Value> for Content {
fn from(value: &'r Value) -> Self {
impl<'r> From<&'r SynthValue> for Content {
fn from(value: &'r SynthValue) -> Self {
match value {
// TODO not sure what the correct behaviour is here
Value::Null => Content::Null(NullContent),
Value::Bool(_) => Content::Bool(BoolContent::default()),
Value::String(_) => Content::String(StringContent::default()),
Value::Array(arr) => {
SynthValue::Null(_) => Content::Null(NullContent),
SynthValue::Bool(_) => Content::Bool(BoolContent::default()),
SynthValue::String(_) => Content::String(StringContent::default()),
SynthValue::Array(arr) => {
let length = arr.len();
let one_of_content = arr.iter().collect();
Content::Array(ArrayContent {
length: Box::new(Content::from(&Value::from(length as u64))),
length: Box::new(Content::from(&SynthValue::from(length as u64))),
content: Box::new(Content::OneOf(one_of_content)),
})
}
Value::Object(obj) => {
SynthValue::Object(obj) => {
let fields = obj
.iter()
.map(|(key, value)| (key.to_string(), Content::from(value)))
Expand All @@ -460,7 +478,7 @@ impl<'r> From<&'r Value> for Content {
..Default::default()
})
}
Value::Number(number_value) => {
SynthValue::Number(number_value) => {
let number_content = if number_value.is_f64() {
let value = number_value.as_f64().unwrap();
NumberContent::F64(number_content::F64::Range(RangeStep::new(
Expand All @@ -487,6 +505,7 @@ impl<'r> From<&'r Value> for Content {
};
Content::Number(number_content)
}
_ => { todo!() }
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions core/src/schema/content/one_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::hash::{Hash, Hasher};
use super::prelude::*;

use super::Weight;
use crate::graph::Value as SynthValue;

#[derive(Debug, Default, Serialize, Deserialize, Clone)]
#[serde(deny_unknown_fields)]
Expand Down Expand Up @@ -71,8 +72,8 @@ impl FromIterator<Content> for OneOfContent {
}
}

impl<'t> FromIterator<&'t Value> for OneOfContent {
fn from_iter<T: IntoIterator<Item = &'t Value>>(iter: T) -> Self {
impl<'t> FromIterator<&'t SynthValue> for OneOfContent {
fn from_iter<T: IntoIterator<Item = &'t SynthValue>>(iter: T) -> Self {
let mut out = Self {
variants: Vec::new(),
};
Expand Down Expand Up @@ -118,9 +119,9 @@ impl OneOfContent {
self.variants.push(VariantContent::new(variant))
}

pub fn insert_with<M>(&mut self, strategy: M, what: &Value)
pub fn insert_with<M>(&mut self, strategy: M, what: &SynthValue)
where
M: MergeStrategy<Self, Value> + MergeStrategy<Content, Value> + Copy,
M: MergeStrategy<Self, SynthValue> + MergeStrategy<Content, SynthValue> + Copy,
{
let res: Vec<_> = self
.iter_mut()
Expand Down
56 changes: 28 additions & 28 deletions core/src/schema/inference/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use serde_json::{Map, Number, Value};

use std::collections::HashSet;
use std::fmt::Display;
use std::collections::BTreeMap;

pub mod value;
pub use value::ValueMergeStrategy;
Expand All @@ -31,8 +32,9 @@ impl std::fmt::Display for OptionalMergeStrategy {
}
}

impl MergeStrategy<Content, Value> for OptionalMergeStrategy {
fn try_merge(self, master: &mut Content, candidate: &Value) -> Result<()> {
use crate::graph::Value as SynthValue;
impl MergeStrategy<Content, SynthValue> for OptionalMergeStrategy {
fn try_merge(self, master: &mut Content, candidate: &SynthValue) -> Result<()> {
match (master, candidate) {
// Logical nodes go first
(Content::SameAs(_), _) => {
Expand All @@ -46,46 +48,46 @@ impl MergeStrategy<Content, Value> for OptionalMergeStrategy {
Self.try_merge(unique_content, candidate)
}
// Non-logical nodes go after
(Content::Object(master_obj), Value::Object(candidate_obj)) => {
(Content::Object(master_obj), SynthValue::Object(candidate_obj)) => {
Self.try_merge(master_obj, candidate_obj)
}
(Content::Array(ArrayContent { content, length }), Value::Array(values)) => {
Self.try_merge(length.as_mut(), &Value::from(values.len()))?;
(Content::Array(ArrayContent { content, length }), SynthValue::Array(values)) => {
Self.try_merge(length.as_mut(), &SynthValue::from(values.len()))?;
values
.iter()
.try_for_each(|value| Self.try_merge(content.as_mut(), value))
}
(Content::String(string_content), Value::String(string)) => {
(Content::String(string_content), SynthValue::String(string)) => {
Self.try_merge(string_content, string)
}
(Content::DateTime(date_time_content), Value::String(string)) => {
(Content::DateTime(date_time_content), SynthValue::String(string)) => {
Self.try_merge(date_time_content, string)
}
(Content::Number(number_content), Value::Number(number)) => {
(Content::Number(number_content), SynthValue::Number(number)) => {
Self.try_merge(number_content, number)
}
(Content::Bool(bool_content), Value::Bool(boolean)) => {
(Content::Bool(bool_content), SynthValue::Bool(boolean)) => {
Self.try_merge(bool_content, boolean)
}
(Content::Null(_), Value::Null) => Ok(()),
(Content::Null(_), SynthValue::Null(_)) => Ok(()),
(master, candidate) => Err(failed!(
target: Release,
"cannot merge a node of type '{}' with a value of type '{}'",
master.kind(),
candidate.kind()
candidate.type_()
)),
}
}
}

impl MergeStrategy<UniqueContent, Value> for OptionalMergeStrategy {
fn try_merge(self, master: &mut UniqueContent, candidate: &Value) -> Result<()> {
impl MergeStrategy<UniqueContent, SynthValue> for OptionalMergeStrategy {
fn try_merge(self, master: &mut UniqueContent, candidate: &SynthValue) -> Result<()> {
Self.try_merge(&mut *master.content, candidate)
}
}

impl MergeStrategy<OneOfContent, Value> for OptionalMergeStrategy {
fn try_merge(self, master: &mut OneOfContent, candidate: &Value) -> Result<()> {
impl MergeStrategy<OneOfContent, SynthValue> for OptionalMergeStrategy {
fn try_merge(self, master: &mut OneOfContent, candidate: &SynthValue) -> Result<()> {
master.insert_with(self, candidate);
Ok(())
}
Expand Down Expand Up @@ -132,11 +134,11 @@ impl MergeStrategy<StringContent, String> for OptionalMergeStrategy {
}
}

impl MergeStrategy<ObjectContent, Map<String, Value>> for OptionalMergeStrategy {
impl MergeStrategy<ObjectContent, BTreeMap<String, SynthValue>> for OptionalMergeStrategy {
fn try_merge(
self,
master: &mut ObjectContent,
candidate_obj: &serde_json::Map<String, Value>,
candidate_obj: &BTreeMap<String, SynthValue>,
) -> Result<()> {
let master_keys: HashSet<_> = master
.iter()
Expand Down Expand Up @@ -321,32 +323,30 @@ impl MergeStrategy<number_content::F32, f32> for OptionalMergeStrategy {
}
}

impl MergeStrategy<NumberContent, Number> for OptionalMergeStrategy {
fn try_merge(self, master: &mut NumberContent, value: &Number) -> Result<()> {
use crate::graph::prelude::Number as SynthNumber;
impl MergeStrategy<NumberContent, SynthNumber> for OptionalMergeStrategy {
fn try_merge(self, master: &mut NumberContent, value: &SynthNumber) -> Result<()> {
match master {
NumberContent::U64(u64_content) => {
if let Some(n) = value.as_u64() {
self.try_merge(u64_content, &n)
} else {
*master = u64_content.clone().upcast(value.kind())?;
self.try_merge(master, value)
}
todo!()
}
}
NumberContent::I64(i64_content) => {
if let Some(n) = value.as_i64() {
self.try_merge(i64_content, &n)
} else {
*master = i64_content.clone().upcast(value.kind())?;
self.try_merge(master, value)
}
todo!()
}
}
NumberContent::F64(f64_content) => {
if let Some(n) = value.as_f64() {
self.try_merge(f64_content, &n)
} else {
*master = f64_content.clone().upcast(value.kind())?;
self.try_merge(master, value)
}
todo!()
}
}
NumberContent::U32(u32_content) => {
if let Some(n) = value.as_u64() {
Expand Down
7 changes: 4 additions & 3 deletions core/src/schema/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl FromIterator<(Name, Content)> for Namespace {
}
}

use crate::graph::Value as SynthValue;
impl Namespace {
pub fn new() -> Self {
Self::default()
Expand All @@ -63,15 +64,15 @@ impl Namespace {
self.collections.keys()
}

pub fn default_try_update(&mut self, name: &Name, value: &Value) -> Result<()> {
pub fn default_try_update(&mut self, name: &Name, value: &SynthValue) -> Result<()> {
self.try_update(OptionalMergeStrategy, name, value)
}

pub fn try_update<M: MergeStrategy<Content, Value>>(
pub fn try_update<M: MergeStrategy<Content, SynthValue>>(
&mut self,
strategy: M,
name: &Name,
value: &Value,
value: &SynthValue,
) -> Result<()> {
let collection = self.get_collection_mut(name)?;
strategy.try_merge(collection, value)?;
Expand Down
8 changes: 8 additions & 0 deletions gen/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ macro_rules! generate_enum {
}
}
)*

impl $id {
pub fn type_(&self) -> &'static str {
match self {
$( Self::$variant(_) => stringify!($variant), )*
}
}
}
};
}

Expand Down