golem_cli/model/deploy_diff/
mod.rs

1// Copyright 2024-2025 Golem Cloud
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// NOTE: This module contains normalized entities for doing diffs before deployment.
16//       This solution is intended to be a naive and temporary one until environments
17//       and atomic deployments will be developed.
18
19pub mod api_definition;
20pub mod api_deployment;
21pub mod component;
22
23use serde::Serialize;
24
25pub trait DiffSerialize {
26    fn to_diffable_string(&self) -> anyhow::Result<String>;
27}
28
29pub trait ToYamlValueWithoutNulls {
30    fn to_yaml_value_without_nulls(&self) -> serde_yaml::Result<serde_yaml::Value>;
31}
32
33impl<T: Serialize> ToYamlValueWithoutNulls for T {
34    fn to_yaml_value_without_nulls(&self) -> serde_yaml::Result<serde_yaml::Value> {
35        Ok(yaml_value_without_nulls(serde_yaml::to_value(self)?))
36    }
37}
38
39fn yaml_value_without_nulls(value: serde_yaml::Value) -> serde_yaml::Value {
40    match value {
41        serde_yaml::Value::Mapping(mapping) => serde_yaml::Value::Mapping(
42            mapping
43                .into_iter()
44                .filter_map(|(key, value)| {
45                    if value == serde_yaml::Value::Null {
46                        None
47                    } else {
48                        Some((key, yaml_value_without_nulls(value)))
49                    }
50                })
51                .collect(),
52        ),
53        serde_yaml::Value::Sequence(sequence) => serde_yaml::Value::Sequence(
54            sequence.into_iter().map(yaml_value_without_nulls).collect(),
55        ),
56        _ => value,
57    }
58}