Inheritance: ICustomDiagnosticMessage
Beispiel #1
0
 public void ToString_Invalid_Precise()
 {
     var mask = new FieldMask { Paths = { "x", "foo__bar", @"x\y" } };
     Assert.AreEqual(
         "{ \"@warning\": \"Invalid FieldMask\", \"paths\": [ \"x\", \"foo__bar\", \"x\\\\y\" ] }",
         mask.ToString());
 }
Beispiel #2
0
 public void ToString_Invalid(string input)
 {
     var mask = new FieldMask { Paths = { input } };
     var text = mask.ToString();
     // More specific test below
     Assert.That(text, Does.Contain("@warning"));
     Assert.That(text, Does.Contain(input));
 }
Beispiel #3
0
        /// <summary>
        /// Constructs a FieldMask from the passed field numbers.
        /// </summary>
        /// <typeparam name="T">The type to validate the field paths against.</typeparam>
        public static FieldMask FromFieldNumbers <T>(IEnumerable <int> fieldNumbers) where T : IMessage
        {
            var descriptor = Activator.CreateInstance <T>().Descriptor;

            var mask = new FieldMask();

            foreach (var fieldNumber in fieldNumbers)
            {
                var field = descriptor.FindFieldByNumber(fieldNumber);
                if (field == null)
                {
                    throw new ArgumentNullException($"{fieldNumber} is not a valid field number for {descriptor.Name}");
                }

                mask.Paths.Add(field.Name);
            }

            return(mask);
        }
Beispiel #4
0
        /// <summary>
        /// Constructs a FieldMask for a list of field paths in a certain type.
        /// </summary>
        /// <typeparam name="T">The type to validate the field paths against.</typeparam>
        public static FieldMask FromStringEnumerable <T>(IEnumerable <string> paths) where T : IMessage
        {
            var mask = new FieldMask();

            foreach (var path in paths)
            {
                if (path.Length == 0)
                {
                    // Ignore empty field paths.
                    continue;
                }

                if (typeof(T) != typeof(Empty) &&
                    !IsValid <T>(path))
                {
                    throw new InvalidProtocolBufferException(path + " is not a valid path for " + typeof(T));
                }

                mask.Paths.Add(path);
            }

            return(mask);
        }
Beispiel #5
0
        public void FieldMaskStandalone()
        {
            var fieldMask = new FieldMask { Paths = { "", "single", "with_underscore", "nested.field.name", "nested..double_dot" } };
            Assert.AreEqual(",single,withUnderscore,nested.field.name,nested..doubleDot", fieldMask.ToString());

            // Invalid, but we shouldn't create broken JSON...
            fieldMask = new FieldMask { Paths = { "x\\y" } };
            Assert.AreEqual(@"x\\y", fieldMask.ToString());
        }
 public void FieldMaskInvalid(string input)
 {
     var mask = new FieldMask { Paths = { input } };
     Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default.Format(mask));
 }
Beispiel #7
0
 public void FromString_Validated()
 {
     // Check whether the field paths are valid if a class parameter is provided.
     Assert.DoesNotThrow(() => FieldMask.FromString <NestedTestAllTypes>(",payload"));
     Assert.Throws <InvalidProtocolBufferException>(() => FieldMask.FromString <NestedTestAllTypes>("payload,nonexist"));
 }
Beispiel #8
0
        /// <summary>
        /// Checks whether paths in a given fields mask are valid.
        /// </summary>
        /// <typeparam name="T">The type to validate the field paths against.</typeparam>
        public static bool IsValid <T>(FieldMask fieldMask) where T : IMessage
        {
            var descriptor = Activator.CreateInstance <T>().Descriptor;

            return(IsValid(descriptor, fieldMask));
        }
Beispiel #9
0
        /// <summary>
        /// Checks whether paths in a given fields mask are valid.
        /// </summary>
        /// <typeparam name="T">The type to validate the field paths against.</typeparam>
        public static bool IsValid <[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] T>(FieldMask fieldMask) where T : IMessage
        {
            var descriptor = Activator.CreateInstance <T>().Descriptor;

            return(IsValid(descriptor, fieldMask));
        }