[couchdb-glib] Add API to list fields for CouchdbDocument's and CouchdbStructField's



commit c4855da7d8c84542a24cd6ae284b231a2e47c5c8
Author: Rodrigo Moya <rodrigo gnome-db org>
Date:   Fri Jul 16 13:40:59 2010 +0200

    Add API to list fields for CouchdbDocument's and CouchdbStructField's

 couchdb-glib/couchdb-document.c     |   45 +++++++++++++++++++++++++++++++++++
 couchdb-glib/couchdb-document.h     |    3 +-
 couchdb-glib/couchdb-struct-field.c |   17 +++++++++++++
 couchdb-glib/couchdb-struct-field.h |    1 +
 couchdb-glib/utils.c                |   21 ++++++++++++++++
 couchdb-glib/utils.h                |    2 +
 6 files changed, 88 insertions(+), 1 deletions(-)
---
diff --git a/couchdb-glib/couchdb-document.c b/couchdb-glib/couchdb-document.c
index e5f162c..e68f3a9 100644
--- a/couchdb-glib/couchdb-document.c
+++ b/couchdb-glib/couchdb-document.c
@@ -291,6 +291,51 @@ couchdb_document_remove_field (CouchdbDocument *document, const char *field)
 }
 
 /**
+ * couchdb_document_get_field_names:
+ * @document: A #CouchdbDocument object
+ *
+ * Retrieve the list of fields contained in the given #CouchdbDocument.
+ *
+ * Return value: A list of strings containing the names of all the fields contained
+ * in the given #CouchdbDocument object. When no longer needed, the list should
+ * be freed by calling #g_slist_free.
+ */
+GSList *
+couchdb_document_get_fields (CouchdbDocument *document)
+{
+	GList *json_list;
+	GSList *result = NULL;
+
+	g_return_val_if_fail (COUCHDB_IS_DOCUMENT (document), NULL);
+
+	json_list = json_object_get_members (document->priv->root_object);
+	while (json_list != NULL) {
+		result = g_slist_append (result, json_list->data);
+
+		json_list = g_list_remove (json_list, json_list->data);
+	}
+
+	return result;
+}
+
+/**
+ * couchdb_document_get_field_type:
+ * @document: A #CouchdbDocument object
+ * @field: Name of the field to get type
+ *
+ * Get the value type of the given #CouchdbDocument's field.
+ *
+ * Return value: Type of the field.
+ */
+GType
+couchdb_document_get_field_type (CouchdbDocument *document, const char *field)
+{
+	g_return_val_if_fail (COUCHDB_IS_DOCUMENT (document), G_TYPE_INVALID);
+
+	return get_type_from_field (document->priv->root_object, field);
+}
+
+/**
  * couchdb_document_get_array_field:
  * @document: A #CouchdbDocument object
  * @field: Name of the field to retrieve
diff --git a/couchdb-glib/couchdb-document.h b/couchdb-glib/couchdb-document.h
index a952b6c..161b08a 100644
--- a/couchdb-glib/couchdb-document.h
+++ b/couchdb-glib/couchdb-document.h
@@ -50,7 +50,6 @@ typedef struct {
 	GObjectClass parent_class;
 } CouchdbDocumentClass;
 
-
 GType               couchdb_document_get_type (void);
 
 CouchdbDocument    *couchdb_document_new (void);
@@ -62,6 +61,8 @@ void                couchdb_document_set_revision (CouchdbDocument *document, co
 
 gboolean            couchdb_document_has_field (CouchdbDocument *document, const char *field);
 void                couchdb_document_remove_field (CouchdbDocument *document, const char *field);
+GSList             *couchdb_document_get_field_names (CouchdbDocument *document);
+GType               couchdb_document_get_field_type (CouchdbDocument *document, const char *field);
 
 CouchdbArrayField  *couchdb_document_get_array_field (CouchdbDocument *document, const char *field);
 void                couchdb_document_set_array_field (CouchdbDocument *document, const char *field, CouchdbArrayField *value);
diff --git a/couchdb-glib/couchdb-struct-field.c b/couchdb-glib/couchdb-struct-field.c
index aa8a964..9fc2971 100644
--- a/couchdb-glib/couchdb-struct-field.c
+++ b/couchdb-glib/couchdb-struct-field.c
@@ -189,6 +189,23 @@ couchdb_struct_field_get_field_names (CouchdbStructField *sf)
 }
 
 /**
+ * couchdb_struct_field_get_field_type:
+ * @sf: A #CouchdbStructField object
+ * @field: Name of the field to get type
+ *
+ * Get the value type of the given #CouchdbStructField's field.
+ *
+ * Return value: Type of the field.
+ */
+GType
+couchdb_struct_field_get_field_type (CouchdbStructField *sf, const char *field)
+{
+	g_return_val_if_fail (COUCHDB_IS_STRUCT_FIELD (sf), G_TYPE_INVALID);
+
+	return get_type_from_field (sf->priv->json_object, field);
+}
+
+/**
  * couchdb_struct_field_get_array_field:
  * @sf: A #CouchdbStructField object
  * @field: Name of the field
diff --git a/couchdb-glib/couchdb-struct-field.h b/couchdb-glib/couchdb-struct-field.h
index ca6b9b2..acf2260 100644
--- a/couchdb-glib/couchdb-struct-field.h
+++ b/couchdb-glib/couchdb-struct-field.h
@@ -51,6 +51,7 @@ gboolean            couchdb_struct_field_has_field (CouchdbStructField *sf, cons
 void                couchdb_struct_field_remove_field (CouchdbStructField *sf, const char *field);
 
 GSList             *couchdb_struct_field_get_field_names (CouchdbStructField *sf);
+GType               couchdb_struct_field_get_field_type (CouchdbStructField *sf, const char *field);
 
 CouchdbArrayField  *couchdb_struct_field_get_array_field (CouchdbStructField *sf, const char *field);
 void                couchdb_struct_field_set_array_field (CouchdbStructField *sf, const char *field, CouchdbArrayField *value);
diff --git a/couchdb-glib/utils.c b/couchdb-glib/utils.c
index 7452c59..d11a8ed 100644
--- a/couchdb-glib/utils.c
+++ b/couchdb-glib/utils.c
@@ -47,3 +47,24 @@ generate_uuid (void)
 
 	return g_strdup (uuid_string);
 }
+
+GType
+get_type_from_field (JsonObject *json_object, const gchar *field)
+{
+	JsonNode *node;
+	GType type;
+
+	node = json_object_get_member (json_object, field);
+	if (node == NULL)
+		G_TYPE_INVALID;
+
+	type = json_node_get_value_type (node);
+	switch (type) {
+	case JSON_NODE_OBJECT:
+		return COUCHDB_TYPE_STRUCT_FIELD;
+	case JSON_NODE_ARRAY:
+		return COUCHDB_TYPE_ARRAY_FIELD;
+	default:
+		return type;
+	}
+}
diff --git a/couchdb-glib/utils.h b/couchdb-glib/utils.h
index 6aa8ce3..316f6e4 100644
--- a/couchdb-glib/utils.h
+++ b/couchdb-glib/utils.h
@@ -38,6 +38,8 @@ GQuark      couchdb_error_quark (void);
 
 char* generate_uuid (void);
 
+GType get_type_from_field (JsonObject *object, const char *field);
+
 /* Private API */
 gboolean            couchdb_session_send_message (CouchdbSession *session,
 						  const char *method,



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