Example #1
0
        public Form1()

        {
            InitializeComponent();



            ExtensibleObject.SetPlatformInformation("internet", "1.0");
        }
Example #2
0
        protected static void AssertNoExtensionData(ExtensibleObject value, string rootName, string id, bool testForUndefinedEnumOptions, Dictionary <Type, HashSet <string> > enumOptionsToIgnore = null)
        {
            var fails = new List <string>();

            TestForExtensionData(value, new List <string> {
                rootName
            }, fails, testForUndefinedEnumOptions, enumOptionsToIgnore);
            Assert.AreEqual(0, fails.Count, $@"{id} has the following issues.
{string.Join(Environment.NewLine, fails)}");
        }
Example #3
0
        public void TestSetAndGetInstance()
        {
            var extObject = new ExtensibleObject();
            var extName   = "HAHAHA";
            var testInt   = 1;

            extObject.SetInstance <int>(testInt, extName);
            Assert.AreEqual(testInt, extObject.GetInstance <int>(extName));
            extObject.SetInstance <int>(2, extName);
            Assert.AreNotEqual(testInt, extObject.GetInstance <int>(extName));
        }
Example #4
0
 /// <summary>
 /// Adds some custom server-specific headers to the outgoing response
 /// </summary>
 /// <param name="exObj">The <see cref="Response"/> or <see cref="Error"/> being returned</param>
 private void AddServerHeaders(ExtensibleObject exObj)
 {
     if (!exObj.CustomTextAttributes.ContainsKey("Message-Daemon"))
     {
         exObj.CustomTextAttributes.Add("Message-Daemon", serverName);
     }
     if (!exObj.CustomTextAttributes.ContainsKey("Timestamp"))
     {
         exObj.CustomTextAttributes.Add("Timestamp", DateTime.Now.ToString());
     }
 }
Example #5
0
        /// <summary>
        /// Given a link and a AccessQuery, assigns link graphics which will
        /// only be displayed when the AccessQuery is satisfied.
        /// </summary>
        private static void UpdateLinkGraphics(ExtensibleObject link, AccessQuery query)
        {
            // First check if the link already has a link graphics extension.
            var linkGraphicsExtension = link.Extensions.GetByType <LinkGraphicsExtension>();

            if (linkGraphicsExtension == null)
            {
                // If it does not, add one.
                linkGraphicsExtension = new LinkGraphicsExtension(new LineGraphics
                {
                    Color = new ConstantGraphicsParameter <Color>(Color.Yellow)
                });
                link.Extensions.Add(linkGraphicsExtension);
            }

            // Configure the link graphics to show the line only when access exists,
            // using a AccessQueryGraphicsParameter for the provided query.
            // In our case, this will be when carrier to noise for the downlink is greater than -16db.
            linkGraphicsExtension.LinkGraphics.DisplayParameters.Display = new AccessQueryGraphicsParameter <bool>(query, true, false, false);
        }
Example #6
0
        private static void TestForExtensionData(ExtensibleObject value, List <string> path, List <string> fails, bool testForUndefinedEnumOptions, Dictionary <Type, HashSet <string> > enumOptionsToIgnore)
        {
            if (value.ExtensionData.Count > 0)
            {
                fails.Add($"{string.Concat(path)} has the extension data {JsonConvert.SerializeObject(new Dictionary<string, object>(value.ExtensionData))}");
            }
            var type     = value.GetType();
            var contract = JsonHelper.InternalPrivateContractResolver.ResolveContract(type);

            if (contract is JsonObjectContract jsonObjectContract)
            {
                foreach (var property in jsonObjectContract.Properties)
                {
                    var propertyUnderlyingName = property.UnderlyingName;
                    var valueProvider          = JsonHelper.InternalPrivateContractResolver.GetBackingFieldInfo(contract.UnderlyingType, propertyUnderlyingName)?.ValueProvider ?? property.ValueProvider;
                    var propertyValue          = valueProvider.GetValue(value);
                    if (propertyValue != null)
                    {
                        path.Add($".{propertyUnderlyingName}");
                        switch (propertyValue)
                        {
                        case ExtensibleObject extensibleObject:
                            TestForExtensionData(extensibleObject, path, fails, testForUndefinedEnumOptions, enumOptionsToIgnore);
                            break;

                        case IList list:
                            var i = 0;
                            foreach (var element in list)
                            {
                                path.Add($"[{i}]");
                                if (element is ExtensibleObject extObj)
                                {
                                    TestForExtensionData(extObj, path, fails, testForUndefinedEnumOptions, enumOptionsToIgnore);
                                }
                                else
                                {
                                    TestForUndefinedEnumOptions(path, fails, enumOptionsToIgnore, element);
                                }
                                path.RemoveAt(path.Count - 1);
                                ++i;
                            }
                            break;

                        case IDictionary dictionary:
                            foreach (DictionaryEntry pair in dictionary)
                            {
                                if (pair.Value is ExtensibleObject extObj)
                                {
                                    path.Add($".{pair.Key}");
                                    TestForExtensionData(extObj, path, fails, testForUndefinedEnumOptions, enumOptionsToIgnore);
                                    path.RemoveAt(path.Count - 1);
                                }
                            }
                            break;

                        default:
                            if (testForUndefinedEnumOptions)
                            {
                                TestForUndefinedEnumOptions(path, fails, enumOptionsToIgnore, propertyValue);
                            }
                            break;
                        }
                        path.RemoveAt(path.Count - 1);
                    }
                }
            }
        }
Example #7
0
        private static void PopulateObject(JObject jObject, JsonObjectContract objectContract, ExtensibleObject?source, ExtensibleObject target)
        {
            var targetExtensionData = target.ExtensionData;

            foreach (var jProperty in jObject.Properties())
            {
                var property = objectContract.Properties.GetClosestMatchProperty(jProperty.Name);
                if (property == null)
                {
                    targetExtensionData[jProperty.Name] = jProperty.Value;
                }
                else
                {
                    var    propertyValueProvider = property.ValueProvider;
                    var    valueProvider         = InternalPrivateContractResolver.GetBackingFieldInfo(objectContract.UnderlyingType, property.UnderlyingName)?.ValueProvider ?? propertyValueProvider;
                    object?targetValue           = valueProvider.GetValue(target);
                    targetValue = targetValue is IValue tv ? tv.Value : targetValue;
                    object?sourceValue = valueProvider.GetValue(source);
                    sourceValue = sourceValue is IValue sv ? sv.Value : sourceValue;
                    var value    = jProperty.Value;
                    var setValue = true;
                    if (targetValue != null && sourceValue != null && !(value is JValue))
                    {
                        var propertyContract = InternalPrivateContractResolver.ResolveContract(property.PropertyType);
                        switch (propertyContract)
                        {
                        case JsonObjectContract propertyObjectContract:
                            PopulateObject((JObject)value, propertyObjectContract, (ExtensibleObject)sourceValue, (ExtensibleObject)targetValue);
                            setValue = false;
                            break;

                        case JsonDictionaryContract dictionaryContract:
                            PopulateDictionary((JObject)value, dictionaryContract, (IDictionary)sourceValue, (IDictionary)targetValue);
                            setValue = false;
                            break;

                        case JsonArrayContract arrayContract:
                            PopulateArray((JArray)value, arrayContract, (IList)sourceValue, (IList)targetValue);
                            setValue = false;
                            break;
                        }
                    }
                    if (setValue)
                    {
                        propertyValueProvider.SetValue(target, sourceValue);
                    }
                }
            }
        }