[couchdb-glib] Add array fields to CouchdbDocument's



commit 5c5668a136ed56da5fda9e7ec61b15f491b30d22
Author: Rodrigo Moya <rodrigo gnome-db org>
Date:   Fri Feb 12 18:55:36 2010 +0100

    Add array fields to CouchdbDocument's

 couchdb-glib/couchdb-array-field.c  |   13 +++++-
 couchdb-glib/couchdb-document.c     |   43 ++++++++++++++++++
 couchdb-glib/couchdb-document.h     |   81 +++++++++++++----------------------
 couchdb-glib/couchdb-struct-field.c |   23 +---------
 couchdb-glib/couchdb-struct-field.h |    1 +
 couchdb-glib/utils.h                |    3 +
 6 files changed, 91 insertions(+), 73 deletions(-)
---
diff --git a/couchdb-glib/couchdb-array-field.c b/couchdb-glib/couchdb-array-field.c
index a448ede..db66b94 100644
--- a/couchdb-glib/couchdb-array-field.c
+++ b/couchdb-glib/couchdb-array-field.c
@@ -20,6 +20,7 @@
  */
 
 #include <json-glib/json-glib.h>
+#include "utils.h"
 #include "couchdb-array-field.h"
 #include "couchdb-struct-field.h"
 
@@ -262,7 +263,7 @@ couchdb_array_field_get_array_element (CouchdbArrayField *array, guint index)
 		returned_array = couchdb_array_field_new ();
 		for (i = 0; i < json_array_get_length (json_array); i++) {
 			json_array_add_element (returned_array->json_array,
-						json_array_get_element (json_array));
+						json_array_get_element (json_array, index));
 		}
 	}
 
@@ -347,7 +348,7 @@ couchdb_array_field_get_string_element (CouchdbArrayField *array, guint index)
  * Return value: Value of the element stored in the given position of the array.
  */
 CouchdbStructField *
-couchdb_array_field_get_string_element (CouchdbArrayField *array, guint index)
+couchdb_array_field_get_struct_element (CouchdbArrayField *array, guint index)
 {
 	JsonObject *json_object;
 
@@ -359,3 +360,11 @@ couchdb_array_field_get_string_element (CouchdbArrayField *array, guint index)
 
 	return NULL;
 }
+
+JsonArray *
+couchdb_array_field_get_json_array (CouchdbArrayField *array)
+{
+	g_return_val_if_fail (array != NULL, NULL);
+
+	return array->json_array;
+}
diff --git a/couchdb-glib/couchdb-document.c b/couchdb-glib/couchdb-document.c
index 7ff5749..81d0ef3 100644
--- a/couchdb-glib/couchdb-document.c
+++ b/couchdb-glib/couchdb-document.c
@@ -389,6 +389,49 @@ couchdb_document_remove_field (CouchdbDocument *document, const char *field)
 }
 
 /**
+ * couchdb_document_get_array_field:
+ * @document: A #CouchdbDocument object
+ * @field: Name of the field to retrieve
+ *
+ * Retrieve the value of an array field from the given document.
+ *
+ * Return value: The value of the given array field.
+ */
+CouchdbArrayField *
+couchdb_document_get_array_field (CouchdbDocument *document, const char *field)
+{
+	g_return_val_if_fail (COUCHDB_IS_DOCUMENT (document), NULL);
+	g_return_val_if_fail (field != NULL, NULL);
+
+	if (!json_object_has_member (json_node_get_object (document->root_node), field))
+		return NULL;
+
+	return couchdb_array_field_new_from_json_array (
+		json_array_ref (json_object_get_array_member (json_node_get_object (document->root_node),
+							       field)));
+}
+
+/**
+ * couchdb_document_set_array_field:
+ * @document: A #CouchdbDocument object
+ * @field: Name of the field to set
+ * @value: Value to set the field to
+ *
+ * Set the value of an array field in the given document.
+ */
+void
+couchdb_document_set_array_field (CouchdbDocument *document, const char *field, CouchdbArrayField *value)
+{
+	g_return_if_fail (COUCHDB_IS_DOCUMENT (document));
+	g_return_if_fail (field != NULL);
+	g_return_if_fail (value != NULL);
+
+	json_object_set_array_member (json_node_get_object (document->root_node),
+				      field,
+				      json_array_ref (couchdb_array_field_get_json_array (value)));
+}
+
+/**
  * couchdb_document_get_boolean_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 4cb4b54..73bf1fd 100644
--- a/couchdb-glib/couchdb-document.h
+++ b/couchdb-glib/couchdb-document.h
@@ -28,6 +28,7 @@
 #include <glib-object.h>
 #include <json-glib/json-glib.h>
 #include "couchdb-types.h"
+#include "couchdb-array-field.h"
 #include "couchdb-struct-field.h"
 
 G_BEGIN_DECLS
@@ -44,62 +45,40 @@ typedef struct {
 } CouchdbDocumentClass;
 
 
-GType            couchdb_document_get_type 		(void);
+GType            couchdb_document_get_type (void);
 
-CouchdbDocument *couchdb_document_new			(CouchdbSession  *couchdb);
-CouchdbDocument *couchdb_document_get			(CouchdbSession  *couchdb,
-	                                                 const char      *dbname,
-	                                                 const char      *docid,
-	                                                 GError          **error);
-gboolean         couchdb_document_put			(CouchdbDocument *document,
-	                                                 const char      *dbname,
-	                                                 GError **error);
-gboolean         couchdb_document_delete		(CouchdbDocument *document,
-	                                                 GError          **error);
+CouchdbDocument *couchdb_document_new (CouchdbSession  *couchdb);
+CouchdbDocument *couchdb_document_get (CouchdbSession  *couchdb,
+				       const char      *dbname,
+				       const char      *docid,
+				       GError          **error);
+gboolean         couchdb_document_put (CouchdbDocument *document,
+				       const char      *dbname,
+				       GError **error);
+gboolean         couchdb_document_delete (CouchdbDocument *document, GError **error);
 
-const char      *couchdb_document_get_id		(CouchdbDocument *document);
-void             couchdb_document_set_id		(CouchdbDocument *document,
-	                                                 const char      *id);
-	                                                 
-const char      *couchdb_document_get_revision		(CouchdbDocument *document);
-void             couchdb_document_set_revision		(CouchdbDocument *document,
-	                                                 const char      *revision);
+const char      *couchdb_document_get_id (CouchdbDocument *document);
+void             couchdb_document_set_id (CouchdbDocument *document, const char *id);
+const char      *couchdb_document_get_revision (CouchdbDocument *document);
+void             couchdb_document_set_revision (CouchdbDocument *document, const char *revision);
 
-gboolean         couchdb_document_has_field		(CouchdbDocument *document,
-	                                                 const char      *field);
-void             couchdb_document_remove_field		(CouchdbDocument *document,
-	                                                 const char      *field);
+gboolean         couchdb_document_has_field (CouchdbDocument *document, const char *field);
+void             couchdb_document_remove_field (CouchdbDocument *document, const char *field);
 
-gboolean         couchdb_document_get_boolean_field	(CouchdbDocument *document,
-	                                                 const char      *field);
-void             couchdb_document_set_boolean_field	(CouchdbDocument *document,
-	                                                 const char      *field,
-	                                                 gboolean        value);
-gint             couchdb_document_get_int_field		(CouchdbDocument *document,
-	                                                 const char      *field);
-void             couchdb_document_set_int_field		(CouchdbDocument *document,
-	                                                 const char      *field,
-	                                                 gint            value);
-gdouble          couchdb_document_get_double_field	(CouchdbDocument *document,
-	                                                 const char      *field);
-void             couchdb_document_set_double_field	(CouchdbDocument *document,
-	                                                 const char      *field,
-	                                                 gdouble         value);
-const char      *couchdb_document_get_string_field	(CouchdbDocument *document,
-	                                                 const char      *field);
-void             couchdb_document_set_string_field	(CouchdbDocument *document,
-	                                                 const char      *field,
-	                                                 const char      *value);
-
-CouchdbStructField *couchdb_document_get_struct_field	(CouchdbDocument *document,
-	                                                 const char      *field);
-void              couchdb_document_set_struct_field	(CouchdbDocument *document,
-	                                                 const char      *field,
-	                                                 CouchdbStructField *value);
+CouchdbArrayField  *couchdb_document_get_array_field (CouchdbDocument *document, const char *field);
+void                couchdb_document_set_array_field (CouchdbDocument *document, const char *field, CouchdbArrayField *value);
+gboolean            couchdb_document_get_boolean_field (CouchdbDocument *document, const char *field);
+void                couchdb_document_set_boolean_field (CouchdbDocument *document, const char *field, gboolean value);
+gint                couchdb_document_get_int_field (CouchdbDocument *document, const char *field);
+void                couchdb_document_set_int_field (CouchdbDocument *document, const char *field, gint value);
+gdouble             couchdb_document_get_double_field (CouchdbDocument *document, const char *field);
+void                couchdb_document_set_double_field (CouchdbDocument *document, const char *field, gdouble value);
+const char         *couchdb_document_get_string_field (CouchdbDocument *document, const char *field);
+void                couchdb_document_set_string_field (CouchdbDocument *document, const char *field, const char *value);
+CouchdbStructField *couchdb_document_get_struct_field (CouchdbDocument *document, const char *field);
+void                couchdb_document_set_struct_field (CouchdbDocument *document, const char *field, CouchdbStructField *value);
       
-char             *couchdb_document_to_string		(CouchdbDocument *document);
-
-JsonObject*	  couchdb_document_get_json_object	(CouchdbDocument *document);
+char             *couchdb_document_to_string (CouchdbDocument *document);
 
 G_END_DECLS
 
diff --git a/couchdb-glib/couchdb-struct-field.c b/couchdb-glib/couchdb-struct-field.c
index a48cdec..4b3a3f4 100644
--- a/couchdb-glib/couchdb-struct-field.c
+++ b/couchdb-glib/couchdb-struct-field.c
@@ -21,6 +21,8 @@
  * Boston, MA 02110-1301, USA.
  */
 
+#include <string.h>
+#include "utils.h"
 #include "couchdb-struct-field.h"
 
 struct _CouchdbStructField {
@@ -99,14 +101,6 @@ couchdb_struct_field_new_from_string (const char *str)
 	return sf;
 }
 
-/**
- * couchdb_struct_field_new_from_json_object:
- * @json_object: A JsonObject
- *
- * Create a new struct field object, filling it with values taken from a JsonObject.
- *
- * Return value: A newly-created #CouchdbStructField object.
- */
 CouchdbStructField *
 couchdb_struct_field_new_from_json_object (JsonObject *json_object)
 {
@@ -230,7 +224,7 @@ couchdb_struct_field_get_array_field (CouchdbStructField *sf, const char *field)
  * Set the value of an array field in the given struct field.
  */
 void
-couchdb_struct_field_set_struct_field (CouchdbStructField *sf, const char *field, CouchdbArrayField *value)
+couchdb_struct_field_set_array_field (CouchdbStructField *sf, const char *field, CouchdbArrayField *value)
 {
 	g_return_if_fail (sf != NULL);
 	g_return_if_fail (field != NULL);
@@ -501,17 +495,6 @@ couchdb_struct_field_to_string (CouchdbStructField *sf)
 	return str;
 }
 
-/**
- * couchdb_struct_field_get_json_object:
- * @sf: A #CouchdbStructField object
- *
- * Retrieve the JsonObject used internally by the given #CouchdbStructField object.
- * This is internal API, and should not be used by applications unless they really
- * need to deal with the internal aspects of the #CouchdbStructField object, which
- * developers are advised not to do.
- *
- * Return value: The internal JsonObject.
- */
 JsonObject *
 couchdb_struct_field_get_json_object (CouchdbStructField *sf)
 {
diff --git a/couchdb-glib/couchdb-struct-field.h b/couchdb-glib/couchdb-struct-field.h
index ef1d81f..e816f79 100644
--- a/couchdb-glib/couchdb-struct-field.h
+++ b/couchdb-glib/couchdb-struct-field.h
@@ -26,6 +26,7 @@
 
 #include <glib.h>
 #include <glib-object.h>
+#include "couchdb-array-field.h"
 #include "couchdb-session.h"
 #include "couchdb-types.h"
 
diff --git a/couchdb-glib/utils.h b/couchdb-glib/utils.h
index 3eebe66..4118d9a 100644
--- a/couchdb-glib/utils.h
+++ b/couchdb-glib/utils.h
@@ -38,8 +38,11 @@ GQuark      couchdb_error_quark (void);
 char* generate_uuid (void);
 
 /* Private API */
+JsonObject*	  couchdb_document_get_json_object	(CouchdbDocument *document);
+
 CouchdbArrayField  *couchdb_array_field_new_from_json_array (JsonArray *json_array);
 JsonArray          *couchdb_array_field_get_json_array (CouchdbArrayField *array);
+
 CouchdbStructField *couchdb_struct_field_new_from_json_object (JsonObject *json_object);
 JsonObject         *couchdb_struct_field_get_json_object (CouchdbStructField *sf);
 



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