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