Example #1
0
    public TypeRepoCollection(Serializer serializer, TypeSchema typeSchema) :
        base(serializer, typeSchema)
    {
        Type[] types = LoadableType.GetGenericArguments();
        if (types.Length > 0)
        {
            _elementType = types[0];
        }

        _addMethod = LoadableType.GetMethods()
                     .FirstOrDefault(m => m.Name == "Add" && m.GetParameters().Count() == 1);
    }
        public void BuildGuidSchema(Type type)
        {
            //Act
            var        builder = new ReflectionSchemaBuilder();
            TypeSchema schema  = builder.BuildSchema(type);


            //Assert
            Assert.IsType <UuidSchema>(schema);
            var uuidSchema = (UuidSchema)schema;

            Assert.NotNull(uuidSchema);
        }
Example #3
0
    public TypeRepoDictionary(Serializer serializer, TypeSchema typeSchema) :
        base(serializer, typeSchema)
    {
        Type[] types = LoadableType.GetGenericArguments();
        if (types.Length > 0)
        {
            _typeKey   = types[0];
            _typeValue = types[1];
        }

        _addMethod = LoadableType.GetMethods()
                     .Where(m => m.Name == "Add" && m.GetParameters().Count() == 2).FirstOrDefault();
    }
        public void BuildTimespanSchema(Type type)
        {
            //Act
            var        builder = new ReflectionSchemaBuilder();
            TypeSchema schema  = builder.BuildSchema(type);


            //Assert
            Assert.IsType <DurationSchema>(schema);
            var resolvedSchema = (DurationSchema)schema;

            Assert.NotNull(resolvedSchema);
        }
Example #5
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="ArraySchema" /> class.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="runtimeType">Type of the runtime.</param>
        /// <param name="attributes">The attributes.</param>
        internal ArraySchema(
            TypeSchema item,
            Type runtimeType,
            Dictionary <string, string> attributes)
            : base(runtimeType, attributes)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            this.itemSchema = item;
        }
        private byte[] ParseFixed(TypeSchema schema, string jsonObject)
        {
            var fixedSchema = (FixedSchema)schema;
            var result      = ConvertToBytes(jsonObject);

            if (result.Length != fixedSchema.Size)
            {
                throw new SerializationException(
                          string.Format(CultureInfo.InvariantCulture, "'{0}' size does not match the size of fixed schema node.", jsonObject));
            }

            return(result);
        }
Example #7
0
        /// <summary>
        ///     Converts current not to JSON according to the avro specification.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="seenSchemas">The seen schemas.</param>
        internal override void ToJsonSafe(JsonTextWriter writer, HashSet <NamedSchema> seenSchemas)
        {
            writer.WriteStartObject();
            writer.WriteProperty("name", Name);
            writer.WriteOptionalProperty("namespace", Namespace);
            writer.WriteOptionalProperty("doc", Doc);
            writer.WriteOptionalProperty("aliases", Aliases);
            writer.WritePropertyName("type");
            TypeSchema.ToJson(writer, seenSchemas);
            writer.WriteOptionalProperty("default", defaultValue, hasDefaultValue);

            writer.WriteEndObject();
        }
        public void BuildDateTimeSchema(Type type)
        {
            //Act
            var        builder = new ReflectionSchemaBuilder();
            TypeSchema schema  = builder.BuildSchema(type);


            //Assert
            Assert.IsType <TimestampMillisecondsSchema>(schema);
            var resolvedSchema = (TimestampMillisecondsSchema)schema;

            Assert.NotNull(resolvedSchema);
        }
Example #9
0
    public FieldSchema(TypeSchema typeSchema, BinaryReader reader)
    {
        OwnerTypeSchema = typeSchema;

        Load(reader);

        if (typeSchema.Type != null)
        {
            FieldInfo = typeSchema.Type.GetField(FieldName);
        }

        Initialize();
    }
        internal static string DetermineColumnName(IQueryBuilder query, string propertyName)
        {
            string[] properties = propertyName.Split('.');
            string   table      = String.Empty;
            string   column     = String.Empty;

            TypeSchema schema = SchemaCache.Current.GetSchema(query.QueriedType);

            for (int i = 0; i < properties.Length; i++)
            {
                if (i == properties.Length - 1)
                {
                    PropertySchema propertySchema = schema.FindPropertySchema(properties[i]);

                    if (propertySchema != null)
                    {
                        column = String.Format(CultureInfo.CurrentCulture, query.Context.ColumnFormat, propertySchema.ColumnName);
                    }
                    else
                    {
                        ParentSchema parentSchema = schema.FindParentSchema(properties[i]);

                        if (parentSchema != null)
                        {
                            column = String.Format(CultureInfo.CurrentCulture, query.Context.ColumnFormat, parentSchema.ColumnName);
                        }
                        else
                        {
                            throw new ObjectServerException(String.Format(CultureInfo.CurrentCulture, "Could not locate schema for {0}.{1}", schema.Type.FullName, properties[i]));
                        }
                    }

                    table = String.Format(CultureInfo.CurrentCulture, query.Context.TableFormat, schema.TableName);
                }
                else
                {
                    ParentSchema parentSchema = schema.FindParentSchema(properties[i]);

                    if (parentSchema == null)
                    {
                        throw new ObjectServerException(String.Format(CultureInfo.CurrentCulture, "Could not locate schema for {0}.{1}", schema.Type.FullName, properties[i]));
                    }

                    schema = SchemaCache.Current.GetSchema(parentSchema.Property.PropertyType);

                    query.AddParentJoin(parentSchema);
                }
            }

            return(String.Format(CultureInfo.CurrentCulture, query.Context.TableColumnFormat, table, column));
        }
Example #11
0
        private void AddRecordFields(
            IEnumerable <MemberSerializationInfo> members,
            Dictionary <string, NamedSchema> schemas,
            uint currentDepth,
            RecordSchema record)
        {
            int index = 0;

            foreach (MemberSerializationInfo info in members)
            {
                var property = info.MemberInfo as PropertyInfo;
                var field    = info.MemberInfo as FieldInfo;

                Type memberType;
                if (property != null)
                {
                    memberType = property.PropertyType;
                }
                else if (field != null)
                {
                    memberType = field.FieldType;
                }
                else
                {
                    throw new SerializationException(
                              string.Format(
                                  CultureInfo.InvariantCulture,
                                  "Type member '{0}' is not supported.",
                                  info.MemberInfo.GetType().Name));
                }

                TypeSchema fieldSchema = this.TryBuildUnionSchema(memberType, info.MemberInfo, schemas, currentDepth)
                                         ?? this.TryBuildFixedSchema(memberType, info.MemberInfo, record)
                                         ?? this.CreateSchema(info.Nullable, memberType, schemas, currentDepth + 1, info.DefaultValue?.GetType(), info.MemberInfo);



                var aliases = info
                              .Aliases
                              .ToList();
                var recordField = new RecordFieldSchema(
                    new NamedEntityAttributes(new SchemaName(info.Name), aliases, info.Doc),
                    fieldSchema,
                    SortOrder.Ascending,
                    info.HasDefaultValue,
                    info.DefaultValue,
                    info.MemberInfo,
                    index++);
                record.AddField(recordField);
            }
        }
Example #12
0
 private bool CreateSchema(string json_schema, out Exception exception)
 {
     exception = null;
     try
     {
         var schema = TypeSchema.Create(json_schema);
         _schema = schema;
     }
     catch (Exception e)
     {
         exception = e;
     }
     return(exception == null);
 }
Example #13
0
    public void Validate(List <TypeSchema> typeSchemas)
    {
        if (TypeIndex < 0)
        {
            return;
        }

        TypeSchema typeSchema = typeSchemas[TypeIndex];

        if (FieldInfo != null && typeSchema.Type != FieldInfo.FieldType.GetNonNullableType())
        {
            IsLoadable = false;
        }
    }
Example #14
0
 private static DataTable[] ConvertSchemaToDataTables(TypeSchema schema)
 {
     if (schema == null)
     {
         return(null);
     }
     IDataSourceViewSchema[] views = schema.GetViews();
     if (views == null)
     {
         return(null);
     }
     DataTable[] tableArray = new DataTable[views.Length];
     for (int i = 0; i < views.Length; i++)
     {
         IDataSourceViewSchema schema2 = views[i];
         tableArray[i] = new DataTable(schema2.Name);
         IDataSourceFieldSchema[] fields = schema2.GetFields();
         if (fields != null)
         {
             List <DataColumn> list = new List <DataColumn>();
             for (int j = 0; j < fields.Length; j++)
             {
                 IDataSourceFieldSchema schema3 = fields[j];
                 DataColumn             column  = new DataColumn {
                     AllowDBNull   = schema3.Nullable,
                     AutoIncrement = schema3.Identity,
                     ColumnName    = schema3.Name,
                     DataType      = schema3.DataType
                 };
                 if (column.DataType == typeof(string))
                 {
                     column.MaxLength = schema3.Length;
                 }
                 column.ReadOnly = schema3.IsReadOnly;
                 column.Unique   = schema3.IsUnique;
                 tableArray[i].Columns.Add(column);
                 if (schema3.PrimaryKey)
                 {
                     list.Add(column);
                 }
             }
             if (list.Count > 0)
             {
                 tableArray[i].PrimaryKey = list.ToArray();
             }
         }
     }
     return(tableArray);
 }
        public Func <IDecoder, T> GenerateDeserializer <T>(TypeSchema schema)
        {
            if (schema == null)
            {
                throw new ArgumentNullException("schema");
            }

            ParameterExpression decoder = Expression.Parameter(typeof(IDecoder), "decoder");

            Expression       result = schema.Serializer.BuildDeserializer(decoder);
            Type             resultingFunctionType = typeof(Func <,>).MakeGenericType(new[] { typeof(IDecoder), typeof(T) });
            LambdaExpression lambda = Expression.Lambda(resultingFunctionType, result, decoder);

            return(lambda.Compile() as Func <IDecoder, T>);
        }
Example #16
0
        internal BaseLineReader(Reader reader, byte[] syncDate, AbstractCodec codec, TypeSchema writeSchema, TypeSchema readSchema)
        {
            _reader      = reader;
            _syncDate    = syncDate;
            _codec       = codec;
            _writeSchema = writeSchema;
            _readSchema  = readSchema;

            if (_reader.IsReadToEnd())
            {
                return;
            }

            LoadNextDataBlock();
        }
        /// <summary>
        /// Parses a JSON string according to given schema and returns the corresponding object.
        /// </summary>
        /// <param name="schema">The schema.</param>
        /// <param name="json">The JSON object.</param>
        /// <returns>The object.</returns>
        internal object Parse(TypeSchema schema, string json)
        {
            if (this.parsersWithoutSchema.ContainsKey(schema.GetType()))
            {
                return(this.parsersWithoutSchema[schema.GetType()](json));
            }

            if (this.parsersWithSchema.ContainsKey(schema.GetType()))
            {
                return(this.parsersWithSchema[schema.GetType()](schema, json));
            }

            throw new SerializationException(
                      string.Format(CultureInfo.InvariantCulture, "Unknown schema type '{0}'.", schema.GetType()));
        }
        private AvroEnum ParseEnum(TypeSchema schema, string jsonObject)
        {
            var enumSchema = (EnumSchema)schema;

            if (!enumSchema.Symbols.Contains(jsonObject))
            {
                throw new SerializationException(
                          string.Format(CultureInfo.InvariantCulture, "'{0}' is not a valid enum value.", jsonObject));
            }

            return(new AvroEnum(schema)
            {
                Value = jsonObject
            });
        }
        public void BuildDecimalSchema(Type type, int precision, int scale)
        {
            //Act
            var        builder = new ReflectionSchemaBuilder();
            TypeSchema schema  = builder.BuildSchema(type);


            //Assert
            Assert.IsType <DecimalSchema>(schema);
            var decimalSchema = (DecimalSchema)schema;

            Assert.NotNull(schema);
            Assert.Equal(precision, decimalSchema.Precision);
            Assert.Equal(scale, decimalSchema.Scale);
        }
Example #20
0
        protected virtual object ResolveFixed(FixedSchema writerSchema, TypeSchema readerSchema, IReader d, Type type)
        {
            FixedSchema rs = (FixedSchema)readerSchema;

            AvroFixed ru = new AvroFixed(rs);

            byte[] bb = ((AvroFixed)ru).Value;
            d.ReadFixed(bb);

            if (type == typeof(Guid))
            {
                return(new Guid(ru.Value));
            }

            return(ru.Value);
        }
        private string ToJsonViaAvro <T>(T data, TypeSchema schema)
        {
            byte[] result;
            using (MemoryStream resultStream = new MemoryStream())
            {
                using (var writer = new Encoder(schema, resultStream, CodecType.Null))
                {
                    writer.Append(data);
                }
                result = resultStream.ToArray();
            }

            var avro2Json = AvroConvert.Avro2Json(result, schema.ToString());

            return(avro2Json);
        }
Example #22
0
        public AvroObject(byte[] avroBinary)
        {
            //TODO: Construtor using avroBinary
            MemoryStream ms = new MemoryStream(avroBinary);

            using (var reader = AvroContainer.CreateGenericReader(ms))
            {
                while (reader.MoveNext())
                {
                    foreach (dynamic record in reader.Current.Objects)
                    {
                    }
                }
                _schema = reader.Schema;
            }
        }
Example #23
0
        private TypeSchema Pop()
        {
            TypeSchema popped = _stack[_stack.Count - 1];
            _stack.RemoveAt(_stack.Count - 1);
            TypeSchema newValue = _stack.LastOrDefault();
            if (newValue != null)
            {
                _currentSchema = newValue.Schema;
            }
            else
            {
                _currentSchema = null;
            }

            return popped;
        }
        public Action <IEncoder, T> GenerateSerializer <T>(TypeSchema schema)
        {
            if (schema == null)
            {
                throw new ArgumentNullException("schema");
            }

            ParameterExpression instance = Expression.Parameter(typeof(T), "instance");
            ParameterExpression encoder  = Expression.Parameter(typeof(IEncoder), "encoder");

            var              result = schema.Serializer.BuildSerializer(encoder, instance);
            Type             resultingFunctionType = typeof(Action <,>).MakeGenericType(new[] { typeof(IEncoder), typeof(T) });
            LambdaExpression lambda = Expression.Lambda(resultingFunctionType, result, encoder, instance);

            return(lambda.Compile() as Action <IEncoder, T>);
        }
        private void CommitChildren(ServerObject obj, object key)
        {
            Trace.WriteLineIf(DebugOutput.Enabled, "Enumerating children");

            TypeSchema schema = SchemaCache.Current.GetSchema(obj.ServerObjectType);

            if (key != null)
            {
                foreach (ChildrenSchema childSchema in schema.ChildrenSchemas)
                {
                    ServerObjectCollection children = obj.Children.GetValue(childSchema.Property.Name);

                    if (children == null)
                    {
                        continue;
                    }

                    TypeSchema   childTypeSchema = SchemaCache.Current.GetSchema(childSchema.ChildType);
                    ParentSchema parentSchema    = childTypeSchema.FindParentSchema(childSchema.PropertyName);

                    foreach (ServerObject child in children)
                    {
                        if (child.Parents.GetValue(parentSchema.Property.Name) != obj)
                        {
                            continue;
                        }

                        child.Data.SetValue(parentSchema.Property.Name, key);
                    }
                }
            }

            foreach (ChildrenSchema childSchema in schema.ChildrenSchemas)
            {
                ServerObjectCollection children = obj.Children.GetValue(childSchema.Property.Name);

                if (children == null)
                {
                    continue;
                }

                foreach (ServerObject child in children)
                {
                    Commit(child, true);
                }
            }
        }
Example #26
0
        protected virtual object ResolveUnion(UnionSchema writerSchema, TypeSchema readerSchema, IReader d, Type type)
        {
            int        index = d.ReadUnionIndex();
            TypeSchema ws    = writerSchema.Schemas[index];

            if (readerSchema is UnionSchema unionSchema)
            {
                readerSchema = FindBranch(unionSchema, ws);
            }
            else
            if (!readerSchema.CanRead(ws))
            {
                throw new AvroException("Schema mismatch. Reader: " + _readerSchema + ", writer: " + _writerSchema);
            }

            return(Resolve(ws, readerSchema, d, type));
        }
Example #27
0
        /// <summary>
        /// Indicates whether to allow events when refreshing the schema.
        /// </summary>
        /// <param name="preferSilent"><see langword=" true"/> to allow events when refreshing the schema;
        /// <see langword=" false"/> to disable the DataSourceChanged and SchemaRefreshed events when refreshing the schema.</param>
        public override void RefreshSchema(bool preferSilent)
        {
            try
            {
                base.SuppressDataSourceEvents();
                Cursor cursor = Cursor.Current;
                try
                {
                    Cursor.Current = Cursors.WaitCursor;
                    Type type = GetType(base.Component.Site, DataObjectTypeName, preferSilent);

                    if (type == null)
                    {
                        return;
                    }

                    IDataSourceViewSchema[] schemas = new TypeSchema(type).GetViews();
                    if ((schemas == null) || (schemas.Length == 0))
                    {
                        DataSourceSchema = null;
                        return;
                    }

                    IDataSourceViewSchema newSchema = schemas[0];
                    IDataSourceViewSchema oldSchema = DataSourceSchema;
                    if (!ViewSchemasEquivalent(oldSchema, newSchema))
                    {
                        DataSourceSchema = newSchema;
                        OnSchemaRefreshed(EventArgs.Empty);
                    }
                }
                catch (Exception ex)
                {
                    DisplayErrorMessage(this.DataSourceControl.Site, String.Format(CultureInfo.CurrentCulture, Resources.CannotRefreshSchema, ex.Message));
                }
                finally
                {
                    Cursor.Current = cursor;
                }
            }
            finally
            {
                base.ResumeDataSourceEvents();
            }
        }
Example #28
0
        public void Resolve(DcSpace space)
        {
            if (!string.IsNullOrEmpty(SchemaName))
            {
                // 1. Resolve schema name
                TypeSchema = space.GetSchema(SchemaName);
                if (TypeSchema == null)
                {
                    return;                     // Cannot resolve
                }
                // 2. Resolve table name
                TypeTable = TypeSchema.GetSubTable(TypeName);
                if (TypeTable == null)
                {
                    return;                    // Cannot resolve
                }
            }
            else if (!string.IsNullOrEmpty(TypeName)) // No schema name (imcomplete info)
            {
                // 1. try to find the table in the mashup
                DcSchema mashup = space.GetSchemas().FirstOrDefault(x => x.GetSchemaKind() == DcSchemaKind.Dc);
                if (mashup != null)
                {
                    TypeTable = mashup.GetSubTable(TypeName);
                    if (TypeTable != null)
                    {
                        TypeSchema = mashup;
                        SchemaName = TypeSchema.Name; // We also reconstruct the name
                        return;
                    }
                }

                // 2. try to find the table in any other schema
                foreach (DcSchema schema in space.GetSchemas())
                {
                    TypeTable = schema.GetSubTable(TypeName);
                    if (TypeTable != null)
                    {
                        TypeSchema = schema;
                        SchemaName = TypeSchema.Name; // We also reconstruct the name
                        return;
                    }
                }
            }
        }
Example #29
0
        public void AvroRecord_CheckInvalidField()
        {
            var schema = TypeSchema.Create(
                @"{
                             ""type"":""record"",
                             ""name"":""testRecord"",
                             ""fields"":
                                       [
                                           { ""name"":""testField"", ""type"":""int"" },
                                       ]
                          }");
            var avroRecord = new AvroRecord(schema);

            Utilities.ShouldThrow <ArgumentException>(() => { var _ = avroRecord[null]; });
            Utilities.ShouldThrow <ArgumentNullException>(() => { avroRecord[null] = 1; });
            Utilities.ShouldThrow <ArgumentOutOfRangeException>(() => { var _ = avroRecord["InvalidField"]; });
            Utilities.ShouldThrow <ArgumentOutOfRangeException>(() => { avroRecord["InvalidField"] = 1; });
        }
Example #30
0
        public static TypeSchema GetItemSchema(TypeSchema schema)
        {
            var unionSchema = schema as UnionSchema;

            if (unionSchema != null)
            {
                schema = unionSchema.Schemas.OfType <ArraySchema>().SingleOrDefault();
            }

            var arraySchema = schema as ArraySchema;

            if (arraySchema != null)
            {
                return(((ArraySchema)AvroSerializer.CreateGeneric(arraySchema.ToString()).WriterSchema).ItemSchema);
            }

            throw new ApplicationException("Schema must be array schema");
        }
 private void Push(TypeSchema typeSchema)
 {
   _currentSchema = typeSchema.Schema;
   _stack.Add(typeSchema);
   _resolver.LoadedSchemas.Add(typeSchema.Schema);
 }