diff --git a/Makefile b/Makefile index a022ccb..0678bf3 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,8 @@ help: include oapi-cli.mk +BIN_DEPENDANCIES=bin/path_to_snakecase bin/line_check bin/get_argument_list bin/funclist + osc-api.json:: ./bin/osc-api-seems-valid.sh osc-api.json "need_remove" @@ -39,16 +41,19 @@ bin/path_to_snakecase: bin/path_to_snakecase.c bin/line_check: bin/line_check.c $(CC) -O3 bin/line_check.c -o bin/line_check -main.c: bin/line_check osc-api.json call_list config.sh main_tpl.c cognac_gen.sh mk_args.c.sh bin/path_to_snakecase +bin/get_argument_list: bin/get_argument_list.c + $(CC) -O3 -g bin/get_argument_list.c $(JSON_C_LDFLAGS) $(JSON_C_CFLAGS) -o bin/get_argument_list + +main.c: $(BIN_DEPENDANCIES) osc-api.json call_list config.sh main_tpl.c cognac_gen.sh mk_args.c.sh ./cognac_gen.sh main_tpl.c main.c c -osc_sdk.c: bin/line_check osc-api.json call_list config.sh lib.c cognac_gen.sh construct_data.c.sh mk_args.c.sh bin/path_to_snakecase +osc_sdk.c: $(BIN_DEPENDANCIES) osc-api.json call_list config.sh lib.c cognac_gen.sh construct_data.c.sh mk_args.c.sh ./cognac_gen.sh lib.c osc_sdk.c c -osc_sdk.h: bin/line_check osc-api.json call_list config.sh lib.h cognac_gen.sh mk_args.c.sh bin/path_to_snakecase +osc_sdk.h: $(BIN_DEPENDANCIES) osc-api.json call_list config.sh lib.h cognac_gen.sh mk_args.c.sh ./cognac_gen.sh lib.h osc_sdk.h c -$(CLI_NAME)-completion.bash: bin/line_check osc-api.json call_list config.sh oapi-cli-completion-tpl.bash cognac_gen.sh bin/path_to_snakecase +$(CLI_NAME)-completion.bash: $(BIN_DEPENDANCIES) osc-api.json call_list config.sh oapi-cli-completion-tpl.bash cognac_gen.sh ./cognac_gen.sh oapi-cli-completion-tpl.bash $(CLI_NAME)-completion.bash bash config.sh: configure config.mk diff --git a/bin/README.md b/bin/README.md index bc5a9a2..49acdd4 100644 --- a/bin/README.md +++ b/bin/README.md @@ -23,3 +23,22 @@ This is also much faster than multiple calls to grep. # osc-api-seems-valid.sh This utility is used to check if osc-api.json has been generated correctly. It verifies that the file is a valid JSON and contains at least one API function. + +# path_to_snakecase +snakecasise an object name. +example `ReadVms` become `read_vms` +`/my/{NAME}/getId` become `my_name_get_id` + +usage: +``` +./path_to_snakecase STRING +``` + +# get_argument_list + +Take an object componant in `paths` or in `components.schema`, and give a list of all it arguments. + +usage: +``` +./get_argument_list file.json componant_name +``` diff --git a/bin/funclist.c b/bin/funclist.c index 830b8f3..7c52c7a 100644 --- a/bin/funclist.c +++ b/bin/funclist.c @@ -64,7 +64,7 @@ int main(int ac, char **av) if (!first) putchar(' '); first = 0; - printf("%s", k+1); + printf("%s", k); } } diff --git a/bin/get_argument_list.c b/bin/get_argument_list.c new file mode 100644 index 0000000..34da87b --- /dev/null +++ b/bin/get_argument_list.c @@ -0,0 +1,66 @@ +#include +#include +#include +#include "json.h" + +#define OBJ_GET(src, name, dst) do { \ + ret = json_object_object_get_ex(src, name, dst); \ + if (!ret) { \ + puts("null"); \ + goto err; \ + } \ + } while (0) \ + +int main(int ac, char **av) +{ + struct json_object *j_file = json_object_from_file(av[1]); + char *componant_name = av[2]; + int func_ret = 1; + int ret; + + /* if / is found, assume in path */ + if (strchr(componant_name, '/')) { + int ret = 0; + struct json_object *paths; + struct json_object *func; + struct json_object *parameters; + struct json_object *post_or_get; + int len; + + OBJ_GET(j_file, "paths", &paths); + OBJ_GET(paths, componant_name, &func); + ret = json_object_object_get_ex(func, "post", &post_or_get); + if (!ret) { + OBJ_GET(func, "get", &post_or_get); + } + OBJ_GET(post_or_get, "parameters", ¶meters); + len = json_object_array_length(parameters); + for (int i = 0; i < len; ++i) { + struct json_object *name; + + OBJ_GET(json_object_array_get_idx(parameters, i), + "name", &name); + printf(" %s" + !i, json_object_get_string(name)); + } + } else { + struct json_object *compo; + struct json_object *schema; + struct json_object *func; + struct json_object *properties; + int i = 0; + + OBJ_GET(j_file, "components", &compo); + OBJ_GET(compo, "schemas", &schema); + OBJ_GET(schema, componant_name, &func); + OBJ_GET(func, "properties", &properties); + json_object_object_foreach(properties, k, v_) { + printf(" %s" + !i++, k); + } + } + putchar('\n'); + func_ret = 0; +err: + json_object_put(j_file); + return func_ret; + +} diff --git a/cognac_gen.sh b/cognac_gen.sh index c8293d2..c8c6ea8 100755 --- a/cognac_gen.sh +++ b/cognac_gen.sh @@ -302,17 +302,20 @@ EOF done elif [ "$arg_check" == "____complex_struct_func_parser____" ]; then debug "____complex_struct_func_parser____" - local CALLS=$(for c in $(cat call_list) ; do echo ${c}${FUNCTION_SUFFIX} ; done) COMPLEX_STRUCT=$(jq .components < osc-api.json | json-search -KR schemas | tr -d '"' | sed 's/,/\n/g' | grep -v Response) - local DIFF=$(echo ${CALLS[@]} ${COMPLEX_STRUCT[@]} | tr ' ' '\n' | sort | uniq -u) - COMPLEX_STRUCT="$DIFF" + if [[ "$FROM_PATH" != "1" ]]; then + local CALLS=$(for c in $(cat call_list) ; do echo ${c}${FUNCTION_SUFFIX} ; done) + local DIFF=$(echo ${CALLS[@]} ${COMPLEX_STRUCT[@]} | tr ' ' '\n' | sort | uniq -u) + + COMPLEX_STRUCT="$DIFF" + fi # prototypes for s in $COMPLEX_STRUCT; do struct_name=$(bin/path_to_snakecase "$s") - debug "structs name: $s $struct_name" - A_LST=$(jq .components.schemas[\"$s\"] < osc-api.json | json-search -Kn properties | tr -d '",[]') + A_LST=$(bin/get_argument_list osc-api.json "$s") + if [ "$A_LST" != "null" ]; then echo "int ${struct_name}_parser(void *s, char *str, char *aa, struct ptr_array *pa);" fi @@ -325,7 +328,7 @@ EOF struct_name=$(to_snakecase <<< $s) local componant=$(jq .components.schemas.$s < osc-api.json) - A_LST=$(json-search -Kn properties <<< $componant | tr -d '",[]') + A_LST=$(bin/get_argument_list osc-api.json "$s") if [ "$A_LST" != "null" ]; then echo "int ${struct_name}_parser(void *v_s, char *str, char *aa, struct ptr_array *pa) {" @@ -372,9 +375,10 @@ EOF debug "____cli_parser____" for l in $CALL_LIST; do snake_l=$(to_snakecase <<< $l) - arg_list=$(json-search ${l}${FUNCTION_SUFFIX} < osc-api.json \ - | json-search -K properties \ - | tr -d "[]\"," | sed '/^$/d') + arg_list=$(bin/get_argument_list osc-api.json ${l}${FUNCTION_SUFFIX}) + + debug "arg list of " ${l}${FUNCTION_SUFFIX} ":" + debug $arg_list cat <