From b6195ee7f1e22683084d0236dda26bdd4799a79d Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Tue, 17 Dec 2024 16:06:52 +0100 Subject: [PATCH] work with guru API Signed-off-by: Matthias Gatto --- bin/get_argument_list.c | 6 +++++ bin/get_path_type.c | 1 + bin/helper.h | 49 +++++++++++++++++++++++++++++++++++++---- bin/path_to_snakecase.c | 2 +- cognac_gen.sh | 37 ++++++++++++++++--------------- construct_data.c.sh | 4 ++-- helper.sh | 13 +++++++++-- mk_args.c.sh | 14 ++++++------ 8 files changed, 92 insertions(+), 34 deletions(-) diff --git a/bin/get_argument_list.c b/bin/get_argument_list.c index 0dd3936..0eab558 100644 --- a/bin/get_argument_list.c +++ b/bin/get_argument_list.c @@ -38,6 +38,12 @@ int main(int ac, char **av) struct json_object *name; struct json_object *param = json_object_array_get_idx(parameters, i); + param = try_get_ref(j_file, param); + if (!param) { + puts(UNFOUND); + goto err; + } + if (only_require) { struct json_object *required; json_object_object_get_ex(param, "required", &required); diff --git a/bin/get_path_type.c b/bin/get_path_type.c index 4a68d2d..0221479 100644 --- a/bin/get_path_type.c +++ b/bin/get_path_type.c @@ -40,6 +40,7 @@ int main(int ac, char **av) struct json_object *param = json_object_array_get_idx(parameters, i); struct json_object *name_obj; + param = try_get_ref(j_file, param); OBJ_GET(param, "name", &name_obj); const char *name = json_object_get_string(name_obj); struct json_object *type; diff --git a/bin/helper.h b/bin/helper.h index 4e75740..362684b 100644 --- a/bin/helper.h +++ b/bin/helper.h @@ -16,7 +16,7 @@ } while (0) \ -char *componant_name_from_get_or_post(struct json_object *post_or_get) +static char *componant_name_from_get_or_post(struct json_object *post_or_get) { char *componant_name; struct json_object *request_body; @@ -46,7 +46,7 @@ char *componant_name_from_get_or_post(struct json_object *post_or_get) return NULL; } -struct json_object *oneof_or_anyof(struct json_object *param) +static struct json_object *oneof_or_anyof(struct json_object *param) { struct json_object *anyof_or_oneof; int ret; @@ -64,7 +64,7 @@ struct json_object *oneof_or_anyof(struct json_object *param) } -struct json_object *get_or_post_from_path(struct json_object *path) +static struct json_object *get_or_post_from_path(struct json_object *path) { struct json_object *post_or_get; int ret; @@ -81,7 +81,7 @@ struct json_object *get_or_post_from_path(struct json_object *path) return NULL; } -struct json_object *get_path_from_file(struct json_object *j_file, char *path) +static struct json_object *get_path_from_file(struct json_object *j_file, char *path) { struct json_object *paths; struct json_object *func; @@ -95,4 +95,45 @@ struct json_object *get_path_from_file(struct json_object *j_file, char *path) return NULL; } +static struct json_object *try_get_ref(struct json_object *in, struct json_object *param) +{ + int ret; + struct json_object *ref = NULL; + struct json_object *json_ret = NULL; + + ret = json_object_object_get_ex(param, "$ref", &ref); + if (!ret) + return param; + const char *ref_str = json_object_get_string(ref); + + ref_str = strchr(ref_str, '/'); + if (!ref_str || !*ref_str) { + return NULL; + } + param = in; + ++ref_str; + char *cpy = strdup(ref_str); + ref_str = cpy; + + char *ref_str_2; + +again: + + ref_str_2 = strchr(ref_str, '/'); + if (ref_str_2) { + *ref_str_2 = 0; + } + ret = json_object_object_get_ex(param, ref_str, ¶m); + if (!ret) + goto err; + if (ref_str_2) { + ref_str = ref_str_2 + 1; + goto again; + } + json_ret = param; +err: + free(cpy); + return json_ret; +} + #endif diff --git a/bin/path_to_snakecase.c b/bin/path_to_snakecase.c index ec57048..68495d5 100644 --- a/bin/path_to_snakecase.c +++ b/bin/path_to_snakecase.c @@ -24,7 +24,7 @@ int main(int ac, char **av) if (c == '{' || c == '}') { continue; } - if (c == '/') { + if (c == '/' || c == '.') { PUT_UNDERSCORE(); } else if (isupper(c)) { if (!first) { diff --git a/cognac_gen.sh b/cognac_gen.sh index 07a5e8d..01cf3cf 100755 --- a/cognac_gen.sh +++ b/cognac_gen.sh @@ -14,7 +14,7 @@ source ./helper.sh debug "debug mode is on" debug "Get functions call from osc-api.json path: $FROM_PATH" -default_endpoint=$(cat osc-api-api.json | jq -r .servers[0].url) +default_endpoint=$(cat osc-api.json | jq -r .servers[0].url) if [[ "$default_endpoint" == "null" ]]; then default_endpoint='https://api.{region}.outscale.com/api/v1' fi @@ -30,8 +30,8 @@ if [[ $path_begin_l != 0 ]]; then default_endpoint=${default_endpoint:0:-$path_begin_l} fi -debug $path_begin -debug $default_endpoint +debug "endpoint:" $default_endpoint +debug "path begin:" $path_begin dash_this_arg() @@ -83,7 +83,7 @@ cli_c_type_parser() type=$2 indent_base=$3 indent_plus="$indent_base " - snake_a=$(to_snakecase <<< $a) + snake_a=$(bin/path_to_snakecase $a) snake_a=$( if [ "default" == "$snake_a" ] ; then echo default_arg; else echo $snake_a; fi ) if [ 'int' == "$type" ]; then @@ -254,7 +254,6 @@ replace_args() echo -ne $D3 elif [ "$arg_check" == "____make_default_endpoint____" ]; then debug "____make_default_endpoint____" - debug "$default_endpoint" bin/construct_endpoint "$default_endpoint" elif [ "$arg_check" == "____call_list_args_descriptions____" ]; then debug "____call_list_args_descriptions____" @@ -302,16 +301,16 @@ replace_args() fi for s in $COMPLEX_STRUCT; do - struct_name=$(to_snakecase <<< $s) + struct_name=$(bin/path_to_snakecase $s) - 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 "static int ${struct_name}_setter(struct ${struct_name} *args, struct osc_str *data);" fi done for s in $COMPLEX_STRUCT; do - struct_name=$(to_snakecase <<< $s) - A_LST=$(jq .components.schemas.$s < osc-api.json | json-search -Kn properties | tr -d '",[]') + struct_name=$(bin/path_to_snakecase $s) + A_LST=$(./bin/get_argument_list osc-api.json "$s") if [ "$A_LST" != "null" ]; then cat < /dev/null) osub_ref=$(cut -c 2- <<< $osub_ref | sed 's|/|.|g') local sub_ref_properties=$(jq $osub_ref.properties < osc-api.json 2> /dev/null) @@ -79,7 +83,12 @@ get_type_direct() { local the_one=$(jq .[0] <<< $one_of) get_type_direct "$the_one" else - echo ref $(json-search -R '$ref' <<< ${arg_info} | cut -d '/' -f 4) + local ref=$(json-search -R '$ref' <<< ${arg_info}) + if [[ $ref == "null" ]]; then + echo "ref null" + else + echo ref $(cut -d '/' -f 4 <<< $ref) + fi fi } diff --git a/mk_args.c.sh b/mk_args.c.sh index 46f60fe..7688ce0 100755 --- a/mk_args.c.sh +++ b/mk_args.c.sh @@ -43,7 +43,7 @@ type_to_ctype() { int is_set_${snake_name}; EOF t=$( cut -f 2 -d ' ' <<< $t ) - c_type="struct $(to_snakecase <<< $t) " + c_type="struct $(bin/path_to_snakecase $t) " elif [ "array" == $( cut -d ' ' -f 1 <<< $t) ]; then if [ "ref" == $( cut -d ' ' -f 2 <<< $t) ]; then t=$( cut -f 3 -d ' ' <<< $t ) @@ -51,7 +51,7 @@ EOF char *${snake_name}_str; int nb_${snake_name}; EOF - c_type="struct $(to_snakecase <<< $t) *" + c_type="struct $(bin/path_to_snakecase $t) *" fi fi echo " ${c_type}${snake_name};" @@ -64,15 +64,15 @@ write_struct() { if [ "$st_info" == "" ]; then st_info=$(jq .components.schemas.$s0 < osc-api.json) - A_LST=$(json-search -K properties <<< $st_info | tr -d '",[]') + A_LST=$(./bin/get_argument_list osc-api.json "$s") fi - st_s_name=$(to_snakecase <<< $s0) + st_s_name=$(bin/path_to_snakecase $s0) echo "struct $st_s_name {" for a in $A_LST; do local t=$(get_type3 "$st_info" "$a") - local snake_n=$(to_snakecase <<< $a) + local snake_n=$(bin/path_to_snakecase $a) echo ' /*' get_type_description "$st_info" "$a" | tr -d '"' | fold -s -w70 | sed -e 's/^/ * /g' echo ' */' @@ -93,7 +93,7 @@ create_struct() { #for s in "skip"; do local s="$1" local st0_info=$(jq .components.schemas.$s < osc-api.json) - local A0_LST=$(json-search -Kn properties <<< $st0_info | tr -d '",[]') + local A0_LST=$(./bin/get_argument_list osc-api.json "$s") if [ "${structs[$s]}" != "" ]; then return @@ -138,7 +138,7 @@ for l in $CALL_LIST ;do echo " */" for x in $ARGS_LIST ;do - snake_name=$(to_snakecase <<< "$x") + snake_name=$(bin/path_to_snakecase "$x") t=$(get_type "$l" "$x") #echo "get type: $func $x"