1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
use serde::{
de::{Deserializer, Error as SError},
ser::Serializer,
Deserialize, Serialize,
};
use serde_json::Value;
use crate::helpers::mandatory;
use crate::misc::OwningApplication;
use crate::Error;
use super::{
Category, CleanupPolicy, CompatibilityMode, EnrichmentStrategy, EventTypeAudience,
EventTypeAuthorization, EventTypeName, EventTypeOptions, EventTypeStatistics,
PartitionKeyFields, PartitionStrategy, SchemaSyntax, SchemaType,
};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EventTypeInput {
pub name: EventTypeName,
pub owning_application: OwningApplication,
pub category: Category,
pub enrichment_strategies: Vec<EnrichmentStrategy>,
pub partition_strategy: PartitionStrategy,
#[serde(skip_serializing_if = "Option::is_none")]
pub partition_key_fields: Option<PartitionKeyFields>,
pub compatibility_mode: CompatibilityMode,
pub schema: EventTypeSchemaInput,
pub cleanup_policy: CleanupPolicy,
#[serde(skip_serializing_if = "Option::is_none")]
pub default_statistic: Option<EventTypeStatistics>,
#[serde(skip_serializing_if = "Option::is_none")]
pub options: Option<EventTypeOptions>,
#[serde(skip_serializing_if = "Option::is_none")]
pub authorization: Option<EventTypeAuthorization>,
#[serde(skip_serializing_if = "Option::is_none")]
pub audience: Option<EventTypeAudience>,
}
impl EventTypeInput {
pub fn builder() -> EventTypeInputBuilder {
EventTypeInputBuilder::default()
}
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct EventTypeInputBuilder {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<EventTypeName>,
#[serde(skip_serializing_if = "Option::is_none")]
pub owning_application: Option<OwningApplication>,
#[serde(skip_serializing_if = "Option::is_none")]
pub category: Option<Category>,
#[serde(skip_serializing_if = "Option::is_none")]
pub enrichment_strategies: Option<Vec<EnrichmentStrategy>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub partition_strategy: Option<PartitionStrategy>,
#[serde(skip_serializing_if = "Option::is_none")]
pub partition_key_fields: Option<PartitionKeyFields>,
#[serde(skip_serializing_if = "Option::is_none")]
pub compatibility_mode: Option<CompatibilityMode>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(flatten)]
pub schema: Option<EventTypeSchemaInput>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cleanup_policy: Option<CleanupPolicy>,
#[serde(skip_serializing_if = "Option::is_none")]
pub default_statistic: Option<EventTypeStatistics>,
#[serde(skip_serializing_if = "Option::is_none")]
pub options: Option<EventTypeOptions>,
#[serde(skip_serializing_if = "Option::is_none")]
pub authorization: Option<EventTypeAuthorization>,
#[serde(skip_serializing_if = "Option::is_none")]
pub audience: Option<EventTypeAudience>,
}
impl EventTypeInputBuilder {
pub fn name<T: Into<EventTypeName>>(mut self, v: T) -> Self {
self.name = Some(v.into());
self
}
pub fn owning_application<T: Into<OwningApplication>>(mut self, v: T) -> Self {
self.owning_application = Some(v.into());
self
}
pub fn category<T: Into<Category>>(mut self, v: T) -> Self {
self.category = Some(v.into());
self
}
pub fn enrichment_strategy<T: Into<EnrichmentStrategy>>(mut self, v: T) -> Self {
if let Some(ref mut strategies) = self.enrichment_strategies {
strategies.push(v.into());
} else {
self.enrichment_strategies = Some(vec![v.into()]);
}
self
}
pub fn partition_strategy<T: Into<PartitionStrategy>>(mut self, v: T) -> Self {
self.partition_strategy = Some(v.into());
self
}
pub fn partition_key_fields<T: Into<PartitionKeyFields>>(mut self, v: T) -> Self {
self.partition_key_fields = Some(v.into());
self
}
pub fn compatibility_mode<T: Into<CompatibilityMode>>(mut self, v: T) -> Self {
self.compatibility_mode = Some(v.into());
self
}
pub fn schema<T: Into<EventTypeSchemaInput>>(mut self, v: T) -> Self {
self.schema = Some(v.into());
self
}
pub fn cleanup_policy<T: Into<CleanupPolicy>>(mut self, v: T) -> Self {
self.cleanup_policy = Some(v.into());
self
}
pub fn default_statistic<T: Into<EventTypeStatistics>>(mut self, v: T) -> Self {
self.default_statistic = Some(v.into());
self
}
pub fn options<T: Into<EventTypeOptions>>(mut self, v: T) -> Self {
self.options = Some(v.into());
self
}
pub fn authorization<T: Into<EventTypeAuthorization>>(mut self, v: T) -> Self {
self.authorization = Some(v.into());
self
}
pub fn audience<T: Into<EventTypeAudience>>(mut self, v: T) -> Self {
self.audience = Some(v.into());
self
}
pub fn build(self) -> Result<EventTypeInput, Error> {
let name = mandatory(self.name, "name")?;
let owning_application = mandatory(self.owning_application, "owning_application")?;
let category = mandatory(self.category, "category")?;
let enrichment_strategies = self.enrichment_strategies.unwrap_or_default();
let partition_strategy = mandatory(self.partition_strategy, "partition_strategy")?;
let partition_key_fields = self.partition_key_fields;
let compatibility_mode = mandatory(self.compatibility_mode, "compatibility_mode")?;
let schema = mandatory(self.schema, "schema")?;
let cleanup_policy = mandatory(self.cleanup_policy, "cleanup_policy")?;
let default_statistic = self.default_statistic;
let options = self.options;
let authorization = self.authorization;
let audience = self.audience;
if partition_strategy == PartitionStrategy::Hash {
if let Some(ref partition_key_fields) = partition_key_fields {
if partition_key_fields.is_empty() {
return Err(Error::new(
"'partition_key_fields' is set but must not be empty if partition strategy is 'Hash'",
));
}
} else {
return Err(Error::new(
"'partition_key_fields' must be set if partition strategy is 'Hash'",
));
}
} else {
return Err(Error::new(
"'partition_key_fields' must be none if partition strategy is not 'Hash'",
));
}
Ok(EventTypeInput {
name,
owning_application,
category,
enrichment_strategies,
partition_strategy,
partition_key_fields,
compatibility_mode,
schema,
cleanup_policy,
default_statistic,
options,
authorization,
audience,
})
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum EventTypeSchemaInput {
Json(Value),
}
impl EventTypeSchemaInput {
pub fn json_schema<T: Into<Value>>(v: T) -> Self {
Self::Json(v.into())
}
pub fn json_schema_parsed(v: &str) -> Result<Self, Error> {
let parsed = serde_json::from_str(v)?;
Ok(Self::Json(parsed))
}
pub fn schema_type(&self) -> SchemaType {
match self {
EventTypeSchemaInput::Json(_) => SchemaType::JsonSchema,
}
}
pub fn schema_syntax(&self) -> SchemaSyntax {
match self {
EventTypeSchemaInput::Json(ref syntax) => {
SchemaSyntax(serde_json::to_string(syntax).unwrap())
}
}
}
}
impl Serialize for EventTypeSchemaInput {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let wrapper = EventTypeSchemaInputSer {
schema_type: self.schema_type(),
schema: self.schema_syntax(),
};
wrapper.serialize(serializer)
}
}
impl<'de> Deserialize<'de> for EventTypeSchemaInput {
fn deserialize<D>(deserializer: D) -> Result<EventTypeSchemaInput, D::Error>
where
D: Deserializer<'de>,
{
let wrapper = EventTypeSchemaInputSer::deserialize(deserializer)?;
match wrapper.schema_type {
SchemaType::JsonSchema => {
let schema_syntax =
serde_json::from_str(wrapper.schema.as_ref()).map_err(SError::custom)?;
Ok(EventTypeSchemaInput::Json(schema_syntax))
}
}
}
}
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
struct EventTypeSchemaInputSer {
#[serde(rename = "type")]
pub schema_type: SchemaType,
pub schema: SchemaSyntax,
}
#[test]
fn a_schema_input_can_be_parsed() {
let input = EventTypeSchemaInput::json_schema_parsed(r#"{"description":"test event b","properties":{"count":{"type":"integer"}},"required":["count"]}"#).unwrap();
assert_eq!(input.schema_type(), SchemaType::JsonSchema);
}