Skip to content

Commit aad8115

Browse files
authored
chore(config): begin laying out primitives for programmatically querying schema (vectordotdev#17130)
1 parent 5247972 commit aad8115

File tree

26 files changed

+1303
-162
lines changed

26 files changed

+1303
-162
lines changed

Cargo.lock

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ members = [
8787
".",
8888
"lib/codecs",
8989
"lib/dnsmsg-parser",
90+
"lib/docs-renderer",
9091
"lib/enrichment",
9192
"lib/fakedata",
9293
"lib/file-source",

lib/docs-renderer/Cargo.toml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "docs-renderer"
3+
version = "0.1.0"
4+
authors = ["Vector Contributors <[email protected]>"]
5+
edition = "2021"
6+
publish = false
7+
8+
[dependencies]
9+
anyhow = { version = "1.0.66", default-features = false, features = ["std"] }
10+
serde = { version = "1.0", default-features = false }
11+
serde_json = { version = "1.0", default-features = false, features = ["std"] }
12+
snafu = { version = "0.7.4", default-features = false }
13+
tracing = { version = "0.1.34", default-features = false }
14+
tracing-subscriber = { version = "0.3.16", default-features = false, features = ["ansi", "env-filter", "fmt", "json", "registry", "tracing-log"] }
15+
vector-config = { path = "../vector-config" }
16+
vector-config-common = { path = "../vector-config-common" }

lib/docs-renderer/src/main.rs

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
mod renderer;
2+
3+
use std::collections::HashMap;
4+
5+
use crate::renderer::SchemaRenderer;
6+
use anyhow::{Context, Result};
7+
use tracing::debug;
8+
use vector_config::schema::parser::{component::ComponentSchema, query::SchemaQuerier};
9+
use vector_config_common::constants::{self, ComponentType};
10+
11+
fn main() -> Result<()> {
12+
let querier = SchemaQuerier::from_schema("/tmp/vector-config-schema.json")
13+
.context("Failed to create querier from given schema file path.")?;
14+
15+
let base_component_types = &[
16+
ComponentType::Source,
17+
ComponentType::Transform,
18+
ComponentType::Sink,
19+
];
20+
for base_component_type in base_component_types {
21+
// Find the base component schema for the component type itself, which is analogous to
22+
// `SourceOuter`, `SinkOuter`, etc. We render the schema for that separately as it's meant
23+
// to be common across components of the same type, etc.
24+
let base_component_schema = querier
25+
.query()
26+
.with_custom_attribute_kv(
27+
constants::DOCS_META_COMPONENT_BASE_TYPE,
28+
base_component_type,
29+
)
30+
.run_single()?;
31+
32+
debug!(
33+
"Got base component schema for component type '{}'.",
34+
base_component_type.as_str()
35+
);
36+
37+
// Find all component schemas of the same component type.
38+
let maybe_component_schemas = querier
39+
.query()
40+
.with_custom_attribute_kv(constants::DOCS_META_COMPONENT_TYPE, base_component_type)
41+
.run()
42+
.into_iter()
43+
.map(ComponentSchema::try_from)
44+
.collect::<Result<Vec<_>, _>>()?;
45+
46+
debug!(
47+
"Found {} component schema(s) for component type '{}'.",
48+
maybe_component_schemas.len(),
49+
base_component_type.as_str()
50+
);
51+
52+
let mut rendered_component_schemas = HashMap::new();
53+
54+
// Render the base component schema.
55+
let base_component_schema_renderer = SchemaRenderer::new(&querier, base_component_schema);
56+
let rendered_base_component_schema =
57+
base_component_schema_renderer.render().context(format!(
58+
"Failed to render the base component schema for component type '{}'.",
59+
base_component_type.as_str()
60+
))?;
61+
rendered_component_schemas.insert(
62+
format!("base/{}", base_component_type.as_str()),
63+
rendered_base_component_schema,
64+
);
65+
66+
// Render each of the component schemas for this component type.
67+
for component_schema in maybe_component_schemas {
68+
let component_name = component_schema.component_name().to_string();
69+
let component_schema_renderer = SchemaRenderer::new(&querier, component_schema);
70+
let rendered_component_schema = component_schema_renderer.render().context(format!(
71+
"Failed to render the '{}' component schema.",
72+
component_name
73+
))?;
74+
rendered_component_schemas.insert(
75+
format!("{}s/base/{}", base_component_type.as_str(), component_name),
76+
rendered_component_schema,
77+
);
78+
}
79+
}
80+
81+
Ok(())
82+
}

0 commit comments

Comments
 (0)