[vala/wip/transform: 15/43] Serialize hash table



commit a83919b346be78ab4416603e3e2e10849a0f4ad5
Author: Luca Bruno <lucabru src gnome org>
Date:   Fri Dec 30 11:27:19 2011 +0100

    Serialize hash table

 codegen/valagvarianttransformer.vala |   24 ++++++++++++++++++++----
 vala/valacodebuilder.vala            |    2 ++
 2 files changed, 22 insertions(+), 4 deletions(-)
---
diff --git a/codegen/valagvarianttransformer.vala b/codegen/valagvarianttransformer.vala
index f086d25..9600e2f 100644
--- a/codegen/valagvarianttransformer.vala
+++ b/codegen/valagvarianttransformer.vala
@@ -229,11 +229,19 @@ public class Vala.GVariantTransformer : CodeTransformer {
 		return expression (@"$builder.end ()");
 	}
 
-	Expression? serialize_expression (DataType type, Expression expr) {
-		if (type.data_type == context.analyzer.gvariant_type.data_type) {
-			return expr;
-		}
+	Expression? serialize_hash_table (ObjectType type, Expression expr) {
+		string temp = b.add_temp_declaration (expr.value_type, expr);
+
+		var builderinit = expression (@"new GLib.VariantBuilder (new GLib.VariantType (\"$(get_type_signature (type))\"))");
+		var builder = b.add_temp_declaration (null, builderinit);
+
+		var for_each = expression (@"$temp.for_each ((k,v) => $builder.add (\"{?*}\", k, v))");
+		b.add_expression (for_each);
 
+		return expression (@"$builder.end ()");
+	}
+
+	Expression? serialize_expression (DataType type, Expression expr) {
 		BasicTypeInfo basic_type;
 		Expression result = null;
 		if (get_basic_type_info (get_type_signature (type), out basic_type)) {
@@ -242,6 +250,14 @@ public class Vala.GVariantTransformer : CodeTransformer {
 			result = serialize_array ((ArrayType) type, expr);
 		} else if (type.data_type is Struct) {
 			result = serialize_struct ((Struct) type.data_type, expr);
+		} else if (type is ObjectType) {
+			if (type.data_type == context.analyzer.gvariant_type.data_type) {
+				var variant_new = (ObjectCreationExpression) expression ("new GLib.Variant.variant ()");
+				variant_new.add_argument (expr);
+				result = variant_new;
+			} else if (type.data_type.get_full_name () == "GLib.HashTable") {
+				result = serialize_hash_table ((ObjectType) type, expr);
+			}
 		}
 		return result;
 	}
diff --git a/vala/valacodebuilder.vala b/vala/valacodebuilder.vala
index f08fc92..c15fc05 100644
--- a/vala/valacodebuilder.vala
+++ b/vala/valacodebuilder.vala
@@ -38,6 +38,8 @@ public class Vala.CodeBuilder {
 
 		var statement_block = context.analyzer.get_current_block (insert_statement);
 		statement_block.insert_before (insert_statement, current_block);
+		this.insert_statement = current_block;
+
 		check_nodes.add (current_block);
 	}
 



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