|
| 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