private void WriteFixedField(ExtendedFieldInfo efi)
        {
            _sb.Append(_indentProvider.Get());
            _sb.Append("{ ");
            _sb.Append("name".ToDoubleQoutedString() + ": ");
            _sb.Append(efi.FieldName.ToDoubleQoutedString());

            if (!WriteDocValue(efi, true))
            {
                _sb.Append(", ");
            }

            _sb.AppendLine("type".ToDoubleQoutedString() + ": {");

            _indentProvider.IncLevel();
            _indentProvider.IncLevel();
            _sb.Append(_indentProvider.Get());
            _sb.Append("name".ToDoubleQoutedString() + ": " + efi.FixedFieldClassName.ToDoubleQoutedString() + ", ");
            _sb.Append("type".ToDoubleQoutedString() + ": " + "fixed".ToDoubleQoutedString() + ", ");
            _sb.Append("size".ToDoubleQoutedString() + ": ");
            _sb.Append(efi.FixedFieldSize);

            WriteDocValue(efi);

            _sb.Append("} ");

            _sb.AppendLine("}, ");
        }
Exemple #2
0
        private void AddSubFields(ExtendedFieldInfo efi)
        {
            var fields = efi.AvroType == AvroFieldType.Record ? efi.FieldInfo.FieldType.GetFields() : efi.ComplexArrayOrMapType?.GetFields();

            if (fields == null)
            {
                return;
            }

            foreach (var field in fields)
            {
                if (field.IsInitOnly || field.IsLiteral || field.IsStatic)
                {
                    continue;
                }

                var subEfi = new ExtendedFieldInfo(field);
                efi.SubFieldList.Add(subEfi);

                _fieldInspector.Inspect(_typeInfoData.Logger, subEfi);

                if (subEfi.AvroType == AvroFieldType.Record || efi.AvroType == AvroFieldType.ArrayWithRecordType || efi.AvroType == AvroFieldType.MapWithRecordType)
                {
                    AddSubFields(subEfi);
                }
            }
        }
Exemple #3
0
        private void SetTypeCodeForPrimitives(ExtendedFieldInfo efi)
        {
            switch (efi.TypeCode)
            {
            case TypeCode.Int32:
                efi.AvroType = efi.FieldInfo.FieldType.BaseType == typeof(Enum) ? AvroFieldType.Enum : AvroFieldType.Int;
                break;

            case TypeCode.Int64:
                efi.AvroType = AvroFieldType.Long;
                break;

            case TypeCode.Single:
                efi.AvroType = AvroFieldType.Float;
                break;

            case TypeCode.Double:
                efi.AvroType = AvroFieldType.Double;
                break;

            case TypeCode.String:
                efi.AvroType = AvroFieldType.String;
                break;

            case TypeCode.Decimal:
                efi.AvroType = AvroFieldType.Logical;
                break;

            case TypeCode.Boolean:
                efi.AvroType = AvroFieldType.Boolean;
                break;
            }
        }
        private void WriteClassTypeInfo(ExtendedFieldInfo efi, bool needTypeString = true)
        {
            if (_generatedTypes.Contains(efi.ImplementingClassName))
            {
                _sb.Append(_indentProvider.Get());
                _sb.Append("{ ");
                _sb.Append("name".ToDoubleQoutedString() + ": ");
                _sb.Append(efi.FieldName.ToDoubleQoutedString() + ", ");
                _sb.Append("type".ToDoubleQoutedString() + ":  ");
                _sb.Append(efi.ImplementingClassName.ToDoubleQoutedString());
                _sb.AppendLine(" }");

                return;
            }

            try
            {
                _sb.Append(_indentProvider.Get());
                _sb.Append("{ ");

                if (needTypeString)
                {
                    _sb.Append("name".ToDoubleQoutedString() + ": ");
                    _sb.AppendLine(efi.FieldName.ToDoubleQoutedString() + ", ");
                }
                _sb.Append(_indentProvider.Get());

                if (needTypeString)
                {
                    _sb.AppendLine("  " + "type".ToDoubleQoutedString() + ": { ");
                }

                _indentProvider.IncLevel();
                _indentProvider.IncLevel();

                _sb.Append(_indentProvider.Get());
                _sb.Append("type".ToDoubleQoutedString() + ": ");
                _sb.AppendLine("record".ToDoubleQoutedString() + ", ");
                _sb.Append(_indentProvider.Get());
                _sb.Append("name".ToDoubleQoutedString() + ": ");
                _sb.AppendLine(efi.ImplementingClassName.ToDoubleQoutedString() + ", ");

                WriteRecord(efi.SubFieldList);

                _sb.Append(_indentProvider.Get());
                _sb.AppendLine("} ");

                _sb.Append(_indentProvider.Get());
                _sb.AppendLine("}, ");

                _generatedTypes.Add(efi.ImplementingClassName);
            }
            finally
            {
                _indentProvider.DecLevel();
                _indentProvider.DecLevel();
            }
        }
Exemple #5
0
        internal void InspectCompiledType(ProgramArgs prgArgs, TypeInfoData typeInfoData)
        {
            _typeInfoData = typeInfoData;

            foreach (var t in typeInfoData.Assembly.GetExportedTypes())
            {
                if (!t.Name.Equals(prgArgs.TypeName))
                {
                    continue;
                }

                typeInfoData.InspectedType = t;
                break;
            }

            if (typeInfoData.InspectedType == null)
            {
                _typeInfoData.Logger.LogIt(LogSeverity.Fatal, "i18n::Cannot load/find type {0}", prgArgs.TypeName);
                throw new TypeLoadException(prgArgs.TypeName);
            }

            typeInfoData.Namespace = typeInfoData.InspectedType.Namespace;

            var ns = typeInfoData.InspectedType.GetCustomAttribute <AvroNamespaceAttribute>()?.NamespaceValue;

            if (!string.IsNullOrWhiteSpace(ns))
            {
                typeInfoData.Namespace = ns;
            }

            var doc = typeInfoData.InspectedType.GetCustomAttribute <AvroDocAttribute>()?.DocValue;

            if (!string.IsNullOrWhiteSpace(doc))
            {
                typeInfoData.DocValue = doc;
            }


            foreach (var field in typeInfoData.InspectedType.GetFields())
            {
                if (field.IsInitOnly || field.IsLiteral || field.IsStatic)
                {
                    continue;
                }

                var efi = new ExtendedFieldInfo(field);
                typeInfoData.FieldList.Add(efi);

                _fieldInspector.Inspect(typeInfoData.Logger, efi);

                if (efi.AvroType == AvroFieldType.Record || efi.AvroType == AvroFieldType.ArrayWithRecordType || efi.AvroType == AvroFieldType.MapWithRecordType)
                {
                    AddSubFields(efi);
                }
            }
        }
Exemple #6
0
        private void WriteDefaultValue(ExtendedFieldInfo extendedFieldInfo)
        {
            if (!extendedFieldInfo.HasDefaultValue)
            {
                return;
            }

            _sb.Append(", " + "default".ToDoubleQoutedString() + ": " +
                       (extendedFieldInfo.TypeCode == TypeCode.String
                           ? extendedFieldInfo.AvroDefaultValue.ToString().ToDoubleQoutedString()
                           : extendedFieldInfo.AvroDefaultValue));
        }
Exemple #7
0
        private bool WriteDocValue(ExtendedFieldInfo extendedFieldInfo, bool appendComma = false)
        {
            if (!extendedFieldInfo.HasDocValue)
            {
                return(false);
            }

            _sb.Append(", " + "doc".ToDoubleQoutedString() + ": " + extendedFieldInfo.AvroDocValue.ToDoubleQoutedString());

            if (appendComma)
            {
                _sb.Append(", ");
            }

            return(true);
        }
        private static IEnumerable <FieldMatch> ScanFields(object value, object expected, IList <object> scanned, string prefix = null)
        {
            var result = new List <FieldMatch>();

            for (var expectedType = expected.GetType(); expectedType != null; expectedType = expectedType.BaseType)
            {
                foreach (var fieldInfo in
                         expectedType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy))
                {
                    var expectedFieldDescription = new ExtendedFieldInfo(prefix, fieldInfo);
                    var actualFieldMatching      = FindField(value.GetType(), expectedFieldDescription.NameInSource);

                    // field not found in SUT
                    if (actualFieldMatching == null)
                    {
                        result.Add(new FieldMatch(expectedFieldDescription, null));
                        continue;
                    }

                    var actualFieldDescription = new ExtendedFieldInfo(prefix, actualFieldMatching);

                    // now, let's get to the values
                    expectedFieldDescription.CaptureFieldValue(expected);
                    actualFieldDescription.CaptureFieldValue(value);

                    if (expectedFieldDescription.ChecksIfImplementsEqual())
                    {
                        result.Add(new FieldMatch(expectedFieldDescription, actualFieldDescription));
                    }
                    else if (!scanned.Contains(expectedFieldDescription.Value))
                    {
                        scanned.Add(expectedFieldDescription.Value);

                        // we need to recurse the scan
                        result.AddRange(
                            ScanFields(
                                actualFieldDescription.Value,
                                expectedFieldDescription.Value,
                                scanned,
                                string.Format("{0}.", expectedFieldDescription.LongFieldName)));
                    }
                }
            }

            return(result);
        }
        private void WriteMapType(ExtendedFieldInfo efi, string typeName)
        {
            _sb.Append(_indentProvider.Get());
            _sb.Append("{ ");
            _sb.Append("name".ToDoubleQoutedString() + ": ");
            _sb.Append(efi.FieldName.ToDoubleQoutedString());

            if (!WriteDocValue(efi, true))
            {
                _sb.Append(", ");
            }

            _sb.AppendLine("type".ToDoubleQoutedString() + ": {");

            _indentProvider.IncLevel();
            _indentProvider.IncLevel();
            _sb.Append(_indentProvider.Get());
            _sb.Append("type".ToDoubleQoutedString() + ": " + "map".ToDoubleQoutedString() + ", ");
            _sb.Append("values".ToDoubleQoutedString() + ": ");


            if (efi.AvroType == AvroFieldType.Map || _generatedTypes.Contains(efi.ImplementingClassName))
            {
                var s = string.IsNullOrWhiteSpace(typeName) ? efi.ImplementingClassName : typeName;

                _sb.Append(s.ToDoubleQoutedString() + " }");
            }
            else
            {
                _indentProvider.DecLevel();
                _indentProvider.DecLevel();
                _sb.AppendLine();

                WriteClassTypeInfo(efi, false);
                _sb.Append(_indentProvider.Get());

                _indentProvider.IncLevel();
                _indentProvider.IncLevel();
            }

            _indentProvider.DecLevel();
            _indentProvider.DecLevel();
            _sb.Append(_indentProvider.Get());
            _sb.AppendLine("}, ");
        }
Exemple #10
0
        private void WritePrimitiveTypeInfo(ExtendedFieldInfo extendedFieldInfo, string avroTypeName)
        {
            _sb.Append(_indentProvider.Get());
            _sb.Append("{ ");
            _sb.Append("name".ToDoubleQoutedString() + ": ");
            _sb.Append(extendedFieldInfo.FieldName.ToDoubleQoutedString() + ", ");
            _sb.Append("type".ToDoubleQoutedString() + ": ");

            if (extendedFieldInfo.IsNullable)
            {
                _sb.Append("[" + "null".ToDoubleQoutedString() + ", " + avroTypeName.ToDoubleQoutedString() + "]");
            }
            else
            {
                _sb.Append(avroTypeName.ToDoubleQoutedString());
            }


            WriteDefaultValue(extendedFieldInfo);
            WriteDocValue(extendedFieldInfo);

            _sb.AppendLine(" }, ");
        }
        private void WriteMapTypeInfo(ExtendedFieldInfo efi)
        {
            switch (efi.AvroType)
            {
            case AvroFieldType.Map:
                switch (efi.TypeCode)
                {
                case TypeCode.Int32:
                    WriteMapType(efi, "int");
                    break;

                case TypeCode.Single:
                    WriteMapType(efi, "float");
                    break;

                case TypeCode.Double:
                    WriteMapType(efi, "double");
                    break;

                case TypeCode.String:
                    WriteMapType(efi, "string");
                    break;

                default:
                    throw new NotSupportedException("type of " + efi.FieldName + " as map not supported");
                }
                break;

            case AvroFieldType.MapWithRecordType:
                WriteMapType(efi, string.Empty);
                break;

            default:
                throw new NotSupportedException("type of " + efi.FieldName + " as map not supported");
            }
        }
 private static bool formatter_FieldCanXElementSerialize(ExtendedFieldInfo efi)
 {
     //Clone时,不序列化实例
     return(efi.FieldInfo.Name != "_ProcessInstance");
 }
Exemple #13
0
 public FieldMatch(ExtendedFieldInfo expected, ExtendedFieldInfo actual)
 {
     this.actual   = actual;
     this.expected = expected;
 }
        private bool OnFieldCanXElementSerialize(ExtendedFieldInfo efi)
        {
            bool result = true;

            if (this.FieldCanXElementSerialize != null)
                result = this.FieldCanXElementSerialize(efi);

            return result;
        }
        private void WriteEnumTypeInfo(ExtendedFieldInfo extendedFieldInfo)
        {
            if (_generatedTypes.Contains(extendedFieldInfo.FieldInfo.FieldType.Name))
            {
                _sb.Append(_indentProvider.Get());
                _sb.Append("{ ");
                _sb.Append("name".ToDoubleQoutedString() + ": ");
                _sb.Append(extendedFieldInfo.FieldName.ToDoubleQoutedString() + ", ");
                _sb.Append("type".ToDoubleQoutedString() + ":  ");
                _sb.Append(extendedFieldInfo.FieldInfo.FieldType.Name.ToDoubleQoutedString());
                _sb.AppendLine(" }, ");
                return;
            }


            try
            {
                _sb.Append(_indentProvider.Get());
                _sb.Append("{ ");
                _sb.Append("name".ToDoubleQoutedString() + ": ");
                _sb.AppendLine(extendedFieldInfo.FieldName.ToDoubleQoutedString() + ", ");
                _sb.Append(_indentProvider.Get());
                _sb.AppendLine("  " + "type".ToDoubleQoutedString() + ": { ");

                _indentProvider.IncLevel();
                _indentProvider.IncLevel();

                _sb.Append(_indentProvider.Get());
                _sb.Append("type".ToDoubleQoutedString() + ": ");
                _sb.AppendLine("enum".ToDoubleQoutedString() + ", ");
                _sb.Append(_indentProvider.Get());
                _sb.Append("name".ToDoubleQoutedString() + ": ");
                _sb.AppendLine(extendedFieldInfo.FieldInfo.FieldType.Name.ToDoubleQoutedString() + ", ");
                _sb.Append(_indentProvider.Get());
                _sb.Append("symbols".ToDoubleQoutedString() + ": [");

                var comma = false;
                foreach (var s in Enum.GetNames(extendedFieldInfo.FieldInfo.FieldType))
                {
                    if (comma)
                    {
                        _sb.Append(", ");
                    }

                    _sb.Append(s.ToDoubleQoutedString());
                    comma = true;
                }

                _sb.AppendLine("]");

                _sb.Append(_indentProvider.Get());
                _sb.AppendLine("} ");

                _sb.Append(_indentProvider.Get());
                _sb.AppendLine("}, ");

                _generatedTypes.Add(extendedFieldInfo.FieldInfo.FieldType.Name);
            }
            finally
            {
                _indentProvider.DecLevel();
                _indentProvider.DecLevel();
            }
        }
Exemple #16
0
        private void WriteFieldToAvro(ExtendedFieldInfo efi)
        {
            _indentProvider.IncLevel();

            try
            {
                switch (efi.AvroType)
                {
                case AvroFieldType.Array:
                case AvroFieldType.ArrayWithRecordType:
                    WriteArrayTypeInfo(efi);
                    break;

                case AvroFieldType.Map:
                case AvroFieldType.MapWithRecordType:
                    WriteMapTypeInfo(efi);
                    break;

                case AvroFieldType.Record:
                    WriteClassTypeInfo(efi);
                    break;

                case AvroFieldType.Enum:
                    WriteEnumTypeInfo(efi);
                    break;

                case AvroFieldType.Boolean:
                    WritePrimitiveTypeInfo(efi, "boolean");
                    break;

                case AvroFieldType.Int:
                    WritePrimitiveTypeInfo(efi, "int");
                    break;

                case AvroFieldType.Long:
                    WritePrimitiveTypeInfo(efi, "long");
                    break;

                case AvroFieldType.Float:
                    WritePrimitiveTypeInfo(efi, "float");
                    break;

                case AvroFieldType.Double:
                    WritePrimitiveTypeInfo(efi, "double");
                    break;

                case AvroFieldType.String:
                    WritePrimitiveTypeInfo(efi, "string");
                    break;

                case AvroFieldType.Fixed:
                    WriteFixedField(efi);
                    break;

                default:
                    throw new NotSupportedException("type of " + efi.FieldName + " not supported");
                }
            }
            finally
            {
                _indentProvider.DecLevel();
            }
        }
Exemple #17
0
        public void Inspect(ILogging logger, ExtendedFieldInfo efi)
        {
            var fieldType = efi.FieldInfo.FieldType;

            efi.TypeCode = Type.GetTypeCode(fieldType);


            SetTypeCodeForPrimitives(efi);

            if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                var t = Nullable.GetUnderlyingType(fieldType);

                if (t != null)
                {
                    efi.TypeCode   = Type.GetTypeCode(t);
                    efi.IsNullable = true;

                    SetTypeCodeForPrimitives(efi);
                }
            }


            if (fieldType.IsArray)
            {
                efi.TypeCode = Type.GetTypeCode(fieldType.GetElementType());
                efi.AvroType = AvroFieldType.Array;
            }


            if (fieldType.IsClass)
            {
                efi.ImplementingClassName = fieldType.Name;
            }

            if (efi.IsClass && efi.TypeCode != TypeCode.String && !fieldType.IsArray)
            {
                efi.AvroType = AvroFieldType.Record;
            }



            if (fieldType.IsGenericType)
            {
                var underlyingName = fieldType.UnderlyingSystemType.Name;

                efi.IsMap = underlyingName.Contains("Dictionary", StringComparison.InvariantCultureIgnoreCase) ||
                            underlyingName.Contains("SortedList", StringComparison.InvariantCultureIgnoreCase) ||
                            underlyingName.Contains("SortedDictionary", StringComparison.InvariantCultureIgnoreCase);

                if (efi.IsMap)
                {
                    if (fieldType.GenericTypeArguments[0] != typeof(string))
                    {
                        logger.LogIt(LogSeverity.Fatal, "i18n::Key of AVRO::MAP must be a string, error in field " + efi.FieldName);
                        throw new NotSupportedException();
                    }

                    efi.TypeCode = Type.GetTypeCode(fieldType.GenericTypeArguments[1]);

                    efi.AvroType = AvroFieldType.Map;


                    if (efi.TypeCode == TypeCode.Object)
                    {
                        efi.AvroType = AvroFieldType.MapWithRecordType;

                        efi.ComplexArrayOrMapType = fieldType.GenericTypeArguments[1];
                        efi.ImplementingClassName = efi.ComplexArrayOrMapType.Name;
                    }
                }
            }


            if (fieldType.IsClass && fieldType.IsArray)
            {
                efi.ImplementingClassName = fieldType.Name.Replace("[]", string.Empty, StringComparison.InvariantCultureIgnoreCase);

                if (efi.TypeCode == TypeCode.Object)
                {
                    efi.AvroType = AvroFieldType.ArrayWithRecordType;

                    efi.ComplexArrayOrMapType = fieldType.GetElementType();
                }
            }


            efi.AvroDefaultValue = efi.FieldInfo.GetCustomAttribute <AvroDefaultValueAttribute>()?.DefaultValue;
            efi.HasDefaultValue  = efi.AvroDefaultValue != null;


            var fsAttr = efi.FieldInfo.GetCustomAttribute <AvroFixedAttribute>();

            if (fsAttr != null)
            {
                efi.AvroType            = AvroFieldType.Fixed;
                efi.FixedFieldSize      = fsAttr.Size;
                efi.FixedFieldClassName = fsAttr.DataClassName;

                if (efi.TypeCode != TypeCode.Byte)
                {
                    logger.LogIt(LogSeverity.Fatal, "i18n::FIXED is not allowed for other types than type BYTE, field: " + efi.FieldName);
                    throw new NotSupportedException("FIXED is not allowed for other types than type BYTE, field: " + efi.FieldName);
                }

                if (string.IsNullOrWhiteSpace(efi.FixedFieldClassName))
                {
                    logger.LogIt(LogSeverity.Fatal, "i18n::You must provide a DataClassName via attribute, field: " + efi.FieldName);
                    throw new ArgumentException("You must provide a DataClassName via attribute, field: " + efi.FieldName);
                }
            }

            var aliasAttr = efi.FieldInfo.GetCustomAttribute <AvroAliasAttribute>()?.AliasList;

            if (aliasAttr != null)
            {
                efi.AliasList.AddRange(aliasAttr);
            }


            var docValueAttr = efi.FieldInfo.GetCustomAttribute <AvroDocAttribute>()?.DocValue;

            if (!string.IsNullOrWhiteSpace(docValueAttr))
            {
                efi.AvroDocValue = docValueAttr;
                efi.HasDocValue  = true;
            }


            var nsValueAttr = efi.FieldInfo.GetCustomAttribute <AvroNamespaceAttribute>()?.NamespaceValue;

            if (!string.IsNullOrWhiteSpace(nsValueAttr))
            {
                efi.AvroNameSpace = nsValueAttr;
                efi.HasNamespace  = true;
            }

            logger.LogIt(LogSeverity.Verbose, "i18n::Field {0} determined as {1}", efi.FieldName, efi.AvroType);
        }