api documentation for ec_parse
[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_dict.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_dict *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_dict *dict = NULL;
328
329         dict = ec_dict();
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_dict_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_dict_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_dict_cmp() */
459 static int
460 ec_config_dict_cmp(const struct ec_dict *d1,
461                 const struct ec_dict *d2)
462 {
463         const struct ec_config *v1, *v2;
464         struct ec_dict_elt_ref *iter = NULL;
465         const char *key;
466
467         if (ec_dict_len(d1) != ec_dict_len(d2))
468                 return -1;
469
470         for (iter = ec_dict_iter(d1);
471              iter != NULL;
472              iter = ec_dict_iter_next(iter)) {
473                 key = ec_dict_iter_get_key(iter);
474                 v1 = ec_dict_iter_get_val(iter);
475                 v2 = ec_dict_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                 break;
504         case EC_CONFIG_TYPE_INT64:
505                 if (value1->i64 == value2->i64)
506                         return 0;
507                 break;
508         case EC_CONFIG_TYPE_UINT64:
509                 if (value1->u64 == value2->u64)
510                         return 0;
511                 break;
512         case EC_CONFIG_TYPE_STRING:
513                 if (!strcmp(value1->string, value2->string))
514                         return 0;
515                 break;
516         case EC_CONFIG_TYPE_NODE:
517                 if (value1->node == value2->node)
518                         return 0;
519                 break;
520         case EC_CONFIG_TYPE_LIST:
521                 return ec_config_list_cmp(&value1->list, &value2->list);
522         case EC_CONFIG_TYPE_DICT:
523                 return ec_config_dict_cmp(value1->dict, value2->dict);
524         default:
525                 break;
526         }
527
528         return -1;
529 }
530
531 static int
532 ec_config_list_validate(const struct ec_config_list *list,
533                         const struct ec_config_schema *sch)
534 {
535         const struct ec_config *value;
536
537         TAILQ_FOREACH(value, list, next) {
538                 if (value->type != sch->type) {
539                         errno = EBADMSG;
540                         return -1;
541                 }
542
543                 if (value->type == EC_CONFIG_TYPE_LIST) {
544                         if (ec_config_list_validate(&value->list,
545                                                         sch->subschema) < 0)
546                                 return -1;
547                 } else if (value->type == EC_CONFIG_TYPE_DICT) {
548                         if (ec_config_dict_validate(value->dict,
549                                         sch->subschema) < 0)
550                                 return -1;
551                 }
552         }
553
554         return 0;
555 }
556
557 static int
558 ec_config_dict_validate(const struct ec_dict *dict,
559                         const struct ec_config_schema *schema)
560 {
561         const struct ec_config *value;
562         struct ec_dict_elt_ref *iter = NULL;
563         const struct ec_config_schema *sch;
564         const char *key;
565
566         for (iter = ec_dict_iter(dict);
567              iter != NULL;
568              iter = ec_dict_iter_next(iter)) {
569
570                 key = ec_dict_iter_get_key(iter);
571                 value = ec_dict_iter_get_val(iter);
572                 sch = ec_config_schema_lookup(schema, key);
573                 if (sch == NULL || sch->type != value->type) {
574                         errno = EBADMSG;
575                         goto fail;
576                 }
577                 if (value->type == EC_CONFIG_TYPE_LIST) {
578                         if (ec_config_list_validate(&value->list,
579                                                         sch->subschema) < 0)
580                                 goto fail;
581                 } else if (value->type == EC_CONFIG_TYPE_DICT) {
582                         if (ec_config_dict_validate(value->dict,
583                                         sch->subschema) < 0)
584                                 goto fail;
585                 }
586         }
587
588         return 0;
589
590 fail:
591         return -1;
592 }
593
594 int
595 ec_config_validate(const struct ec_config *dict,
596                 const struct ec_config_schema *schema)
597 {
598         if (dict->type != EC_CONFIG_TYPE_DICT || schema == NULL) {
599                 errno = EINVAL;
600                 goto fail;
601         }
602
603         if (ec_config_dict_validate(dict->dict, schema) < 0)
604                 goto fail;
605
606         return 0
607 ;
608 fail:
609         return -1;
610 }
611
612 struct ec_config *
613 ec_config_dict_get(const struct ec_config *config, const char *key)
614 {
615         if (config == NULL) {
616                 errno = EINVAL;
617                 return NULL;
618         }
619
620         if (config->type != EC_CONFIG_TYPE_DICT) {
621                 errno = EINVAL;
622                 return NULL;
623         }
624
625         return ec_dict_get(config->dict, key);
626 }
627
628 struct ec_config *
629 ec_config_list_first(struct ec_config *list)
630 {
631         if (list  == NULL || list->type != EC_CONFIG_TYPE_LIST) {
632                 errno = EINVAL;
633                 return NULL;
634         }
635
636         return TAILQ_FIRST(&list->list);
637 }
638
639 struct ec_config *
640 ec_config_list_next(struct ec_config *list, struct ec_config *config)
641 {
642         (void)list;
643         return TAILQ_NEXT(config, next);
644 }
645
646 /* value is consumed */
647 int ec_config_dict_set(struct ec_config *config, const char *key,
648                 struct ec_config *value)
649 {
650         void (*free_cb)(struct ec_config *) = ec_config_free;
651
652         if (config == NULL || key == NULL || value == NULL) {
653                 errno = EINVAL;
654                 goto fail;
655         }
656         if (config->type != EC_CONFIG_TYPE_DICT) {
657                 errno = EINVAL;
658                 goto fail;
659         }
660
661         return ec_dict_set(config->dict, key, value,
662                         (void (*)(void *))free_cb);
663
664 fail:
665         ec_config_free(value);
666         return -1;
667 }
668
669 int ec_config_dict_del(struct ec_config *config, const char *key)
670 {
671         if (config == NULL || key == NULL) {
672                 errno = EINVAL;
673                 return -1;
674         }
675         if (config->type != EC_CONFIG_TYPE_DICT) {
676                 errno = EINVAL;
677                 return -1;
678         }
679
680         return ec_dict_del(config->dict, key);
681 }
682
683 /* value is consumed */
684 int
685 ec_config_list_add(struct ec_config *list,
686                 struct ec_config *value)
687 {
688         if (list == NULL || list->type != EC_CONFIG_TYPE_LIST || value == NULL) {
689                 errno = EINVAL;
690                 goto fail;
691         }
692
693         TAILQ_INSERT_TAIL(&list->list, value, next);
694
695         return 0;
696
697 fail:
698         ec_config_free(value);
699         return -1;
700 }
701
702 int ec_config_list_del(struct ec_config *list, struct ec_config *config)
703 {
704         if (list == NULL || list->type != EC_CONFIG_TYPE_LIST) {
705                 errno = EINVAL;
706                 return -1;
707         }
708
709         TAILQ_REMOVE(&list->list, config, next);
710         ec_config_free(config);
711         return 0;
712 }
713
714 static struct ec_config *
715 ec_config_list_dup(const struct ec_config_list *list)
716 {
717         struct ec_config *dup = NULL, *v, *value;
718
719         dup = ec_config_list();
720         if (dup == NULL)
721                 goto fail;
722
723         TAILQ_FOREACH(v, list, next) {
724                 value = ec_config_dup(v);
725                 if (value == NULL)
726                         goto fail;
727                 if (ec_config_list_add(dup, value) < 0)
728                         goto fail;
729         }
730
731         return dup;
732
733 fail:
734         ec_config_free(dup);
735         return NULL;
736 }
737
738 static struct ec_config *
739 ec_config_dict_dup(const struct ec_dict *dict)
740 {
741         struct ec_config *dup = NULL, *value;
742         struct ec_dict_elt_ref *iter = NULL;
743         const char *key;
744
745         dup = ec_config_dict();
746         if (dup == NULL)
747                 goto fail;
748
749         for (iter = ec_dict_iter(dict);
750              iter != NULL;
751              iter = ec_dict_iter_next(iter)) {
752                 key = ec_dict_iter_get_key(iter);
753                 value = ec_config_dup(ec_dict_iter_get_val(iter));
754                 if (value == NULL)
755                         goto fail;
756                 if (ec_config_dict_set(dup, key, value) < 0)
757                         goto fail;
758         }
759
760         return dup;
761
762 fail:
763         ec_config_free(dup);
764         return NULL;
765 }
766
767 struct ec_config *
768 ec_config_dup(const struct ec_config *config)
769 {
770         if (config == NULL) {
771                 errno = EINVAL;
772                 return NULL;
773         }
774
775         switch (config->type) {
776         case EC_CONFIG_TYPE_BOOL:
777                 return ec_config_bool(config->boolean);
778         case EC_CONFIG_TYPE_INT64:
779                 return ec_config_i64(config->i64);
780         case EC_CONFIG_TYPE_UINT64:
781                 return ec_config_u64(config->u64);
782         case EC_CONFIG_TYPE_STRING:
783                 return ec_config_string(config->string);
784         case EC_CONFIG_TYPE_NODE:
785                 return ec_config_node(ec_node_clone(config->node));
786         case EC_CONFIG_TYPE_LIST:
787                 return ec_config_list_dup(&config->list);
788         case EC_CONFIG_TYPE_DICT:
789                 return ec_config_dict_dup(config->dict);
790         default:
791                 errno = EINVAL;
792                 break;
793         }
794
795         return NULL;
796 }
797
798 static int
799 ec_config_list_dump(FILE *out, const char *key,
800                 const struct ec_config_list *list, size_t indent)
801 {
802         const struct ec_config *v;
803
804         fprintf(out, "%*s" "%s%s%stype=list\n", (int)indent * 4, "",
805                 key ? "key=": "",
806                 key ? key: "",
807                 key ? " ": "");
808
809         TAILQ_FOREACH(v, list, next) {
810                 if (__ec_config_dump(out, NULL, v, indent + 1) < 0)
811                         return -1;
812         }
813
814         return 0;
815 }
816
817 static int
818 ec_config_dict_dump(FILE *out, const char *key, const struct ec_dict *dict,
819                 size_t indent)
820 {
821         const struct ec_config *value;
822         struct ec_dict_elt_ref *iter;
823         const char *k;
824
825         fprintf(out, "%*s" "%s%s%stype=dict\n", (int)indent * 4, "",
826                 key ? "key=": "",
827                 key ? key: "",
828                 key ? " ": "");
829
830         for (iter = ec_dict_iter(dict);
831              iter != NULL;
832              iter = ec_dict_iter_next(iter)) {
833                 k = ec_dict_iter_get_key(iter);
834                 value = ec_dict_iter_get_val(iter);
835                 if (__ec_config_dump(out, k, value, indent + 1) < 0)
836                         goto fail;
837         }
838         return 0;
839
840 fail:
841         return -1;
842 }
843
844 static int
845 __ec_config_dump(FILE *out, const char *key, const struct ec_config *value,
846                 size_t indent)
847 {
848         char *val_str = NULL;
849
850         switch (value->type) {
851         case EC_CONFIG_TYPE_BOOL:
852                 if (value->boolean)
853                         ec_asprintf(&val_str, "true");
854                 else
855                         ec_asprintf(&val_str, "false");
856                 break;
857         case EC_CONFIG_TYPE_INT64:
858                 ec_asprintf(&val_str, "%"PRIu64, value->u64);
859                 break;
860         case EC_CONFIG_TYPE_UINT64:
861                 ec_asprintf(&val_str, "%"PRIi64, value->i64);
862                 break;
863         case EC_CONFIG_TYPE_STRING:
864                 ec_asprintf(&val_str, "%s", value->string);
865                 break;
866         case EC_CONFIG_TYPE_NODE:
867                 ec_asprintf(&val_str, "%p", value->node);
868                 break;
869         case EC_CONFIG_TYPE_LIST:
870                 return ec_config_list_dump(out, key, &value->list, indent);
871         case EC_CONFIG_TYPE_DICT:
872                 return ec_config_dict_dump(out, key, value->dict, indent);
873         default:
874                 errno = EINVAL;
875                 break;
876         }
877
878         /* errno is already set on error */
879         if (val_str == NULL)
880                 goto fail;
881
882         fprintf(out, "%*s" "%s%s%stype=%s val=%s\n", (int)indent * 4, "",
883                 key ? "key=": "",
884                 key ? key: "",
885                 key ? " ": "",
886                 ec_config_type_str(value->type), val_str);
887
888         ec_free(val_str);
889         return 0;
890
891 fail:
892         ec_free(val_str);
893         return -1;
894 }
895
896 void
897 ec_config_dump(FILE *out, const struct ec_config *config)
898 {
899         fprintf(out, "------------------- config dump:\n");
900
901         if (config == NULL) {
902                 fprintf(out, "no config\n");
903                 return;
904         }
905
906         if (__ec_config_dump(out, NULL, config, 0) < 0)
907                 fprintf(out, "error while dumping\n");
908 }
909
910 /* LCOV_EXCL_START */
911 static const struct ec_config_schema sch_intlist_elt[] = {
912         {
913                 .desc = "This is a description for int",
914                 .type = EC_CONFIG_TYPE_INT64,
915         },
916         {
917                 .type = EC_CONFIG_TYPE_NONE,
918         },
919 };
920
921 static const struct ec_config_schema sch_dict[] = {
922         {
923                 .key = "my_int",
924                 .desc = "This is a description for int",
925                 .type = EC_CONFIG_TYPE_INT64,
926         },
927         {
928                 .key = "my_int2",
929                 .desc = "This is a description for int2",
930                 .type = EC_CONFIG_TYPE_INT64,
931         },
932         {
933                 .type = EC_CONFIG_TYPE_NONE,
934         },
935 };
936
937 static const struct ec_config_schema sch_dictlist_elt[] = {
938         {
939                 .desc = "This is a description for dict",
940                 .type = EC_CONFIG_TYPE_DICT,
941                 .subschema = sch_dict,
942         },
943         {
944                 .type = EC_CONFIG_TYPE_NONE,
945         },
946 };
947
948 static const struct ec_config_schema sch_baseconfig[] = {
949         {
950                 .key = "my_bool",
951                 .desc = "This is a description for bool",
952                 .type = EC_CONFIG_TYPE_BOOL,
953         },
954         {
955                 .key = "my_int",
956                 .desc = "This is a description for int",
957                 .type = EC_CONFIG_TYPE_INT64,
958         },
959         {
960                 .key = "my_string",
961                 .desc = "This is a description for string",
962                 .type = EC_CONFIG_TYPE_STRING,
963         },
964         {
965                 .key = "my_node",
966                 .desc = "This is a description for node",
967                 .type = EC_CONFIG_TYPE_NODE,
968         },
969         {
970                 .key = "my_intlist",
971                 .desc = "This is a description for list",
972                 .type = EC_CONFIG_TYPE_LIST,
973                 .subschema = sch_intlist_elt,
974         },
975         {
976                 .key = "my_dictlist",
977                 .desc = "This is a description for list",
978                 .type = EC_CONFIG_TYPE_LIST,
979                 .subschema = sch_dictlist_elt,
980         },
981         {
982                 .type = EC_CONFIG_TYPE_NONE,
983         },
984 };
985
986 static int ec_config_testcase(void)
987 {
988         struct ec_node *node = NULL;
989         struct ec_dict *dict = NULL;
990         const struct ec_config *value = NULL;
991         struct ec_config *config = NULL, *config2 = NULL;
992         struct ec_config *list = NULL, *subconfig = NULL;
993         struct ec_config *list_, *config_;
994         int testres = 0;
995         int ret;
996
997         testres |= EC_TEST_CHECK(ec_config_key_is_reserved("id"),
998                 "'id' should be reserved");
999         testres |= EC_TEST_CHECK(!ec_config_key_is_reserved("foo"),
1000                 "'foo' should not be reserved");
1001
1002         node = ec_node("empty", EC_NO_ID);
1003         if (node == NULL)
1004                 goto fail;
1005
1006         if (ec_config_schema_validate(sch_baseconfig) < 0) {
1007                 EC_LOG(EC_LOG_ERR, "invalid config schema\n");
1008                 goto fail;
1009         }
1010
1011         ec_config_schema_dump(stdout, sch_baseconfig);
1012
1013         config = ec_config_dict();
1014         if (config == NULL)
1015                 goto fail;
1016
1017         ret = ec_config_dict_set(config, "my_bool", ec_config_bool(true));
1018         testres |= EC_TEST_CHECK(ret == 0, "cannot set boolean");
1019         value = ec_config_dict_get(config, "my_bool");
1020         testres |= EC_TEST_CHECK(
1021                 value != NULL &&
1022                 value->type == EC_CONFIG_TYPE_BOOL &&
1023                 value->boolean == true,
1024                 "unexpected boolean value");
1025
1026         ret = ec_config_dict_set(config, "my_int", ec_config_i64(1234));
1027         testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
1028         value = ec_config_dict_get(config, "my_int");
1029         testres |= EC_TEST_CHECK(
1030                 value != NULL &&
1031                 value->type == EC_CONFIG_TYPE_INT64 &&
1032                 value->i64 == 1234,
1033                 "unexpected int value");
1034
1035         testres |= EC_TEST_CHECK(
1036                 ec_config_validate(config, sch_baseconfig) == 0,
1037                 "cannot validate config\n");
1038
1039         ret = ec_config_dict_set(config, "my_string", ec_config_string("toto"));
1040         testres |= EC_TEST_CHECK(ret == 0, "cannot set string");
1041         value = ec_config_dict_get(config, "my_string");
1042         testres |= EC_TEST_CHECK(
1043                 value != NULL &&
1044                 value->type == EC_CONFIG_TYPE_STRING &&
1045                 !strcmp(value->string, "toto"),
1046                 "unexpected string value");
1047
1048         list = ec_config_list();
1049         if (list == NULL)
1050                 goto fail;
1051
1052         subconfig = ec_config_dict();
1053         if (subconfig == NULL)
1054                 goto fail;
1055
1056         ret = ec_config_dict_set(subconfig, "my_int", ec_config_i64(1));
1057         testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
1058         value = ec_config_dict_get(subconfig, "my_int");
1059         testres |= EC_TEST_CHECK(
1060                 value != NULL &&
1061                 value->type == EC_CONFIG_TYPE_INT64 &&
1062                 value->i64 == 1,
1063                 "unexpected int value");
1064
1065         ret = ec_config_dict_set(subconfig, "my_int2", ec_config_i64(2));
1066         testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
1067         value = ec_config_dict_get(subconfig, "my_int2");
1068         testres |= EC_TEST_CHECK(
1069                 value != NULL &&
1070                 value->type == EC_CONFIG_TYPE_INT64 &&
1071                 value->i64 == 2,
1072                 "unexpected int value");
1073
1074         testres |= EC_TEST_CHECK(
1075                 ec_config_validate(subconfig, sch_dict) == 0,
1076                 "cannot validate subconfig\n");
1077
1078         ret = ec_config_list_add(list, subconfig);
1079         subconfig = NULL; /* freed */
1080         testres |= EC_TEST_CHECK(ret == 0, "cannot add in list");
1081
1082         subconfig = ec_config_dict();
1083         if (subconfig == NULL)
1084                 goto fail;
1085
1086         ret = ec_config_dict_set(subconfig, "my_int", ec_config_i64(3));
1087         testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
1088         value = ec_config_dict_get(subconfig, "my_int");
1089         testres |= EC_TEST_CHECK(
1090                 value != NULL &&
1091                 value->type == EC_CONFIG_TYPE_INT64 &&
1092                 value->i64 == 3,
1093                 "unexpected int value");
1094
1095         ret = ec_config_dict_set(subconfig, "my_int2", ec_config_i64(4));
1096         testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
1097         value = ec_config_dict_get(subconfig, "my_int2");
1098         testres |= EC_TEST_CHECK(
1099                 value != NULL &&
1100                 value->type == EC_CONFIG_TYPE_INT64 &&
1101                 value->i64 == 4,
1102                 "unexpected int value");
1103
1104         testres |= EC_TEST_CHECK(
1105                 ec_config_validate(subconfig, sch_dict) == 0,
1106                 "cannot validate subconfig\n");
1107
1108         ret = ec_config_list_add(list, subconfig);
1109         subconfig = NULL; /* freed */
1110         testres |= EC_TEST_CHECK(ret == 0, "cannot add in list");
1111
1112         ret = ec_config_dict_set(config, "my_dictlist", list);
1113         list = NULL;
1114         testres |= EC_TEST_CHECK(ret == 0, "cannot set list");
1115
1116         testres |= EC_TEST_CHECK(
1117                 ec_config_validate(config, sch_baseconfig) == 0,
1118                 "cannot validate config\n");
1119
1120         list_ = ec_config_dict_get(config, "my_dictlist");
1121         for (config_ = ec_config_list_first(list_); config_ != NULL;
1122              config_ = ec_config_list_next(list_, config_)) {
1123                 ec_config_dump(stdout, config_);
1124         }
1125
1126         ec_config_dump(stdout, config);
1127
1128         config2 = ec_config_dup(config);
1129         testres |= EC_TEST_CHECK(config2 != NULL, "cannot duplicate config");
1130         testres |= EC_TEST_CHECK(
1131                 ec_config_cmp(config, config2) == 0,
1132                 "fail to compare config");
1133         ec_config_free(config2);
1134         config2 = NULL;
1135
1136         /* remove the first element */
1137         ec_config_list_del(list_, ec_config_list_first(list_));
1138         testres |= EC_TEST_CHECK(
1139                 ec_config_validate(config, sch_baseconfig) == 0,
1140                 "cannot validate config\n");
1141
1142         ec_config_dump(stdout, config);
1143
1144         ec_config_free(list);
1145         ec_config_free(subconfig);
1146         ec_config_free(config);
1147         ec_dict_free(dict);
1148         ec_node_free(node);
1149
1150         return testres;
1151
1152 fail:
1153         ec_config_free(list);
1154         ec_config_free(subconfig);
1155         ec_config_free(config);
1156         ec_config_free(config2);
1157         ec_dict_free(dict);
1158         ec_node_free(node);
1159
1160         return -1;
1161 }
1162 /* LCOV_EXCL_STOP */
1163
1164 static struct ec_test ec_config_test = {
1165         .name = "config",
1166         .test = ec_config_testcase,
1167 };
1168
1169 EC_TEST_REGISTER(ec_config_test);