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.
- avrotize/__init__.py +66 -0
- avrotize/__main__.py +6 -0
- avrotize/_version.py +34 -0
- avrotize/asn1toavro.py +160 -0
- avrotize/avrotize.py +152 -0
- avrotize/avrotocpp/CMakeLists.txt.jinja +77 -0
- avrotize/avrotocpp/build.bat.jinja +7 -0
- avrotize/avrotocpp/build.sh.jinja +7 -0
- avrotize/avrotocpp/dataclass_body.jinja +108 -0
- avrotize/avrotocpp/vcpkg.json.jinja +21 -0
- avrotize/avrotocpp.py +483 -0
- avrotize/avrotocsharp/README.md.jinja +166 -0
- avrotize/avrotocsharp/class_test.cs.jinja +266 -0
- avrotize/avrotocsharp/dataclass_core.jinja +293 -0
- avrotize/avrotocsharp/enum_test.cs.jinja +20 -0
- avrotize/avrotocsharp/project.csproj.jinja +30 -0
- avrotize/avrotocsharp/project.sln.jinja +34 -0
- avrotize/avrotocsharp/run_coverage.ps1.jinja +98 -0
- avrotize/avrotocsharp/run_coverage.sh.jinja +149 -0
- avrotize/avrotocsharp/testproject.csproj.jinja +19 -0
- avrotize/avrotocsharp.py +1180 -0
- avrotize/avrotocsv.py +121 -0
- avrotize/avrotodatapackage.py +173 -0
- avrotize/avrotodb.py +1383 -0
- avrotize/avrotogo/go_enum.jinja +12 -0
- avrotize/avrotogo/go_helpers.jinja +31 -0
- avrotize/avrotogo/go_struct.jinja +151 -0
- avrotize/avrotogo/go_test.jinja +47 -0
- avrotize/avrotogo/go_union.jinja +38 -0
- avrotize/avrotogo.py +476 -0
- avrotize/avrotographql.py +197 -0
- avrotize/avrotoiceberg.py +210 -0
- avrotize/avrotojava/class_test.java.jinja +212 -0
- avrotize/avrotojava/enum_test.java.jinja +21 -0
- avrotize/avrotojava/testproject.pom.jinja +54 -0
- avrotize/avrotojava.py +2156 -0
- avrotize/avrotojs.py +250 -0
- avrotize/avrotojsons.py +481 -0
- avrotize/avrotojstruct.py +345 -0
- avrotize/avrotokusto.py +364 -0
- avrotize/avrotomd/README.md.jinja +49 -0
- avrotize/avrotomd.py +137 -0
- avrotize/avrotools.py +168 -0
- avrotize/avrotoparquet.py +208 -0
- avrotize/avrotoproto.py +359 -0
- avrotize/avrotopython/dataclass_core.jinja +241 -0
- avrotize/avrotopython/enum_core.jinja +87 -0
- avrotize/avrotopython/pyproject_toml.jinja +18 -0
- avrotize/avrotopython/test_class.jinja +97 -0
- avrotize/avrotopython/test_enum.jinja +23 -0
- avrotize/avrotopython.py +626 -0
- avrotize/avrotorust/dataclass_enum.rs.jinja +74 -0
- avrotize/avrotorust/dataclass_struct.rs.jinja +204 -0
- avrotize/avrotorust/dataclass_union.rs.jinja +105 -0
- avrotize/avrotorust.py +435 -0
- avrotize/avrotots/class_core.ts.jinja +140 -0
- avrotize/avrotots/class_test.ts.jinja +77 -0
- avrotize/avrotots/enum_core.ts.jinja +46 -0
- avrotize/avrotots/gitignore.jinja +34 -0
- avrotize/avrotots/index.ts.jinja +0 -0
- avrotize/avrotots/package.json.jinja +23 -0
- avrotize/avrotots/tsconfig.json.jinja +21 -0
- avrotize/avrotots.py +687 -0
- avrotize/avrotoxsd.py +344 -0
- avrotize/cddltostructure.py +1841 -0
- avrotize/commands.json +3496 -0
- avrotize/common.py +834 -0
- avrotize/constants.py +87 -0
- avrotize/csvtoavro.py +132 -0
- avrotize/datapackagetoavro.py +76 -0
- avrotize/dependencies/cpp/vcpkg/vcpkg.json +19 -0
- avrotize/dependencies/cs/net90/dependencies.csproj +29 -0
- avrotize/dependencies/go/go121/go.mod +6 -0
- avrotize/dependencies/java/jdk21/pom.xml +91 -0
- avrotize/dependencies/python/py312/requirements.txt +13 -0
- avrotize/dependencies/rust/stable/Cargo.toml +17 -0
- avrotize/dependencies/typescript/node22/package.json +16 -0
- avrotize/dependency_resolver.py +348 -0
- avrotize/dependency_version.py +432 -0
- avrotize/generic/generic.avsc +57 -0
- avrotize/jsonstoavro.py +2167 -0
- avrotize/jsonstostructure.py +2864 -0
- avrotize/jstructtoavro.py +878 -0
- avrotize/kstructtoavro.py +93 -0
- avrotize/kustotoavro.py +455 -0
- avrotize/openapitostructure.py +717 -0
- avrotize/parquettoavro.py +157 -0
- avrotize/proto2parser.py +498 -0
- avrotize/proto3parser.py +403 -0
- avrotize/prototoavro.py +382 -0
- avrotize/prototypes/any.avsc +19 -0
- avrotize/prototypes/api.avsc +106 -0
- avrotize/prototypes/duration.avsc +20 -0
- avrotize/prototypes/field_mask.avsc +18 -0
- avrotize/prototypes/struct.avsc +60 -0
- avrotize/prototypes/timestamp.avsc +20 -0
- avrotize/prototypes/type.avsc +253 -0
- avrotize/prototypes/wrappers.avsc +117 -0
- avrotize/structuretocddl.py +597 -0
- avrotize/structuretocpp/CMakeLists.txt.jinja +76 -0
- avrotize/structuretocpp/build.bat.jinja +3 -0
- avrotize/structuretocpp/build.sh.jinja +3 -0
- avrotize/structuretocpp/dataclass_body.jinja +50 -0
- avrotize/structuretocpp/vcpkg.json.jinja +11 -0
- avrotize/structuretocpp.py +697 -0
- avrotize/structuretocsharp/class_test.cs.jinja +180 -0
- avrotize/structuretocsharp/dataclass_core.jinja +156 -0
- avrotize/structuretocsharp/enum_test.cs.jinja +36 -0
- avrotize/structuretocsharp/json_structure_converters.cs.jinja +399 -0
- avrotize/structuretocsharp/program.cs.jinja +49 -0
- avrotize/structuretocsharp/project.csproj.jinja +17 -0
- avrotize/structuretocsharp/project.sln.jinja +34 -0
- avrotize/structuretocsharp/testproject.csproj.jinja +18 -0
- avrotize/structuretocsharp/tuple_converter.cs.jinja +121 -0
- avrotize/structuretocsharp.py +2295 -0
- avrotize/structuretocsv.py +365 -0
- avrotize/structuretodatapackage.py +659 -0
- avrotize/structuretodb.py +1125 -0
- avrotize/structuretogo/go_enum.jinja +12 -0
- avrotize/structuretogo/go_helpers.jinja +26 -0
- avrotize/structuretogo/go_interface.jinja +18 -0
- avrotize/structuretogo/go_struct.jinja +187 -0
- avrotize/structuretogo/go_test.jinja +70 -0
- avrotize/structuretogo.py +729 -0
- avrotize/structuretographql.py +502 -0
- avrotize/structuretoiceberg.py +355 -0
- avrotize/structuretojava/choice_core.jinja +34 -0
- avrotize/structuretojava/class_core.jinja +23 -0
- avrotize/structuretojava/enum_core.jinja +18 -0
- avrotize/structuretojava/equals_hashcode.jinja +30 -0
- avrotize/structuretojava/pom.xml.jinja +26 -0
- avrotize/structuretojava/tuple_core.jinja +49 -0
- avrotize/structuretojava.py +938 -0
- avrotize/structuretojs/class_core.js.jinja +33 -0
- avrotize/structuretojs/enum_core.js.jinja +10 -0
- avrotize/structuretojs/package.json.jinja +12 -0
- avrotize/structuretojs/test_class.js.jinja +84 -0
- avrotize/structuretojs/test_enum.js.jinja +58 -0
- avrotize/structuretojs/test_runner.js.jinja +45 -0
- avrotize/structuretojs.py +657 -0
- avrotize/structuretojsons.py +498 -0
- avrotize/structuretokusto.py +639 -0
- avrotize/structuretomd/README.md.jinja +204 -0
- avrotize/structuretomd.py +322 -0
- avrotize/structuretoproto.py +764 -0
- avrotize/structuretopython/dataclass_core.jinja +363 -0
- avrotize/structuretopython/enum_core.jinja +45 -0
- avrotize/structuretopython/map_alias.jinja +21 -0
- avrotize/structuretopython/pyproject_toml.jinja +23 -0
- avrotize/structuretopython/test_class.jinja +103 -0
- avrotize/structuretopython/test_enum.jinja +34 -0
- avrotize/structuretopython.py +799 -0
- avrotize/structuretorust/dataclass_enum.rs.jinja +63 -0
- avrotize/structuretorust/dataclass_struct.rs.jinja +121 -0
- avrotize/structuretorust/dataclass_union.rs.jinja +81 -0
- avrotize/structuretorust.py +714 -0
- avrotize/structuretots/class_core.ts.jinja +78 -0
- avrotize/structuretots/enum_core.ts.jinja +6 -0
- avrotize/structuretots/gitignore.jinja +8 -0
- avrotize/structuretots/index.ts.jinja +1 -0
- avrotize/structuretots/package.json.jinja +39 -0
- avrotize/structuretots/test_class.ts.jinja +35 -0
- avrotize/structuretots/tsconfig.json.jinja +21 -0
- avrotize/structuretots.py +740 -0
- avrotize/structuretoxsd.py +679 -0
- avrotize/xsdtoavro.py +413 -0
- avrotize-2.21.1.dist-info/METADATA +1319 -0
- avrotize-2.21.1.dist-info/RECORD +171 -0
- avrotize-2.21.1.dist-info/WHEEL +4 -0
- avrotize-2.21.1.dist-info/entry_points.txt +3 -0
- avrotize-2.21.1.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
using System;
|
|
2
|
+
using System.Collections.Generic;
|
|
3
|
+
using NUnit.Framework;
|
|
4
|
+
|
|
5
|
+
{% if namespace %}
|
|
6
|
+
namespace {{ namespace }}
|
|
7
|
+
{
|
|
8
|
+
{% endif %}
|
|
9
|
+
{% set ind=4 if namespace else 0 %}
|
|
10
|
+
{% filter indent(width=ind, first=True) %}
|
|
11
|
+
/// <summary> Test class for {{ class_base_name }} </summary>
|
|
12
|
+
[TestFixture]
|
|
13
|
+
public class {{ test_class_name }}
|
|
14
|
+
{
|
|
15
|
+
private {{ class_base_name }} _instance;
|
|
16
|
+
|
|
17
|
+
/// <summary> Constructor </summary>
|
|
18
|
+
public {{ test_class_name }}()
|
|
19
|
+
{
|
|
20
|
+
_instance = CreateInstance();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/// <summary> Create instance of {{ class_base_name }} </summary>
|
|
24
|
+
public {{ class_base_name }} CreateInstance()
|
|
25
|
+
{
|
|
26
|
+
var instance = new {{ class_base_name }}()
|
|
27
|
+
{
|
|
28
|
+
{%- for field in fields %}
|
|
29
|
+
{%- if not field.is_const %}
|
|
30
|
+
{{ field.field_name }} = {{ field.test_value }},
|
|
31
|
+
{%- endif %}
|
|
32
|
+
{%- endfor %}
|
|
33
|
+
};
|
|
34
|
+
return instance;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/// <summary> Testing Equals and GetHashCode - positive case </summary>
|
|
38
|
+
[Test]
|
|
39
|
+
public void TestEqualsAndHashCode_PositiveCase()
|
|
40
|
+
{
|
|
41
|
+
var instance1 = CreateInstance();
|
|
42
|
+
var instance2 = CreateInstance();
|
|
43
|
+
|
|
44
|
+
// Test equality
|
|
45
|
+
Assert.That(instance1.Equals(instance2), Is.True, "Two instances with same values should be equal");
|
|
46
|
+
Assert.That(instance1 == instance2, Is.False, "Operator == should use reference equality");
|
|
47
|
+
|
|
48
|
+
// Test hash codes
|
|
49
|
+
Assert.That(instance1.GetHashCode(), Is.EqualTo(instance2.GetHashCode()),
|
|
50
|
+
"Equal instances should have equal hash codes");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/// <summary> Testing Equals and GetHashCode - negative case </summary>
|
|
54
|
+
[Test]
|
|
55
|
+
public void TestEqualsAndHashCode_NegativeCase()
|
|
56
|
+
{
|
|
57
|
+
var instance1 = CreateInstance();
|
|
58
|
+
var instance2 = CreateInstance();
|
|
59
|
+
|
|
60
|
+
{%- set test_field = fields | selectattr("is_primitive", "equalto", true) | selectattr("is_const", "equalto", false) | first %}
|
|
61
|
+
{%- if not test_field %}
|
|
62
|
+
{%- set test_field = fields | selectattr("is_const", "equalto", false) | first %}
|
|
63
|
+
{%- endif %}
|
|
64
|
+
{%- if test_field %}
|
|
65
|
+
// Modify a field to make instances different
|
|
66
|
+
{%- if 'Enum' in test_field.field_type %}
|
|
67
|
+
// For enum types, just skip the negative test as we can't easily change enum values
|
|
68
|
+
{%- elif test_field.field_type.startswith('List<') %}
|
|
69
|
+
// For List types, create a list with a different element
|
|
70
|
+
instance2.{{ test_field.field_name }} = new {{ test_field.field_type.replace('?', '') }} { {%- if 'string' in test_field.field_type %}"modified_value"{% elif 'int' in test_field.field_type %}999{% else %}default{% endif %} };
|
|
71
|
+
{%- elif test_field.field_type.startswith('Dictionary<') %}
|
|
72
|
+
// For Dictionary types, create with a different entry
|
|
73
|
+
instance2.{{ test_field.field_name }} = new {{ test_field.field_type.replace('?', '') }} { ["key"] = {%- if 'string' in test_field.field_type %}"modified_value"{% elif 'int' in test_field.field_type %}999{% else %}default{% endif %} };
|
|
74
|
+
{%- elif test_field.field_type.startswith('HashSet<') %}
|
|
75
|
+
// For HashSet types, create with a different element
|
|
76
|
+
instance2.{{ test_field.field_name }} = new {{ test_field.field_type.replace('?', '') }} { {%- if 'string' in test_field.field_type %}"modified_value"{% elif 'int' in test_field.field_type %}999{% else %}default{% endif %} };
|
|
77
|
+
{%- elif 'string' in test_field.field_type and not '<' in test_field.field_type %}
|
|
78
|
+
instance2.{{ test_field.field_name }} = "different_value";
|
|
79
|
+
{%- elif 'int' in test_field.field_type or 'long' in test_field.field_type or 'short' in test_field.field_type or 'byte' in test_field.field_type %}
|
|
80
|
+
instance2.{{ test_field.field_name }} = ({{ test_field.field_type.replace('?', '') }})999;
|
|
81
|
+
{%- elif 'bool' in test_field.field_type %}
|
|
82
|
+
instance2.{{ test_field.field_name }} = false;
|
|
83
|
+
{%- elif 'float' in test_field.field_type or 'double' in test_field.field_type or 'decimal' in test_field.field_type %}
|
|
84
|
+
instance2.{{ test_field.field_name }} = 999.99{{ 'f' if 'float' in test_field.field_type else 'm' if 'decimal' in test_field.field_type else '' }};
|
|
85
|
+
{%- else %}
|
|
86
|
+
// For complex types (tuples, nested objects), create with different test values
|
|
87
|
+
{%- if 'Coordinate' in test_field.field_type %}
|
|
88
|
+
instance2.{{ test_field.field_name }} = new {{test_field.field_type.replace('?', '').replace('global::', '')}}(999.0, 999.0);
|
|
89
|
+
{%- elif 'PersonTuple' in test_field.field_type %}
|
|
90
|
+
instance2.{{ test_field.field_name }} = new {{test_field.field_type.replace('?', '').replace('global::', '')}}("different_person", 999, false);
|
|
91
|
+
{%- elif 'Payload' in test_field.field_type %}
|
|
92
|
+
instance2.{{ test_field.field_name }} = new {{test_field.field_type.replace('?', '').replace('global::', '')}}("different_value");
|
|
93
|
+
{%- else %}
|
|
94
|
+
instance2.{{ test_field.field_name }} = {{ test_field.test_value }};
|
|
95
|
+
{%- endif %}
|
|
96
|
+
{%- endif %}
|
|
97
|
+
|
|
98
|
+
{%- if 'Enum' not in test_field.field_type %}
|
|
99
|
+
// Test inequality
|
|
100
|
+
Assert.That(instance1.Equals(instance2), Is.False, "Two instances with different values should not be equal");
|
|
101
|
+
{%- else %}
|
|
102
|
+
// Skip negative test for enum types
|
|
103
|
+
Assert.That(true, Is.True);
|
|
104
|
+
{%- endif %}
|
|
105
|
+
{%- endif %}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/// <summary> Test null equality </summary>
|
|
109
|
+
[Test]
|
|
110
|
+
public void TestEquals_Null()
|
|
111
|
+
{
|
|
112
|
+
var instance = CreateInstance();
|
|
113
|
+
Assert.That(instance.Equals(null), Is.False, "Instance should not equal null");
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/// <summary> Test wrong type equality </summary>
|
|
117
|
+
[Test]
|
|
118
|
+
public void TestEquals_WrongType()
|
|
119
|
+
{
|
|
120
|
+
var instance = CreateInstance();
|
|
121
|
+
Assert.That(instance.Equals("wrong type"), Is.False, "Instance should not equal object of different type");
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
{%- if system_text_json_annotation %}
|
|
125
|
+
/// <summary> Test JSON serialization </summary>
|
|
126
|
+
[Test]
|
|
127
|
+
public void TestJsonSerialization()
|
|
128
|
+
{
|
|
129
|
+
var instance = CreateInstance();
|
|
130
|
+
var json = System.Text.Json.JsonSerializer.Serialize(instance);
|
|
131
|
+
Assert.That(json, Is.Not.Null.And.Not.Empty);
|
|
132
|
+
|
|
133
|
+
var deserialized = System.Text.Json.JsonSerializer.Deserialize<{{ class_base_name }}>(json);
|
|
134
|
+
Assert.That(deserialized, Is.Not.Null);
|
|
135
|
+
Assert.That(instance.Equals(deserialized), Is.True, "Serialized and deserialized instances should be equal");
|
|
136
|
+
}
|
|
137
|
+
{%- endif %}
|
|
138
|
+
|
|
139
|
+
{%- if newtonsoft_json_annotation %}
|
|
140
|
+
/// <summary> Test JSON serialization with Newtonsoft </summary>
|
|
141
|
+
[Test]
|
|
142
|
+
public void TestNewtonsoftJsonSerialization()
|
|
143
|
+
{
|
|
144
|
+
var instance = CreateInstance();
|
|
145
|
+
var json = Newtonsoft.Json.JsonConvert.SerializeObject(instance);
|
|
146
|
+
Assert.That(json, Is.Not.Null.And.Not.Empty);
|
|
147
|
+
|
|
148
|
+
var deserialized = Newtonsoft.Json.JsonConvert.DeserializeObject<{{ class_base_name }}>(json);
|
|
149
|
+
Assert.That(deserialized, Is.Not.Null);
|
|
150
|
+
Assert.That(instance.Equals(deserialized), Is.True, "Serialized and deserialized instances should be equal");
|
|
151
|
+
}
|
|
152
|
+
{%- endif %}
|
|
153
|
+
|
|
154
|
+
{%- if system_xml_annotation %}
|
|
155
|
+
/// <summary> Test XML serialization </summary>
|
|
156
|
+
[Test]
|
|
157
|
+
public void TestXmlSerialization()
|
|
158
|
+
{
|
|
159
|
+
var instance = CreateInstance();
|
|
160
|
+
var serializer = new System.Xml.Serialization.XmlSerializer(typeof({{ class_base_name }}));
|
|
161
|
+
using (var writer = new System.IO.StringWriter())
|
|
162
|
+
{
|
|
163
|
+
serializer.Serialize(writer, instance);
|
|
164
|
+
var xml = writer.ToString();
|
|
165
|
+
Assert.That(xml, Is.Not.Null.And.Not.Empty);
|
|
166
|
+
|
|
167
|
+
using (var reader = new System.IO.StringReader(xml))
|
|
168
|
+
{
|
|
169
|
+
var deserialized = ({{ class_base_name }}?)serializer.Deserialize(reader);
|
|
170
|
+
Assert.That(deserialized, Is.Not.Null);
|
|
171
|
+
Assert.That(instance.Equals(deserialized), Is.True, "Serialized and deserialized instances should be equal");
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
{%- endif %}
|
|
176
|
+
}
|
|
177
|
+
{% endfilter %}
|
|
178
|
+
{% if namespace %}
|
|
179
|
+
}
|
|
180
|
+
{% endif %}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
{%- if system_text_json_annotation or newtonsoft_json_annotation or system_xml_annotation %}
|
|
2
|
+
/// <summary>
|
|
3
|
+
/// Creates an object from the data
|
|
4
|
+
/// </summary>
|
|
5
|
+
/// <param name="data">The input data to convert</param>
|
|
6
|
+
/// <param name="contentTypeString">The content type string of the desired encoding</param>
|
|
7
|
+
/// <returns>The converted object</returns>
|
|
8
|
+
public static {{ class_name }}? FromData(object? data, string? contentTypeString )
|
|
9
|
+
{
|
|
10
|
+
if ( data == null ) return null;
|
|
11
|
+
if ( data is {{ class_name }}) return ({{ class_name }})data;
|
|
12
|
+
if ( contentTypeString == null ) contentTypeString = System.Net.Mime.MediaTypeNames.Application.Octet;
|
|
13
|
+
var contentType = new System.Net.Mime.ContentType(contentTypeString);
|
|
14
|
+
{%- if system_text_json_annotation or newtonsoft_json_annotation or system_xml_annotation %}
|
|
15
|
+
if ( contentType.MediaType.EndsWith("+gzip"))
|
|
16
|
+
{
|
|
17
|
+
var stream = data switch
|
|
18
|
+
{
|
|
19
|
+
System.IO.Stream s => s, System.BinaryData bd => bd.ToStream(), byte[] bytes => new System.IO.MemoryStream(bytes),
|
|
20
|
+
_ => throw new NotSupportedException("Data is not of a supported type for gzip decompression")
|
|
21
|
+
};
|
|
22
|
+
using (var gzip = new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionMode.Decompress))
|
|
23
|
+
{
|
|
24
|
+
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
|
|
25
|
+
gzip.CopyTo(memoryStream);
|
|
26
|
+
memoryStream.Position = 0;
|
|
27
|
+
data = memoryStream.ToArray();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
{%- endif %}
|
|
31
|
+
{%- if system_text_json_annotation %}
|
|
32
|
+
if ( contentType.MediaType.StartsWith(System.Net.Mime.MediaTypeNames.Application.Json))
|
|
33
|
+
{
|
|
34
|
+
if (data is System.Text.Json.JsonElement)
|
|
35
|
+
{
|
|
36
|
+
return System.Text.Json.JsonSerializer.Deserialize<{{ class_name }}>((System.Text.Json.JsonElement)data);
|
|
37
|
+
}
|
|
38
|
+
else if ( data is string)
|
|
39
|
+
{
|
|
40
|
+
return System.Text.Json.JsonSerializer.Deserialize<{{ class_name }}>((string)data);
|
|
41
|
+
}
|
|
42
|
+
else if (data is System.BinaryData)
|
|
43
|
+
{
|
|
44
|
+
return ((System.BinaryData)data).ToObjectFromJson<{{ class_name }}>();
|
|
45
|
+
}
|
|
46
|
+
else if (data is byte[])
|
|
47
|
+
{
|
|
48
|
+
return System.Text.Json.JsonSerializer.Deserialize<{{ class_name }}>(new ReadOnlySpan<byte>((byte[])data));
|
|
49
|
+
}
|
|
50
|
+
else if (data is System.IO.Stream)
|
|
51
|
+
{
|
|
52
|
+
return System.Text.Json.JsonSerializer.DeserializeAsync<{{ class_name }}>((System.IO.Stream)data).Result;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
{%- endif %}
|
|
56
|
+
{%- if newtonsoft_json_annotation %}
|
|
57
|
+
if ( contentType.MediaType.StartsWith(System.Net.Mime.MediaTypeNames.Application.Json))
|
|
58
|
+
{
|
|
59
|
+
if (data is string)
|
|
60
|
+
{
|
|
61
|
+
return Newtonsoft.Json.JsonConvert.DeserializeObject<{{ class_name }}>((string)data);
|
|
62
|
+
}
|
|
63
|
+
else if (data is System.BinaryData)
|
|
64
|
+
{
|
|
65
|
+
return ((System.BinaryData)data).ToObjectFromJson<{{ class_name }}>();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
{%- endif %}
|
|
69
|
+
{%- if system_xml_annotation %}
|
|
70
|
+
if ( contentType.MediaType.StartsWith(System.Net.Mime.MediaTypeNames.Text.Xml) || contentType.MediaType.StartsWith(System.Net.Mime.MediaTypeNames.Application.Xml) || contentType.MediaType.EndsWith("+xml"))
|
|
71
|
+
{
|
|
72
|
+
var serializer = new System.Xml.Serialization.XmlSerializer(typeof({{ class_name }}));
|
|
73
|
+
if (data is string)
|
|
74
|
+
{
|
|
75
|
+
using (var reader = new System.IO.StringReader((string)data))
|
|
76
|
+
{
|
|
77
|
+
return ({{ class_name }}?)serializer.Deserialize(reader);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
else if (data is System.IO.Stream)
|
|
81
|
+
{
|
|
82
|
+
return ({{ class_name }}?)serializer.Deserialize((System.IO.Stream)data);
|
|
83
|
+
}
|
|
84
|
+
else if (data is System.BinaryData)
|
|
85
|
+
{
|
|
86
|
+
var memoryStream = new System.IO.MemoryStream(((System.BinaryData)data).ToArray());
|
|
87
|
+
return ({{ class_name }}?)serializer.Deserialize(memoryStream);
|
|
88
|
+
}
|
|
89
|
+
else if (data is byte[])
|
|
90
|
+
{
|
|
91
|
+
var memoryStream = new System.IO.MemoryStream((byte[])data);
|
|
92
|
+
return ({{ class_name }}?)serializer.Deserialize(memoryStream);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
{%- endif %}
|
|
96
|
+
throw new System.NotSupportedException($"Unsupported media type {contentType.MediaType}");
|
|
97
|
+
}
|
|
98
|
+
{%- endif %}
|
|
99
|
+
|
|
100
|
+
{%- if system_text_json_annotation or newtonsoft_json_annotation or system_xml_annotation %}
|
|
101
|
+
/// <summary>
|
|
102
|
+
/// Converts the object to a byte array
|
|
103
|
+
/// </summary>
|
|
104
|
+
/// <param name="contentTypeString">The content type string of the desired encoding</param>
|
|
105
|
+
/// <returns>The encoded data</returns>
|
|
106
|
+
public byte[] ToByteArray(string contentTypeString)
|
|
107
|
+
{
|
|
108
|
+
var contentType = new System.Net.Mime.ContentType(contentTypeString);
|
|
109
|
+
byte[]? result = null;
|
|
110
|
+
{%- if system_text_json_annotation %}
|
|
111
|
+
if (contentType.MediaType.StartsWith(System.Net.Mime.MediaTypeNames.Application.Json))
|
|
112
|
+
{
|
|
113
|
+
// Use runtime type to ensure polymorphic serialization works correctly
|
|
114
|
+
result = System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(this, this.GetType());
|
|
115
|
+
}
|
|
116
|
+
{%- endif %}
|
|
117
|
+
{%- if newtonsoft_json_annotation %}
|
|
118
|
+
{%- if system_text_json_annotation %}
|
|
119
|
+
else if (result == null && contentType.MediaType.StartsWith(System.Net.Mime.MediaTypeNames.Application.Json))
|
|
120
|
+
{%- else %}
|
|
121
|
+
if (contentType.MediaType.StartsWith(System.Net.Mime.MediaTypeNames.Application.Json))
|
|
122
|
+
{%- endif %}
|
|
123
|
+
{
|
|
124
|
+
result = System.Text.Encoding.GetEncoding(contentType.CharSet??"utf-8").GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
|
|
125
|
+
}
|
|
126
|
+
{%- endif %}
|
|
127
|
+
{%- if system_xml_annotation %}
|
|
128
|
+
if (contentType.MediaType.StartsWith(System.Net.Mime.MediaTypeNames.Text.Xml) || contentType.MediaType.StartsWith(System.Net.Mime.MediaTypeNames.Application.Xml) || contentType.MediaType.EndsWith("+xml"))
|
|
129
|
+
{
|
|
130
|
+
var serializer = new System.Xml.Serialization.XmlSerializer(typeof({{ class_name }}));
|
|
131
|
+
using (var stream = new System.IO.MemoryStream())
|
|
132
|
+
{
|
|
133
|
+
using (var writer = new System.IO.StreamWriter(stream))
|
|
134
|
+
{
|
|
135
|
+
serializer.Serialize(writer, this);
|
|
136
|
+
writer.Flush();
|
|
137
|
+
result = stream.ToArray();
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
{%- endif %}
|
|
142
|
+
if ( result == null ) throw new System.NotSupportedException($"Unsupported media type {contentType.MediaType}");
|
|
143
|
+
if ( contentType.MediaType.EndsWith("+gzip"))
|
|
144
|
+
{
|
|
145
|
+
using (var output = new System.IO.MemoryStream())
|
|
146
|
+
{
|
|
147
|
+
using (var gzip = new System.IO.Compression.GZipStream(output, System.IO.Compression.CompressionMode.Compress))
|
|
148
|
+
{
|
|
149
|
+
gzip.Write(result, 0, result.Length);
|
|
150
|
+
}
|
|
151
|
+
result = output.ToArray();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return result;
|
|
155
|
+
}
|
|
156
|
+
{%- endif %}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
using System;
|
|
2
|
+
using NUnit.Framework;
|
|
3
|
+
|
|
4
|
+
namespace {{ namespace }}
|
|
5
|
+
{
|
|
6
|
+
/// <summary> Test class for {{ enum_base_name }} </summary>
|
|
7
|
+
[TestFixture]
|
|
8
|
+
public class {{ test_class_name }}
|
|
9
|
+
{
|
|
10
|
+
/// <summary> Test {{ enum_base_name }} Enum </summary>
|
|
11
|
+
[Test]
|
|
12
|
+
public void Test_{{ enum_base_name }}_Enum()
|
|
13
|
+
{
|
|
14
|
+
var values = Enum.GetValues(typeof({{ enum_base_name }}));
|
|
15
|
+
{%- for symbol in symbols %}
|
|
16
|
+
Assert.That(Enum.IsDefined(typeof({{ enum_base_name }}), {{ enum_base_name }}.{{ symbol }}));
|
|
17
|
+
{%- endfor %}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
{%- if system_text_json_annotation %}
|
|
21
|
+
/// <summary> Test JSON serialization of enum </summary>
|
|
22
|
+
[Test]
|
|
23
|
+
public void TestEnumJsonSerialization()
|
|
24
|
+
{
|
|
25
|
+
{%- if symbols %}
|
|
26
|
+
var value = {{ enum_base_name }}.{{ symbols[0] }};
|
|
27
|
+
var json = System.Text.Json.JsonSerializer.Serialize(value);
|
|
28
|
+
Assert.That(json, Is.Not.Null.And.Not.Empty);
|
|
29
|
+
|
|
30
|
+
var deserialized = System.Text.Json.JsonSerializer.Deserialize<{{ enum_base_name }}>(json);
|
|
31
|
+
Assert.That(deserialized, Is.EqualTo(value));
|
|
32
|
+
{%- endif %}
|
|
33
|
+
}
|
|
34
|
+
{%- endif %}
|
|
35
|
+
}
|
|
36
|
+
}
|