github-code-scanning[bot] commented on code in PR #1823:
URL: https://github.com/apache/avro/pull/1823#discussion_r944161766
##########
lang/csharp/src/apache/main/Reflect/ClassCache.cs:
##########
@@ -179,126 +182,108 @@
/// <returns></returns>
public DotnetClass GetClass(RecordSchema schema)
{
- DotnetClass c;
- if (!_nameClassMap.TryGetValue(schema.Fullname, out c))
- {
- return null;
- }
-
- return c;
+ return (DotnetClass)GetClass(schema.Fullname);
}
/// <summary>
- /// Add an entry to the class cache.
+ /// Add an entry to the class cache.
/// </summary>
/// <param name="objType">Type of the C# class</param>
/// <param name="s">Schema</param>
+ [Obsolete()]
public void LoadClassCache(Type objType, Schema s)
{
- switch (s)
- {
- case RecordSchema rs:
- if (!objType.IsClass)
- {
- throw new AvroException($"Cant map scalar type
{objType.Name} to record {rs.Fullname}");
- }
+ _reflectFactory.LoadClassCache(objType, s);
+ }
- if (typeof(byte[]).IsAssignableFrom(objType)
- || typeof(string).IsAssignableFrom(objType)
- || typeof(IEnumerable).IsAssignableFrom(objType)
- || typeof(IDictionary).IsAssignableFrom(objType))
- {
- throw new AvroException($"Cant map type {objType.Name}
to record {rs.Fullname}");
- }
+ // IReflectCache and IConverterService are implemented for backward
compatibility
+ #region IReflectCahce
- AddClassNameMapItem(rs, objType);
- var c = GetClass(rs);
- foreach (var f in rs.Fields)
- {
- /*
- //.StackOverflowException
- var t = c.GetPropertyType(f);
- LoadClassCache(t, f.Schema);
- */
- if (_previousFields.TryAdd(f.Name, f.Schema))
- {
- var t = c.GetPropertyType(f);
- LoadClassCache(t, f.Schema);
- }
- }
+ /// <summary>
+ /// Find a class that matches the schema full name.
+ /// </summary>
+ /// <param name="schemaFullName"></param>
+ /// <returns></returns>
+ [Obsolete()]
+ public IDotnetClass GetClass(string schemaFullName)
+ {
+ IDotnetClass c;
+ if (!_nameClassMap.TryGetValue(schemaFullName, out c))
+ {
+ return null;
+ }
- break;
- case ArraySchema ars:
- if (!typeof(IEnumerable).IsAssignableFrom(objType))
- {
- throw new AvroException($"Cant map type {objType.Name}
to array {ars.Name}");
- }
+ return c;
+ }
- if (!objType.IsGenericType)
- {
- throw new AvroException($"{objType.Name} needs to be a
generic type");
- }
+ /// <summary>
+ /// Add a class that for schema full name.
+ /// </summary>
+ /// <param name="schemaFullName"></param>
+ /// <param name="dotnetClass"></param>
+ [Obsolete()]
+ public void AddClass(string schemaFullName, IDotnetClass dotnetClass)
+ {
+ _nameClassMap.TryAdd(schemaFullName, dotnetClass);
+ }
- LoadClassCache(objType.GenericTypeArguments[0],
ars.ItemSchema);
- break;
- case MapSchema ms:
- if (!typeof(IDictionary).IsAssignableFrom(objType))
- {
- throw new AvroException($"Cant map type {objType.Name}
to map {ms.Name}");
- }
+ /// <summary>
+ /// Find a enum type that matches the schema full name.
+ /// </summary>
+ /// <param name="schemaFullName"></param>
+ /// <returns></returns>
+ [Obsolete()]
+ public Type GetEnum(string schemaFullName)
+ {
+ return EnumCache.GetEnumeration(schemaFullName);
+ }
- if (!objType.IsGenericType)
- {
- throw new AvroException($"Cant map non-generic type
{objType.Name} to map {ms.Name}");
- }
+ /// <summary>
+ /// Add a class that for schema full name.
+ /// </summary>
+ /// <param name="schemaFullName"></param>
+ /// <param name="enumType"></param>
+ [Obsolete()]
+ public void AddEnum(string schemaFullName, Type enumType)
+ {
+ EnumCache.AddEnumNameMapItem(schemaFullName, enumType);
+ }
- if
(!typeof(string).IsAssignableFrom(objType.GenericTypeArguments[0]))
- {
- throw new AvroException($"First type parameter of
{objType.Name} must be assignable to string");
- }
+ /// <summary>
+ /// Add an array helper type. Array helpers are used for collections
that are not generic lists.
+ /// </summary>
+ /// <param name="arrayHelperName">Name of the helper. Corresponds to
metadata "helper" field in the schema.</param>
+ public void AddArrayHelperType<T>(string arrayHelperName) where T :
IArrayHelper
+ {
+ _nameArrayMap.TryAdd(arrayHelperName, typeof(T));
+ }
- LoadClassCache(objType.GenericTypeArguments[1],
ms.ValueSchema);
- break;
- case NamedSchema ns:
- EnumCache.AddEnumNameMapItem(ns, objType);
- break;
- case UnionSchema us:
- if (us.Schemas.Count == 2 && (us.Schemas[0].Tag ==
Schema.Type.Null || us.Schemas[1].Tag == Schema.Type.Null))
- {
- // in this case objType will match the non null type
in the union
- foreach (var o in us.Schemas)
- {
- if (o.Tag == Schema.Type.Null)
- {
- continue;
- }
+ /// <summary>
+ /// Find an array helper type for an array schema node.
+ /// </summary>
+ /// <param name="arrayHelperName">Schema</param>
+ /// <param name="arrayHelperType">Schema</param>
+ /// <returns></returns>
+ public bool TryGetArrayHelperType(string arrayHelperName, out Type
arrayHelperType)
+ {
+ return _nameArrayMap.TryGetValue(arrayHelperName, out
arrayHelperType);
+ }
- if (objType.IsClass)
- {
- LoadClassCache(objType, o);
- }
+ #endregion
- var innerType =
Nullable.GetUnderlyingType(objType);
- if (innerType != null && innerType.IsEnum)
- {
- LoadClassCache(innerType, o);
- }
- }
- }
- else
- {
- // check the schema types are registered
- foreach (var o in us.Schemas)
- {
- if (o.Tag == Schema.Type.Record && GetClass(o as
RecordSchema) == null)
- {
- throw new AvroException($"Class for union
record type {o.Fullname} is not registered. Create a ClassCache object and call
LoadClassCache");
- }
- }
- }
+ #region IConverterService
- break;
- }
+ /// <summary>
+ /// Get converter
+ /// </summary>
+ /// <param name="schema"></param>
+ /// <param name="property"></param>
+ /// <returns></returns>
+ public IAvroFieldConverter GetConverter(Schema schema, PropertyInfo
property)
+ {
+ return _converterService.GetConverter(schema, property) ??
GetDefaultConverter(schema.Tag, property.PropertyType);
Review Comment:
## Call to obsolete method
Call to obsolete method [GetDefaultConverter](1).
[Show more
details](https://github.com/apache/avro/security/code-scanning/2894)
--
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]