[banshee/gtk3] build: account for null defaultValues in GSettingsSchemaExtractor



commit c60a8d356db962c7df1becd573d463383536e504
Author: Andres G. Aragoneses <knocte gmail com>
Date:   Tue Dec 18 23:07:38 2012 +0000

    build: account for null defaultValues in GSettingsSchemaExtractor
    
    When finding null default values in some SchemaEntry fields, it was
    blowing up with NullReferenceExceptions.
    
    To avoid this, we now use the property FieldType of the FieldInfo object
    instead of querying value.GetType (). Added also a unit test that covers
    this.

 build/GSettingsSchemaExtractor.cs      |   21 +++++++++++++--------
 build/GSettingsSchemaExtractorTests.cs |   32 ++++++++++++++++++++++++++++++--
 2 files changed, 43 insertions(+), 10 deletions(-)
---
diff --git a/build/GSettingsSchemaExtractor.cs b/build/GSettingsSchemaExtractor.cs
index 10eb544..7b36271 100644
--- a/build/GSettingsSchemaExtractor.cs
+++ b/build/GSettingsSchemaExtractor.cs
@@ -47,7 +47,11 @@ public class GSettingsSchemaExtractorProgram
 
                     object schema = field.GetValue (null);
 
-                    AddSchemaEntry (schema.GetType ().GetField ("DefaultValue").GetValue (schema),
+                    var default_value = schema.GetType ().GetField ("DefaultValue");
+
+                    AddSchemaEntry (
+                        default_value.GetValue (schema),
+                        default_value.FieldType,
                         GetString (schema, "Namespace"),
                         GetString (schema, "Key"),
                         GetString (schema, "ShortDescription"),
@@ -106,25 +110,26 @@ public class GSettingsSchemaExtractorProgram
         return o == null ? null : o.ToString ();
     }
 
-    private static void AddSchemaEntry (object value, string namespce, string key,
-        string summary, string description)
+    private static void AddSchemaEntry (object defaultValue, Type defaultValueType,
+                                        string namespce, string key,
+                                        string summary, string description)
     {
         schema_count++;
 
         string id = CreateId (namespce);
         
-        bool list = value.GetType ().IsArray;
-        Type type = list ? Type.GetTypeArray ((object [])value) [0] : value.GetType ();
+        bool list = defaultValueType.IsArray;
+        Type type = list ? Type.GetTypeArray ((object [])defaultValue) [0] : defaultValueType;
         string str_val = null;
         string str_type = null;
         
         if (list) {
-            if (value == null || ((object[])value).Length == 0) {
+            if (defaultValue == null || ((object[])defaultValue).Length == 0) {
                 GetValueString (type, null, out str_type);
                 str_val = "[]";
             } else {
                 str_val = "[";
-                object [] arr = (object [])value;
+                object [] arr = (object [])defaultValue;
                 for (int i = 0; i < arr.Length; i++) {
                     str_val += GetValueString (type, arr [i], out str_type).Replace (",", "\\,");
                     if (i < arr.Length - 1) {
@@ -134,7 +139,7 @@ public class GSettingsSchemaExtractorProgram
                 str_val += "]";
             }
         } else {
-            str_val = GetValueString (type, value, out str_type);
+            str_val = GetValueString (type, defaultValue, out str_type);
         }
 
         string type_attrib = str_type;
diff --git a/build/GSettingsSchemaExtractorTests.cs b/build/GSettingsSchemaExtractorTests.cs
index 6181787..869712b 100644
--- a/build/GSettingsSchemaExtractorTests.cs
+++ b/build/GSettingsSchemaExtractorTests.cs
@@ -154,8 +154,8 @@ namespace GSettingsSchemaExtractor
             public static readonly SchemaEntry<string[]> CurrentFiltersSchema = new SchemaEntry<string[]> (
                 "sources.fsq", "current_filters",
                 new string[] { "album", "artist" },
-            null,
-            null
+                null,
+                null
             );
         }
 
@@ -210,6 +210,34 @@ namespace GSettingsSchemaExtractor
                 .Trim ()));
         }
 
+        internal class StringTypeWithNullDefaultValue {
+            public static readonly SchemaEntry<string> LastScrobbleUrlSchema = new SchemaEntry<string> (
+                "plugins.audioscrobbler", "api_url",
+                null,
+                "AudioScrobbler API URL",
+                "URL for the AudioScrobbler API (supports turtle.libre.fm, for instance)"
+            );
+        }
+
+        [Test]
+        public void SchemaWithNullDefaultValue ()
+        {
+            StringBuilder result = GSettingsSchemaExtractorProgram.Extract (new Type [] { typeof (StringTypeWithNullDefaultValue) });
+
+            Assert.That (result, Is.Not.Null);
+            Assert.That (result.ToString ().Trim (), Is.EqualTo (@"
+<schemalist>
+  <schema id=""org.gnome.banshee.plugins.audioscrobbler"" path=""/apps/banshee/plugins/audioscrobbler/"" gettext-domain=""banshee"">
+    <key name=""api_url"" type=""s"">
+      <default></default>
+      <_summary>AudioScrobbler API URL</_summary>
+      <_description>URL for the AudioScrobbler API (supports turtle.libre.fm, for instance)</_description>
+    </key>
+  </schema>
+</schemalist>"
+                .Trim ()));
+        }
+
     }
 }
 



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