]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Deserialize structs
authorLuca Bruno <lucabru@src.gnome.org>
Fri, 30 Dec 2011 16:56:43 +0000 (17:56 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Mon, 11 Mar 2019 12:49:26 +0000 (13:49 +0100)
codegen/valagvarianttransformer.vala

index 2c2cec5323a3cb51d979c9dbb5382055b36c5d88..535de531a349469fa3b29ef96235c853611ee501 100644 (file)
@@ -318,6 +318,34 @@ public class Vala.GVariantTransformer : CodeTransformer {
                b.close ();
        }
 
+       Expression? deserialize_struct (Struct st, Expression expr) {
+               var variant = b.add_temp_declaration (data_type ("GLib.Variant", true), null);
+               var iterator = b.add_temp_declaration (data_type ("GLib.VariantIter", true), null);
+               b.add_assignment (expression (variant), expr);
+               b.add_assignment (expression (iterator), expression (@"$variant.iterator ()"));
+               var type = context.analyzer.get_data_type_for_symbol (st);
+               type.value_owned = true;
+               var result = b.add_temp_declaration (type, expression ("{}"));
+
+               bool field_found = false;
+
+               foreach (var f in st.get_fields ()) {
+                       if (f.binding != MemberBinding.INSTANCE) {
+                               continue;
+                       }
+
+                       field_found = true;
+
+                       b.add_assignment (expression (@"$result.$(f.name)"), deserialize_expression (f.variable_type, expression (@"$iterator.next_value ()")));
+               }
+
+               if (!field_found) {
+                       return null;
+               }
+
+               return expression (result);
+       }
+
        Expression? deserialize_expression (DataType type, Expression expr) {
                BasicTypeInfo basic_type;
                Expression result = null;
@@ -325,6 +353,8 @@ public class Vala.GVariantTransformer : CodeTransformer {
                        result = deserialize_basic (basic_type, expr);
                } else if (type is ArrayType) {
                        result = deserialize_array ((ArrayType) type, expr);
+               } else if (type.data_type is Struct) {
+                       result = deserialize_struct ((Struct) type.data_type, expr);
                }
                return result;
        }