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