Beispiel #1
0
        private static Object ConvertOptionToLoggingLevel(SupportedProperty property, Object value)
        {
            string valueAsString = value == null ? null : value.ToString();

            switch (valueAsString)
            {
            case "1 - Errors Only":
                return(LoggingLevel.ErrorsOnly);

            case "2 - Errors and Warnings":
                return(LoggingLevel.ErrorsAndWarnings);

            case "3 - Errors, Warnings, and Info":
                return(LoggingLevel.ErrorsWarningsAndInfo);

            case "4 - Errors, Warnings, Info, and Debug":
                return(LoggingLevel.ErrorsWarningsInfoAndDebug);

            case "5 - Errors, Warnings, Info, Debug, and Trace":
                return(LoggingLevel.ErrorsWarningsInfoDebugAndTrace);

            default:
                throw new Exception(string.Format("Supported property '{0}' could not convert option '{1}' to enum type '{2}'.", property.Name, value == null ? "null" : value, property.OptionsEnumType.Name));
            }
        }
        /// <summary>
        /// Creates a hydra <see cref="SupportedProperty" /> from a type's property
        /// using sensible defaults.
        /// </summary>
        public virtual SupportedProperty Create(PropertyInfo prop, IReadOnlyDictionary <Type, Uri> classIds)
        {
            IriRef?mappedType = this.rangeRetrieval.GetRange(prop, classIds);
            var    meta       = this.metaProvider.GetMeta(prop);
            string propertyId = this.propertyPredicateIdPolicy.GetPropertyId(prop);

            if (propertyId == null)
            {
                throw new ApiDocumentationException(string.Format("Property {0} is not included in the context", prop));
            }

            var propertyTypes = new List <string>();

            if (meta.IsLink)
            {
                propertyTypes.Add(Vocab.Hydra.Link);
            }

            var property = new SupportedProperty
            {
                Title       = meta.Title,
                Description = meta.Description,
                Writeable   = meta.Writeable,
                Readable    = meta.Readable,
                Required    = meta.Required,
                Property    = new Property(propertyTypes)
                {
                    Id    = propertyId,
                    Range = mappedType ?? (IriRef)Vocab.Hydra.Resource,
                    SupportedOperations = this.operationFactory.CreateOperations(prop, classIds).ToList()
                }
            };

            return(property);
        }
Beispiel #3
0
        public AdapterInstance(Dictionary <string, object> properties, DataSource associatedDataSource)
        {
            AssociatedDataSource = associatedDataSource;

            if (properties == null)
            {
                properties = new Dictionary <string, object>();
            }

            // convert to case insensitve
            var supportedProperties     = GetSupportedProperties().ToDictionary(d => d.Key, d => d.Value, StringComparer.OrdinalIgnoreCase);
            var adapterConfigProperties = properties.ToDictionary(d => d.Key, d => d.Value, StringComparer.OrdinalIgnoreCase);

            var requiredProperties = supportedProperties.Values.Where(d => d.IsRequired);

            // check if required properties were provided
            foreach (var requiredProp in requiredProperties)
            {
                if (!adapterConfigProperties.ContainsKey(requiredProp.Name))
                {
                    throw new Exception(string.Format("Required adapter property '{0}' is missing.", requiredProp.Name));
                }
            }

            // check if non-supported adapter properties are provided
            foreach (var adapterProp in adapterConfigProperties)
            {
                if (!supportedProperties.ContainsKey(adapterProp.Key))
                {
                    throw new Exception(string.Format("Property '{0}' is not supported by this adapter.", adapterProp.Key));
                }
            }

            // add properties to adapter; if not provided and not required, add with default value
            foreach (var supportedProperty in supportedProperties)
            {
                if (adapterConfigProperties.ContainsKey(supportedProperty.Value.Name))
                {
                    SupportedProperty.ThrowExceptionIfInvalid(supportedProperty.Value, adapterConfigProperties[supportedProperty.Value.Name]);
                    _properties.Add(supportedProperty.Value.Name, adapterConfigProperties[supportedProperty.Value.Name]);
                }
                else
                {
                    _properties.Add(supportedProperty.Value.Name, supportedProperty.Value.DefaultValue);
                }
            }
        }
        /// <summary>
        /// Creates a hydra <see cref="SupportedProperty" /> from a type's property
        /// using sensible defaults.
        /// </summary>
        public virtual SupportedProperty Create(PropertyInfo prop, IReadOnlyDictionary <Type, Uri> classIds)
        {
            IriRef?mappedType = _rangeRetrieval.GetRange(prop, classIds);
            var    meta       = _metaProvider.GetMeta(prop);
            string propertyId = _propertyPredicateIdPolicy.GetPropertyId(prop, classIds[prop.ReflectedType]);

            var property = new SupportedProperty
            {
                Title       = meta.Title,
                Description = meta.Description,
                Writeable   = meta.Writeable,
                Readable    = meta.Readable,
                Property    =
                {
                    Id    = propertyId,
                    Range = mappedType ?? (IriRef)Hydra.Resource
                }
            };

            return(property);
        }