1
- use std:: marker:: PhantomData ;
1
+ use std:: { cell :: RefCell , marker:: PhantomData } ;
2
2
3
3
use snafu:: Snafu ;
4
4
use toml:: Value ;
5
+ use vector_config_common:: attributes:: CustomAttribute ;
5
6
6
7
use super :: { ComponentMarker , GenerateConfig } ;
8
+ use crate :: schema:: { SchemaGenerator , SchemaObject } ;
9
+ use crate :: { schema, Configurable , ConfigurableRef , GenerateError , Metadata } ;
7
10
8
11
#[ derive( Debug , Snafu , Clone , PartialEq , Eq ) ]
9
12
pub enum ExampleError {
@@ -17,7 +20,11 @@ pub enum ExampleError {
17
20
/// Description of a component.
18
21
pub struct ComponentDescription < T : ComponentMarker + Sized > {
19
22
component_name : & ' static str ,
23
+ description : & ' static str ,
24
+ label : & ' static str ,
25
+ logical_name : & ' static str ,
20
26
example_value : fn ( ) -> Option < Value > ,
27
+ config : ConfigurableRef ,
21
28
_component_type : PhantomData < T > ,
22
29
}
23
30
@@ -33,11 +40,21 @@ where
33
40
/// type `T` and the component name. As such, if `T` is `SourceComponent`, and the name is
34
41
/// `stdin`, you would say that the component is a "source called `stdin`".
35
42
///
36
- /// The type parameter `C` must be the component's configuration type that implements `GenerateConfig`.
37
- pub const fn new < C : GenerateConfig > ( component_name : & ' static str ) -> Self {
43
+ /// The type parameter `C` must be the component's configuration type that implements
44
+ /// `Configurable` and `GenerateConfig`.
45
+ pub const fn new < C : GenerateConfig + Configurable + ' static > (
46
+ component_name : & ' static str ,
47
+ label : & ' static str ,
48
+ logical_name : & ' static str ,
49
+ description : & ' static str ,
50
+ ) -> Self {
38
51
ComponentDescription {
39
52
component_name,
53
+ description,
54
+ label,
55
+ logical_name,
40
56
example_value : || Some ( C :: generate_config ( ) ) ,
57
+ config : ConfigurableRef :: new :: < C > ( ) ,
41
58
_component_type : PhantomData ,
42
59
}
43
60
}
67
84
types. sort_unstable ( ) ;
68
85
types
69
86
}
87
+
88
+ /// Generate a schema object covering all the descriptions of this type.
89
+ pub fn generate_schemas ( gen : & RefCell < SchemaGenerator > ) -> Result < SchemaObject , GenerateError > {
90
+ let mut descriptions: Vec < _ > = inventory:: iter :: < Self > . into_iter ( ) . collect ( ) ;
91
+ descriptions. sort_unstable_by_key ( |desc| desc. component_name ) ;
92
+ let subschemas: Vec < SchemaObject > = descriptions
93
+ . into_iter ( )
94
+ . map ( |description| description. generate_schema ( gen) )
95
+ . collect :: < Result < _ , _ > > ( ) ?;
96
+ Ok ( schema:: generate_one_of_schema ( & subschemas) )
97
+ }
98
+
99
+ /// Generate a schema object for this description.
100
+ fn generate_schema (
101
+ & self ,
102
+ gen : & RefCell < SchemaGenerator > ,
103
+ ) -> Result < SchemaObject , GenerateError > {
104
+ let tag_schema = schema:: generate_internal_tagged_variant_schema ( "type" . to_string ( ) , {
105
+ let mut tag_subschema =
106
+ schema:: generate_const_string_schema ( self . component_name . to_string ( ) ) ;
107
+ let mut variant_tag_metadata = Metadata :: default ( ) ;
108
+ variant_tag_metadata. set_description ( self . description ) ;
109
+ schema:: apply_base_metadata ( & mut tag_subschema, variant_tag_metadata) ;
110
+ tag_subschema
111
+ } ) ;
112
+ let flattened_subschemas = vec ! [ tag_schema] ;
113
+
114
+ let mut field_metadata = Metadata :: default ( ) ;
115
+ field_metadata. set_transparent ( ) ;
116
+ let mut subschema =
117
+ schema:: get_or_generate_schema ( & self . config , gen, Some ( field_metadata) ) ?;
118
+
119
+ schema:: convert_to_flattened_schema ( & mut subschema, flattened_subschemas) ;
120
+
121
+ let mut variant_metadata = Metadata :: default ( ) ;
122
+ variant_metadata. set_description ( self . description ) ;
123
+ variant_metadata. add_custom_attribute ( CustomAttribute :: kv ( "docs::label" , self . label ) ) ;
124
+ variant_metadata
125
+ . add_custom_attribute ( CustomAttribute :: kv ( "logical_name" , self . logical_name ) ) ;
126
+ schema:: apply_base_metadata ( & mut subschema, variant_metadata) ;
127
+
128
+ Ok ( subschema)
129
+ }
70
130
}
0 commit comments