Example #1
0
        public void TestMapper()
        {
            IIonValue ionStruct = IonLoader.Default
                                  .Load("{name: \"John\", age: 22, address: {city: \"cityName\", street: \"streetName\", isApartment: false}}");

            var    lossyIonMapper = IonMapper.LOSSY();
            Person person         = lossyIonMapper.FromIon <Person>(ionStruct);

            Console.WriteLine(person.ToString());

            IIonValue ionValue = lossyIonMapper.ToIon <Person>(person);

            Console.WriteLine(ionValue.ToPrettyString());

            Console.WriteLine("\n \n ==== Now with a customized mapper === \n");

            Func <IIonString, string> specialIonStringMapper = ionValue => (ionValue.StringValue).ToUpper();
            Func <IIonInt, int>       specialIonIntMapper    = ionValue => (ionValue.IntValue) * 3;
            Func <IIonBool, bool>     specialIonBoolMapper   = ionValue => !(ionValue.BoolValue);

            var customizedMapper = new CustomIonMapper(MapperTypes.LOSSY)
                                   .WithMapperFromIonTo <string>(specialIonStringMapper)
                                   .WithMapperFromIonTo <int>(specialIonIntMapper)
                                   .WithMapperFromIonTo <bool>(specialIonBoolMapper)
                                   .Build();

            person = customizedMapper.FromIon <Person>(ionStruct);
            Console.WriteLine(person.ToString());

            ionValue = customizedMapper.ToIon <Person>(person);
            Console.WriteLine(ionValue.ToPrettyString());
        }
            internal override IIonReader GetIonReader(IIonValue ionValue)
            {
                MemoryStream ms     = new MemoryStream();
                IIonWriter   writer = IonBinaryWriterBuilder.Build(ms);
                IIonReader   reader = IonReaderBuilder.Build(ionValue.ToPrettyString());

                writer.WriteValues(reader);
                writer.Flush();
                return(IonReaderBuilder.Build(ms.ToArray()));
            }
        private async Task <T> GetFromQldb <T>(object eventId) where T : AuditEvent
        {
            var driver = QldbDriver.Value;

            var(insertDocumentId, tableName) = (ValueTuple <string, string>)eventId;
            IIonValue selectedEvent = null;
            await driver.Execute(async trx =>
            {
                selectedEvent = await(await trx.Execute(
                                          $@"SELECT e.*
                      FROM {tableName} AS e BY eid                      
                      WHERE eid = ?",
                                          new ValueFactory().NewString(insertDocumentId))).FirstAsync();
            });

            var selectedAuditEvent = AuditEvent.FromJson <T>(selectedEvent.ToPrettyString());

            return(selectedAuditEvent);
        }
 internal override IIonReader GetIonReader(IIonValue ionValue)
 {
     return(IonReaderBuilder.Build(ionValue.ToPrettyString()));
 }