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