Beispiel #1
0
        Expression Struct(IParser parser, RuntimeSchema schema, bool isBase)
        {
            var metadata   = schema.HasValue ? Expression.Constant(schema.StructDef.metadata) : noMetadata;
            var baseSchema = schema.HasBase ? schema.GetBaseSchema() : RuntimeSchema.Empty;

            return(parser.Apply(new Transform(
                                    Begin: () => isBase ? writer.WriteBaseBegin(metadata) : writer.WriteStructBegin(metadata),
                                    End: () => isBase ? writer.WriteBaseEnd() : writer.WriteStructEnd(),
                                    Fields: schema.HasValue ?
                                    from field in schema.StructDef.fields
                                    select new Field(
                                        Id: field.id,
                                        Value: (fieldParser, fieldType) =>
                                        Expression.Block(
                                            writer.WriteFieldBegin(fieldType, field.id, field.metadata),
                                            Value(fieldParser, fieldType, schema.GetFieldSchema(field)),
                                            writer.WriteFieldEnd()),
                                        Omitted: () => writer.WriteFieldOmitted(field.type.id, field.id, field.metadata)) :
                                    null,
                                    UnknownField: (fieldParser, fieldType, fieldId) =>
                                    Expression.Block(
                                        writer.WriteFieldBegin(fieldType, fieldId, noMetadata),
                                        Value(fieldParser, fieldType),
                                        writer.WriteFieldEnd()),
                                    Base: baseParser =>
                                    baseSchema.HasValue ? Struct(baseParser, baseSchema, isBase: true) : Expression.Empty(),
                                    UnknownEnd: () => writer.WriteBaseEnd())));
        }
Beispiel #2
0
 protected PullParser(PullParser <T> that, RuntimeSchema schema, bool flatten)
 {
     Audit.ArgNotNull(that, "that");
     Audit.ArgRule(schema.HasValue, "PullParser requires runtime schema");
     this.schema  = schema;
     this.flatten = flatten;
 }
Beispiel #3
0
        Expression Value(IParser parser, Expression valueType, RuntimeSchema schema)
        {
            Debug.Assert(schema.HasValue);

            if (parser.IsBonded || (untaggedWriter && schema.IsBonded))
            {
                return(parser.Bonded(value =>
                                     writer.WriteBonded(PrunedExpression.Convert(value, typeof(IBonded)))));
            }


            if (schema.IsStruct)
            {
                return(GenerateSerialize(Struct, parser, schema));
            }

            if (schema.IsMap)
            {
                return(GenerateSerialize(Map, parser, schema));
            }

            if (schema.IsContainer)
            {
                return(GenerateSerialize(Container, parser, schema));
            }

            return(parser.Scalar(valueType, schema.TypeDef.id,
                                 value => writer.Write(value, schema.TypeDef.id)));
        }
Beispiel #4
0
 UntaggedParser(UntaggedReader <R> reader, DeferredSkip deferredSkip, RuntimeSchema schema)
 {
     this.reader       = reader;
     this.schema       = schema;
     this.deferredSkip = deferredSkip;
     hierarchyDepth    = schema.GetHierarchyDepth();
 }
Beispiel #5
0
 UntaggedParser(UntaggedReader <R> reader, DeferredSkip deferredSkip, RuntimeSchema schema, PayloadBondedFactory bondedFactory)
 {
     this.reader        = reader;
     this.schema        = schema;
     this.deferredSkip  = deferredSkip;
     this.bondedFactory = bondedFactory ?? NewBonded;
     hierarchyDepth     = schema.GetHierarchyDepth();
 }
Beispiel #6
0
            public Deserializer <R> Deserializer <R, T>(RuntimeSchema schema)
            {
                var parser = schema.HasValue
                                 ? ParserFactory <R> .Create(schema, PayloadBondedFactory)
                                 : ParserFactory <R> .Create(typeof(T), PayloadBondedFactory);

                return(new Deserializer <R>(typeof(T), parser, Factory, false));
            }
Beispiel #7
0
        Transcoder(RuntimeSchema schema, IParser parser)
        {
            var serializerTransform = SerializerGeneratorFactory <R, W> .Create(
                (r, w, i) => transcode[i](r, w), schema);

            var expressions = serializerTransform.Generate(parser);

            debugView = DebugViewHelper.ToString(expressions);
        }
        public Expression Apply(ITransform transform)
        {
            pairs.Add(new TransformSchemaPair(transform, schema));

            if (schema.HasBase)
            {
                schema = schema.GetBaseSchema();
                transform.Base(this);
            }

            return(Expression.Empty());
        }
Beispiel #9
0
        public Expression Apply(ITransform transform)
        {
            pairs.Add(new TransformSchemaPair(transform, schema));

            if (schema.HasBase)
            {
                schema = schema.GetBaseSchema();
                transform.Base(this);
            }

            return Expression.Empty();
        }
Beispiel #10
0
        internal static int GetHierarchyDepth(this RuntimeSchema schema)
        {
            if (!schema.IsStruct)
            {
                return(0);
            }

            var depth = 0;

            for (var type = schema.TypeDef; type != null; type = schema.SchemaDef.structs[type.struct_def].base_def)
            {
                depth++;
            }
            return(depth);
        }
Beispiel #11
0
        Expression Container(IParser parser, RuntimeSchema schema)
        {
            var expectedValueType = schema.HasValue ? schema.TypeDef.element.id : (BondDataType?)null;

            return(parser.Container(expectedValueType,
                                    (valueParser, elementType, next, count) =>
            {
                var body = ControlExpression.While(next,
                                                   Expression.Block(
                                                       writer.WriteItemBegin(),
                                                       schema.HasValue ?
                                                       Value(valueParser, elementType, schema.GetElementSchema()) :
                                                       Value(valueParser, elementType),
                                                       writer.WriteItemEnd()));

                var blob = parser.Blob(count);
                if (blob != null)
                {
                    body = PrunedExpression.IfThenElse(
                        Expression.Equal(elementType, Expression.Constant(BondDataType.BT_INT8)),
                        writer.WriteBytes(blob),
                        body);

                    // For binary protocols we can write blob directly using protocols's WriteBytes
                    // even if the container is not a blob (blob is BT_LIST of BT_INT8).
                    if (binaryWriter)
                    {
                        body = PrunedExpression.IfThenElse(
                            Expression.Equal(elementType, Expression.Constant(BondDataType.BT_UINT8)),
                            writer.WriteBytes(blob),
                            body);
                    }
                }

                return Expression.Block(
                    writer.WriteContainerBegin(count, elementType),
                    body,
                    writer.WriteContainerEnd());
            }));
        }
Beispiel #12
0
        static void Main()
        {
            var output = new OutputBuffer();
            var writer = new CompactBinaryWriter <OutputBuffer>(output);

            // Get runtime schema for type Example and serialize SchemaDef
            Serialize.To(writer, Schema <Example> .RuntimeSchema.SchemaDef);

            var input  = new InputBuffer(output.Data);
            var reader = new CompactBinaryReader <InputBuffer>(input);

            var schemaDef = Deserialize <SchemaDef> .From(reader);

            var schema = new RuntimeSchema(schemaDef);

            Debug.Assert(schema.IsStruct);
            Debug.Assert(schema.StructDef.metadata.qualified_name == "Examples.Example");
            Debug.Assert(schema.StructDef.metadata.attributes["StructAttribute"] == "Value of the attribute");
            Debug.Assert(schema.StructDef.fields[0].metadata.attributes["FieldAttribute"] == "Value of the attribute");
            Debug.Assert(schema.StructDef.fields[0].type.key.id == BondDataType.BT_UINT32);
            Debug.Assert(schema.SchemaDef.structs[1].fields[0].metadata.default_value.string_value == "this is a string");
        }
Beispiel #13
0
        Expression Map(IParser parser, RuntimeSchema schema)
        {
            var expectedValueType = schema.HasValue ? schema.TypeDef.element.id : (BondDataType?)null;
            var expectedKeyType   = schema.HasValue ? schema.TypeDef.key.id : (BondDataType?)null;

            return(parser.Map(expectedKeyType, expectedValueType,
                              (keyParser, valueParser, keyType, valueType, nextKey, nextValue, count) =>
                              Expression.Block(
                                  writer.WriteContainerBegin(count, keyType, valueType),
                                  ControlExpression.While(nextKey,
                                                          Expression.Block(
                                                              writer.WriteItemBegin(),
                                                              schema.HasValue ?
                                                              Value(keyParser, keyType, schema.GetKeySchema()) :
                                                              Value(keyParser, keyType),
                                                              writer.WriteItemEnd(),
                                                              nextValue,
                                                              writer.WriteItemBegin(),
                                                              schema.HasValue ?
                                                              Value(valueParser, valueType, schema.GetElementSchema()) :
                                                              Value(valueParser, valueType),
                                                              writer.WriteItemEnd())),
                                  writer.WriteContainerEnd())));
        }
 public TestSerializer(Expression <Action <R, W, int> > deferred, RuntimeSchema schema)
 {
 }
Beispiel #15
0
 public TaggedParser(RuntimeSchema schema)
 {
     isBase      = false;
     baseParser  = new TaggedParser <R>(this, isBase: true);
     fieldParser = this;
 }
Beispiel #16
0
 protected XmlParser(XmlParser <R> that, RuntimeSchema schema, bool flatten) : base(that, schema, flatten)
 {
     reader = that.reader;
 }
Beispiel #17
0
 protected XmlParser(RuntimeSchema schema, bool flatten) : base(schema, flatten)
 {
 }
Beispiel #18
0
 public TransformSchemaPair(ITransform transform, RuntimeSchema schema)
 {
     Transform = transform;
     Schema = schema;
 }
 public FlatteningParser(RuntimeSchema rootSchema)
 {
     schema = rootSchema;
 }
Beispiel #20
0
 public SerializerTransform(Expression <Action <R, W, int> > deferredSerialize, RuntimeSchema schema)
     : base(deferredSerialize)
 {
     runtimeSchema = schema;
 }
 public TransformSchemaPair(ITransform transform, RuntimeSchema schema)
 {
     Transform = transform;
     Schema    = schema;
 }
Beispiel #22
0
 Expression Struct(IParser parser, RuntimeSchema schema)
 {
     return(Struct(parser, schema, false));
 }
Beispiel #23
0
 public UntaggedParser(RuntimeSchema schema)
     : this(new UntaggedReader <R>(), new DeferredSkip(), schema)
 {
     Audit.ArgRule(schema.HasValue, "UntaggedParser requires runtime schema");
 }
Beispiel #24
0
 UntaggedParser(UntaggedParser <R> that, RuntimeSchema schema)
     : this(that.reader, that.deferredSkip, schema, that.bondedFactory)
 {
 }
Beispiel #25
0
 public UntaggedParser(RuntimeSchema schema)
     : this(schema, null)
 {
 }
 protected SimpleJsonParser(SimpleJsonParser <R> that, RuntimeSchema schema)
     : base(that, schema, flatten: true)
 {
 }
Beispiel #27
0
 public Transcoder(RuntimeSchema schema)
     : this(schema, ParserFactory <R> .Create(schema))
 {
 }
Beispiel #28
0
 public SimpleXmlParser(RuntimeSchema schema)
     : base(schema, flatten: true)
 {
 }
Beispiel #29
0
        Expression GenerateSerialize(SerializeWithSchema serializeWithSchema, IParser parser, RuntimeSchema schema)
        {
            Debug.Assert(schema.HasValue);

            Serialize serialize;

            if (!serializeDelegates.TryGetValue(schema, out serialize))
            {
                serialize = serializeDelegates[schema] = p => serializeWithSchema(p, schema);
            }
            // Transcoding from tagged protocol with runtime schema generates enormous expression tree
            // and for large schemas JIT fails to compile resulting lambda (InvalidProgramException).
            // As a workaround we don't inline nested serialize expressions in this case.
            var inline = !typeof(ITaggedProtocolReader).IsAssignableFrom(parser.ReaderParam.Type);

            inline = inline && (this.inlineNested || !schema.IsStruct);

            return(GenerateSerialize(serialize, parser, writer.Param, inline));
        }
Beispiel #30
0
 public UntaggedParser(RuntimeSchema schema, PayloadBondedFactory bondedFactory)
     : this(new UntaggedReader <R>(), new DeferredSkip(), schema, bondedFactory)
 {
     Audit.ArgRule(schema.HasValue, "UntaggedParser requires runtime schema");
 }
Beispiel #31
0
 private SimpleXmlParser(XmlParser <R> that, RuntimeSchema schema)
     : base(that, schema, flatten: true)
 {
 }
Beispiel #32
0
 public SerializerTransform(Expression <Action <R, W, int> > deferredSerialize, RuntimeSchema schema, bool inlineNested = true)
     : base(deferredSerialize)
 {
     runtimeSchema     = schema;
     this.inlineNested = inlineNested;
 }
Beispiel #33
0
 public FlatteningParser(RuntimeSchema rootSchema)
 {
     schema = rootSchema;
 }