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