Esempio n. 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AvroEnum"/> class.
 /// </summary>
 /// <param name="schema">The schema.</param>
 public AvroEnum(Schema.Schema schema)
 {
     this.schema = schema as EnumSchema;
     if (this.schema == null)
     {
         throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Enum schema expected."), "schema");
     }
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="AvroEnum"/> class.
 /// </summary>
 /// <param name="schema">The schema.</param>
 public AvroEnum(Schema.Schema schema)
 {
     this.schema = schema as EnumSchema;
     if (this.schema == null)
     {
         throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Enum schema expected."), "schema");
     }
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="AvroRecord"/> class.
 /// </summary>
 /// <param name="schema">The schema.</param>
 public AvroRecord(Schema.Schema schema)
 {
     this.schema = schema as RecordSchema;
     if (this.schema == null)
     {
         throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Expected record schema."), "schema");
     }
     this.values = new object[this.schema.Fields.Count];
 }
Esempio n. 4
0
        public ODataAvroWriter(ODataAvroOutputContext outputContext, Action<object> writeAction, Schema schema, bool writingFeed)
        {
            Debug.Assert(outputContext != null, "outputContext != null");
            Debug.Assert(writeAction != null, "flushAction != null");

            this.outputContext = outputContext;
            this.writeAction = writeAction;
            this.schema = schema;
            this.writingFeed = writingFeed;
            if (writingFeed)
            {
                this.entityObjectList = new List<AvroRecord>();
            }
        }
        /// <summary>
        /// Parses a schema and generates code from it and writes it to the stream.
        /// </summary>
        /// <param name="schema">The schema.</param>
        /// <param name="defaultNamespace">The default namespace.</param>
        /// <param name="forceNamespace">Determines whether the defaultNamespace should be used.</param>
        /// <param name="stream">The stream.</param>
        public static void Generate(Schema schema, string defaultNamespace, bool forceNamespace, Stream stream)
        {
            if (schema == null)
            {
                throw new ArgumentNullException("schema");
            }

            if (defaultNamespace == null)
            {
                throw new ArgumentNullException("defaultNamespace");
            }

            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            GeneratorCore.Generate(schema, defaultNamespace, forceNamespace, stream);
        }
        /// <summary>
        /// Reads the schema and generates the code to the stream.
        /// </summary>
        /// <param name="schema">The schema.</param>
        /// <param name="defaultNamespace">The default namespace.</param>
        /// <param name="forceNamespace">Determines whether the defaultNamespace should be used.</param>
        /// <param name="stream">The stream.</param>
        public static void Generate(Schema schema, string defaultNamespace, bool forceNamespace, Stream stream)
        {
            var recordSchema = schema as RecordSchema;
            var enumSchema = schema as EnumSchema;
            string code;
            if (recordSchema != null)
            {
                code = GenerateCore(recordSchema, defaultNamespace, forceNamespace);
            }
            else if (enumSchema != null)
            {
                code = GenerateCore(enumSchema, defaultNamespace, forceNamespace);
            }
            else
            {
                throw new ArgumentException("Unsupported schema: only record and enum schemas are supported.");
            }

            var encoding = new UTF8Encoding();
            byte[] bytes = encoding.GetBytes(code);
            stream.Write(bytes, 0, bytes.Length);
        }
Esempio n. 7
0
        private void WriteEntryImplementation(ODataEntry entry)
        {
            if (this.schema == null)
            {
                this.schema = this.outputContext.AvroWriter.UpdateSchema(entry, null, this.writingFeed);
            }

            var obj = (AvroRecord)ODataAvroConvert.FromODataObject(entry, this.schema);

            if (this.writingFeed)
            {
                this.entityObjectList.Add(obj);
            }
            else
            {
                this.currentEntityObject = obj;
            }
        }
 private void VisitDynamic(Schema schema)
 {
     this.VisitCore((dynamic)schema);
 }
 public void Visit(Schema schema)
 {
     this.visited.Clear();
     this.VisitDynamic(schema);
 }
Esempio n. 10
0
        public static object FromODataObject(object value, Schema schema)
        {
            var complexValue = value as ODataComplexValue;
            if (complexValue != null)
            {
                RecordSchema recordSchema = schema as RecordSchema;

                if (recordSchema == null)
                {
                    var unionSchema = schema as UnionSchema;
                    if (unionSchema != null)
                    {
                        recordSchema = unionSchema.Schemas.OfType<RecordSchema>().Single();
                    }
                    else
                    {
                        throw new ApplicationException("not supported schema found.");
                    }
                }

                var record = new AvroRecord(recordSchema);
                foreach (var prop in complexValue.Properties)
                {
                    if (prop.Value is ODataComplexValue)
                    {
                        continue;
                    }

                    record[prop.Name] = prop.Value;
                }

                return record;
            }

            var entry = value as ODataEntry;
            if (entry != null)
            {
                var record = new AvroRecord(schema);
                foreach (var property in entry.Properties)
                {
                    var recordSchema = (RecordSchema)schema;
                    RecordField field;
                    recordSchema.TryGetField(property.Name, out field);

                    record[property.Name] = FromODataObject(property.Value, field != null ? field.TypeSchema : null);
                }

                return record;
            }

            var error = value as ODataError;
            if (error != null)
            {
                var record = new AvroRecord(schema);
                record["ErrorCode"] = error.ErrorCode;
                record["Message"] = error.Message;
                return record;
            }

            var collection = value as ODataCollectionValue;
            if (collection != null)
            {
                if (collection.Items is IEnumerable<object>)
                {
                    return ((IEnumerable<object>)collection.Items).ToArray();
                }

                return collection.Items;
            }

            return value;
        }