avrotize 2.21.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (171) hide show
  1. avrotize/__init__.py +66 -0
  2. avrotize/__main__.py +6 -0
  3. avrotize/_version.py +34 -0
  4. avrotize/asn1toavro.py +160 -0
  5. avrotize/avrotize.py +152 -0
  6. avrotize/avrotocpp/CMakeLists.txt.jinja +77 -0
  7. avrotize/avrotocpp/build.bat.jinja +7 -0
  8. avrotize/avrotocpp/build.sh.jinja +7 -0
  9. avrotize/avrotocpp/dataclass_body.jinja +108 -0
  10. avrotize/avrotocpp/vcpkg.json.jinja +21 -0
  11. avrotize/avrotocpp.py +483 -0
  12. avrotize/avrotocsharp/README.md.jinja +166 -0
  13. avrotize/avrotocsharp/class_test.cs.jinja +266 -0
  14. avrotize/avrotocsharp/dataclass_core.jinja +293 -0
  15. avrotize/avrotocsharp/enum_test.cs.jinja +20 -0
  16. avrotize/avrotocsharp/project.csproj.jinja +30 -0
  17. avrotize/avrotocsharp/project.sln.jinja +34 -0
  18. avrotize/avrotocsharp/run_coverage.ps1.jinja +98 -0
  19. avrotize/avrotocsharp/run_coverage.sh.jinja +149 -0
  20. avrotize/avrotocsharp/testproject.csproj.jinja +19 -0
  21. avrotize/avrotocsharp.py +1180 -0
  22. avrotize/avrotocsv.py +121 -0
  23. avrotize/avrotodatapackage.py +173 -0
  24. avrotize/avrotodb.py +1383 -0
  25. avrotize/avrotogo/go_enum.jinja +12 -0
  26. avrotize/avrotogo/go_helpers.jinja +31 -0
  27. avrotize/avrotogo/go_struct.jinja +151 -0
  28. avrotize/avrotogo/go_test.jinja +47 -0
  29. avrotize/avrotogo/go_union.jinja +38 -0
  30. avrotize/avrotogo.py +476 -0
  31. avrotize/avrotographql.py +197 -0
  32. avrotize/avrotoiceberg.py +210 -0
  33. avrotize/avrotojava/class_test.java.jinja +212 -0
  34. avrotize/avrotojava/enum_test.java.jinja +21 -0
  35. avrotize/avrotojava/testproject.pom.jinja +54 -0
  36. avrotize/avrotojava.py +2156 -0
  37. avrotize/avrotojs.py +250 -0
  38. avrotize/avrotojsons.py +481 -0
  39. avrotize/avrotojstruct.py +345 -0
  40. avrotize/avrotokusto.py +364 -0
  41. avrotize/avrotomd/README.md.jinja +49 -0
  42. avrotize/avrotomd.py +137 -0
  43. avrotize/avrotools.py +168 -0
  44. avrotize/avrotoparquet.py +208 -0
  45. avrotize/avrotoproto.py +359 -0
  46. avrotize/avrotopython/dataclass_core.jinja +241 -0
  47. avrotize/avrotopython/enum_core.jinja +87 -0
  48. avrotize/avrotopython/pyproject_toml.jinja +18 -0
  49. avrotize/avrotopython/test_class.jinja +97 -0
  50. avrotize/avrotopython/test_enum.jinja +23 -0
  51. avrotize/avrotopython.py +626 -0
  52. avrotize/avrotorust/dataclass_enum.rs.jinja +74 -0
  53. avrotize/avrotorust/dataclass_struct.rs.jinja +204 -0
  54. avrotize/avrotorust/dataclass_union.rs.jinja +105 -0
  55. avrotize/avrotorust.py +435 -0
  56. avrotize/avrotots/class_core.ts.jinja +140 -0
  57. avrotize/avrotots/class_test.ts.jinja +77 -0
  58. avrotize/avrotots/enum_core.ts.jinja +46 -0
  59. avrotize/avrotots/gitignore.jinja +34 -0
  60. avrotize/avrotots/index.ts.jinja +0 -0
  61. avrotize/avrotots/package.json.jinja +23 -0
  62. avrotize/avrotots/tsconfig.json.jinja +21 -0
  63. avrotize/avrotots.py +687 -0
  64. avrotize/avrotoxsd.py +344 -0
  65. avrotize/cddltostructure.py +1841 -0
  66. avrotize/commands.json +3496 -0
  67. avrotize/common.py +834 -0
  68. avrotize/constants.py +87 -0
  69. avrotize/csvtoavro.py +132 -0
  70. avrotize/datapackagetoavro.py +76 -0
  71. avrotize/dependencies/cpp/vcpkg/vcpkg.json +19 -0
  72. avrotize/dependencies/cs/net90/dependencies.csproj +29 -0
  73. avrotize/dependencies/go/go121/go.mod +6 -0
  74. avrotize/dependencies/java/jdk21/pom.xml +91 -0
  75. avrotize/dependencies/python/py312/requirements.txt +13 -0
  76. avrotize/dependencies/rust/stable/Cargo.toml +17 -0
  77. avrotize/dependencies/typescript/node22/package.json +16 -0
  78. avrotize/dependency_resolver.py +348 -0
  79. avrotize/dependency_version.py +432 -0
  80. avrotize/generic/generic.avsc +57 -0
  81. avrotize/jsonstoavro.py +2167 -0
  82. avrotize/jsonstostructure.py +2864 -0
  83. avrotize/jstructtoavro.py +878 -0
  84. avrotize/kstructtoavro.py +93 -0
  85. avrotize/kustotoavro.py +455 -0
  86. avrotize/openapitostructure.py +717 -0
  87. avrotize/parquettoavro.py +157 -0
  88. avrotize/proto2parser.py +498 -0
  89. avrotize/proto3parser.py +403 -0
  90. avrotize/prototoavro.py +382 -0
  91. avrotize/prototypes/any.avsc +19 -0
  92. avrotize/prototypes/api.avsc +106 -0
  93. avrotize/prototypes/duration.avsc +20 -0
  94. avrotize/prototypes/field_mask.avsc +18 -0
  95. avrotize/prototypes/struct.avsc +60 -0
  96. avrotize/prototypes/timestamp.avsc +20 -0
  97. avrotize/prototypes/type.avsc +253 -0
  98. avrotize/prototypes/wrappers.avsc +117 -0
  99. avrotize/structuretocddl.py +597 -0
  100. avrotize/structuretocpp/CMakeLists.txt.jinja +76 -0
  101. avrotize/structuretocpp/build.bat.jinja +3 -0
  102. avrotize/structuretocpp/build.sh.jinja +3 -0
  103. avrotize/structuretocpp/dataclass_body.jinja +50 -0
  104. avrotize/structuretocpp/vcpkg.json.jinja +11 -0
  105. avrotize/structuretocpp.py +697 -0
  106. avrotize/structuretocsharp/class_test.cs.jinja +180 -0
  107. avrotize/structuretocsharp/dataclass_core.jinja +156 -0
  108. avrotize/structuretocsharp/enum_test.cs.jinja +36 -0
  109. avrotize/structuretocsharp/json_structure_converters.cs.jinja +399 -0
  110. avrotize/structuretocsharp/program.cs.jinja +49 -0
  111. avrotize/structuretocsharp/project.csproj.jinja +17 -0
  112. avrotize/structuretocsharp/project.sln.jinja +34 -0
  113. avrotize/structuretocsharp/testproject.csproj.jinja +18 -0
  114. avrotize/structuretocsharp/tuple_converter.cs.jinja +121 -0
  115. avrotize/structuretocsharp.py +2295 -0
  116. avrotize/structuretocsv.py +365 -0
  117. avrotize/structuretodatapackage.py +659 -0
  118. avrotize/structuretodb.py +1125 -0
  119. avrotize/structuretogo/go_enum.jinja +12 -0
  120. avrotize/structuretogo/go_helpers.jinja +26 -0
  121. avrotize/structuretogo/go_interface.jinja +18 -0
  122. avrotize/structuretogo/go_struct.jinja +187 -0
  123. avrotize/structuretogo/go_test.jinja +70 -0
  124. avrotize/structuretogo.py +729 -0
  125. avrotize/structuretographql.py +502 -0
  126. avrotize/structuretoiceberg.py +355 -0
  127. avrotize/structuretojava/choice_core.jinja +34 -0
  128. avrotize/structuretojava/class_core.jinja +23 -0
  129. avrotize/structuretojava/enum_core.jinja +18 -0
  130. avrotize/structuretojava/equals_hashcode.jinja +30 -0
  131. avrotize/structuretojava/pom.xml.jinja +26 -0
  132. avrotize/structuretojava/tuple_core.jinja +49 -0
  133. avrotize/structuretojava.py +938 -0
  134. avrotize/structuretojs/class_core.js.jinja +33 -0
  135. avrotize/structuretojs/enum_core.js.jinja +10 -0
  136. avrotize/structuretojs/package.json.jinja +12 -0
  137. avrotize/structuretojs/test_class.js.jinja +84 -0
  138. avrotize/structuretojs/test_enum.js.jinja +58 -0
  139. avrotize/structuretojs/test_runner.js.jinja +45 -0
  140. avrotize/structuretojs.py +657 -0
  141. avrotize/structuretojsons.py +498 -0
  142. avrotize/structuretokusto.py +639 -0
  143. avrotize/structuretomd/README.md.jinja +204 -0
  144. avrotize/structuretomd.py +322 -0
  145. avrotize/structuretoproto.py +764 -0
  146. avrotize/structuretopython/dataclass_core.jinja +363 -0
  147. avrotize/structuretopython/enum_core.jinja +45 -0
  148. avrotize/structuretopython/map_alias.jinja +21 -0
  149. avrotize/structuretopython/pyproject_toml.jinja +23 -0
  150. avrotize/structuretopython/test_class.jinja +103 -0
  151. avrotize/structuretopython/test_enum.jinja +34 -0
  152. avrotize/structuretopython.py +799 -0
  153. avrotize/structuretorust/dataclass_enum.rs.jinja +63 -0
  154. avrotize/structuretorust/dataclass_struct.rs.jinja +121 -0
  155. avrotize/structuretorust/dataclass_union.rs.jinja +81 -0
  156. avrotize/structuretorust.py +714 -0
  157. avrotize/structuretots/class_core.ts.jinja +78 -0
  158. avrotize/structuretots/enum_core.ts.jinja +6 -0
  159. avrotize/structuretots/gitignore.jinja +8 -0
  160. avrotize/structuretots/index.ts.jinja +1 -0
  161. avrotize/structuretots/package.json.jinja +39 -0
  162. avrotize/structuretots/test_class.ts.jinja +35 -0
  163. avrotize/structuretots/tsconfig.json.jinja +21 -0
  164. avrotize/structuretots.py +740 -0
  165. avrotize/structuretoxsd.py +679 -0
  166. avrotize/xsdtoavro.py +413 -0
  167. avrotize-2.21.1.dist-info/METADATA +1319 -0
  168. avrotize-2.21.1.dist-info/RECORD +171 -0
  169. avrotize-2.21.1.dist-info/WHEEL +4 -0
  170. avrotize-2.21.1.dist-info/entry_points.txt +3 -0
  171. avrotize-2.21.1.dist-info/licenses/LICENSE +201 -0
@@ -0,0 +1,74 @@
1
+ {%- if avro_annotation %}
2
+ use lazy_static::lazy_static;
3
+ use apache_avro::Schema;
4
+ use apache_avro::{Reader, Writer};
5
+ {%- endif %}
6
+ {%- if serde_annotation %}
7
+ use serde::{self,Serialize, Deserialize};
8
+ {%- endif %}
9
+
10
+ #[derive(Debug{%- if serde_annotation %}, Serialize, Deserialize{%- endif %}, PartialEq, Clone, Default)]
11
+ pub enum {{ enum_name }} {
12
+ #[default]
13
+ {%- for symbol in symbols %}
14
+ {{ symbol }},
15
+ {%- endfor %}
16
+ }
17
+
18
+ impl {{ enum_name }} {
19
+ {%- if avro_annotation %}
20
+ lazy_static! {
21
+ /// The static Avro schema as a parsed object
22
+ pub static ref SCHEMA: Schema = Schema::parse_str(r#"{{ avro_schema | escape }}"#).unwrap();
23
+ }
24
+ {%- endif %}
25
+ }
26
+
27
+ {%- if serde_annotation or avro_annotation %}
28
+ impl {{ enum_name }} {
29
+ pub fn is_json_match(node: &serde_json::Value) -> bool {
30
+ return node.is_string();
31
+ }
32
+
33
+ /// Returns the struct instance itself
34
+ pub fn to_object(&self) -> &Self {
35
+ return self;
36
+ }
37
+ }
38
+ {%- endif %}
39
+
40
+ #[cfg(test)]
41
+ impl {{ enum_name }} {
42
+ pub fn generate_random_instance() -> {{ enum_name }} {
43
+ let mut rng = rand::thread_rng();
44
+ match rand::Rng::gen_range(&mut rng, 0..{{ symbols | length }}) {
45
+ {%- for symbol in symbols %}
46
+ {{ loop.index0 }} => {{ enum_name }}::{{ symbol }},
47
+ {%- endfor %}
48
+ _ => panic!("Invalid random index generated"),
49
+ }
50
+ }
51
+ }
52
+
53
+ #[cfg(test)]
54
+ mod tests {
55
+ use super::*;
56
+ use rand::Rng;
57
+
58
+ #[test]
59
+ fn test_serialize_deserialize_{{ enum_name.lower() }}() {
60
+ {%- for symbol in symbols %}
61
+ let instance = {{ enum_name }}::{{ symbol }};
62
+ {%- if serde_annotation %}
63
+ let json_bytes = serde_json::to_vec(&instance).unwrap();
64
+ let deserialized_instance: {{ enum_name }} = serde_json::from_slice(&json_bytes).unwrap();
65
+ assert_eq!(instance, deserialized_instance);
66
+ {%- endif %}
67
+ {%- if avro_annotation %}
68
+ let avro_bytes = instance.to_byte_array("avro/binary").unwrap();
69
+ let deserialized_avro_instance: {{ enum_name }} = {{ enum_name }}::from_data(&avro_bytes, "avro/binary").unwrap();
70
+ assert_eq!(instance, deserialized_avro_instance);
71
+ {%- endif %}
72
+ {%- endfor %}
73
+ }
74
+ }
@@ -0,0 +1,204 @@
1
+ {%- if avro_annotation %}
2
+ use lazy_static::lazy_static;
3
+ use apache_avro::Schema;
4
+ {%- endif %}
5
+ {%- if serde_annotation or avro_annotation %}
6
+ use serde::{Serialize, Deserialize};
7
+ {%- endif %}
8
+ use std::io::Write;
9
+ use flate2::write::GzEncoder;
10
+ use flate2::read::GzDecoder;
11
+ {%- set uses_chrono = false %}
12
+ {%- set uses_uuid = false %}
13
+ {%- set uses_hashmap = false%}
14
+ {%- for field in fields %}
15
+ {%- if field.type == "NaiveDate" or field.type == "NaiveTime" or field.type == "NaiveDateTime" %}
16
+ {%- set uses_chrono = true %}
17
+ {%- endif %}
18
+ {%- if field.type == "Uuid" %}
19
+ {%- set uses_uuid = true %}
20
+ {%- endif %}
21
+ {%- if field.type.startswith("HashMap<") %}
22
+ {%- set uses_hashmap = true %}
23
+ {%- endif %}
24
+ {%- endfor %}
25
+ {%- if uses_chrono %}
26
+ use chrono::{NaiveDate, NaiveTime, NaiveDateTime};
27
+ {%- endif %}
28
+ {%- if uuid %}
29
+ use uuid::Uuid;
30
+ {%- endif %}
31
+ {%- if uses_hashmap %}
32
+ use std::collections::HashMap;
33
+ {%- endif %}
34
+
35
+
36
+ {% if doc %}
37
+ /// {{ doc }}
38
+ {%- endif %}
39
+ #[derive(Debug{%- if serde_annotation or avro_annotation %}, Serialize, Deserialize{%- endif %}, PartialEq, Clone, Default)]
40
+ pub struct {{ struct_name }} {
41
+ {%- for field in fields %}
42
+ {%- if field.serde_rename %}
43
+ #[serde(rename = "{{ field.original_name }}")]
44
+ {%- endif %}
45
+ pub {{ field.name }}: {{ field.type }},
46
+ {%- endfor %}
47
+ }
48
+
49
+ {% if avro_annotation %}
50
+ lazy_static! {
51
+ /// The static Avro schema as a parsed object
52
+ pub static ref SCHEMA: Schema = Schema::parse_str(
53
+ {{ avro_schema }}).unwrap();
54
+ }
55
+ {%- endif %}
56
+
57
+ impl {{ struct_name }} {
58
+ /// Serializes the struct to a byte array based on the provided content type
59
+ pub fn to_byte_array(&self, content_type: &str) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
60
+ let result: Vec<u8>;
61
+ let media_type = content_type.split(';').next().unwrap_or("");
62
+ {%- if serde_annotation %}
63
+ if media_type.starts_with("application/json") {
64
+ result = serde_json::to_vec(self)?;
65
+ }
66
+ else {% endif -%}
67
+ {%- if avro_annotation %}
68
+ if media_type.starts_with("avro/binary") ||
69
+ media_type.starts_with("application/vnd.apache.avro+avro") {
70
+ let value = apache_avro::to_value(self).unwrap();
71
+ result = apache_avro::to_avro_datum(&SCHEMA, value).unwrap();
72
+ }
73
+ {%- endif %}
74
+ {%- if avro_annotation or serde_annotation %}
75
+ {
76
+ {%- endif %}
77
+ return Err(format!("unsupported media type: {}", media_type).into())
78
+ {%- if avro_annotation or serde_annotation %}
79
+ }
80
+ {%- endif %}
81
+ if media_type.ends_with("+gzip") {
82
+ let mut encoder = GzEncoder::new(Vec::new(), flate2::Compression::default());
83
+ encoder.write_all(&result)?;
84
+ let gzipresult = encoder.finish()?;
85
+ return Ok(gzipresult)
86
+ } else {
87
+ return Ok(result)
88
+ }
89
+ }
90
+
91
+ /// Deserializes the struct from a byte array based on the provided content type
92
+ pub fn from_data(data: impl AsRef<[u8]>, content_type: &str) -> Result<Self, Box<dyn std::error::Error>> {
93
+ let media_type = content_type.split(';').next().unwrap_or("");
94
+ let data = if media_type.ends_with("+gzip") {
95
+ let mut decoder = GzDecoder::new(data.as_ref());
96
+ let mut decompressed_data = Vec::new();
97
+ std::io::copy(&mut decoder, &mut decompressed_data)?;
98
+ decompressed_data
99
+ } else {
100
+ data.as_ref().to_vec()
101
+ };
102
+ {%- if serde_annotation %}
103
+ if media_type.starts_with("application/json") {
104
+ let result = serde_json::from_slice(&data)?;
105
+ return Ok(result)
106
+ }
107
+ {%- endif %}
108
+ {%- if avro_annotation %}
109
+ if media_type.starts_with("avro/binary") ||
110
+ media_type.starts_with("application/vnd.apache.avro+avro") {
111
+ let value = apache_avro::from_avro_datum(&SCHEMA, &mut &data[..], None).unwrap();
112
+ return Ok(apache_avro::from_value(&value).unwrap());
113
+ }
114
+ {%- endif %}
115
+ Err(format!("unsupported media type: {}", media_type).into())
116
+ }
117
+
118
+ /// Checks if the given JSON value matches the schema of the struct
119
+ pub fn is_json_match(node: &serde_json::Value) -> bool {
120
+ {%- for predicate in json_match_predicates %}
121
+ {{ predicate }} &&
122
+ {%- endfor %}
123
+ true
124
+ }
125
+
126
+ /// Returns the struct instance itself
127
+ pub fn to_object(&self) -> &Self {
128
+ self
129
+ }
130
+ }
131
+
132
+ #[cfg(test)]
133
+ impl {{ struct_name }} {
134
+ pub fn generate_random_instance() -> {{ struct_name }} {
135
+ let mut rng = rand::thread_rng();
136
+ {{ struct_name }} {
137
+ {%- for field in fields %}
138
+ {%- if field.type.startswith("Option<") %}
139
+ {{ field.name }}: Some({{ field.random_value }}),
140
+ {%- else %}
141
+ {{ field.name }}: {{ field.random_value }},
142
+ {%- endif %}
143
+ {%- endfor %}
144
+ }
145
+ }
146
+ }
147
+
148
+ #[cfg(test)]
149
+ mod tests {
150
+ use super::*;
151
+
152
+ #[test]
153
+ fn test_create_{{ struct_name.lower() }}() {
154
+ let instance = {{struct_name}}::generate_random_instance();
155
+ {%- for field in fields %}
156
+ {%- if field.type.startswith("Option<") %}
157
+ assert!(instance.{{ field.name }}.is_some()); // Check that {{ field.name }} is not None
158
+ {%- elif field.type.startswith("Vec<") %}
159
+ {%- set field_type = 'Vec::<'+field.type[4:] %}
160
+ assert!(instance.{{ field.name }} != {{ field_type }}::default()); // Check that {{ field.name }} is not default
161
+ {%- elif field.type.startswith("std::collections::HashMap<")%}
162
+ assert!(instance.{{ field.name }}.len() >= 0); // Check that {{ field.name }} is not empty
163
+ {%- elif field.type != "bool" %}
164
+ assert!(instance.{{ field.name }} != {{ field.type }}::default()); // Check that {{ field.name }} is not default
165
+ {%- endif %}
166
+ {%- endfor %}
167
+ }
168
+
169
+ {%- if serde_annotation or avro_annotation %}
170
+ #[test]
171
+ fn test_serialize_deserialize_{{ struct_name.lower() }}() {
172
+ let instance = {{struct_name}}::generate_random_instance();
173
+ {%- if serde_annotation %}
174
+ // Test JSON serialization and deserialization
175
+ let json_bytes = instance.to_byte_array("application/json").unwrap();
176
+ let deserialized_instance: {{ struct_name }} = {{ struct_name }}::from_data(&json_bytes, "application/json").unwrap();
177
+ assert_eq!(instance, deserialized_instance);
178
+ // Test JSON serialization and deserialization with compression
179
+ let json_gzip_bytes = instance.to_byte_array("application/json+gzip").unwrap();
180
+ let deserialized_gzip_instance: {{ struct_name }} = {{ struct_name }}::from_data(&json_gzip_bytes, "application/json+gzip").unwrap();
181
+ assert_eq!(instance, deserialized_gzip_instance);
182
+ {%- endif %}
183
+ {%- if avro_annotation %}
184
+ // Test Avro serialization and deserialization
185
+ let avro_bytes = instance.to_byte_array("avro/binary").unwrap();
186
+ let deserialized_avro_instance: {{ struct_name }} = {{ struct_name }}::from_data(&avro_bytes, "avro/binary").unwrap();
187
+ assert_eq!(instance, deserialized_avro_instance);
188
+ // Test Avro serialization and deserialization with compression
189
+ let avro_gzip_bytes = instance.to_byte_array("avro/binary+gzip").unwrap();
190
+ let deserialized_avro_gzip_instance: {{ struct_name }} = {{ struct_name }}::from_data(&avro_gzip_bytes, "avro/binary+gzip").unwrap();
191
+ assert_eq!(instance, deserialized_avro_gzip_instance);
192
+ {%- endif %}
193
+ }
194
+ {%- endif %}
195
+
196
+ {%- if serde_annotation %}
197
+ #[test]
198
+ fn test_is_json_match() {
199
+ let instance = {{ struct_name }}::generate_random_instance();
200
+ let json = serde_json::to_value(&instance).unwrap();
201
+ assert!({{ struct_name }}::is_json_match(&json));
202
+ }
203
+ {%- endif %}
204
+ }
@@ -0,0 +1,105 @@
1
+ {%- if serde_annotation %}
2
+ use serde::{self, Serialize, Deserialize};
3
+ {%- endif %}
4
+
5
+ #[derive(Debug, PartialEq, Clone)]
6
+ pub enum {{ union_enum_name }} {
7
+ {%- for union_field in union_fields %}
8
+ {{ union_field.name }}({{ union_field.type -}})
9
+ {%- if not loop.last %},{% endif %}
10
+ {%- endfor %}
11
+ }
12
+
13
+ impl Default for {{ union_enum_name }} {
14
+ fn default() -> Self {
15
+ return {{ union_enum_name }}::{{ union_fields[0].name }}({{ union_fields[0].default_value }})
16
+ }
17
+ }
18
+
19
+ {% if serde_annotation or avro_annotation %}
20
+ impl Serialize for {{ union_enum_name }} {
21
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
22
+ where
23
+ S: serde::ser::Serializer
24
+ {
25
+ match self {
26
+ {%- for union_field in union_fields %}
27
+ {{ union_enum_name }}::{{ union_field.name}}(value) => value.serialize(serializer),
28
+ {%- endfor %}
29
+ }
30
+ }
31
+ }
32
+
33
+ impl<'de> Deserialize<'de> for {{ union_enum_name }} {
34
+ fn deserialize<D>(deserializer: D) -> Result<{{ union_enum_name }}, D::Error>
35
+ where
36
+ D: serde::de::Deserializer<'de>
37
+ {
38
+ let node = serde_json::Value::deserialize(deserializer)?;
39
+ {%- for union_field in union_fields %}
40
+ if {{ union_field.json_match_predicate }} {
41
+ {%- if union_field.type.startswith("Vec<") %}
42
+ {%- set field_type = 'Vec::<'+union_field.type[4:] %}
43
+ let result = {{ field_type }}::deserialize(node).unwrap();
44
+ {%- elif union_field.type.startswith("std::collections::HashMap<") %}
45
+ {%- set field_type = 'std::collections::HashMap::<'+union_field.type[26:] %}
46
+ let result = {{ field_type }}::deserialize(node).unwrap();
47
+ {%-else %}
48
+ let result = {{ union_field.type }}::deserialize(node).unwrap();
49
+ {%-endif %}
50
+ return Ok({{ union_enum_name }}::{{ union_field.name }}(result));
51
+ }
52
+ {%- endfor %}
53
+ Err(serde::de::Error::custom("No valid variant found"))
54
+ }
55
+ }
56
+
57
+ impl {{ union_enum_name }} {
58
+ pub fn is_json_match(node: &serde_json::Value) -> bool {
59
+ return{%- for predicate in json_match_predicates %}
60
+ {{ predicate }}{%if not loop.last%}||{%endif%}
61
+ {%- endfor %};
62
+ }
63
+
64
+ /// Returns the struct instance itself
65
+ pub fn to_object(&self) -> &Self {
66
+ return self;
67
+ }
68
+ }
69
+ {% endif %}
70
+
71
+ #[cfg(test)]
72
+ impl {{ union_enum_name }} {
73
+ pub fn generate_random_instance() -> {{ union_enum_name }} {
74
+ let mut rng = rand::thread_rng();
75
+ match rand::Rng::gen_range(&mut rng, 0..{{ union_fields | length }}) {
76
+ {%- for union_field in union_fields %}
77
+ {{ loop.index0 }} => {{ union_enum_name }}::{{ union_field.name }}({{ union_field.random_value }}),
78
+ {%- endfor %}
79
+ _ => panic!("Invalid random index generated"),
80
+ }
81
+ }
82
+ }
83
+
84
+ #[cfg(test)]
85
+ mod tests {
86
+ use super::*;
87
+
88
+ #[test]
89
+ fn test_serialize_deserialize_{{ union_enum_name.lower() }}() {
90
+ let mut rng = rand::thread_rng();
91
+ {%- for union_field in union_fields %}
92
+ let instance = {{ union_enum_name }}::{{ union_field.name }}({{ union_field.random_value }});
93
+ {%- if serde_annotation %}
94
+ let json_bytes = serde_json::to_vec(&instance).unwrap();
95
+ let deserialized_instance: {{ union_enum_name }} = serde_json::from_slice(&json_bytes).unwrap();
96
+ assert_eq!(instance, deserialized_instance);
97
+ {%- endif %}
98
+ {%- if avro_annotation %}
99
+ let avro_bytes = instance.to_byte_array("avro/binary").unwrap();
100
+ let deserialized_avro_instance: {{ union_enum_name }} = {{ union_enum_name }}::from_data(&avro_bytes, "avro/binary").unwrap();
101
+ assert_eq!(instance, deserialized_avro_instance);
102
+ {%- endif %}
103
+ {%- endfor %}
104
+ }
105
+ }