jklinaku commented on code in PR #253: URL: https://github.com/apache/pulsar-dotpulsar/pull/253#discussion_r1968523700
########## src/DotPulsar/Schemas/AvroISpecificRecordSchema.cs: ########## @@ -0,0 +1,233 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace DotPulsar.Schemas; + +using DotPulsar.Abstractions; +using DotPulsar.Exceptions; +using DotPulsar.Internal.Extensions; +using System.Buffers; +using System.Collections.Generic; +using System.Reflection; +using System.Text; + +public sealed class AvroISpecificRecordSchema<T> : ISchema<T> +{ + private readonly Type _typeT; + private readonly object _avroSchema; + private readonly object _avroWriter; + private readonly MethodInfo _avroWriterWriteMethod; + private readonly object _avroReader; + private readonly MethodInfo _avroReaderReadMethod; + private readonly TypeInfo _binaryEncoderTypeInfo; + private readonly TypeInfo _binaryDecoderTypeInfo; + + public SchemaInfo SchemaInfo { get; } + public AvroISpecificRecordSchema() Review Comment: moved as much as I thought I could, also would like an opinion on the way I handled the exception, might not be the best way! ########## src/DotPulsar/Schemas/AvroISpecificRecordSchema.cs: ########## @@ -0,0 +1,233 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace DotPulsar.Schemas; + +using DotPulsar.Abstractions; +using DotPulsar.Exceptions; +using DotPulsar.Internal.Extensions; +using System.Buffers; +using System.Collections.Generic; +using System.Reflection; +using System.Text; + +public sealed class AvroISpecificRecordSchema<T> : ISchema<T> +{ + private readonly Type _typeT; + private readonly object _avroSchema; + private readonly object _avroWriter; + private readonly MethodInfo _avroWriterWriteMethod; + private readonly object _avroReader; + private readonly MethodInfo _avroReaderReadMethod; + private readonly TypeInfo _binaryEncoderTypeInfo; + private readonly TypeInfo _binaryDecoderTypeInfo; + + public SchemaInfo SchemaInfo { get; } + public AvroISpecificRecordSchema() + { + const string schemaFullName = "Avro.Schema"; + const string ISpecificRecordFullName = "Avro.Specific.ISpecificRecord"; + _typeT = typeof(T); + string SchemaName; + string SchemaData; + try + { + if (!_typeT.GetInterfaces().Any(i => i.FullName == ISpecificRecordFullName)) + throw new SchemaException(string.Format("The type {0} must implement {1}", _typeT, ISpecificRecordFullName)); + _avroSchema = _typeT.GetField("_SCHEMA")?.GetValue(null) ?? + throw new SchemaException(string.Format("The static field named '_SCHEMA' must not be null in type: {0}", _typeT)); + Type avroSchemaType = _avroSchema.GetType(); + if (!avroSchemaType.ImplementsBaseTypeFullName(schemaFullName)) + { + throw new Exception(string.Format("field '_SCHEMA' must be of type {0}", schemaFullName)); Review Comment: yep. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
