|
1 | 1 | mod hint_set; |
2 | 2 |
|
3 | 3 | pub use hint_set::HintSet; |
| 4 | +use std::collections::HashSet; |
4 | 5 |
|
5 | 6 | #[derive(Debug)] |
6 | 7 | pub struct Hints<'a> { |
| 8 | + enums: HintSet<'a>, |
7 | 9 | values: HintSet<'a>, |
8 | 10 | discriminator: HintSet<'a>, |
9 | 11 | } |
10 | 12 |
|
11 | 13 | impl<'a> Hints<'a> { |
12 | | - pub fn new(values: HintSet<'a>, discriminator: HintSet<'a>) -> Self { |
| 14 | + pub fn new(enums: HintSet<'a>, values: HintSet<'a>, discriminator: HintSet<'a>) -> Self { |
13 | 15 | Hints { |
| 16 | + enums, |
14 | 17 | values, |
15 | 18 | discriminator, |
16 | 19 | } |
17 | 20 | } |
18 | 21 |
|
19 | 22 | fn sub_hints(&self, key: &str) -> Self { |
20 | 23 | Self::new( |
| 24 | + self.enums.sub_hints(key), |
21 | 25 | self.values.sub_hints(key), |
22 | 26 | self.discriminator.sub_hints(key), |
23 | 27 | ) |
24 | 28 | } |
25 | 29 |
|
| 30 | + fn is_enum_active(&self) -> bool { |
| 31 | + self.enums.is_active() |
| 32 | + } |
| 33 | + |
26 | 34 | fn is_values_active(&self) -> bool { |
27 | 35 | self.values.is_active() |
28 | 36 | } |
@@ -52,6 +60,7 @@ pub enum InferredSchema { |
52 | 60 | Float64, |
53 | 61 | String, |
54 | 62 | Timestamp, |
| 63 | + Enum(HashSet<String>), |
55 | 64 | Array(Box<InferredSchema>), |
56 | 65 | Properties { |
57 | 66 | required: HashMap<String, InferredSchema>, |
@@ -85,7 +94,12 @@ impl InferredSchema { |
85 | 94 | (InferredSchema::Unknown, Value::Bool(_)) => InferredSchema::Bool, |
86 | 95 | (InferredSchema::Unknown, Value::Number(n)) => minimum_number_type(n), |
87 | 96 | (InferredSchema::Unknown, Value::String(s)) => { |
88 | | - if DateTime::parse_from_rfc3339(&s).is_ok() { |
| 97 | + if hints.is_enum_active() { |
| 98 | + let mut values = HashSet::new(); |
| 99 | + values.insert(s); |
| 100 | + |
| 101 | + InferredSchema::Enum(values) |
| 102 | + } else if DateTime::parse_from_rfc3339(&s).is_ok() { |
89 | 103 | InferredSchema::Timestamp |
90 | 104 | } else { |
91 | 105 | InferredSchema::String |
@@ -113,8 +127,6 @@ impl InferredSchema { |
113 | 127 | if let Some(Value::String(mapping_key)) = obj.remove(discriminator) { |
114 | 128 | let infer_rest = InferredSchema::Unknown.infer(Value::Object(obj), hints); |
115 | 129 |
|
116 | | - dbg!("infer rest", &discriminator, &infer_rest); |
117 | | - |
118 | 130 | let mut mapping = HashMap::new(); |
119 | 131 | mapping.insert(mapping_key.to_owned(), infer_rest); |
120 | 132 |
|
@@ -193,6 +205,11 @@ impl InferredSchema { |
193 | 205 | (InferredSchema::Timestamp, _) => InferredSchema::Any, |
194 | 206 | (InferredSchema::String, Value::String(_)) => InferredSchema::String, |
195 | 207 | (InferredSchema::String, _) => InferredSchema::Any, |
| 208 | + (InferredSchema::Enum(mut values), Value::String(s)) => { |
| 209 | + values.insert(s); |
| 210 | + InferredSchema::Enum(values) |
| 211 | + } |
| 212 | + (InferredSchema::Enum(_), _) => InferredSchema::Any, |
196 | 213 | (InferredSchema::Array(prior), Value::Array(vals)) => { |
197 | 214 | let mut sub_infer = *prior; |
198 | 215 | for (i, v) in vals.into_iter().enumerate() { |
@@ -320,6 +337,10 @@ impl InferredSchema { |
320 | 337 | nullable: false, |
321 | 338 | type_value: TypeValue::Timestamp, |
322 | 339 | }), |
| 340 | + InferredSchema::Enum(values) => Form::Enum(form::Enum { |
| 341 | + nullable: false, |
| 342 | + values, |
| 343 | + }), |
323 | 344 | InferredSchema::Array(sub_infer) => Form::Elements(form::Elements { |
324 | 345 | nullable: false, |
325 | 346 | schema: Box::new(sub_infer.into_schema()), |
|
0 commit comments