Esempio n. 1
0
        public void SerializingBlock()
        {
            var context = new OclConversionContext(new OclSerializerOptions());
            var result  = (OclBlock) new DefaultBlockOclConverter().ToElements(context, typeof(DummyWithBlock).GetProperty(nameof(DummyWithBlock.BlockProperty)) !, new DummyWithAttribute()).Single();

            result.Name.Should().Be("AnotherName");
        }
Esempio n. 2
0
        public override OclDocument ToDocument(OclConversionContext context, object obj)
        {
            var properties = GetProperties(obj.GetType());
            var children   = GetElements(obj, properties, context);

            return(new OclDocument(children));
        }
Esempio n. 3
0
        public IEnumerable <IOclElement> ToElements(OclConversionContext context, PropertyInfo?propertyInfo, object obj)
        {
            var type = obj.GetType();

            if (!type.IsValueType && type == null)
            {
                yield break;
            }

            if (type.IsValueType)
            {
                if (obj.Equals(false) || obj.Equals(0) || obj.Equals(0m) || obj.Equals(0f) || obj.Equals('\0'))
                {
                    yield break;
                }
            }

            if (!(obj is string) && obj is IEnumerable enumerable)
            {
                if (!enumerable.GetEnumerator().MoveNext())
                {
                    yield break;
                }
            }

            yield return(new OclAttribute(context.Namer.GetName(propertyInfo !), obj));
        }
        public void ReadOnlyPropertiesWorkIfTheReferenceMatches()
        {
            var document = new OclDocument
            {
                new OclBlock("ReadOnlyPassengers")
                {
                    new OclAttribute("Name", "Bob")
                }
            };

            var expected = new Car();

            expected.ReadOnlyPassengers.Add(new Person
            {
                Name = "Bob"
            });

            var context = new OclConversionContext(new OclSerializerOptions() ?? new OclSerializerOptions());
            var result  = context.FromElement(typeof(Car), document, null);

            if (result == null)
            {
                throw new OclException("Document conversion resulted in null, which is not valid");
            }
            ((Car)result)
            .Should()
            .BeEquivalentTo(expected);
        }
Esempio n. 5
0
        public object?FromElement(OclConversionContext context, Type type, IOclElement element, object?currentValue)
        {
            if (!(element is OclAttribute attribute))
            {
                throw new OclException("The properties must be an attribute");
            }

            var properties = currentValue == null ? new PropertiesDictionary() : (PropertiesDictionary)currentValue;

            if (!(attribute.Value is Dictionary <string, object?> source))
            {
                throw new OclException("The properties attribute value must be a dictionary");
            }

            foreach (var item in source)
            {
                var itemValue = item.Value is OclStringLiteral lit ? lit.Value : item.Value?.ToString();
                if (itemValue != null)
                {
                    properties[item.Key] = new PropertyValue(itemValue);
                }
            }

            return(properties);
        }
        public void CollectionSingleItem()
        {
            var document = new OclDocument
            {
                new OclBlock("Passengers")
                {
                    new OclAttribute("Name", "Bob")
                }
            };

            var context = new OclConversionContext(new OclSerializerOptions() ?? new OclSerializerOptions());
            var result  = context.FromElement(typeof(Car), document, null);

            if (result == null)
            {
                throw new OclException("Document conversion resulted in null, which is not valid");
            }
            ((Car)result)
            .Should()
            .BeEquivalentTo(new Car
            {
                Passengers = new List <Person>
                {
                    new Person
                    {
                        Name = "Bob"
                    }
                }
            });
        }
        public object?FromElement(OclConversionContext context, Type type, IOclElement element, object?currentValue)
        {
            var collectionType = GetElementType(type);

            var collection = currentValue ?? CreateNewCollection(type, collectionType);

            var elements = element is OclDocument root
                ? root.ToArray()
                : new[] { element };

            foreach (var item in elements.Select(e => context.FromElement(collectionType, e, null)))
            {
                if (collection is IList list)
                {
                    list.Add(item);
                }
                else
                {
                    var addMethod = collection.GetType().GetMethod("Add", new[] { collectionType });
                    if (addMethod == null)
                    {
                        throw new Exception("Only collections that implement an Add method are supported");
                    }

                    addMethod.Invoke(collection, new[] { item });
                }
            }

            return(collection);
        }
        public void NameCaseIsKept()
        {
            var context = new OclConversionContext(new OclSerializerOptions());
            var result  = (OclAttribute) new DefaultAttributeOclConverter().ToElements(context, typeof(Dummy).GetProperty(nameof(Dummy.Test)) !, "Value").Single();

            result.Name.Should().Be("test");
        }
Esempio n. 9
0
        public void SerializingAttribute()
        {
            var context = new OclConversionContext(new OclSerializerOptions());
            var result  = (OclAttribute) new DefaultAttributeOclConverter().ToElements(context, typeof(DummyWithAttribute).GetProperty(nameof(DummyWithAttribute.AttributeProperty)) !, "whatever").Single();

            result.Name.Should().Be("NewName");
        }
Esempio n. 10
0
        protected virtual IEnumerable <IOclElement> GetElements(object obj, OclConversionContext context)
        {
            var properties = GetProperties(obj.GetType())
                             .Except(GetLabelProperties(obj.GetType()))
                             .ToArray();

            return(GetElements(obj, properties, context));
        }
Esempio n. 11
0
        public virtual IEnumerable <IOclElement> ToElements(OclConversionContext context, PropertyInfo?propertyInfo, object obj)
        {
            var element = ConvertInternal(context, propertyInfo, obj);

            return(element != null
                ? new[] { element }
                : Array.Empty <IOclElement>());
        }
        public void NameCaseIsKept()
        {
            var context = new OclConversionContext(new OclSerializerOptions());
            var data    = new object();
            var result  = (OclBlock) new DefaultBlockOclConverter().ToElements(context, typeof(WithIndexer).GetProperty(nameof(WithIndexer.MyProp)) !, data).Single();

            result.Name.Should().Be("my_prop");
        }
Esempio n. 13
0
        public object?FromElement(OclConversionContext context, Type type, IOclElement element, object?currentValue)
        {
            if (element is OclAttribute attribute)
            {
                return(attribute.Value);
            }

            throw new OclException("Can only convert attribute elements");
        }
Esempio n. 14
0
        public IEnumerable <IOclElement> ToElements(OclConversionContext context, PropertyInfo?propertyInfo, object obj)
        {
            var isDefault = Activator.CreateInstance(obj.GetType()).Equals(obj);

            if (!isDefault)
            {
                yield return(new OclAttribute(context.Namer.GetName(propertyInfo !), obj.ToString()));
            }
        }
Esempio n. 15
0
            public VcsRunbook FromRemainingElements(OclConversionContext context, IReadOnlyList <IOclElement> runbookElements)
            {
                var name       = runbookElements.OfType <OclAttribute>().First(r => r.Name == "name").Value?.ToString();
                var runbook    = new VcsRunbook(name ?? "");
                var properties = GetProperties(typeof(VcsRunbook)).ToArray();

                SetProperties(context, runbookElements, runbook, properties);
                return(runbook);
            }
Esempio n. 16
0
        protected virtual void SetProperties(OclConversionContext context, Type type, IEnumerable <IOclElement> elements, object target)
        {
            var properties = GetProperties(type).Except(GetLabelProperties(type)).ToArray();
            var notFound   = SetProperties(context, elements, target, properties);

            if (notFound.Any())
            {
                throw new OclException($"The propert{(notFound.Count > 1 ? "ies" : "y")} '{string.Join("', '", notFound.Select(a => a.Name))}' {(notFound.Count > 1 ? "were" : "was")} not found on '{type.Name}'");
            }
        }
        public IEnumerable <IOclElement> ToElements(OclConversionContext context, PropertyInfo?propertyInfo, object value)
        {
            var collection = (ReferenceCollection)value;

            if (collection.Count == 0)
            {
                return(Array.Empty <IOclElement>());
            }

            return(new[]
Esempio n. 18
0
        protected override IOclElement ConvertInternal(OclConversionContext context, PropertyInfo?propertyInfo, object obj)
        {
            var step = (DeploymentStep)obj;

            if (string.IsNullOrWhiteSpace(step.Name))
            {
                throw new Exception("The name of the action must be set");
            }

            return(base.ConvertInternal(context, propertyInfo, obj));
        }
Esempio n. 19
0
            public (RunbookProcess process, IReadOnlyList <IOclElement> notFound) FromDocument(
                OclConversionContext context,
                OclDocument doc
                )
            {
                var runbook = new RunbookProcess("PLACEHOLDER", "PLACEHOLDER");

                var properties = GetProperties(typeof(RunbookProcess)).ToArray();
                var notFound   = SetProperties(context, doc, runbook, properties);

                return(runbook, notFound);
            }
Esempio n. 20
0
        public void Deserializing()
        {
            var context = new OclConversionContext(new OclSerializerOptions());
            var result  = (DummyWithBlock?)new DefaultBlockOclConverter().FromElement(context,
                                                                                      typeof(DummyWithBlock),
                                                                                      new OclBlock("whatever",
                                                                                                   Array.Empty <string>(),
                                                                                                   new[] { new OclBlock("AnotherName", Array.Empty <string>(), new[] { new OclAttribute("NewName", "Boo") }) }),
                                                                                      null);

            result?.BlockProperty.AttributeProperty.Should().Be("Boo");
        }
        public IEnumerable <IOclElement> ToElements(OclConversionContext context, PropertyInfo?propertyInfo, object value)
        {
            var values = ((IEnumerable)value)
                         .Cast <object>()
                         .Select(v => v.ToString())
                         .ToArray();

            if (values.Length == 0)
            {
                return(Array.Empty <IOclElement>());
            }

            return(new[]
        public void Empty()
        {
            var document = new OclDocument();
            var context  = new OclConversionContext(new OclSerializerOptions());
            var result   = context.FromElement(typeof(Car), document, null);

            if (result == null)
            {
                throw new OclException("Document conversion resulted in null, which is not valid");
            }
            ((Car)result)
            .Should()
            .BeEquivalentTo(new Car());
        }
        public void AttributesComeBeforeBlocks()
        {
            var context = new OclConversionContext(new OclSerializerOptions());
            var data    = new
            {
                MyBlock = new { BlockProp = "OtherValue" },
                MyProp  = "MyValue"
            };
            var result = (OclBlock) new DefaultBlockOclConverter().ToElements(context, typeof(WithIndexer).GetProperty(nameof(WithIndexer.MyProp)), data).Single();

            result.First()
            .Should()
            .BeEquivalentTo(new OclAttribute("my_prop", "MyValue"));
        }
        public IEnumerable <IOclElement> ToElements(OclConversionContext context, PropertyInfo?propertyInfo, object value)
        {
            var items = (IEnumerable)value;

            foreach (var item in items)
            {
                if (item != null)
                {
                    foreach (var element in context.ToElements(propertyInfo, item))
                    {
                        yield return(element);
                    }
                }
            }
        }
Esempio n. 25
0
        public object?FromElement(OclConversionContext context, Type type, IOclElement element, object?currentValue)
        {
            if (!(element is OclDocument doc))
            {
                throw new OclException("Can only convert from OclDocument");
            }

            var(process, runbookElements) = processConverter.FromDocument(context, doc);

            var runbook = runbookConverter.FromRemainingElements(context, runbookElements);

            return(new VcsRunbookPersistenceModel(runbook)
            {
                Process = process
            });
        }
        public void IndexersAreIgnored()
        {
            var context = new OclConversionContext(new OclSerializerOptions());
            var data    =
                new Dummy
            {
                Foo = new WithIndexer()
            };
            var result = (OclBlock) new DefaultBlockOclConverter().ToElements(context, typeof(Dummy).GetProperty(nameof(Dummy.Foo)), data.Foo).Single();

            result.Should()
            .Be(
                new OclBlock("foo")
            {
                new OclAttribute("my_prop", "MyValue")
            }
                );
        }
Esempio n. 27
0
        public OclDocument ToDocument(OclConversionContext context, object obj)
        {
            var model = (VcsRunbookPersistenceModel)obj;

            var doc = runbookConverter.ToDocument(context, model.Runbook);

            if (model.Process != null)
            {
                var process = processConverter.ToDocument(context, model.Process);

                foreach (var child in process)
                {
                    doc.Add(child);
                }
            }

            return(doc);
        }
Esempio n. 28
0
        public override object?FromElement(OclConversionContext context, Type type, IOclElement element, object?currentValue)
        {
            var target = CreateInstance(type, element);

            if (!(element is OclBody body))
            {
                throw new OclException("Cannot convert attribute element");
            }

            if (body is OclBlock block && block.Labels.Any())
            {
                SetLabels(type, block, target);
            }

            SetProperties(context, type, body, target);

            return(target);
        }
        public void StringAttribute()
        {
            var document = new OclDocument
            {
                new OclAttribute("Name", "Mystery Machine")
            };

            var context = new OclConversionContext(new OclSerializerOptions() ?? new OclSerializerOptions());
            var result  = context.FromElement(typeof(Car), document, null);

            if (result == null)
            {
                throw new OclException("Document conversion resulted in null, which is not valid");
            }
            ((Car)result)
            .Should()
            .BeEquivalentTo(new Car
            {
                Name = "Mystery Machine"
            });
        }
Esempio n. 30
0
        public IEnumerable <IOclElement> ToElements(OclConversionContext context, PropertyInfo?propertyInfo, object value)
        {
            var dict = (PropertiesDictionary)value;

            if (dict.None())
            {
                yield break;
            }

            if (dict.Values.Any(v => v.IsSensitive))
            {
                throw new NotSupportedException("Sensitive property values are not supported");
            }

            var stringDict = dict.ToDictionary(
                kvp => kvp.Key,
                kvp => kvp.Value.Value
                );

            yield return(new OclAttribute("properties", stringDict));
        }