コード例 #1
0
        public virtual void TestAllowNullBytesSchemaEncodeAndDecode()
        {
            var fooAvroSchema = AvroSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).Build());

            var barAvroSchema = AvroSchema <Bar> .Of(ISchemaDefinition <Bar> .Builder().WithPojo(typeof(Bar)).Build());

            var bar = new Bar
            {
                Field1 = true
            };

            var foo = new Foo
            {
                Field1 = "field1",
                Field2 = "field2",
                Field3 = "3",
                Field4 = bar,
                Color  = Color.RED
            };

            var fooBytes = fooAvroSchema.Encode(foo);
            var barBytes = barAvroSchema.Encode(bar);

            var encodeBytes = ISchema <object> .KvBytes().Encode(new KeyValue <byte[], byte[]>(fooBytes, barBytes));

            var decodeKV = ISchema <object> .KvBytes().Decode(encodeBytes);

            var fooBack = fooAvroSchema.Decode(decodeKV.Key);
            var barBack = barAvroSchema.Decode(decodeKV.Value);

            Assert.True(foo.Equals(fooBack));
            Assert.True(bar.Equals(barBack));
        }
コード例 #2
0
        public virtual void TestInlineKeyValueEncodingTypeSchemaEncodeAndDecode()
        {
            var fooSchema = AvroSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).Build());

            var barSchema = AvroSchema <Bar> .Of(ISchemaDefinition <Bar> .Builder().WithPojo(typeof(Bar)).Build());

            var keyValueSchema = ISchema <object> .KeyValue(fooSchema, barSchema, KeyValueEncodingType.INLINE);


            var bar = new Bar
            {
                Field1 = true
            };

            var foo = new Foo
            {
                Field1 = "field1",
                Field2 = "field2",
                Field3 = "3",
                Field4 = bar,
                Color  = Color.RED
            };

            // Check kv.encoding.type INLINE
            var encodeBytes = keyValueSchema.Encode(new KeyValue <Foo, Bar>(foo, bar));

            Assert.True(encodeBytes.Length > 0);
            var keyValue = keyValueSchema.Decode(encodeBytes);
            var fooBack  = keyValue.Key;
            var barBack  = keyValue.Value;

            Assert.True(foo.Equals(fooBack));
            Assert.True(bar.Equals(barBack));
        }
コード例 #3
0
        public virtual void TestJsonSchemaCreate()
        {
            var fooSchema = JSONSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).WithAlwaysAllowNull(false).Build());

            var barSchema = JSONSchema <Bar> .Of(ISchemaDefinition <Bar> .Builder().WithPojo(typeof(Bar)).WithAlwaysAllowNull(false).Build());

            var keyValueSchema1 = ISchema <object> .KeyValue(fooSchema, barSchema);

            var keyValueSchema2 = ISchema <object> .KeyValue(JSONSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).WithAlwaysAllowNull(false).Build()), JSONSchema <Bar> .Of(ISchemaDefinition <Bar> .Builder().WithPojo(typeof(Bar)).WithAlwaysAllowNull(false).Build()));

            var keyValueSchema3 = ISchema <object> .KeyValue(JSONSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).WithAlwaysAllowNull(false).Build()), JSONSchema <Bar> .Of(ISchemaDefinition <Bar> .Builder().WithPojo(typeof(Bar)).WithAlwaysAllowNull(false).Build()));

            Assert.Equal(keyValueSchema1.SchemaInfo.Type, SchemaType.KeyValue);
            Assert.Equal(keyValueSchema2.SchemaInfo.Type, SchemaType.KeyValue);
            Assert.Equal(keyValueSchema3.SchemaInfo.Type, SchemaType.KeyValue);

            Assert.Equal(((KeyValueSchema <Foo, Bar>)keyValueSchema1).KeySchema.SchemaInfo.Type, SchemaType.JSON);
            Assert.Equal(((KeyValueSchema <Foo, Bar>)keyValueSchema1).ValueSchema.SchemaInfo.Type, SchemaType.JSON);
            Assert.Equal(((KeyValueSchema <Foo, Bar>)keyValueSchema2).KeySchema.SchemaInfo.Type, SchemaType.JSON);
            Assert.Equal(((KeyValueSchema <Foo, Bar>)keyValueSchema2).ValueSchema.SchemaInfo.Type, SchemaType.JSON);
            Assert.Equal(((KeyValueSchema <Foo, Bar>)keyValueSchema3).KeySchema.SchemaInfo.Type, SchemaType.JSON);
            Assert.Equal(((KeyValueSchema <Foo, Bar>)keyValueSchema3).ValueSchema.SchemaInfo.Type, SchemaType.JSON);

            var schemaInfo1 = Encoding.UTF8.GetString(keyValueSchema1.SchemaInfo.Schema);
            var schemaInfo2 = Encoding.UTF8.GetString(keyValueSchema2.SchemaInfo.Schema);
            var schemaInfo3 = Encoding.UTF8.GetString(keyValueSchema3.SchemaInfo.Schema);

            Assert.Equal(schemaInfo1, schemaInfo2);
            Assert.Equal(schemaInfo1, schemaInfo3);
        }
コード例 #4
0
        public static JSONSchema <T> Of(ISchemaDefinition <T> schemaDefinition)
        {
            ISchemaReader <T> Reader = schemaDefinition.SchemaReaderOpt.GetOrElse(new JsonReader <T>(_jsonMapper));
            ISchemaWriter <T> Writer = schemaDefinition.SchemaWriterOpt.GetOrElse(new JsonWriter <T>(_jsonMapper));

            return(new JSONSchema <T>(SchemaUtils.ParseSchemaInfo(schemaDefinition, SchemaType.JSON), schemaDefinition.Pojo, Reader, Writer));
        }
コード例 #5
0
        public virtual void TestNotAllowNullSchemaEncodeAndDecode()
        {
            var keyValueSchema = ISchema <object> .KeyValue(JSONSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).WithAlwaysAllowNull(false).Build())
                                                            , JSONSchema <Bar> .Of(ISchemaDefinition <Bar> .Builder().WithPojo(typeof(Bar)).WithAlwaysAllowNull(false).Build()));

            var bar = new Bar
            {
                Field1 = true
            };

            var foo = new Foo
            {
                Field1 = "field1",
                Field2 = "field2",
                Field3 = "3",
                Field4 = bar,
                Color  = Color.RED
            };

            var encodeBytes = keyValueSchema.Encode(new KeyValue <Foo, Bar>(foo, bar));

            Assert.True(encodeBytes.Length > 0);

            var keyValue = keyValueSchema.Decode(encodeBytes);
            var fooBack  = keyValue.Key;
            var barBack  = keyValue.Value;

            Assert.True(foo.Equals(fooBack));
            Assert.True(bar.Equals(barBack));
        }
コード例 #6
0
ファイル: MessageTest.cs プロジェクト: eaba/SharpPulsar
        public virtual void TestSeparatedGetProducerDataAssigned()
        {
            var fooSchema = AvroSchema <SchemaTestUtils.Foo> .Of(ISchemaDefinition <SchemaTestUtils.Foo> .Builder().WithPojo(typeof(SchemaTestUtils.Foo)).Build());

            var barSchema = AvroSchema <SchemaTestUtils.Bar> .Of(ISchemaDefinition <SchemaTestUtils.Bar> .Builder().WithPojo(typeof(SchemaTestUtils.Bar)).Build());

            var keyValueSchema = ISchema <int> .KeyValue(fooSchema, barSchema, KeyValueEncodingType.SEPARATED);

            var foo = new SchemaTestUtils.Foo
            {
                Field1 = "field1",
                Field2 = "field2",
                Field3 = 3
            };
            var bar = new SchemaTestUtils.Bar
            {
                Field1 = true
            };

            // Check kv.encoding.type SPRAERATE
            var encodeBytes = keyValueSchema.Encode(new KeyValue <SchemaTestUtils.Foo, SchemaTestUtils.Bar>(foo, bar));
            var builder     = new MessageMetadata();

            builder.ProducerName           = "separated";
            builder.PartitionKey           = Convert.ToBase64String(fooSchema.Encode(foo));
            builder.PartitionKeyB64Encoded = true;
            var msg = Message <KeyValue <SchemaTestUtils.Foo, SchemaTestUtils.Bar> > .Create(builder, new ReadOnlySequence <byte>(encodeBytes), keyValueSchema);

            var keyValue = msg.Value;

            Assert.Equal(keyValue.Key, foo);
            Assert.Equal(keyValue.Value, bar);
            Assert.True(builder.ShouldSerializePartitionKey());
        }
コード例 #7
0
ファイル: MessageTest.cs プロジェクト: eaba/SharpPulsar
        public virtual void TestDefaultGetProducerDataAssigned()
        {
            var fooSchema = AvroSchema <SchemaTestUtils.Foo> .Of(ISchemaDefinition <SchemaTestUtils.Foo> .Builder().WithPojo(typeof(SchemaTestUtils.Foo)).Build());

            var barSchema = AvroSchema <SchemaTestUtils.Bar> .Of(ISchemaDefinition <SchemaTestUtils.Bar> .Builder().WithPojo(typeof(SchemaTestUtils.Bar)).Build());

            var keyValueSchema = ISchema <object> .KeyValue(fooSchema, barSchema);

            var foo = new SchemaTestUtils.Foo
            {
                Field1 = "field1",
                Field2 = "field2",
                Field3 = 3
            };
            var bar = new SchemaTestUtils.Bar
            {
                Field1 = true
            };

            // // Check kv.encoding.type default, not set value
            var encodeBytes = keyValueSchema.Encode(new KeyValue <SchemaTestUtils.Foo, SchemaTestUtils.Bar>(foo, bar));
            var builder     = new MessageMetadata();

            builder.ProducerName = "default";

            var msg = Message <KeyValue <SchemaTestUtils.Foo, SchemaTestUtils.Bar> > .Create(builder, new ReadOnlySequence <byte>(encodeBytes), keyValueSchema);

            var keyValue = msg.Value;

            Assert.Equal(keyValue.Key, foo);
            Assert.Equal(keyValue.Value, bar);
            Assert.False(builder.ShouldSerializePartitionKey());
        }
コード例 #8
0
        public void AddUndefinedObjects( ISchemaDefinition src,
                                         ISchemaDefinition edustructuresSchema )
        {
            IDictionary srcHash = src.GetAllEnums();
            foreach ( string enumKey in srcHash.Keys ) {
                if ( edustructuresSchema.GetEnum( enumKey ) == null ) {

                    // Exclude YesNo enums
                    EnumDef srcDef = src.GetEnum( enumKey );
                    if( srcDef.Values.Count < 4 && srcDef.ContainsValue( "Yes") && srcDef.ContainsValue( "No" ) )
                    {
                        continue;
                    }

                    AddUndefinedEnum( srcHash[enumKey] as EnumDef, edustructuresSchema.Version );
                }
            }

            srcHash = src.GetAllObjects();
            foreach ( string key in srcHash.Keys ) {
                if ( key.StartsWith( "State" ) || key.StartsWith( "Country" ) ) {
                    continue;
                }
                System.Diagnostics.Debug.Assert(key != "LAInfo");
                if ( edustructuresSchema.GetObject( key ) == null ) {

                    AddUndefinedObject( srcHash[key] as ObjectDef, edustructuresSchema.Version );
                }
            }

            saveChanges();
        }
コード例 #9
0
        public GenericAvroReaderTest()
        {
            fooSchema = AvroSchema <SchemaTestUtils.Foo> .Of(typeof(SchemaTestUtils.Foo));

            fooV2Schema = AvroSchema <SchemaTestUtils.FooV2> .Of(typeof(SchemaTestUtils.FooV2));

            fooSchemaNotNull = AvroSchema <SchemaTestUtils.Foo> .Of(ISchemaDefinition <SchemaTestUtils.Foo> .Builder().WithAlwaysAllowNull(false).WithPojo(typeof(SchemaTestUtils.Foo)).Build());

            fooOffsetSchema = AvroSchema <SchemaTestUtils.Foo> .Of(typeof(SchemaTestUtils.Foo));

            fooOffsetSchema.SchemaInfo.Properties.Add(GenericAvroSchema.OFFSET_PROP, "5");
            var prop = fooOffsetSchema.AvroSchema.GetProperty(GenericAvroSchema.OFFSET_PROP);

            foo = new SchemaTestUtils.Foo
            {
                Field1          = "foo1",
                Field2          = "bar1",
                Field4          = new SchemaTestUtils.Bar(),
                FieldUnableNull = "notNull"
            };

            fooV2 = new SchemaTestUtils.FooV2
            {
                Field1 = ("foo1"),
                Field3 = (10)
            };
        }
コード例 #10
0
ファイル: AvroSchemaTest.cs プロジェクト: eaba/SharpPulsar
        public virtual void TestSchemaDefinition()
        {
            var schema1 = typeof(DefaultStruct).GetSchema();
            var schema2 = AvroSchema <StructWithAnnotations> .Of(typeof(StructWithAnnotations));

            var schemaDef1 = schema1.ToString();
            var schemaDef2 = Encoding.UTF8.GetString(schema2.SchemaInfo.Schema);

            Assert.NotEqual(schemaDef1, schemaDef2);

            var schema3 = AvroSchema <StructWithAnnotations> .Of(ISchemaDefinition <StructWithAnnotations> .Builder().WithJsonDef(schemaDef1).Build());

            var schemaDef3 = Encoding.UTF8.GetString(schema3.SchemaInfo.Schema);

            Assert.True(schemaDef1.Contains("DefaultStruct") && schemaDef3.Contains("DefaultStruct"));
            Assert.NotEqual(schemaDef2, schemaDef3);

            var @struct = new StructWithAnnotations
            {
                Field1 = 5678
            };

            // schema2 is using the schema generated from POJO,
            // it allows field2 to be nullable, and field3 has default value.
            schema2.Encode(@struct);
            // schema3 is using the schema passed in, which doesn't allow nullable
            schema3.Encode(@struct);
        }
コード例 #11
0
        public virtual void TestNotAllowNullEncodeAndDecode()
        {
            var jsonSchema = JSONSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).WithAlwaysAllowNull(false).Build());

            var foo1 = new Foo
            {
                Field1 = "foo1",
                Field2 = "bar1",
                Field4 = new Bar()
            };

            var foo2 = new Foo
            {
                Field1 = "foo2",
                Field2 = "bar2"
            };

            var bytes1  = jsonSchema.Encode(foo1);
            var object1 = jsonSchema.Decode(bytes1);

            Assert.True(bytes1.Length > 0);
            Assert.True(object1.Equals(foo1));

            try
            {
                jsonSchema.Encode(foo2);
            }
            catch (Exception e)
            {
                Assert.True(e is SchemaSerializationException);
            }
        }
コード例 #12
0
        public virtual void TestFillParametersToSchemainfo()
        {
            var fooSchema = AvroSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).Build());

            var barSchema = AvroSchema <Bar> .Of(ISchemaDefinition <Bar> .Builder().WithPojo(typeof(Bar)).Build());

            fooSchema.SchemaInfo.Name = "foo";
            fooSchema.SchemaInfo.Type = SchemaType.AVRO;
            IDictionary <string, string> keyProperties = new Dictionary <string, string>
            {
                ["foo.key1"] = "value",
                ["foo.key2"] = "value"
            };

            fooSchema.SchemaInfo.Properties = keyProperties;
            barSchema.SchemaInfo.Name       = "bar";
            barSchema.SchemaInfo.Type       = SchemaType.AVRO;
            IDictionary <string, string> valueProperties = new Dictionary <string, string>
            {
                ["bar.key"] = "key"
            };

            barSchema.SchemaInfo.Properties = valueProperties;
            var keyValueSchema1 = ISchema <object> .KeyValue(fooSchema, barSchema);

            Assert.Equal("foo", keyValueSchema1.SchemaInfo.Properties["key.schema.name"]);
            Assert.Equal(SchemaType.AVRO.ToString(), keyValueSchema1.SchemaInfo.Properties["key.schema.type"]);
            Assert.Equal(@"{""foo.key1"":""value"",""foo.key2"":""value""}", keyValueSchema1.SchemaInfo.Properties["key.schema.properties"]);
            Assert.Equal("bar", keyValueSchema1.SchemaInfo.Properties["value.schema.name"]);
            Assert.Equal(SchemaType.AVRO.ToString(), keyValueSchema1.SchemaInfo.Properties["value.schema.type"]);
            Assert.Equal(@"{""bar.key"":""key""}", keyValueSchema1.SchemaInfo.Properties["value.schema.properties"]);
        }
コード例 #13
0
        private IFieldSet BuildExternalMetadataFieldSet(IContentLibraryItem eclItem)
        {
            string externalMetadataXml = eclItem.MetadataXml;

            if (string.IsNullOrEmpty(externalMetadataXml))
            {
                // No external metadata available; nothing to do.
                return(null);
            }

            ISchemaDefinition externalMetadataSchema = eclItem.MetadataXmlSchema;

            if (externalMetadataSchema == null)
            {
                _log.Warning(string.Format("ECL Item '{0}' has external metadata, but no schema defining it.", eclItem.Id));
                return(null);
            }

            try
            {
                XmlDocument externalMetadataDoc = new XmlDocument();
                externalMetadataDoc.LoadXml(externalMetadataXml);
                IFieldSet result = CreateExternalMetadataFieldSet(externalMetadataSchema.Fields, externalMetadataDoc.DocumentElement);

                _log.Debug(string.Format("ECL Item '{0}' has external metadata: {1}", eclItem.Id, string.Join(", ", result.Keys)));
                return(result);
            }
            catch (Exception ex)
            {
                _log.Error("An error occurred while parsing the external metadata for ECL Item " + eclItem.Id);
                _log.Error(ex.Message);
                return(null);
            }
        }
コード例 #14
0
        public GenericAvroSchemaTest()
        {
            var avroFooV2Schema = AvroSchema <SchemaTestUtils.FooV2> .Of(ISchemaDefinition <SchemaTestUtils.FooV2> .Builder().WithAlwaysAllowNull(false).WithPojo(typeof(SchemaTestUtils.FooV2)).Build());

            //var avroFooSchema = AvroSchema<SchemaTestUtils.Foo>.Of(ISchemaDefinition<SchemaTestUtils.Foo>.Builder().WithAlwaysAllowNull(false).WithPojo(typeof(SchemaTestUtils.Foo)).Build());
            writerSchema = new GenericAvroSchema(avroFooV2Schema.SchemaInfo);
            readerSchema = new GenericAvroSchema(avroFooV2Schema.SchemaInfo);
        }
コード例 #15
0
 protected override void BuildMetadataXmlSchema(ISchemaDefinition schema)
 {
     schema.Fields.Add(EclProvider.HostServices.CreateMultiLineTextFieldDefinition("Description", "Description", 0, 1, 7));
     schema.Fields.Add(EclProvider.HostServices.CreateSingleLineTextFieldDefinition("Manufacturer", "Manufacturer", 0, 1));
     schema.Fields.Add(EclProvider.HostServices.CreateSingleLineTextFieldDefinition("Purchasable", "Purchasable", 0, 1));
     schema.Fields.Add(EclProvider.HostServices.CreateNumberFieldDefinition("Price", "Price", 0, 1));
     schema.Fields.Add(EclProvider.HostServices.CreateNumberFieldDefinition("StockLevel", "StockLevel", 0, 1));
     schema.Fields.Add(EclProvider.HostServices.CreateNumberFieldDefinition("AverageRating", "AverageRating", 0, 1));
 }
コード例 #16
0
        public static AvroSchema <T> Of(ISchemaDefinition <T> schemaDefinition)
        {
            if (schemaDefinition.SchemaReaderOpt.HasValue && schemaDefinition.SchemaWriterOpt.HasValue)
            {
                return(new AvroSchema <T>(schemaDefinition.SchemaReaderOpt.Value, schemaDefinition.SchemaWriterOpt.Value, SchemaUtils.ParseSchemaInfo(schemaDefinition, SchemaType.AVRO)));
            }

            return(new AvroSchema <T>(SchemaUtils.ParseSchemaInfo(schemaDefinition, SchemaType.AVRO)));
        }
コード例 #17
0
        internal ExtensionSchemaDefinition(ISchemaDefinition definition, ISchema extendedSchema, SchemaConstraintExtensionMode mode)
            : base(definition.SchemaName)
        {
            DebugContract.Requires(extendedSchema, "extendedSchema");
            DebugContract.Requires(definition, "definition");

            _definition     = definition;
            _extendedSchema = extendedSchema;
            _mode           = mode;
        }
コード例 #18
0
        public virtual void TestNotAllowNullSchema()
        {
            var jsonSchema = JSONSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).WithAlwaysAllowNull(false).Build());

            Assert.Equal(jsonSchema.SchemaInfo.Type, SchemaType.JSON);
            var schemaJson = jsonSchema.SchemaInfo.SchemaDefinition;
            var schema     = Avro.Schema.Parse(schemaJson);

            Assert.NotNull(schema);
        }
コード例 #19
0
		public static Avro.Schema ExtractAvroSchema<T>(ISchemaDefinition<T> schemaDefinition, Type pojo)
		{
			try
			{
				return ParseAvroSchema(pojo.GetField("SCHEMA$").GetValue(null).ToString());
			}
			catch (Exception ignored) when (ignored is ArgumentException)
			{
				//return schemaDefinition.AlwaysAllowNull ? ReflectData.AllowNull.get().getSchema(pojo) : ReflectData.get().getSchema(pojo);
				return Avro.Schema.Parse(pojo.GetSchema());
			}
		}
コード例 #20
0
		public static Avro.Schema CreateAvroSchema<T>(ISchemaDefinition<T> schemaDefinition)
		{
			var pojo = schemaDefinition.Pojo;

			if (!string.IsNullOrWhiteSpace(schemaDefinition.JsonDef))
			{
				return ParseAvroSchema(schemaDefinition.JsonDef);
			}

			var schema = pojo.GetSchema();
			return ParseAvroSchema(schema);

		}
コード例 #21
0
ファイル: AvroSchemaTest.cs プロジェクト: eaba/SharpPulsar
        public void TestDateTimeDecimalLogicalType()
        {
            var avroSchema = AvroSchema <LogicalMessage> .Of(ISchemaDefinition <LogicalMessage> .Builder().WithPojo(typeof(LogicalMessage)).WithJSR310ConversionEnabled(true).Build());

            var logMsg = new LogicalMessage {
                Schema = Avro.Schema.Parse(avroSchema.SchemaInfo.SchemaDefinition), CreatedTime = DateTime.Now, DayOfWeek = "Saturday", Size = new AvroDecimal(102.65M)
            };

            var bytes1 = avroSchema.Encode(logMsg);

            Assert.True(bytes1.Length > 0);

            var msg = avroSchema.Decode(bytes1);

            Assert.NotNull(msg);
            Assert.True(msg.Size == 102.65M);
        }
コード例 #22
0
        protected override void BuildMetadataXmlSchema(ISchemaDefinition schema)
        {
            schema.Fields.Add(EclProvider.HostServices.CreateMultiLineTextFieldDefinition("Description", "Description", 0, 1, 7));
            schema.Fields.Add(EclProvider.HostServices.CreateSingleLineTextFieldDefinition("Price", "Price", 0, 1));
            var fhProduct = (FredhopperProduct)this.product;

            foreach (var attribute in fhProduct.AdditionalAttributes)
            {
                var name = getSchemaAttributeName(attribute.Key);
                if (attribute.Value.GetType() == typeof(string))
                {
                    schema.Fields.Add(EclProvider.HostServices.CreateSingleLineTextFieldDefinition(name, name, 0, 1));
                }
                else if (attribute.Value.GetType() == typeof(List <string>))
                {
                    schema.Fields.Add(EclProvider.HostServices.CreateSingleLineTextFieldDefinition(name, name, 0, 100));
                }
            }
        }
コード例 #23
0
        public virtual void TestSeparatedKeyValueEncodingTypeSchemaEncodeAndDecode()
        {
            var fooSchema = AvroSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).Build());

            var barSchema = AvroSchema <Bar> .Of(ISchemaDefinition <Bar> .Builder().WithPojo(typeof(Bar)).Build());


            var keyValueSchema = ISchema <object> .KeyValue(fooSchema, barSchema, KeyValueEncodingType.SEPARATED);

            var bar = new Bar
            {
                Field1 = true
            };

            var foo = new Foo
            {
                Field1 = "field1",
                Field2 = "field2",
                Field3 = "3",
                Field4 = bar,
                Color  = Color.RED
            };

            // Check kv.encoding.type SEPARATED
            var encodeBytes = keyValueSchema.Encode(new KeyValue <Foo, Bar>(foo, bar));

            Assert.True(encodeBytes.Length > 0);
            try
            {
                keyValueSchema.Decode(encodeBytes);
                Assert.True(false, "This method cannot be used under this SEPARATED encoding type");
            }
            catch (SchemaSerializationException e)
            {
                Assert.Contains("This method cannot be used under this SEPARATED encoding type", e.Message);
            }
            var keyValue = ((KeyValueSchema <Foo, Bar>)keyValueSchema).Decode(fooSchema.Encode(foo), encodeBytes, null);
            var fooBack  = keyValue.Key;
            var barBack  = keyValue.Value;

            Assert.True(foo.Equals(fooBack));
            Assert.True(bar.Equals(barBack));
        }
コード例 #24
0
ファイル: AvroSchemaTest.cs プロジェクト: eaba/SharpPulsar
        public void TestAvroSchemaUserDefinedReadAndWriter()
        {
            var reader           = new JsonReader <Foo>();
            var writer           = new JsonWriter <Foo>();
            var schemaDefinition = ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Bar)).WithSchemaReader(reader).WithSchemaWriter(writer).Build();

            var schema = AvroSchema <Foo> .Of(schemaDefinition);

            var foo = new Foo();

            foo.Color = Color.RED;
            var field1 = "test";

            foo.Field1 = field1;
            var encoded = schema.Encode(foo);

            foo = schema.Decode(encoded);
            Assert.Equal(Color.RED, foo.Color);
            Assert.Equal(field1, foo.Field1);
        }
コード例 #25
0
ファイル: AvroSchemaTest.cs プロジェクト: eaba/SharpPulsar
        public void TestLogicalType()
        {
            var avroSchema = AvroSchema <SchemaLogicalType> .Of(ISchemaDefinition <SchemaLogicalType> .Builder().WithPojo(typeof(SchemaLogicalType)).WithJSR310ConversionEnabled(true).Build());

            var schemaLogicalType = new SchemaLogicalType
            {
                TimestampMicros = DateTimeHelper.CurrentUnixTimeMillis() * 1000,
                TimestampMillis = DateTime.Parse("2019-03-26T04:39:58.469Z").Ticks,
                Decimal         = 12.34D,
                Date            = DateTimeOffset.Now.ToUnixTimeMilliseconds(),
                TimeMicros      = DateTimeHelper.CurrentUnixTimeMillis() * 1000,
                TimeMillis      = (DateTime.Now - DateTime.Today).Ticks
            };

            var bytes1 = avroSchema.Encode(schemaLogicalType);

            Assert.True(bytes1.Length > 0);

            var object1 = avroSchema.Decode(bytes1);

            Assert.True(schemaLogicalType.Equals(object1));
        }
コード例 #26
0
        public void AddUndefinedObjects(ISchemaDefinition src,
                                        ISchemaDefinition edustructuresSchema)
        {
            IDictionary srcHash = src.GetAllEnums();

            foreach (string enumKey in srcHash.Keys)
            {
                if (edustructuresSchema.GetEnum(enumKey) == null)
                {
                    // Exclude YesNo enums
                    EnumDef srcDef = src.GetEnum(enumKey);
                    if (srcDef.Values.Count < 4 && srcDef.ContainsValue("Yes") && srcDef.ContainsValue("No"))
                    {
                        continue;
                    }


                    AddUndefinedEnum(srcHash[enumKey] as EnumDef, edustructuresSchema.Version);
                }
            }

            srcHash = src.GetAllObjects();
            foreach (string key in srcHash.Keys)
            {
                if (key.StartsWith("State") || key.StartsWith("Country"))
                {
                    continue;
                }
                System.Diagnostics.Debug.Assert(key != "LAInfo");
                if (edustructuresSchema.GetObject(key) == null)
                {
                    AddUndefinedObject(srcHash[key] as ObjectDef, edustructuresSchema.Version);
                }
            }


            saveChanges();
        }
コード例 #27
0
        public void Compare(ISchemaDefinition officialSpec,
                            ISchemaDefinition unofficialSpec,
                            ICompareOutput output,
                            bool detailedCheck)
        {
            if (officialSpec.Version != unofficialSpec.Version)
            {
                output.ComparisonFailed
                    (officialSpec.Name, "SifVersion", "ToString", officialSpec.Version.ToString(),
                    unofficialSpec.Name, unofficialSpec.Version.ToString(), "");
            }

            CompareObjectDictionary
                (officialSpec.GetAllObjects(), officialSpec.Name, unofficialSpec.GetAllObjects(),
                unofficialSpec.Name, output, detailedCheck);
            // Only compare enums that are defined in the official spec, but not in the unofficial spec

            // TODO: Provide warnings for undocumented members

            CompareEnumDictionary
                (officialSpec.GetAllEnums(), officialSpec.Name, unofficialSpec.GetAllEnums(),
                unofficialSpec.Name, output);
        }
コード例 #28
0
        public virtual void TestAllowNullEncodeAndDecode()
        {
            var jsonSchema = JSONSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).Build());

            var bar = new Bar
            {
                Field1 = true
            };

            var foo1 = new Foo
            {
                Field1 = "foo1",
                Field2 = "bar1",
                Field4 = bar,
                Color  = Color.BLUE
            };

            var foo2 = new Foo
            {
                Field1 = "foo2",
                Field2 = "bar2"
            };

            var bytes1 = jsonSchema.Encode(foo1);

            Assert.True(bytes1.Length > 0);

            var bytes2 = jsonSchema.Encode(foo2);

            Assert.True(bytes2.Length > 0);

            var object1 = jsonSchema.Decode(bytes1);
            var object2 = jsonSchema.Decode(bytes2);

            Assert.True(object1.Equals(foo1));
            Assert.True(object2.Equals(foo2));
        }
コード例 #29
0
 public static AvroSchema <T> Of(Type pojo, IDictionary <string, string> properties)
 {
     return(Of(ISchemaDefinition <T> .Builder().WithPojo(pojo).WithProperties(properties).Build()));
 }
コード例 #30
0
 public ISchemaDefinition[] GetSchemaDefinitions()
 {
     ISchemaDefinition[] dbs = new ISchemaDefinition[fDBs.Count];
     fDBs.Values.CopyTo( dbs, 0 );
     return dbs;
 }
コード例 #31
0
 /// <summary>
 /// Append to metadata XML schema with implementation specific metadata definitions
 /// </summary>
 /// <param name="schema"></param>
 protected abstract void BuildMetadataXmlSchema(ISchemaDefinition schema);
コード例 #32
0
        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  Specialised constructor for use only by derived classes.
        /// </summary>
        /// <param name="name">
        ///  The name.
        /// </param>
        /// <param name="behavior">
        ///  (Optional) the behavior.
        /// </param>
        /// <param name="desc">
        ///  (Optional) the description.
        /// </param>
        ///-------------------------------------------------------------------------------------------------
        protected SchemaDefinition(string name, DomainBehavior behavior = DomainBehavior.Standard, ISchemaDefinition desc = null)
            : base(desc)
        {
            Contract.Requires(name, "name");
            Conventions.CheckValidDomainName(name);

            _name    = name;
            Behavior = behavior;
        }