[banshee/gtk3] GSettings: avoid crashing when extracting schemas with default empty array



commit c635f65a90e2d4422f58cc1dff3b9c3336ee6a68
Author: Andres G. Aragoneses <knocte gmail com>
Date:   Mon Apr 1 07:22:05 2013 +0100

    GSettings: avoid crashing when extracting schemas with default empty array
    
    If the schema was an array with an empty list as default value, the
    gsettings-schema-extractor tool would crash without this change to
    properly guess the inner type without depending on inspecting the first
    element.
    
    The first use case in Banshee sources is the current_filters key inside
    the sources namespace (DatabaseSource class). Plus, a new unit test is
    added too to GSettingsSchemaExtractorTests.

 build/GSettingsSchemaExtractor.cs      |   22 +++++++---------------
 build/GSettingsSchemaExtractorTests.cs |   28 ++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 15 deletions(-)
---
diff --git a/build/GSettingsSchemaExtractor.cs b/build/GSettingsSchemaExtractor.cs
index e4c2804..b597d97 100644
--- a/build/GSettingsSchemaExtractor.cs
+++ b/build/GSettingsSchemaExtractor.cs
@@ -278,34 +278,27 @@ public class GSettingsSchemaExtractorProgram
 
     internal static string GetTypeAttrib (object defaultValue, Type defaultValueType)
     {
-        string str_type;
+        var i_enumerable_interface = defaultValueType.GetInterfaces ()
+            .Where (i => i.IsGenericType && i.GetGenericTypeDefinition () == typeof 
(IEnumerable<>)).FirstOrDefault ();
         bool list = defaultValueType.IsArray;
-        Type type = null;
         if (list) {
-            type = Type.GetTypeArray ((object [])defaultValue) [0];
-            object [] arr = (object[])defaultValue;
-            GetValueString (type, arr [0]);
-            str_type = "a" + GetGcType (type);
-        } else {
-            type = defaultValueType;
-            GetValueString (type, defaultValue);
-            str_type = GetGcType (type);
+            var inner_type = i_enumerable_interface.GetGenericArguments () [0];
+            return "a" + GetGcType (inner_type);
         }
-        return str_type;
+        return GetGcType (defaultValueType);
     }
 
     internal static string GetDefault (object defaultValue, Type defaultValueType)
     {
         bool list = defaultValueType.IsArray;
 
-        Type type = null;
         string str_val = null;
 
         if (list) {
-            type = Type.GetTypeArray ((object [])defaultValue) [0];
             if (defaultValue == null || ((object[])defaultValue).Length == 0) {
                 str_val = "[]";
             } else {
+                var type = Type.GetTypeArray ((object [])defaultValue) [0];
                 str_val = "[";
                 object [] arr = (object [])defaultValue;
                 for (int i = 0; i < arr.Length; i++) {
@@ -317,8 +310,7 @@ public class GSettingsSchemaExtractorProgram
                 str_val += "]";
             }
         } else {
-            type = defaultValueType;
-            str_val = GetValueString (type, defaultValue);
+            str_val = GetValueString (defaultValueType, defaultValue);
         }
         return str_val;
     }
diff --git a/build/GSettingsSchemaExtractorTests.cs b/build/GSettingsSchemaExtractorTests.cs
index 3d20c97..2e91980 100644
--- a/build/GSettingsSchemaExtractorTests.cs
+++ b/build/GSettingsSchemaExtractorTests.cs
@@ -180,6 +180,34 @@ namespace GSettingsSchemaExtractor
                 .Trim ()));
         }
 
+        internal class ArrayTypeWithEmptyDefaultValue {
+            public static readonly SchemaEntry<string[]> CurrentFiltersSchema = new SchemaEntry<string[]> (
+                "sources.fsq", "current_filters",
+                new string [0],
+                null,
+                null
+            );
+        }
+
+        [Test]
+        public void SchemaWithEmptyArrayAsDefaultValue ()
+        {
+            StringBuilder result = GSettingsSchemaExtractorProgram.Extract (new Type [] { typeof 
(ArrayTypeWithEmptyDefaultValue) });
+
+            Assert.That (result, Is.Not.Null);
+            Assert.That (result.ToString ().Trim (), Is.EqualTo (@"
+<schemalist>
+  <schema id=""org.gnome.banshee.sources.fsq"" path=""/apps/banshee/sources/fsq/"" 
gettext-domain=""banshee"">
+    <key name=""current-filters"" type=""as"">
+      <default>[]</default>
+      <summary></summary>
+      <description></description>
+    </key>
+  </schema>
+</schemalist>"
+                .Trim ()));
+        }
+
         [Test]
         public void SchemaWithMoreThanOneKey ()
         {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]