Skip to content

Commit

Permalink
Remove complex value restriction
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaeling committed Nov 6, 2024
1 parent d6b1ed6 commit 79fe1a2
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 210 deletions.
6 changes: 3 additions & 3 deletions databroker-cli/src/sdv_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ mod test {
entry_type: proto::v1::EntryType::Sensor.into(),
change_type: proto::v1::ChangeType::OnChange.into(),
description: "".into(),
value_restriction: None,
value_restrictions: None,
},
proto::v1::Metadata {
id: 2,
Expand All @@ -1271,7 +1271,7 @@ mod test {
entry_type: proto::v1::EntryType::Sensor.into(),
change_type: proto::v1::ChangeType::OnChange.into(),
description: "".into(),
value_restriction: None,
value_restrictions: None,
},
proto::v1::Metadata {
id: 3,
Expand All @@ -1280,7 +1280,7 @@ mod test {
entry_type: proto::v1::EntryType::Sensor.into(),
change_type: proto::v1::ChangeType::OnChange.into(),
description: "".into(),
value_restriction: None,
value_restrictions: None,
},
]
.to_vec();
Expand Down
115 changes: 64 additions & 51 deletions databroker/src/grpc/kuksa_val_v2/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,21 +215,23 @@ impl From<&proto::Datapoint> for broker::DataValue {
}
}

fn value_restriction_from(metadata: &broker::Metadata) -> Option<proto::ValueRestriction> {
fn value_restriction_from(metadata: &broker::Metadata) -> Option<proto::ValueRestrictions> {
match metadata.data_type {
DataType::String | DataType::StringArray => {
let allowed = match metadata.allowed.as_ref() {
Some(broker::DataValue::StringArray(vec)) => vec.clone(),
_ => Vec::new(),
Some(broker::DataValue::StringArray(vec)) => Some(vec.clone()),
_ => None,
};

if !allowed.is_empty() {
return Some(proto::ValueRestriction {
r#type: Some(proto::value_restriction::Type::String(
proto::ValueRestrictionString {
allowed_values: allowed,
},
)),
if allowed.is_some() {
return Some(proto::ValueRestrictions {
allowed: allowed.map(|v| proto::Allowed {
values: Some(proto::allowed::Values::StringValues(proto::StringArray {
values: v,
})),
}),
min: None,
max: None,
});
};
}
Expand All @@ -254,23 +256,27 @@ fn value_restriction_from(metadata: &broker::Metadata) -> Option<proto::ValueRes
let allowed = match metadata.allowed.as_ref() {
Some(allowed) => match allowed {
broker::DataValue::Int32Array(vec) => {
vec.iter().cloned().map(i64::from).collect()
Some(vec.iter().cloned().map(i64::from).collect())
}
broker::DataValue::Int64Array(vec) => vec.to_vec(),
_ => Vec::new(),
broker::DataValue::Int64Array(vec) => Some(vec.to_vec()),
_ => None,
},
_ => Vec::new(),
_ => None,
};

if min_value.is_some() | max_value.is_some() | !allowed.is_empty() {
return Some(proto::ValueRestriction {
r#type: Some(proto::value_restriction::Type::Signed(
proto::ValueRestrictionInt {
allowed_values: allowed,
min: min_value.unwrap_or(i64::MIN),
max: max_value.unwrap_or(i64::MAX),
},
)),
if min_value.is_some() | max_value.is_some() | allowed.is_some() {
return Some(proto::ValueRestrictions {
allowed: allowed.map(|v| proto::Allowed {
values: Some(proto::allowed::Values::Int64Values(proto::Int64Array {
values: v,
})),
}),
min: min_value.map(|v| proto::Value {
typed_value: { Some(proto::value::TypedValue::Int64(v)) },
}),
max: max_value.map(|v| proto::Value {
typed_value: { Some(proto::value::TypedValue::Int64(v)) },
}),
});
};
}
Expand All @@ -295,23 +301,26 @@ fn value_restriction_from(metadata: &broker::Metadata) -> Option<proto::ValueRes
let allowed = match metadata.allowed.as_ref() {
Some(allowed) => match allowed {
broker::DataValue::Uint32Array(vec) => {
vec.iter().cloned().map(u64::from).collect()
Some(vec.iter().cloned().map(u64::from).collect())
}
broker::DataValue::Uint64Array(vec) => vec.to_vec(),
_ => Vec::new(),
broker::DataValue::Uint64Array(vec) => Some(vec.to_vec()),
_ => None,
},
_ => Vec::new(),
_ => None,
};

if min_value.is_some() | max_value.is_some() | !allowed.is_empty() {
return Some(proto::ValueRestriction {
r#type: Some(proto::value_restriction::Type::Unsigned(
proto::ValueRestrictionUint {
allowed_values: allowed,
min: min_value.unwrap_or(u64::MIN),
max: max_value.unwrap_or(u64::MAX),
},
)),
if min_value.is_some() | max_value.is_some() | allowed.is_some() {
return Some(proto::ValueRestrictions {
allowed: allowed.map(|v| proto::Allowed {
values: Some(proto::allowed::Values::Uint64Values(proto::Uint64Array {
values: v,
})),
}),
min: min_value.map(|v| proto::Value {
typed_value: { Some(proto::value::TypedValue::Uint64(v)) },
}),
max: max_value.map(|v| proto::Value {
typed_value: { Some(proto::value::TypedValue::Uint64(v)) },
}),
});
};
}
Expand All @@ -329,23 +338,27 @@ fn value_restriction_from(metadata: &broker::Metadata) -> Option<proto::ValueRes
let allowed = match metadata.allowed.as_ref() {
Some(allowed) => match allowed {
broker::DataValue::FloatArray(vec) => {
vec.iter().cloned().map(f64::from).collect()
Some(vec.iter().cloned().map(f64::from).collect())
}
broker::DataValue::DoubleArray(vec) => vec.to_vec(),
_ => Vec::new(),
broker::DataValue::DoubleArray(vec) => Some(vec.to_vec()),
_ => None,
},
_ => Vec::new(),
_ => None,
};

if min_value.is_some() | max_value.is_some() | !allowed.is_empty() {
return Some(proto::ValueRestriction {
r#type: Some(proto::value_restriction::Type::FloatingPoint(
proto::ValueRestrictionFloat {
allowed_values: allowed,
min: min_value.unwrap_or(f64::MIN),
max: max_value.unwrap_or(f64::MAX),
},
)),
if min_value.is_some() | max_value.is_some() | allowed.is_some() {
return Some(proto::ValueRestrictions {
allowed: allowed.map(|v| proto::Allowed {
values: Some(proto::allowed::Values::DoubleValues(proto::DoubleArray {
values: v,
})),
}),
min: min_value.map(|v| proto::Value {
typed_value: { Some(proto::value::TypedValue::Double(v)) },
}),
max: max_value.map(|v| proto::Value {
typed_value: { Some(proto::value::TypedValue::Double(v)) },
}),
});
};
}
Expand All @@ -367,7 +380,7 @@ impl From<&broker::Metadata> for proto::Metadata {
comment: String::new(),
deprecation: String::new(),
unit: metadata.unit.clone().unwrap_or_default(),
value_restriction: value_restriction_from(metadata),
value_restrictions: value_restriction_from(metadata),
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions databroker/src/grpc/kuksa_val_v2/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2212,18 +2212,18 @@ mod tests {
let entries_size = list_response.metadata.len();
assert_eq!(entries_size, 1);

let value_restriction = Some(proto::ValueRestriction {
r#type: Some(proto::value_restriction::Type::Signed(
proto::ValueRestrictionInt {
allowed_values: Vec::new(),
min: -7,
max: 19,
},
)),
let value_restrictions = Some(proto::ValueRestrictions {
allowed: None,
min: Some(proto::Value {
typed_value: Some(proto::value::TypedValue::Int64(-7)),
}),
max: Some(proto::Value {
typed_value: Some(proto::value::TypedValue::Int64(19)),
}),
});
assert_eq!(
list_response.metadata.first().unwrap().value_restriction,
value_restriction
list_response.metadata.first().unwrap().value_restrictions,
value_restrictions
)
}
Err(_status) => panic!("failed to execute get request"),
Expand Down
114 changes: 64 additions & 50 deletions databroker/src/grpc/sdv_databroker_v1/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,21 +303,23 @@ impl From<&proto::ChangeType> for broker::ChangeType {
}
}

fn value_restriction_from(metadata: &broker::Metadata) -> Option<proto::ValueRestriction> {
fn value_restriction_from(metadata: &broker::Metadata) -> Option<proto::ValueRestrictions> {
match metadata.data_type {
broker::DataType::String | broker::DataType::StringArray => {
let allowed = match metadata.allowed.as_ref() {
Some(broker::DataValue::StringArray(vec)) => vec.clone(),
_ => Vec::new(),
Some(broker::DataValue::StringArray(vec)) => Some(vec.clone()),
_ => None,
};

if !allowed.is_empty() {
return Some(proto::ValueRestriction {
r#type: Some(proto::value_restriction::Type::String(
proto::ValueRestrictionString {
allowed_values: allowed,
},
)),
if allowed.is_some() {
return Some(proto::ValueRestrictions {
allowed: allowed.map(|v| proto::Allowed {
values: Some(proto::allowed::Values::StringValues(proto::StringArray {
values: v,
})),
}),
min: None,
max: None,
});
};
}
Expand All @@ -342,23 +344,27 @@ fn value_restriction_from(metadata: &broker::Metadata) -> Option<proto::ValueRes
let allowed = match metadata.allowed.as_ref() {
Some(allowed) => match allowed {
broker::DataValue::Int32Array(vec) => {
vec.iter().cloned().map(i64::from).collect()
Some(vec.iter().cloned().map(i64::from).collect())
}
broker::DataValue::Int64Array(vec) => vec.to_vec(),
_ => Vec::new(),
broker::DataValue::Int64Array(vec) => Some(vec.to_vec()),
_ => None,
},
_ => Vec::new(),
_ => None,
};

if min_value.is_some() | max_value.is_some() | !allowed.is_empty() {
return Some(proto::ValueRestriction {
r#type: Some(proto::value_restriction::Type::Signed(
proto::ValueRestrictionInt {
allowed_values: allowed,
min: min_value.unwrap_or(i64::MIN),
max: max_value.unwrap_or(i64::MAX),
},
)),
if min_value.is_some() | max_value.is_some() | allowed.is_some() {
return Some(proto::ValueRestrictions {
allowed: allowed.map(|v| proto::Allowed {
values: Some(proto::allowed::Values::Int64Values(proto::Int64Array {
values: v,
})),
}),
min: min_value.map(|v| proto::ValueRestriction {
typed_value: { Some(proto::value_restriction::TypedValue::Int64(v)) },
}),
max: max_value.map(|v| proto::ValueRestriction {
typed_value: { Some(proto::value_restriction::TypedValue::Int64(v)) },
}),
});
};
}
Expand All @@ -383,23 +389,27 @@ fn value_restriction_from(metadata: &broker::Metadata) -> Option<proto::ValueRes
let allowed = match metadata.allowed.as_ref() {
Some(allowed) => match allowed {
broker::DataValue::Uint32Array(vec) => {
vec.iter().cloned().map(u64::from).collect()
Some(vec.iter().cloned().map(u64::from).collect())
}
broker::DataValue::Uint64Array(vec) => vec.to_vec(),
_ => Vec::new(),
broker::DataValue::Uint64Array(vec) => Some(vec.to_vec()),
_ => None,
},
_ => Vec::new(),
_ => None,
};

if min_value.is_some() | max_value.is_some() | !allowed.is_empty() {
return Some(proto::ValueRestriction {
r#type: Some(proto::value_restriction::Type::Unsigned(
proto::ValueRestrictionUint {
allowed_values: allowed,
min: min_value.unwrap_or(u64::MIN),
max: max_value.unwrap_or(u64::MAX),
},
)),
if min_value.is_some() | max_value.is_some() | allowed.is_some() {
return Some(proto::ValueRestrictions {
allowed: allowed.map(|v| proto::Allowed {
values: Some(proto::allowed::Values::Uint64Values(proto::Uint64Array {
values: v,
})),
}),
min: min_value.map(|v| proto::ValueRestriction {
typed_value: { Some(proto::value_restriction::TypedValue::Uint64(v)) },
}),
max: max_value.map(|v| proto::ValueRestriction {
typed_value: { Some(proto::value_restriction::TypedValue::Uint64(v)) },
}),
});
};
}
Expand All @@ -420,23 +430,27 @@ fn value_restriction_from(metadata: &broker::Metadata) -> Option<proto::ValueRes
let allowed = match metadata.allowed.as_ref() {
Some(allowed) => match allowed {
broker::DataValue::FloatArray(vec) => {
vec.iter().cloned().map(f64::from).collect()
Some(vec.iter().cloned().map(f64::from).collect())
}
broker::DataValue::DoubleArray(vec) => vec.to_vec(),
_ => Vec::new(),
broker::DataValue::DoubleArray(vec) => Some(vec.to_vec()),
_ => None,
},
_ => Vec::new(),
_ => None,
};

if min_value.is_some() | max_value.is_some() | !allowed.is_empty() {
return Some(proto::ValueRestriction {
r#type: Some(proto::value_restriction::Type::FloatingPoint(
proto::ValueRestrictionFloat {
allowed_values: allowed,
min: min_value.unwrap_or(f64::MIN),
max: max_value.unwrap_or(f64::MAX),
},
)),
if min_value.is_some() | max_value.is_some() | allowed.is_some() {
return Some(proto::ValueRestrictions {
allowed: allowed.map(|v| proto::Allowed {
values: Some(proto::allowed::Values::DoubleValues(proto::DoubleArray {
values: v,
})),
}),
min: min_value.map(|v| proto::ValueRestriction {
typed_value: { Some(proto::value_restriction::TypedValue::Double(v)) },
}),
max: max_value.map(|v| proto::ValueRestriction {
typed_value: { Some(proto::value_restriction::TypedValue::Double(v)) },
}),
});
};
}
Expand All @@ -457,7 +471,7 @@ impl From<&broker::Metadata> for proto::Metadata {
data_type: proto::DataType::from(&metadata.data_type) as i32,
change_type: proto::ChangeType::Continuous as i32, // TODO: Add to metadata
description: metadata.description.to_owned(),
value_restriction: value_restriction_from(metadata),
value_restrictions: value_restriction_from(metadata),
}
}
}
Expand Down
Loading

0 comments on commit 79fe1a2

Please sign in to comment.