rework keyval
[protos/libecoli.git] / src / ecoli_config.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2018, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #include <sys/queue.h>
6 #include <stddef.h>
7 #include <stdio.h>
8 #include <errno.h>
9 #include <inttypes.h>
10
11 #include <ecoli_string.h>
12 #include <ecoli_malloc.h>
13 #include <ecoli_keyval.h>
14 #include <ecoli_node.h>
15 #include <ecoli_log.h>
16 #include <ecoli_test.h>
17 #include <ecoli_config.h>
18
19 EC_LOG_TYPE_REGISTER(config);
20
21 const char *ec_config_reserved_keys[] = {
22         "id",
23         "attrs",
24         "help",
25         "type",
26 };
27
28 static int
29 __ec_config_dump(FILE *out, const char *key, const struct ec_config *config,
30         size_t indent);
31 static int
32 ec_config_dict_validate(const struct ec_keyval *dict,
33                         const struct ec_config_schema *schema);
34
35 bool
36 ec_config_key_is_reserved(const char *name)
37 {
38         size_t i;
39
40         for (i = 0; i < EC_COUNT_OF(ec_config_reserved_keys); i++) {
41                 if (!strcmp(name, ec_config_reserved_keys[i]))
42                         return true;
43         }
44         return false;
45 }
46
47 /* return ec_value type as a string */
48 static const char *
49 ec_config_type_str(enum ec_config_type type)
50 {
51         switch (type) {
52         case EC_CONFIG_TYPE_BOOL: return "bool";
53         case EC_CONFIG_TYPE_INT64: return "int64";
54         case EC_CONFIG_TYPE_UINT64: return "uint64";
55         case EC_CONFIG_TYPE_STRING: return "string";
56         case EC_CONFIG_TYPE_NODE: return "node";
57         case EC_CONFIG_TYPE_LIST: return "list";
58         case EC_CONFIG_TYPE_DICT: return "dict";
59         default: return "unknown";
60         }
61 }
62
63 static size_t
64 ec_config_schema_len(const struct ec_config_schema *schema)
65 {
66         size_t i;
67
68         if (schema == NULL)
69                 return 0;
70         for (i = 0; schema[i].type != EC_CONFIG_TYPE_NONE; i++)
71                 ;
72         return i;
73 }
74
75 static int
76 __ec_config_schema_validate(const struct ec_config_schema *schema,
77                         enum ec_config_type type)
78 {
79         size_t i, j;
80         int ret;
81
82         if (type == EC_CONFIG_TYPE_LIST) {
83                 if (schema[0].key != NULL) {
84                         errno = EINVAL;
85                         EC_LOG(EC_LOG_ERR, "list schema key must be NULL\n");
86                         return -1;
87                 }
88         } else if (type == EC_CONFIG_TYPE_DICT) {
89                 for (i = 0; schema[i].type != EC_CONFIG_TYPE_NONE; i++) {
90                         if (schema[i].key == NULL) {
91                                 errno = EINVAL;
92                                 EC_LOG(EC_LOG_ERR,
93                                         "dict schema key should not be NULL\n");
94                                 return -1;
95                         }
96                 }
97         } else {
98                 errno = EINVAL;
99                 EC_LOG(EC_LOG_ERR, "invalid schema type\n");
100                 return -1;
101         }
102
103         for (i = 0; schema[i].type != EC_CONFIG_TYPE_NONE; i++) {
104                 if (schema[i].key != NULL &&
105                                 ec_config_key_is_reserved(schema[i].key)) {
106                         errno = EINVAL;
107                         EC_LOG(EC_LOG_ERR,
108                                 "key name <%s> is reserved\n", schema[i].key);
109                         return -1;
110                 }
111                 /* check for duplicate name if more than one element */
112                 for (j = i + 1; schema[j].type != EC_CONFIG_TYPE_NONE; j++) {
113                         if (!strcmp(schema[i].key, schema[j].key)) {
114                                 errno = EEXIST;
115                                 EC_LOG(EC_LOG_ERR,
116                                         "duplicate key <%s> in schema\n",
117                                         schema[i].key);
118                                 return -1;
119                         }
120                 }
121
122                 switch (schema[i].type) {
123                 case EC_CONFIG_TYPE_BOOL:
124                 case EC_CONFIG_TYPE_INT64:
125                 case EC_CONFIG_TYPE_UINT64:
126                 case EC_CONFIG_TYPE_STRING:
127                 case EC_CONFIG_TYPE_NODE:
128                         if (schema[i].subschema != NULL || ec_config_schema_len(
129                                         schema[i].subschema) != 0) {
130                                 errno = EINVAL;
131                                 EC_LOG(EC_LOG_ERR,
132                                         "key <%s> should not have subtype/subschema\n",
133                                         schema[i].key);
134                                 return -1;
135                         }
136                         break;
137                 case EC_CONFIG_TYPE_LIST:
138                         if (schema[i].subschema == NULL || ec_config_schema_len(
139                                         schema[i].subschema) != 1) {
140                                 errno = EINVAL;
141                                 EC_LOG(EC_LOG_ERR,
142                                         "key <%s> must have subschema of length 1\n",
143                                         schema[i].key);
144                                 return -1;
145                         }
146                         break;
147                 case EC_CONFIG_TYPE_DICT:
148                         if (schema[i].subschema == NULL || ec_config_schema_len(
149                                         schema[i].subschema) == 0) {
150                                 errno = EINVAL;
151                                 EC_LOG(EC_LOG_ERR,
152                                         "key <%s> must have subschema\n",
153                                         schema[i].key);
154                                 return -1;
155                         }
156                         break;
157                 default:
158                         EC_LOG(EC_LOG_ERR, "invalid type for key <%s>\n",
159                                 schema[i].key);
160                         errno = EINVAL;
161                         return -1;
162                 }
163
164                 if (schema[i].subschema == NULL)
165                         continue;
166
167                 ret = __ec_config_schema_validate(schema[i].subschema,
168                                                 schema[i].type);
169                 if (ret < 0) {
170                         EC_LOG(EC_LOG_ERR, "cannot parse subschema %s%s\n",
171                                 schema[i].key ? "key=" : "",
172                                 schema[i].key ? : "");
173                         return ret;
174                 }
175         }
176
177         return 0;
178 }
179
180 int
181 ec_config_schema_validate(const struct ec_config_schema *schema)
182 {
183         return __ec_config_schema_validate(schema, EC_CONFIG_TYPE_DICT);
184 }
185
186 static void
187 __ec_config_schema_dump(FILE *out, const struct ec_config_schema *schema,
188                         size_t indent)
189 {
190         size_t i;
191
192         for (i = 0; schema[i].type != EC_CONFIG_TYPE_NONE; i++) {
193                 fprintf(out, "%*s" "%s%s%stype=%s desc='%s'\n",
194                         (int)indent * 4, "",
195                         schema[i].key ? "key=": "",
196                         schema[i].key ? : "",
197                         schema[i].key ? " ": "",
198                         ec_config_type_str(schema[i].type),
199                         schema[i].desc);
200                 if (schema[i].subschema == NULL)
201                         continue;
202                 __ec_config_schema_dump(out, schema[i].subschema,
203                                         indent + 1);
204         }
205 }
206
207 void
208 ec_config_schema_dump(FILE *out, const struct ec_config_schema *schema)
209 {
210         fprintf(out, "------------------- schema dump:\n");
211
212         if (schema == NULL) {
213                 fprintf(out, "no schema\n");
214                 return;
215         }
216
217         __ec_config_schema_dump(out, schema, 0);
218 }
219
220 enum ec_config_type ec_config_get_type(const struct ec_config *config)
221 {
222         return config->type;
223 }
224
225 struct ec_config *
226 ec_config_bool(bool boolean)
227 {
228         struct ec_config *value = NULL;
229
230         value = ec_calloc(1, sizeof(*value));
231         if (value == NULL)
232                 return NULL;
233
234         value->type = EC_CONFIG_TYPE_BOOL;
235         value->boolean = boolean;
236
237         return value;
238 }
239
240 struct ec_config *
241 ec_config_i64(int64_t i64)
242 {
243         struct ec_config *value = NULL;
244
245         value = ec_calloc(1, sizeof(*value));
246         if (value == NULL)
247                 return NULL;
248
249         value->type = EC_CONFIG_TYPE_INT64;
250         value->i64 = i64;
251
252         return value;
253 }
254
255 struct ec_config *
256 ec_config_u64(uint64_t u64)
257 {
258         struct ec_config *value = NULL;
259
260         value = ec_calloc(1, sizeof(*value));
261         if (value == NULL)
262                 return NULL;
263
264         value->type = EC_CONFIG_TYPE_UINT64;
265         value->u64 = u64;
266
267         return value;
268 }
269
270 /* duplicate string */
271 struct ec_config *
272 ec_config_string(const char *string)
273 {
274         struct ec_config *value = NULL;
275         char *s = NULL;
276
277         if (string == NULL)
278                 goto fail;
279
280         s = ec_strdup(string);
281         if (s == NULL)
282                 goto fail;
283
284         value = ec_calloc(1, sizeof(*value));
285         if (value == NULL)
286                 goto fail;
287
288         value->type = EC_CONFIG_TYPE_STRING;
289         value->string = s;
290
291         return value;
292
293 fail:
294         ec_free(value);
295         ec_free(s);
296         return NULL;
297 }
298
299 /* "consume" the node */
300 struct ec_config *
301 ec_config_node(struct ec_node *node)
302 {
303         struct ec_config *value = NULL;
304
305         if (node == NULL)
306                 goto fail;
307
308         value = ec_calloc(1, sizeof(*value));
309         if (value == NULL)
310                 goto fail;
311
312         value->type = EC_CONFIG_TYPE_NODE;
313         value->node = node;
314
315         return value;
316
317 fail:
318         ec_node_free(node);
319         ec_free(value);
320         return NULL;
321 }
322
323 struct ec_config *
324 ec_config_dict(void)
325 {
326         struct ec_config *value = NULL;
327         struct ec_keyval *dict = NULL;
328
329         dict = ec_keyval();
330         if (dict == NULL)
331                 goto fail;
332
333         value = ec_calloc(1, sizeof(*value));
334         if (value == NULL)
335                 goto fail;
336
337         value->type = EC_CONFIG_TYPE_DICT;
338         value->dict = dict;
339
340         return value;
341
342 fail:
343         ec_keyval_free(dict);
344         ec_free(value);
345         return NULL;
346 }
347
348 struct ec_config *
349 ec_config_list(void)
350 {
351         struct ec_config *value = NULL;
352
353         value = ec_calloc(1, sizeof(*value));
354         if (value == NULL)
355                 return NULL;
356
357         value->type = EC_CONFIG_TYPE_LIST;
358         TAILQ_INIT(&value->list);
359
360         return value;
361 }
362
363 ssize_t ec_config_count(const struct ec_config *config)
364 {
365         const struct ec_config *child;
366         ssize_t n;
367
368         switch (config->type) {
369         case EC_CONFIG_TYPE_LIST:
370                 n = 0;
371                 TAILQ_FOREACH(child, &config->list, next)
372                         n++;
373                 return n;
374         case EC_CONFIG_TYPE_DICT:
375                 // XXX todo
376         default:
377                 errno = EINVAL;
378                 return -1;
379         }
380 }
381
382 const struct ec_config_schema *
383 ec_config_schema_lookup(const struct ec_config_schema *schema,
384                         const char *key)
385 {
386         size_t i;
387
388         for (i = 0; schema[i].type != EC_CONFIG_TYPE_NONE; i++) {
389                 if (!strcmp(key, schema[i].key))
390                         return &schema[i];
391         }
392
393         errno = ENOENT;
394         return NULL;
395 }
396
397 enum ec_config_type
398 ec_config_schema_type(const struct ec_config_schema *schema_elt)
399 {
400         return schema_elt->type;
401 }
402
403 const struct ec_config_schema *
404 ec_config_schema_sub(const struct ec_config_schema *schema_elt)
405 {
406         return schema_elt->subschema;
407 }
408
409 void
410 ec_config_free(struct ec_config *value)
411 {
412         if (value == NULL)
413                 return;
414
415         switch (value->type) {
416         case EC_CONFIG_TYPE_STRING:
417                 ec_free(value->string);
418                 break;
419         case EC_CONFIG_TYPE_NODE:
420                 ec_node_free(value->node);
421                 break;
422         case EC_CONFIG_TYPE_LIST:
423                 while (!TAILQ_EMPTY(&value->list)) {
424                         struct ec_config *v;
425                         v = TAILQ_FIRST(&value->list);
426                         TAILQ_REMOVE(&value->list, v, next);
427                         ec_config_free(v);
428                 }
429                 break;
430         case EC_CONFIG_TYPE_DICT:
431                 ec_keyval_free(value->dict);
432                 break;
433         default:
434                 break;
435         }
436
437         ec_free(value);
438 }
439
440 static int
441 ec_config_list_cmp(const struct ec_config_list *list1,
442                 const struct ec_config_list *list2)
443 {
444         const struct ec_config *v1, *v2;
445
446         for (v1 = TAILQ_FIRST(list1), v2 = TAILQ_FIRST(list2);
447              v1 != NULL && v2 != NULL;
448              v1 = TAILQ_NEXT(v1, next), v2 = TAILQ_NEXT(v2, next)) {
449                 if (ec_config_cmp(v1, v2))
450                         return -1;
451         }
452         if (v1 != NULL || v2 != NULL)
453                 return -1;
454
455         return 0;
456 }
457
458 /* XXX -> ec_keyval_cmp() */
459 static int
460 ec_config_dict_cmp(const struct ec_keyval *d1,
461                 const struct ec_keyval *d2)
462 {
463         const struct ec_config *v1, *v2;
464         struct ec_keyval_elt_ref *iter = NULL;
465         const char *key;
466
467         if (ec_keyval_len(d1) != ec_keyval_len(d2))
468                 return -1;
469
470         for (iter = ec_keyval_iter(d1);
471              iter != NULL;
472              iter = ec_keyval_iter_next(iter)) {
473                 key = ec_keyval_iter_get_key(iter);
474                 v1 = ec_keyval_iter_get_val(iter);
475                 v2 = ec_keyval_get(d2, key);
476
477                 if (ec_config_cmp(v1, v2))
478                         goto fail;
479         }
480
481         return 0;
482
483 fail:
484         return -1;
485 }
486
487 int
488 ec_config_cmp(const struct ec_config *value1,
489                 const struct ec_config *value2)
490 {
491         if (value1 == NULL || value2 == NULL) {
492                 errno = EINVAL;
493                 return -1;
494         }
495
496         if (value1->type != value2->type)
497                 return -1;
498
499         switch (value1->type) {
500         case EC_CONFIG_TYPE_BOOL:
501                 if (value1->boolean == value2->boolean)
502                         return 0;
503         case EC_CONFIG_TYPE_INT64:
504                 if (value1->i64 == value2->i64)
505                         return 0;
506         case EC_CONFIG_TYPE_UINT64:
507                 if (value1->u64 == value2->u64)
508                         return 0;
509         case EC_CONFIG_TYPE_STRING:
510                 if (!strcmp(value1->string, value2->string))
511                         return 0;
512         case EC_CONFIG_TYPE_NODE:
513                 if (value1->node == value2->node)
514                         return 0;
515         case EC_CONFIG_TYPE_LIST:
516                 return ec_config_list_cmp(&value1->list, &value2->list);
517         case EC_CONFIG_TYPE_DICT:
518                 return ec_config_dict_cmp(value1->dict, value2->dict);
519         default:
520                 break;
521         }
522
523         return -1;
524 }
525
526 static int
527 ec_config_list_validate(const struct ec_config_list *list,
528                         const struct ec_config_schema *sch)
529 {
530         const struct ec_config *value;
531
532         TAILQ_FOREACH(value, list, next) {
533                 if (value->type != sch->type) {
534                         errno = EBADMSG;
535                         return -1;
536                 }
537
538                 if (value->type == EC_CONFIG_TYPE_LIST) {
539                         if (ec_config_list_validate(&value->list,
540                                                         sch->subschema) < 0)
541                                 return -1;
542                 } else if (value->type == EC_CONFIG_TYPE_DICT) {
543                         if (ec_config_dict_validate(value->dict,
544                                         sch->subschema) < 0)
545                                 return -1;
546                 }
547         }
548
549         return 0;
550 }
551
552 static int
553 ec_config_dict_validate(const struct ec_keyval *dict,
554                         const struct ec_config_schema *schema)
555 {
556         const struct ec_config *value;
557         struct ec_keyval_elt_ref *iter = NULL;
558         const struct ec_config_schema *sch;
559         const char *key;
560
561         for (iter = ec_keyval_iter(dict);
562              iter != NULL;
563              iter = ec_keyval_iter_next(iter)) {
564
565                 key = ec_keyval_iter_get_key(iter);
566                 value = ec_keyval_iter_get_val(iter);
567                 sch = ec_config_schema_lookup(schema, key);
568                 if (sch == NULL || sch->type != value->type) {
569                         errno = EBADMSG;
570                         goto fail;
571                 }
572                 if (value->type == EC_CONFIG_TYPE_LIST) {
573                         if (ec_config_list_validate(&value->list,
574                                                         sch->subschema) < 0)
575                                 goto fail;
576                 } else if (value->type == EC_CONFIG_TYPE_DICT) {
577                         if (ec_config_dict_validate(value->dict,
578                                         sch->subschema) < 0)
579                                 goto fail;
580                 }
581         }
582
583         return 0;
584
585 fail:
586         return -1;
587 }
588
589 int
590 ec_config_validate(const struct ec_config *dict,
591                 const struct ec_config_schema *schema)
592 {
593         if (dict->type != EC_CONFIG_TYPE_DICT || schema == NULL) {
594                 errno = EINVAL;
595                 goto fail;
596         }
597
598         if (ec_config_dict_validate(dict->dict, schema) < 0)
599                 goto fail;
600
601         return 0
602 ;
603 fail:
604         return -1;
605 }
606
607 struct ec_config *
608 ec_config_dict_get(const struct ec_config *config, const char *key)
609 {
610         if (config == NULL) {
611                 errno = EINVAL;
612                 return NULL;
613         }
614
615         if (config->type != EC_CONFIG_TYPE_DICT) {
616                 errno = EINVAL;
617                 return NULL;
618         }
619
620         return ec_keyval_get(config->dict, key);
621 }
622
623 struct ec_config *
624 ec_config_list_first(struct ec_config *list)
625 {
626         if (list  == NULL || list->type != EC_CONFIG_TYPE_LIST) {
627                 errno = EINVAL;
628                 return NULL;
629         }
630
631         return TAILQ_FIRST(&list->list);
632 }
633
634 struct ec_config *
635 ec_config_list_next(struct ec_config *list, struct ec_config *config)
636 {
637         (void)list;
638         return TAILQ_NEXT(config, next);
639 }
640
641 /* value is consumed */
642 int ec_config_dict_set(struct ec_config *config, const char *key,
643                 struct ec_config *value)
644 {
645         void (*free_cb)(struct ec_config *) = ec_config_free;
646
647         if (config == NULL || key == NULL || value == NULL) {
648                 errno = EINVAL;
649                 goto fail;
650         }
651         if (config->type != EC_CONFIG_TYPE_DICT) {
652                 errno = EINVAL;
653                 goto fail;
654         }
655
656         return ec_keyval_set(config->dict, key, value,
657                         (void (*)(void *))free_cb);
658
659 fail:
660         ec_config_free(value);
661         return -1;
662 }
663
664 int ec_config_dict_del(struct ec_config *config, const char *key)
665 {
666         if (config == NULL || key == NULL) {
667                 errno = EINVAL;
668                 return -1;
669         }
670         if (config->type != EC_CONFIG_TYPE_DICT) {
671                 errno = EINVAL;
672                 return -1;
673         }
674
675         return ec_keyval_del(config->dict, key);
676 }
677
678 /* value is consumed */
679 int
680 ec_config_list_add(struct ec_config *list,
681                 struct ec_config *value)
682 {
683         if (list == NULL || list->type != EC_CONFIG_TYPE_LIST || value == NULL) {
684                 errno = EINVAL;
685                 goto fail;
686         }
687
688         TAILQ_INSERT_TAIL(&list->list, value, next);
689
690         return 0;
691
692 fail:
693         ec_config_free(value);
694         return -1;
695 }
696
697 int ec_config_list_del(struct ec_config *list, struct ec_config *config)
698 {
699         if (list == NULL || list->type != EC_CONFIG_TYPE_LIST) {
700                 errno = EINVAL;
701                 return -1;
702         }
703
704         TAILQ_REMOVE(&list->list, config, next);
705         ec_config_free(config);
706         return 0;
707 }
708
709 static struct ec_config *
710 ec_config_list_dup(const struct ec_config_list *list)
711 {
712         struct ec_config *dup = NULL, *v, *value;
713
714         dup = ec_config_list();
715         if (dup == NULL)
716                 goto fail;
717
718         TAILQ_FOREACH(v, list, next) {
719                 value = ec_config_dup(v);
720                 if (value == NULL)
721                         goto fail;
722                 if (ec_config_list_add(dup, value) < 0)
723                         goto fail;
724         }
725
726         return dup;
727
728 fail:
729         ec_config_free(dup);
730         return NULL;
731 }
732
733 static struct ec_config *
734 ec_config_dict_dup(const struct ec_keyval *dict)
735 {
736         struct ec_config *dup = NULL, *value;
737         struct ec_keyval_elt_ref *iter = NULL;
738         const char *key;
739
740         dup = ec_config_dict();
741         if (dup == NULL)
742                 goto fail;
743
744         for (iter = ec_keyval_iter(dict);
745              iter != NULL;
746              iter = ec_keyval_iter_next(iter)) {
747                 key = ec_keyval_iter_get_key(iter);
748                 value = ec_config_dup(ec_keyval_iter_get_val(iter));
749                 if (value == NULL)
750                         goto fail;
751                 if (ec_config_dict_set(dup, key, value) < 0)
752                         goto fail;
753         }
754
755         return dup;
756
757 fail:
758         ec_config_free(dup);
759         return NULL;
760 }
761
762 struct ec_config *
763 ec_config_dup(const struct ec_config *config)
764 {
765         if (config == NULL) {
766                 errno = EINVAL;
767                 return NULL;
768         }
769
770         switch (config->type) {
771         case EC_CONFIG_TYPE_BOOL:
772                 return ec_config_bool(config->boolean);
773         case EC_CONFIG_TYPE_INT64:
774                 return ec_config_i64(config->i64);
775         case EC_CONFIG_TYPE_UINT64:
776                 return ec_config_u64(config->u64);
777         case EC_CONFIG_TYPE_STRING:
778                 return ec_config_string(config->string);
779         case EC_CONFIG_TYPE_NODE:
780                 return ec_config_node(ec_node_clone(config->node));
781         case EC_CONFIG_TYPE_LIST:
782                 return ec_config_list_dup(&config->list);
783         case EC_CONFIG_TYPE_DICT:
784                 return ec_config_dict_dup(config->dict);
785         default:
786                 errno = EINVAL;
787                 break;
788         }
789
790         return NULL;
791 }
792
793 static int
794 ec_config_list_dump(FILE *out, const char *key,
795                 const struct ec_config_list *list, size_t indent)
796 {
797         const struct ec_config *v;
798
799         fprintf(out, "%*s" "%s%s%stype=list\n", (int)indent * 4, "",
800                 key ? "key=": "",
801                 key ? key: "",
802                 key ? " ": "");
803
804         TAILQ_FOREACH(v, list, next) {
805                 if (__ec_config_dump(out, NULL, v, indent + 1) < 0)
806                         return -1;
807         }
808
809         return 0;
810 }
811
812 static int
813 ec_config_dict_dump(FILE *out, const char *key, const struct ec_keyval *dict,
814                 size_t indent)
815 {
816         const struct ec_config *value;
817         struct ec_keyval_elt_ref *iter;
818         const char *k;
819
820         fprintf(out, "%*s" "%s%s%stype=dict\n", (int)indent * 4, "",
821                 key ? "key=": "",
822                 key ? key: "",
823                 key ? " ": "");
824
825         for (iter = ec_keyval_iter(dict);
826              iter != NULL;
827              iter = ec_keyval_iter_next(iter)) {
828                 k = ec_keyval_iter_get_key(iter);
829                 value = ec_keyval_iter_get_val(iter);
830                 if (__ec_config_dump(out, k, value, indent + 1) < 0)
831                         goto fail;
832         }
833         return 0;
834
835 fail:
836         return -1;
837 }
838
839 static int
840 __ec_config_dump(FILE *out, const char *key, const struct ec_config *value,
841                 size_t indent)
842 {
843         char *val_str = NULL;
844
845         switch (value->type) {
846         case EC_CONFIG_TYPE_BOOL:
847                 if (value->boolean)
848                         ec_asprintf(&val_str, "true");
849                 else
850                         ec_asprintf(&val_str, "false");
851                 break;
852         case EC_CONFIG_TYPE_INT64:
853                 ec_asprintf(&val_str, "%"PRIu64, value->u64);
854                 break;
855         case EC_CONFIG_TYPE_UINT64:
856                 ec_asprintf(&val_str, "%"PRIi64, value->i64);
857                 break;
858         case EC_CONFIG_TYPE_STRING:
859                 ec_asprintf(&val_str, "%s", value->string);
860                 break;
861         case EC_CONFIG_TYPE_NODE:
862                 ec_asprintf(&val_str, "%p", value->node);
863                 break;
864         case EC_CONFIG_TYPE_LIST:
865                 return ec_config_list_dump(out, key, &value->list, indent);
866         case EC_CONFIG_TYPE_DICT:
867                 return ec_config_dict_dump(out, key, value->dict, indent);
868         default:
869                 errno = EINVAL;
870                 break;
871         }
872
873         /* errno is already set on error */
874         if (val_str == NULL)
875                 goto fail;
876
877         fprintf(out, "%*s" "%s%s%stype=%s val=%s\n", (int)indent * 4, "",
878                 key ? "key=": "",
879                 key ? key: "",
880                 key ? " ": "",
881                 ec_config_type_str(value->type), val_str);
882
883         ec_free(val_str);
884         return 0;
885
886 fail:
887         ec_free(val_str);
888         return -1;
889 }
890
891 void
892 ec_config_dump(FILE *out, const struct ec_config *config)
893 {
894         fprintf(out, "------------------- config dump:\n");
895
896         if (config == NULL) {
897                 fprintf(out, "no config\n");
898                 return;
899         }
900
901         if (__ec_config_dump(out, NULL, config, 0) < 0)
902                 fprintf(out, "error while dumping\n");
903 }
904
905 /* LCOV_EXCL_START */
906 static const struct ec_config_schema sch_intlist_elt[] = {
907         {
908                 .desc = "This is a description for int",
909                 .type = EC_CONFIG_TYPE_INT64,
910         },
911         {
912                 .type = EC_CONFIG_TYPE_NONE,
913         },
914 };
915
916 static const struct ec_config_schema sch_dict[] = {
917         {
918                 .key = "my_int",
919                 .desc = "This is a description for int",
920                 .type = EC_CONFIG_TYPE_INT64,
921         },
922         {
923                 .key = "my_int2",
924                 .desc = "This is a description for int2",
925                 .type = EC_CONFIG_TYPE_INT64,
926         },
927         {
928                 .type = EC_CONFIG_TYPE_NONE,
929         },
930 };
931
932 static const struct ec_config_schema sch_dictlist_elt[] = {
933         {
934                 .desc = "This is a description for dict",
935                 .type = EC_CONFIG_TYPE_DICT,
936                 .subschema = sch_dict,
937         },
938         {
939                 .type = EC_CONFIG_TYPE_NONE,
940         },
941 };
942
943 static const struct ec_config_schema sch_baseconfig[] = {
944         {
945                 .key = "my_bool",
946                 .desc = "This is a description for bool",
947                 .type = EC_CONFIG_TYPE_BOOL,
948         },
949         {
950                 .key = "my_int",
951                 .desc = "This is a description for int",
952                 .type = EC_CONFIG_TYPE_INT64,
953         },
954         {
955                 .key = "my_string",
956                 .desc = "This is a description for string",
957                 .type = EC_CONFIG_TYPE_STRING,
958         },
959         {
960                 .key = "my_node",
961                 .desc = "This is a description for node",
962                 .type = EC_CONFIG_TYPE_NODE,
963         },
964         {
965                 .key = "my_intlist",
966                 .desc = "This is a description for list",
967                 .type = EC_CONFIG_TYPE_LIST,
968                 .subschema = sch_intlist_elt,
969         },
970         {
971                 .key = "my_dictlist",
972                 .desc = "This is a description for list",
973                 .type = EC_CONFIG_TYPE_LIST,
974                 .subschema = sch_dictlist_elt,
975         },
976         {
977                 .type = EC_CONFIG_TYPE_NONE,
978         },
979 };
980
981 static int ec_config_testcase(void)
982 {
983         struct ec_node *node = NULL;
984         struct ec_keyval *dict = NULL;
985         const struct ec_config *value = NULL;
986         struct ec_config *config = NULL, *config2 = NULL;
987         struct ec_config *list = NULL, *subconfig = NULL;
988         struct ec_config *list_, *config_;
989         int testres = 0;
990         int ret;
991
992         testres |= EC_TEST_CHECK(ec_config_key_is_reserved("id"),
993                 "'id' should be reserved");
994         testres |= EC_TEST_CHECK(!ec_config_key_is_reserved("foo"),
995                 "'foo' should not be reserved");
996
997         node = ec_node("empty", EC_NO_ID);
998         if (node == NULL)
999                 goto fail;
1000
1001         if (ec_config_schema_validate(sch_baseconfig) < 0) {
1002                 EC_LOG(EC_LOG_ERR, "invalid config schema\n");
1003                 goto fail;
1004         }
1005
1006         ec_config_schema_dump(stdout, sch_baseconfig);
1007
1008         config = ec_config_dict();
1009         if (config == NULL)
1010                 goto fail;
1011
1012         ret = ec_config_dict_set(config, "my_bool", ec_config_bool(true));
1013         testres |= EC_TEST_CHECK(ret == 0, "cannot set boolean");
1014         value = ec_config_dict_get(config, "my_bool");
1015         testres |= EC_TEST_CHECK(
1016                 value != NULL &&
1017                 value->type == EC_CONFIG_TYPE_BOOL &&
1018                 value->boolean == true,
1019                 "unexpected boolean value");
1020
1021         ret = ec_config_dict_set(config, "my_int", ec_config_i64(1234));
1022         testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
1023         value = ec_config_dict_get(config, "my_int");
1024         testres |= EC_TEST_CHECK(
1025                 value != NULL &&
1026                 value->type == EC_CONFIG_TYPE_INT64 &&
1027                 value->i64 == 1234,
1028                 "unexpected int value");
1029
1030         testres |= EC_TEST_CHECK(
1031                 ec_config_validate(config, sch_baseconfig) == 0,
1032                 "cannot validate config\n");
1033
1034         ret = ec_config_dict_set(config, "my_string", ec_config_string("toto"));
1035         testres |= EC_TEST_CHECK(ret == 0, "cannot set string");
1036         value = ec_config_dict_get(config, "my_string");
1037         testres |= EC_TEST_CHECK(
1038                 value != NULL &&
1039                 value->type == EC_CONFIG_TYPE_STRING &&
1040                 !strcmp(value->string, "toto"),
1041                 "unexpected string value");
1042
1043         list = ec_config_list();
1044         if (list == NULL)
1045                 goto fail;
1046
1047         subconfig = ec_config_dict();
1048         if (subconfig == NULL)
1049                 goto fail;
1050
1051         ret = ec_config_dict_set(subconfig, "my_int", ec_config_i64(1));
1052         testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
1053         value = ec_config_dict_get(subconfig, "my_int");
1054         testres |= EC_TEST_CHECK(
1055                 value != NULL &&
1056                 value->type == EC_CONFIG_TYPE_INT64 &&
1057                 value->i64 == 1,
1058                 "unexpected int value");
1059
1060         ret = ec_config_dict_set(subconfig, "my_int2", ec_config_i64(2));
1061         testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
1062         value = ec_config_dict_get(subconfig, "my_int2");
1063         testres |= EC_TEST_CHECK(
1064                 value != NULL &&
1065                 value->type == EC_CONFIG_TYPE_INT64 &&
1066                 value->i64 == 2,
1067                 "unexpected int value");
1068
1069         testres |= EC_TEST_CHECK(
1070                 ec_config_validate(subconfig, sch_dict) == 0,
1071                 "cannot validate subconfig\n");
1072
1073         ret = ec_config_list_add(list, subconfig);
1074         subconfig = NULL; /* freed */
1075         testres |= EC_TEST_CHECK(ret == 0, "cannot add in list");
1076
1077         subconfig = ec_config_dict();
1078         if (subconfig == NULL)
1079                 goto fail;
1080
1081         ret = ec_config_dict_set(subconfig, "my_int", ec_config_i64(3));
1082         testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
1083         value = ec_config_dict_get(subconfig, "my_int");
1084         testres |= EC_TEST_CHECK(
1085                 value != NULL &&
1086                 value->type == EC_CONFIG_TYPE_INT64 &&
1087                 value->i64 == 3,
1088                 "unexpected int value");
1089
1090         ret = ec_config_dict_set(subconfig, "my_int2", ec_config_i64(4));
1091         testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
1092         value = ec_config_dict_get(subconfig, "my_int2");
1093         testres |= EC_TEST_CHECK(
1094                 value != NULL &&
1095                 value->type == EC_CONFIG_TYPE_INT64 &&
1096                 value->i64 == 4,
1097                 "unexpected int value");
1098
1099         testres |= EC_TEST_CHECK(
1100                 ec_config_validate(subconfig, sch_dict) == 0,
1101                 "cannot validate subconfig\n");
1102
1103         ret = ec_config_list_add(list, subconfig);
1104         subconfig = NULL; /* freed */
1105         testres |= EC_TEST_CHECK(ret == 0, "cannot add in list");
1106
1107         ret = ec_config_dict_set(config, "my_dictlist", list);
1108         list = NULL;
1109         testres |= EC_TEST_CHECK(ret == 0, "cannot set list");
1110
1111         testres |= EC_TEST_CHECK(
1112                 ec_config_validate(config, sch_baseconfig) == 0,
1113                 "cannot validate config\n");
1114
1115         list_ = ec_config_dict_get(config, "my_dictlist");
1116         for (config_ = ec_config_list_first(list_); config_ != NULL;
1117              config_ = ec_config_list_next(list_, config_)) {
1118                 ec_config_dump(stdout, config_);
1119         }
1120
1121         ec_config_dump(stdout, config);
1122
1123         config2 = ec_config_dup(config);
1124         testres |= EC_TEST_CHECK(config2 != NULL, "cannot duplicate config");
1125         testres |= EC_TEST_CHECK(
1126                 ec_config_cmp(config, config2) == 0,
1127                 "fail to compare config");
1128         ec_config_free(config2);
1129         config2 = NULL;
1130
1131         /* remove the first element */
1132         ec_config_list_del(list_, ec_config_list_first(list_));
1133         testres |= EC_TEST_CHECK(
1134                 ec_config_validate(config, sch_baseconfig) == 0,
1135                 "cannot validate config\n");
1136
1137         ec_config_dump(stdout, config);
1138
1139         ec_config_free(list);
1140         ec_config_free(subconfig);
1141         ec_config_free(config);
1142         ec_keyval_free(dict);
1143         ec_node_free(node);
1144
1145         return testres;
1146
1147 fail:
1148         ec_config_free(list);
1149         ec_config_free(subconfig);
1150         ec_config_free(config);
1151         ec_config_free(config2);
1152         ec_keyval_free(dict);
1153         ec_node_free(node);
1154
1155         return -1;
1156 }
1157 /* LCOV_EXCL_STOP */
1158
1159 static struct ec_test ec_config_test = {
1160         .name = "config",
1161         .test = ec_config_testcase,
1162 };
1163
1164 EC_TEST_REGISTER(ec_config_test);