Skip to content

Commit

Permalink
work with guru API
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Gatto <[email protected]>
  • Loading branch information
outscale-mgo committed Dec 17, 2024
1 parent a68744d commit b6195ee
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 34 deletions.
6 changes: 6 additions & 0 deletions bin/get_argument_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions bin/get_path_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
49 changes: 45 additions & 4 deletions bin/helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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, &param);
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
2 changes: 1 addition & 1 deletion bin/path_to_snakecase.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
37 changes: 19 additions & 18 deletions cognac_gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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____"
Expand Down Expand Up @@ -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 <<EOF
static int ${struct_name}_setter(struct ${struct_name} *args, struct osc_str *data) {
Expand Down Expand Up @@ -352,18 +351,18 @@ EOF
# functions
for s in $COMPLEX_STRUCT; do
#for s in "skip"; do
struct_name=$(to_snakecase <<< $s)
struct_name=$(bin/path_to_snakecase $s)

local componant=$(jq .components.schemas.$s < osc-api.json)
A_LST=$(bin/get_argument_list osc-api.json "$s")
if [ "$A_LST" != "null" ]; then
if [[ "$A_LST" != "null" ]]; then
echo "int ${struct_name}_parser(void *v_s, char *str, char *aa, struct ptr_array *pa) {"

echo " struct $struct_name *s = v_s;"
echo " int aret = 0;"
for a in $A_LST; do
t=$(get_type2 "$s" "$a")
snake_n=$(to_snakecase <<< $a)
snake_n=$(bin/path_to_snakecase $a)

echo " if ((aret = argcmp(str, \"$a\")) == 0 || aret == '=' || aret == '.') {"
cli_c_type_parser "$a" "$t" " "
Expand Down Expand Up @@ -453,11 +452,12 @@ EOF
}
EOF

for a in $arg_list ; do
type=$(get_type $l $a)
snake_a=$(to_snakecase <<< $a)
if [[ "$arg_list" != "null" ]]; then
for a in $arg_list ; do
type=$(get_type $l $a)
snake_a=$(bin/path_to_snakecase $a)

cat <<EOF
cat <<EOF
if ((aret = argcmp(next_a, "$a")) == 0 || aret == '=' || aret == '.') {
char *eq_ptr = strchr(next_a, '=');
if (eq_ptr) {
Expand All @@ -466,8 +466,9 @@ EOF
incr = 1;
}
EOF
cli_c_type_parser "$a" "$type" " "
done
cli_c_type_parser "$a" "$type" " "
done
fi

cat <<EOF
{
Expand Down
4 changes: 2 additions & 2 deletions construct_data.c.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ debug "========= $func ========"

if [[ "complex_struct" == "$2" ]]; then
base=$(jq .components.schemas.$func < osc-api.json)
args=$(json-search -Kn properties <<< $base | tr -d '",[]')
args=$(bin/get_argument_list osc-api.json $func)
alias get_type=get_type2
else
base=$(json-search ${func}${FUNCTION_SUFFIX} < osc-api.json)
Expand Down Expand Up @@ -43,7 +43,7 @@ fi

for x in $args ;do
placement=$(bin/arg_placement osc-api.json $func $x)
snake_x=$(to_snakecase <<< $x)
snake_x=$(bin/path_to_snakecase $x)
snake_x=$( if [ "default" == "$snake_x" ] ; then echo default_arg; else echo $snake_x; fi )
t=$(get_type $func $x)

Expand Down
13 changes: 11 additions & 2 deletions helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ get_type_direct() {
elif [[ "$sub_type" == 'number' ]]; then
types="array double"
elif [[ "$sub_type" == 'null' ]]; then
local osub_ref=$(json-search -R '$ref' <<< ${arg_info})
local osub_ref=$(json-search -Rn '$ref' <<< ${arg_info})
if [[ $osub_ref == "null" ]]; then
echo "array string"
return 0
fi
local sub_ref=$(cut -d '/' -f 4 <<< $osub_ref 2> /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)
Expand All @@ -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
}

Expand Down
14 changes: 7 additions & 7 deletions mk_args.c.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ 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 )
cat << 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};"
Expand All @@ -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 ' */'
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit b6195ee

Please sign in to comment.