|
| 1 | +/** {{ class_name }} class. */ |
| 2 | +{%- if typed_json_annotation %} |
| 3 | +import 'reflect-metadata'; |
| 4 | +import { jsonObject, jsonMember, TypedJSON } from 'typedjson'; |
| 5 | +{%- endif %} |
| 6 | +{%- if avro_annotation %} |
| 7 | +import { Type } from 'avro-js'; |
| 8 | +{%- endif %} |
| 9 | +{%- for import_type, import_path in import_types.items() %} |
| 10 | +import { {{ import_type }} } from '{{ import_path }}'; |
| 11 | +{%- endfor %} |
| 12 | +{%- if avro_annotation or typed_json_annotation %} |
| 13 | +import pako from 'pako'; |
| 14 | +{%- endif %} |
| 15 | + |
| 16 | +@jsonObject |
| 17 | +export class {{ class_name }} { |
| 18 | + {%- if avro_annotation %} |
| 19 | + public static AvroType: Type = Type.forSchema({{ avro_schema_json }}); |
| 20 | + {%- endif %} |
| 21 | + |
| 22 | + {%- for field in fields %} |
| 23 | + /** {{ field.docstring }} */ |
| 24 | + {%- if typed_json_annotation %} |
| 25 | + @jsonMember |
| 26 | + {%- endif %} |
| 27 | + public {{ field.name }}: {{ field.type }}; |
| 28 | + {%- endfor %} |
| 29 | + |
| 30 | + constructor({%- for field in fields %}{{ field.name }}: {{ field.type }}{%- if not loop.last %}, {%- endif %}{%- endfor %}) { |
| 31 | + {%- for field in fields %} |
| 32 | + {%- if field.is_enum %} |
| 33 | + if ( typeof {{ field.name }} === 'number' ) { |
| 34 | + this.{{ field.name }} = {{ field.type }}Utils.fromOrdinal({{ field.name }}); |
| 35 | + } else { |
| 36 | + this.{{ field.name }} = {{ field.name }}; |
| 37 | + } |
| 38 | + {%- else %} |
| 39 | + this.{{ field.name }} = {{ field.name }}; |
| 40 | + {%- endif %} |
| 41 | + {%- endfor %} |
| 42 | + } |
| 43 | + |
| 44 | + {%- if avro_annotation or typed_json_annotation %} |
| 45 | + public toByteArray(contentTypeString: string): Uint8Array { |
| 46 | + const contentType = contentTypeString.split(';')[0].trim(); |
| 47 | + let result: Uint8Array | null = null; |
| 48 | + |
| 49 | + {%- if avro_annotation %} |
| 50 | + if (contentType.startsWith('avro/binary') || contentType.startsWith('application/vnd.apache.avro+avro')) { |
| 51 | + result = ({{ class_name }}.AvroType.toBuffer(this) as unknown) as Uint8Array; |
| 52 | + } |
| 53 | + {%- endif %} |
| 54 | + |
| 55 | + {%- if typed_json_annotation %} |
| 56 | + if (contentType.startsWith('application/json')) { |
| 57 | + const serializer = new TypedJSON({{ class_name }}); |
| 58 | + const jsonString = serializer.stringify(this); |
| 59 | + result = new TextEncoder().encode(jsonString); |
| 60 | + } |
| 61 | + {%- endif %} |
| 62 | + |
| 63 | + if (result && contentTypeString.endsWith('+gzip')) { |
| 64 | + result = pako.gzip(result); |
| 65 | + } |
| 66 | + |
| 67 | + if (result) { |
| 68 | + return result; |
| 69 | + } else { |
| 70 | + throw new Error(`Unsupported media type: ${contentTypeString}`); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public static fromData(data: any, contentTypeString: string): {{ class_name }} { |
| 75 | + const contentType = contentTypeString.split(';')[0].trim(); |
| 76 | + |
| 77 | + if (contentTypeString.endsWith('+gzip')) { |
| 78 | + data = pako.ungzip(data); |
| 79 | + } |
| 80 | + |
| 81 | + {%- if avro_annotation %} |
| 82 | + if (contentType.startsWith('avro/binary') || contentType.startsWith('application/vnd.apache.avro+avro')) { |
| 83 | + return {{ class_name }}.AvroType.fromBuffer(data); |
| 84 | + } |
| 85 | + {%- endif %} |
| 86 | + |
| 87 | + {%- if typed_json_annotation %} |
| 88 | + if (contentType.startsWith('application/json')) { |
| 89 | + const serializer = new TypedJSON({{ class_name }}); |
| 90 | + const retval = serializer.parse(new TextDecoder().decode(data)); |
| 91 | + if (!(retval instanceof {{ class_name }})) { |
| 92 | + throw new Error(`Deserialized object is not an instance of {{ class_name }}`); |
| 93 | + } |
| 94 | + return retval; |
| 95 | + } |
| 96 | + {%- endif %} |
| 97 | + |
| 98 | + throw new Error(`Unsupported media type: ${contentTypeString}`); |
| 99 | + } |
| 100 | + |
| 101 | + public static isJsonMatch(element: any): boolean { |
| 102 | + {%- if fields|length == 0 %} |
| 103 | + return true; |
| 104 | + {%- else %} |
| 105 | + return ( |
| 106 | + {%- for field in fields %} |
| 107 | + {{ get_is_json_match_clause(field.name, field.type, field.is_enum) }}{%- if not loop.last %} && |
| 108 | + {%- endif %} |
| 109 | + {%- endfor %} |
| 110 | + ); |
| 111 | + {%- endif %} |
| 112 | + } |
| 113 | + {%- endif %} |
| 114 | +} |
0 commit comments