示例#1
0
        private object DeserializeObject(Type type, IIonValue ionValue)
        {
            Object obj;
            // Get type's attributes
            IList <PropertyInfo> properties = new List <PropertyInfo>(type.GetProperties());

            // Array to hold parameters to create object
            object[] paramArray = new object[properties.Count];

            int count = 0;

            foreach (PropertyInfo property in properties)
            {
                Type propertyType = property.PropertyType;
                if (Type.GetTypeCode(propertyType) == TypeCode.Object)
                {
                    obj = DeserializeObject(propertyType, ionValue.GetField(property.Name));
                }
                else
                {
                    obj = DeserializingMappers(propertyType, ionValue.GetField(property.Name));
                }
                paramArray[count] = obj;
                count++;
            }

            // Create object by passing in array of parameters into constructor
            obj = Activator.CreateInstance(type, args: paramArray);
            return(obj);
        }
示例#2
0
        private void RunTest(
            string testName,
            IIonValue testCase,
            IIonValue expectedHashLog,
            TestIonHasherProvider hasherProvider,
            IonHashTester testObject
            )
        {
            if (expectedHashLog == null)
            {
                return;
            }

            IIonValue ionValue  = testCase.GetField("ion");
            IIonValue ionBinary = testCase.GetField("10n");

            if (ionValue != null && ionBinary != null)
            {
                throw new Exception("Test must not define both 'ion' and '10n' fields");
            }

            var reader = ionValue == null
                ? testObject.GetIonReader(ContainerToBytes(ionBinary))
                : testObject.GetIonReader(ionValue);

            testObject.Traverse(reader, hasherProvider);

            IIonValue actualHashLog         = testObject.GetHashLog();
            IIonValue actualHashLogFiltered = FilterHashLog(actualHashLog, expectedHashLog);

            Assert.AreEqual(HashLogToString(expectedHashLog), HashLogToString(actualHashLogFiltered));
        }
示例#3
0
        public object ConvertTo(IIonValue value, Type type)
        {
            var props    = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            var instance = Activator.CreateInstance(type);

            var members = type.GetMembers(BindingFlags.Public | BindingFlags.Instance)
                          .Where(m => (m.MemberType == MemberTypes.Field || m.MemberType == MemberTypes.Property));

            foreach (MemberInfo member in members)
            {
                var field        = value.GetField(member.Name);
                var memberType   = member.GetUnderlyingType();
                var fieldHandler = FieldHandlers.GetHandler(memberType);
                if (field != null)
                {
                    var fieldValue = fieldHandler.ConvertTo(field, memberType);
                    if (member.MemberType == MemberTypes.Field)
                    {
                        type.GetField(member.Name).SetValue(instance, fieldValue);
                        continue;
                    }

                    if (member.MemberType == MemberTypes.Property)
                    {
                        if (((PropertyInfo)member).CanWrite)
                        {
                            type.GetProperty(member.Name).SetValue(instance, fieldValue);
                        }
                    }
                }
            }

            return(instance);
        }
        public IEnumerable <object[]> GetData(MethodInfo methodInfo)
        {
            var dataList = new List <object[]>();

            var loader          = IonLoader.Default;
            var file            = DirStructure.IonHashDotnetTestFile("ion_hash_tests.ion");
            var ionHashTests    = loader.Load(file);
            var testsEnumerator = ionHashTests.GetEnumerator();

            while (testsEnumerator.MoveNext())
            {
                IIonValue testCase = testsEnumerator.Current;

                string testName = "unknown";
                if (testCase.ContainsField("ion"))
                {
                    testName = testCase.GetField("ion").ToPrettyString();
                }

                IReadOnlyCollection <SymbolToken> annotations = testCase.GetTypeAnnotationSymbols();
                if (annotations.Count > 0)
                {
                    testName = annotations.ElementAt(0).Text;
                }

                IIonValue expect           = testCase.GetField("expect");
                var       expectEnumerator = expect.GetEnumerator();
                while (expectEnumerator.MoveNext())
                {
                    IIonValue expectedHashLog = expectEnumerator.Current;
                    String    hasherName      = expectedHashLog.FieldNameSymbol.Text;

                    object[] data = new object[] {
                        hasherName.Equals("identity") ? testName : testName + "." + hasherName,
                        testCase,
                        expectedHashLog,
                        TestIonHasherProvider.GetInstance(hasherName)
                    };
                    dataList.Add(data);
                }
            }

            return(dataList);
        }
        public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
        {
            string vin = request.QueryStringParameters["VIN"];
            IEnumerable <IIonValue> selectResult = this.qldbDriver.Execute(transactionExecutor =>
            {
                string vehicleRegistrationDocumentId = this.tableMetadataService.GetDocumentId(transactionExecutor, VehicleRegistrationTableName, "VIN", vin);
                context.Logger.Log($"Getting history for vehicle registration where document ID is {vehicleRegistrationDocumentId}.");

                IIonValue ionVehicleRegistrationDocumentId = this.valueFactory.NewString(vehicleRegistrationDocumentId);
                IResult result = transactionExecutor.Execute("SELECT data, data.Owners.PrimaryOwner AS PrimaryOwner, data.Owners.SecondaryOwners AS SecondaryOwners, metadata.version "
                                                             + $"FROM history(VehicleRegistration) "
                                                             + "AS history WHERE history.metadata.id = ?", ionVehicleRegistrationDocumentId);
                return(result);
            });

            IEnumerable <VehicleRegistrationHistory> vehicles = selectResult.Select(x =>
            {
                IIonValue ionData    = x.GetField("data");
                IIonValue ionVersion = x.GetField("version");

                Owners owners = new Owners
                {
                    PrimaryOwner = new Owner {
                        PersonId = x.GetField("PrimaryOwner").GetField("PersonId").StringValue
                    }
                };

                var ionSecondaryOwners = x.GetField("SecondaryOwners") as IIonList;
                foreach (var secondaryOwner in ionSecondaryOwners)
                {
                    owners.SecondaryOwners.Add(new Owner {
                        PersonId = secondaryOwner.GetField("PersonId").StringValue
                    });
                }

                return(new VehicleRegistrationHistory
                {
                    Version = ionVersion.IntValue,
                    VehicleRegistration = new VehicleRegistration
                    {
                        Vin = ionData.GetField("VIN").StringValue,
                        LicensePlateNumber = ionData.GetField("LicensePlateNumber").StringValue,
                        State = ionData.GetField("State").StringValue,
                        PendingPenaltyTicketAmount = Convert.ToDouble(ionData.GetField("PendingPenaltyTicketAmount").DecimalValue),
                        ValidFromDate = DateTime.Parse(ionData.GetField("ValidFromDate").StringValue),
                        ValidToDate = DateTime.Parse(ionData.GetField("ValidToDate").StringValue),
                        Owners = owners
                    }
                });
            });

            return(new APIGatewayProxyResponse
            {
                StatusCode = vehicles.Any() ? (int)HttpStatusCode.OK : (int)HttpStatusCode.NotFound,
                Body = JsonConvert.SerializeObject(vehicles, Formatting.Indented)
            });
        }
        /// <summary>
        /// Asynchronously inserts an event into AmazonQLDB
        /// </summary>
        public override async Task <object> InsertEventAsync(AuditEvent auditEvent)
        {
            var       driver    = QldbDriver.Value;
            var       tableName = GetTableName(auditEvent);
            IIonValue inserted  = null;
            await driver.Execute(async txn =>
            {
                var json       = auditEvent.ToJson();
                var insertInto = $@"INSERT INTO {tableName} VALUE ?";
                try
                {
                    inserted = await(await txn.Execute(insertInto, IonLoader.Default.Load(json))).FirstAsync();
                }
                catch (BadRequestException e) when(e.Message.Contains($"No such variable named '{tableName}'"))
                {
                    await txn.Execute($"CREATE TABLE {tableName}");
                    inserted = await(await txn.Execute(insertInto, IonLoader.Default.Load(json))).FirstAsync();
                }
            });

            var insertDocumentId = inserted.GetField("documentId").StringValue;

            return(insertDocumentId, tableName);
        }
示例#7
0
 public Person(IIonValue ionValue)
 {
     FirstName = ionValue.GetField("FirstName").StringValue;
     LastName  = ionValue.GetField("LastName").StringValue;
     CreatedAt = ionValue.GetField("CreatedAt").LongValue;
 }