summaryrefslogtreecommitdiff
path: root/plugins/yaml/parser.c
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2023-05-07 09:10:31 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2023-05-07 09:10:31 (GMT)
commit9e4480706b28abc41618bd598c00a194beb14c4f (patch)
tree15fc4fc623407863f0ecdec5ca97a4cac63f8ad2 /plugins/yaml/parser.c
parent25bac01127581767639a5bd9024c41eb803388fa (diff)
Aggregate YAML values from sequences when requested and possible.
Diffstat (limited to 'plugins/yaml/parser.c')
-rw-r--r--plugins/yaml/parser.c60
1 files changed, 38 insertions, 22 deletions
diff --git a/plugins/yaml/parser.c b/plugins/yaml/parser.c
index 92e78be..8c06723 100644
--- a/plugins/yaml/parser.c
+++ b/plugins/yaml/parser.c
@@ -37,12 +37,24 @@
#include "pair.h"
+#define SCALAR_STYLE_TO_ORIGINAL_STYLE(v) \
+ ({ \
+ YamlOriginalStyle __result; \
+ if (v == YAML_SINGLE_QUOTED_SCALAR_STYLE) \
+ __result = YOS_SINGLE_QUOTED; \
+ else if (v == YAML_DOUBLE_QUOTED_SCALAR_STYLE) \
+ __result = YOS_DOUBLE_QUOTED; \
+ else \
+ __result = YOS_PLAIN; \
+ __result; \
+ })
+
/* Construit la version GLib d'un noeud YAML brut. */
static GYamlPair *build_pair_from_yaml(yaml_document_t *, int, int);
/* Transforme un noeud YAML brut en sa version Glib. */
-static GYamlCollection *translate_yaml_node(yaml_document_t *, yaml_node_t *);
+static GYamlNode *translate_yaml_node(yaml_document_t *, yaml_node_t *);
@@ -65,7 +77,7 @@ static GYamlPair *build_pair_from_yaml(yaml_document_t *document, int key, int v
GYamlPair *result; /* Racine à retourner */
yaml_node_t *key_node; /* Noeud brut de la clef */
yaml_node_t *value_node; /* Noeud brut de la valeur */
- GYamlCollection *children; /* Collection de noeuds YAML */
+ GYamlNode *children; /* Collection de noeuds YAML */
result = NULL;
@@ -79,7 +91,10 @@ static GYamlPair *build_pair_from_yaml(yaml_document_t *document, int key, int v
assert(value_node != NULL);
if (value_node->type == YAML_SCALAR_NODE)
- result = g_yaml_pair_new((char *)key_node->data.scalar.value, (char *)value_node->data.scalar.value);
+ result = g_yaml_pair_new((char *)key_node->data.scalar.value,
+ SCALAR_STYLE_TO_ORIGINAL_STYLE(key_node->data.scalar.style),
+ (char *)value_node->data.scalar.value,
+ SCALAR_STYLE_TO_ORIGINAL_STYLE(value_node->data.scalar.style));
else
{
@@ -87,9 +102,11 @@ static GYamlPair *build_pair_from_yaml(yaml_document_t *document, int key, int v
if (children != NULL)
{
- result = g_yaml_pair_new((char *)key_node->data.scalar.value, NULL);
+ result = g_yaml_pair_new((char *)key_node->data.scalar.value,
+ SCALAR_STYLE_TO_ORIGINAL_STYLE(key_node->data.scalar.style),
+ NULL, YOS_PLAIN);
- g_yaml_pair_set_children(result, children);
+ g_yaml_pair_set_children(result, G_YAML_COLLEC(children));
}
@@ -115,35 +132,41 @@ static GYamlPair *build_pair_from_yaml(yaml_document_t *document, int key, int v
* *
******************************************************************************/
-static GYamlCollection *translate_yaml_node(yaml_document_t *document, yaml_node_t *node)
+static GYamlNode *translate_yaml_node(yaml_document_t *document, yaml_node_t *node)
{
- GYamlCollection *result; /* Racine à retourner */
+ GYamlNode *result; /* Racine à retourner */
yaml_node_item_t *index; /* Elément d'une série */
yaml_node_t *item; /* Elément d'une série */
- GYamlCollection *collec; /* Version GLib de l'élément */
+ GYamlNode *child; /* Version GLib de l'élément */
yaml_node_pair_t *pair; /* Combinaison clef/valeur */
GYamlPair *sub; /* Sous-noeud à intégrer */
switch (node->type)
{
+ case YAML_SCALAR_NODE:
+ result = G_YAML_NODE(g_yaml_pair_new((char *)node->data.scalar.value,
+ SCALAR_STYLE_TO_ORIGINAL_STYLE(node->data.scalar.style),
+ NULL, YOS_PLAIN));
+ break;
+
case YAML_SEQUENCE_NODE:
- result = g_yaml_collection_new(true);
+ result = G_YAML_NODE(g_yaml_collection_new(true));
for (index = node->data.sequence.items.start; index < node->data.sequence.items.top; index++)
{
item = yaml_document_get_node(document, *index);
assert(item != NULL);
- collec = translate_yaml_node(document, item);
+ child = translate_yaml_node(document, item);
- if (collec == NULL)
+ if (child == NULL)
{
g_clear_object(&result);
break;
}
- g_yaml_collection_add_node(result, G_YAML_NODE(collec));
+ g_yaml_collection_add_node(G_YAML_COLLEC(result), child);
}
@@ -151,7 +174,7 @@ static GYamlCollection *translate_yaml_node(yaml_document_t *document, yaml_node
case YAML_MAPPING_NODE:
- result = g_yaml_collection_new(false);
+ result = G_YAML_NODE(g_yaml_collection_new(false));
for (pair = node->data.mapping.pairs.start; pair < node->data.mapping.pairs.top; pair++)
{
@@ -163,7 +186,7 @@ static GYamlCollection *translate_yaml_node(yaml_document_t *document, yaml_node
break;
}
- g_yaml_collection_add_node(result, G_YAML_NODE(sub));
+ g_yaml_collection_add_node(G_YAML_COLLEC(result), G_YAML_NODE(sub));
}
@@ -201,7 +224,6 @@ GYamlNode *parse_yaml_from_text(const char *text, size_t len)
yaml_document_t document; /* Document YAML constitué */
int ret; /* Bilan de la constitution */
yaml_node_t *root; /* Elément racine brut */
- GYamlCollection *collec; /* Version Glib de la racine */
result = NULL;
@@ -215,13 +237,7 @@ GYamlNode *parse_yaml_from_text(const char *text, size_t len)
root = yaml_document_get_root_node(&document);
if (root != NULL)
- {
- collec = translate_yaml_node(&document, root);
-
- if (collec != NULL)
- result = G_YAML_NODE(collec);
-
- }
+ result = translate_yaml_node(&document, root);
yaml_document_delete(&document);