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

How to iterate all the properties path? #366

Open
tisonkun opened this issue Jan 18, 2025 · 0 comments
Open

How to iterate all the properties path? #366

tisonkun opened this issue Jan 18, 2025 · 0 comments

Comments

@tisonkun
Copy link

tisonkun commented Jan 18, 2025

The new Transform doesn't seem walk through all defs properly, that I have to iterate all defs manually with:

#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
struct OptionEntry {
    path: String,
    env: String,
    ty: String,
}

    fn dump_config_schema(
        prefix: &str,
        defs: &Map<String, Value>,
        o: &Map<String, Value>,
        result: &mut BTreeMap<String, OptionEntry>,
    ) {
        if let Some(r) = o.get("$ref") {
            let r = r.as_str().unwrap();
            let o = defs.get(&r[8..]).unwrap().as_object().unwrap();
            return dump_config_schema(prefix, defs, o, result);
        }

        if let Some(one_of) = o.get("oneOf") {
            let one_of = one_of.as_array().unwrap();
            for o in one_of {
                dump_config_schema(&prefix, defs, o.as_object().unwrap(), result);
            }
            return;
        }

        if let Some(any_of) = o.get("anyOf") {
            let any_of = any_of.as_array().unwrap();
            for o in any_of {
                dump_config_schema(&prefix, defs, o.as_object().unwrap(), result);
            }
            return;
        }

        let ty = o.get("type").unwrap();
        let types = if let Some(types) = ty.as_array() {
            types.clone()
        } else {
            vec![ty.clone()]
        };

        for ty in types {
            let ty = ty.as_str().unwrap();
            match ty {
                "null" => {}
                "object" => {
                    let props = o.get("properties").unwrap().as_object().unwrap();
                    for (k, v) in props {
                        let prefix = format!("{prefix}.{k}");
                        dump_config_schema(&prefix, defs, v.as_object().unwrap(), result);
                    }
                }
                ty => {
                    let path = &prefix["SCOPEDB_CONFIG.".len()..];
                    let prefix = prefix.to_ascii_uppercase().replace(".", "_");
                    match result.entry(prefix.to_string()) {
                        Entry::Vacant(ent) => {
                            ent.insert(OptionEntry {
                                path: path.to_string(),
                                env: prefix,
                                ty: ty.to_string(),
                            });
                        }
                        Entry::Occupied(ent) => {
                            assert_eq!(ent.get().ty, ty);
                        }
                    }
                }
            }
        }
    }

And it generates:

runtime.exec_runtime_threads, SCOPEDB_CONFIG_RUNTIME_EXEC_RUNTIME_THREADS, integers
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant