Exemplo n.º 1
0
        private static unsafe bool ParseEmailValue(ref ParseContext pctx, AccountStub stub)
        {
            stub.Fields |= UpdatedFields.Email;
            pctx.Move(1); // skip quote

            var separatorIndex = pctx.IndexOfAny((byte)'"', (byte)'@');

            if (pctx[separatorIndex] == '"')
            {
                return(false);
            }

            var p = pctx.CurrentPointer;

            pctx.Move(separatorIndex + 1);

            var quoteIndex = pctx.IndexOf((byte)'"');

            var domainString = new Utf8String(pctx.CurrentPointer, quoteIndex);

            ushort domainIndex = StringIndexer.Domains.Find(domainString);

            var spanEmail = new SpanEmail(p, (byte)separatorIndex, domainIndex);

            if (!Database.IsUniqueEmail(spanEmail))
            {
                return(false);
            }

            stub.email = new Email(UnsafeStringContainer.Clone(new ReadOnlySpan <byte>(p, separatorIndex)), domainIndex);

            pctx.Move(quoteIndex + 2);

            return(true);
        }
Exemplo n.º 2
0
        private static bool ParseStatusValue(ref ParseContext pctx, AccountStub stub)
        {
            stub.Fields |= UpdatedFields.Status;
            if (pctx[0] != '"')
            {
                return(false);
            }

            pctx.Move(1);
            var endString = pctx.IndexOf((byte)'"');

            if (pctx.StartsWith(statusFree.AsSpan(0, 6)))
            {
                stub.sexStatus |= SexStatus.Free;
                pctx.Move(endString + 2);
                return(true);
            }

            if (pctx.StartsWith(statusNotFree.AsSpan(0, 6)))
            {
                stub.sexStatus |= SexStatus.NotFree;
                pctx.Move(endString + 2);
                return(true);
            }

            if (pctx.StartsWith(statusComplex.AsSpan(0, 6)))
            {
                stub.sexStatus |= SexStatus.Complex;
                pctx.Move(endString + 2);
                return(true);
            }
            return(false);
        }
Exemplo n.º 3
0
        private static bool ParseSexValue(ref ParseContext pctx, AccountStub stub)
        {
            if (pctx[0] != '"' || pctx[2] != '"')
            {
                return(false);
            }

            var symbol = pctx[1];

            if (symbol == 'm')
            {
                stub.sexStatus |= SexStatus.Male;
            }
            else if (symbol == 'f')
            {
                stub.sexStatus |= SexStatus.Female;
            }
            else
            {
                return(false);
            }

            pctx.Move(4); //+4 for "m", or "f", (include comma)
            stub.Fields |= UpdatedFields.Sex;
            return(true);
        }
Exemplo n.º 4
0
        private static bool ParseLikesValue(ref ParseContext pctx, AccountStub stub)
        {
            if (pctx[0] != '[')
            {
                return(false);
            }

            pctx.Move(1);

            //TODO: check if it's not a object start
            //TODO: check whether liked account exists
            while (pctx[0] == '{')
            {
                if (!TryParseLike(ref pctx, out LikeStub like))
                {
                    return(false);
                }

                stub.likes.Add(like);
                pctx.Move(1); // comma
            }

            pctx.Move(1); // +1 for end array +1 for comma or anything else.
            stub.Fields |= UpdatedFields.Likes;
            return(true);
        }
Exemplo n.º 5
0
        public unsafe void ShouldParse(string json)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(json);
            fixed(byte *p = bytes)
            {
                var pctx = new ParseContext(p, bytes.Length);
                var stub = new AccountStub();

                Assert.That(NewHandler.TryParseAccount(ref pctx, stub), Is.EqualTo(201));
            }
        }
Exemplo n.º 6
0
        public unsafe void Should_parse_json()
        {
            var body = new
            {
                id     = 123,
                email  = "*****@*****.**",
                status = @"\u0432\u0441\u0451 \u0441\u043b\u043e\u0436\u043d\u043e",
                sex    = "m",
                fname  = "Jury",
                sname  = "Soldatenkov",

                interests = new[] { "sex", "drugs", "rock'n'roll" },

                likes = new object[]
                {
                    new { id = 245, ts = 123533214 },
                    new { id = 123, ts = 123532514 },
                },
                premium = new
                {
                    start  = 1000,
                    finish = 2000
                }
            };
            var stringBody = JsonConvert.SerializeObject(body);

            byte[] bytes = Encoding.UTF8.GetBytes(stringBody);

            fixed(byte *p = &bytes[0])
            {
                ParseContext pctx = new ParseContext(p, bytes.Length);

                byte[] idBytes = Encoding.UTF8.GetBytes("123");
                Console.WriteLine(stringBody);
                Console.WriteLine((byte)',');
                Console.WriteLine(string.Join(" ", bytes));
                Console.WriteLine(string.Join(" ", idBytes));

                var account = new AccountStub();
                var result  = NewHandler.TryParseAccount(ref pctx, account);

                Assert.That(result, Is.EqualTo(201));
                Assert.That(account.id, Is.EqualTo(body.id));
                Assert.That(account.email, Is.EqualTo(body.email));
                Assert.That(account.sexStatus.IsMale(), Is.True);
                //Assert.That(account.fname, Is.EqualTo(body.fname));
                Assert.That(account.sname, Is.EqualTo(body.sname));

                //CollectionAssert.AreEquivalent(account.interests, body.interests);
                Assert.That(account.likes.Count, Is.EqualTo(body.likes.Length));
                Assert.That(account.premium.start, Is.EqualTo(body.premium.start));
                Assert.That(account.premium.finish, Is.EqualTo(body.premium.finish));
            }
        }
Exemplo n.º 7
0
        private static bool ParsePremiumValue(ref ParseContext pctx, AccountStub stub)
        {
            if (pctx[0] != '{')
            {
                return(false);
            }

            pctx.Move(1);


            if (!ReadPremiumProperty(ref pctx, ref stub.premium) ||
                !ReadPremiumProperty(ref pctx, ref stub.premium) ||
                !TimeStamp.IsValidPremium(stub.premium.start, stub.premium.finish))
            {
                return(false);
            }

            pctx.Move(1);

            stub.Fields |= UpdatedFields.Premium;
            return(true);
        }
Exemplo n.º 8
0
        private static bool ParseInterestsValue(ref ParseContext pctx, AccountStub stub)
        {
            if (pctx[0] != '[')
            {
                return(false);
            }

            pctx.Move(1);
            while (pctx[0] == '"') //begin of the string
            {
                if (!TryReadString(ref pctx, out var value))
                {
                    return(false);
                }

                stub.interests.Add(value);
            }

            pctx.Move(1);
            stub.Fields |= UpdatedFields.Interests;
            return(true);
        }
Exemplo n.º 9
0
 private static bool ParseSnameValue(ref ParseContext pctx, AccountStub stub)
 {
     stub.Fields |= UpdatedFields.SName;
     return(TryReadString(ref pctx, out stub.sname));
 }
Exemplo n.º 10
0
 private static bool ParseIdValue(ref ParseContext pctx, AccountStub stub)
 => TryReadUInt(ref pctx, out stub.id) &&
 stub.id > 0 &&
 Database.NotExists(stub.id);
Exemplo n.º 11
0
 private static bool ParsePhoneValue(ref ParseContext pctx, AccountStub stub)
 {
     stub.Fields |= UpdatedFields.Phone;
     return(TryReadString(ref pctx, out stub.phone));
 }
Exemplo n.º 12
0
 private static bool ParseBirthValue(ref ParseContext pctx, AccountStub stub)
 {
     stub.Fields |= UpdatedFields.Birth;
     return(TryReadInt(ref pctx, out stub.birth) && TimeStamp.IsValidBirth(stub.birth));
 }
Exemplo n.º 13
0
 private static bool ParseCountryValue(ref ParseContext pctx, AccountStub stub)
 {
     stub.Fields |= UpdatedFields.Country;
     return(TryReadString(ref pctx, out stub.country));
 }
Exemplo n.º 14
0
 private static bool ParseJoinedValue(ref ParseContext pctx, AccountStub stub)
 {
     stub.Fields |= UpdatedFields.Joined;
     return(TryReadInt(ref pctx, out stub.joined) && TimeStamp.IsValidJoined(stub.joined));
 }