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);
        }
Example #2
0
        protected override object CreateInstance(Type type, IOclElement oclElement)
        {
            var block = (OclBlock)oclElement;

            if (block.Labels.Count != 1)
            {
                throw new OclException("The block for a deployment action must contain one label, the name of the step");
            }

            var actionTypeElement = (OclAttribute?)block.FirstOrDefault(b => b.Name == "action_type");

            if (actionTypeElement == null)
            {
                throw new OclException("The block for a deployment action must contain the type field");
            }

            var actionType = actionTypeElement.Value as string;

            if (actionType == null)
            {
                throw new OclException("The action type must be a string and not null");
            }

            return(new DeploymentAction(block.Labels[0], actionType));
        }
Example #3
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);
        }
Example #4
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");
        }
Example #5
0
        protected override object CreateInstance(Type type, IOclElement oclElement)
        {
            var block = (OclBlock)oclElement;

            if (block.Labels.Count != 1)
            {
                throw new OclException("The block for a deployment step must contain one label, the name of the step");
            }

            return(new DeploymentStep(block.Labels[0]));
        }
Example #6
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
            });
        }
Example #7
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);
        }
Example #8
0
        public void Write(IOclElement element)
        {
            switch (element)
            {
            case OclAttribute attribute:
                Write(attribute);
                return;

            case OclBlock block:
                Write(block);
                return;

            case OclDocument document:
                foreach (var item in document)
                {
                    Write(item);
                }
                return;

            default:
                throw new ArgumentOutOfRangeException(element.GetType().Name);
            }
        }
Example #9
0
        public object?FromElement(OclConversionContext context, Type type, IOclElement element, object?currentValue)
        {
            if (element is OclAttribute attribute)
            {
                if (attribute.Value == null)
                {
                    return(null);
                }

                if (attribute.Value is string str)
                {
                    return(Enum.Parse(type, str));
                }

                if (attribute.Value is OclStringLiteral strLit)
                {
                    return(Enum.Parse(type, strLit.Value));
                }

                throw new Exception("Enum values must be specified as a string");
            }

            throw new OclException("Can only convert attribute elements");
        }
 public object?FromElement(OclConversionContext context, Type type, IOclElement element, object?currentValue)
 => throw new NotImplementedException();
Example #11
0
 public abstract object?FromElement(OclConversionContext context, Type type, IOclElement element, object?currentValue);
Example #12
0
 internal object?FromElement(Type type, IOclElement element, object?getCurrentValue)
 => GetConverterFor(type)
 .FromElement(this, type, element, getCurrentValue);
Example #13
0
 protected virtual object CreateInstance(Type type, IOclElement oclElement)
 => Activator.CreateInstance(type)
 ?? throw new OclException("Could not create instance of " + type.Name);