Skip to content

Commit

Permalink
fix: manifests yaml stream options impl (#1724)
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy authored Oct 29, 2024
1 parent 8302dcf commit 68a5dac
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 25 deletions.
2 changes: 1 addition & 1 deletion kclvm/runtime/src/manifests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub extern "C" fn kclvm_manifests_yaml_stream(
.as_bool(),
ignore_private: opts
.get_by_key("ignore_private")
.unwrap_or_else(|| ValueRef::bool(false))
.unwrap_or_else(|| ValueRef::bool(true))
.as_bool(),
ignore_none: opts
.get_by_key("ignore_none")
Expand Down
6 changes: 6 additions & 0 deletions kclvm/runtime/src/manifests/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub(crate) fn encode_yaml_stream_to_manifests(
values: &ValueRef,
opts: YamlEncodeOptions,
) {
// Update custom plan manifests output.
ctx.buffer.custom_manifests_output = Some(
values
.as_list_ref()
Expand All @@ -16,6 +17,11 @@ pub(crate) fn encode_yaml_stream_to_manifests(
.collect::<Vec<String>>()
.join(&format!("\n{}\n", opts.sep)),
);
// Update plan options.
ctx.plan_opts.disable_none = opts.ignore_none;
ctx.plan_opts.sort_keys = opts.sort_keys;
ctx.plan_opts.show_hidden = !opts.ignore_private;
ctx.plan_opts.sep = Some(opts.sep.clone())
}

#[cfg(test)]
Expand Down
34 changes: 17 additions & 17 deletions kclvm/runtime/src/value/val_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,9 @@ impl ValueRef {
writer.to_str().unwrap().to_string()
}

pub fn to_json_string_with_options(&self, opt: &JsonEncodeOptions) -> String {
let json = self.build_json(opt);
let formatter = JsonFormatter::with_indent(opt.indent);
pub fn to_json_string_with_options(&self, opts: &JsonEncodeOptions) -> String {
let json = self.build_json(opts);
let formatter = JsonFormatter::with_indent(opts.indent);
let mut writer = Vec::with_capacity(128);
let mut serializer = serde_json::Serializer::with_formatter(&mut writer, formatter);
json.serialize(&mut serializer).unwrap();
Expand All @@ -444,7 +444,7 @@ impl ValueRef {
writer.to_str().unwrap().to_string()
}

pub(crate) fn build_json(&self, opt: &JsonEncodeOptions) -> JsonValue {
pub(crate) fn build_json(&self, opts: &JsonEncodeOptions) -> JsonValue {
match &*self.rc.borrow() {
crate::Value::undefined => JsonValue::Null,
crate::Value::none => JsonValue::Null,
Expand All @@ -471,15 +471,15 @@ impl ValueRef {
continue;
}
crate::Value::none => {
if !opt.ignore_none {
val_array.push(x.build_json(opt));
if !opts.ignore_none {
val_array.push(x.build_json(opts));
}
}
crate::Value::func_value(_) => {
// ignore func
}
_ => {
val_array.push(x.build_json(opt));
val_array.push(x.build_json(opts));
}
}
}
Expand All @@ -488,27 +488,27 @@ impl ValueRef {
crate::Value::dict_value(ref v) => {
let mut val_map = IndexMap::new();
let mut vals = v.values.clone();
if opt.sort_keys {
if opts.sort_keys {
vals.sort_keys();
}
for (key, val) in vals.iter() {
if opt.ignore_private && (*key).starts_with(KCL_PRIVATE_VAR_PREFIX) {
if opts.ignore_private && (*key).starts_with(KCL_PRIVATE_VAR_PREFIX) {
continue;
}
match *val.rc.borrow() {
crate::Value::undefined => {
continue;
}
crate::Value::none => {
if !opt.ignore_none {
val_map.insert(key.clone(), val.build_json(opt));
if !opts.ignore_none {
val_map.insert(key.clone(), val.build_json(opts));
}
}
crate::Value::func_value(_) => {
// ignore func
}
_ => {
val_map.insert(key.clone(), val.build_json(opt));
val_map.insert(key.clone(), val.build_json(opts));
}
}
}
Expand All @@ -518,27 +518,27 @@ impl ValueRef {
crate::Value::schema_value(ref v) => {
let mut val_map = IndexMap::new();
let mut vals = v.config.values.clone();
if opt.sort_keys {
if opts.sort_keys {
vals.sort_keys();
}
for (key, val) in vals.iter() {
if opt.ignore_private && (*key).starts_with(KCL_PRIVATE_VAR_PREFIX) {
if opts.ignore_private && (*key).starts_with(KCL_PRIVATE_VAR_PREFIX) {
continue;
}
match *val.rc.borrow() {
crate::Value::undefined => {
continue;
}
crate::Value::none => {
if !opt.ignore_none {
val_map.insert(key.clone(), val.build_json(opt));
if !opts.ignore_none {
val_map.insert(key.clone(), val.build_json(opts));
}
}
crate::Value::func_value(_) => {
// ignore func
}
_ => {
val_map.insert(key.clone(), val.build_json(opt));
val_map.insert(key.clone(), val.build_json(opts));
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion kclvm/runtime/src/value/val_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub struct PlanOptions {
pub disable_empty_list: bool,
/// Filter planned value with the path selector.
pub query_paths: Vec<String>,
/// YAML plan separator string, default is `---`.
pub sep: Option<String>,
}

/// Filter list or config results with context options.
Expand Down Expand Up @@ -220,6 +222,11 @@ impl ValueRef {
};
if value.is_list_or_config() {
let results = filter_results(ctx, &value);
let sep = ctx
.plan_opts
.sep
.clone()
.unwrap_or_else(|| "---".to_string());
// Plan YAML result
let yaml_result = results
.iter()
Expand All @@ -230,7 +237,7 @@ impl ValueRef {
.to_string()
})
.collect::<Vec<String>>()
.join(YAML_STREAM_SEP);
.join(&format!("\n{}\n", sep));
// Plan JSON result
let json_result = results
.iter()
Expand Down
12 changes: 6 additions & 6 deletions kclvm/runtime/src/value/val_yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,16 @@ impl ValueRef {
}
}

pub fn to_yaml_string_with_options(&self, opt: &YamlEncodeOptions) -> String {
pub fn to_yaml_string_with_options(&self, opts: &YamlEncodeOptions) -> String {
// convert Value to json in order to reuse
// "crate::val_json::JsonValue" to customize the serialized results
let json_opt = JsonEncodeOptions {
sort_keys: opt.sort_keys,
let json_opts = JsonEncodeOptions {
sort_keys: opts.sort_keys,
indent: 0,
ignore_private: opt.ignore_private,
ignore_none: opt.ignore_none,
ignore_private: opts.ignore_private,
ignore_none: opts.ignore_none,
};
let json = self.to_json_string_with_options(&json_opt);
let json = self.to_json_string_with_options(&json_opts);
let yaml_value: serde_yaml::Value = serde_json::from_str(json.as_ref()).unwrap();
match serde_yaml::to_string(&yaml_value) {
Ok(s) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import manifests

schema XYZ:
[str]: str

xyz = {
"_foo": "bar"
"foo": "bar"
xyz: XYZ {
_bla: "bla"
}
a = None
}

manifests.yaml_stream([xyz], opts={
ignore_private = False
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
_foo: bar
foo: bar
xyz:
_bla: bla
a: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import manifests

schema XYZ:
[str]: str

xyz = {
"_foo": "bar"
"foo": "bar"
xyz: XYZ {
_bla: "bla"
}
a = None
}

manifests.yaml_stream([xyz], opts={
ignore_private = True
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo: bar
xyz: {}
a: null
13 changes: 13 additions & 0 deletions test/grammar/builtins/manifests/yaml_stream/config_sep/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import manifests

schema Person:
name: str = "kcl"
age: int = 1

x0 = Person {}
x1 = Person {
age = 101
}
manifests.yaml_stream([x0, x1], {
sep = "\n---\n"
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: kcl
age: 1

---

name: kcl
age: 101

0 comments on commit 68a5dac

Please sign in to comment.