Skip to content

Commit

Permalink
add more helper functions
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 24, 2024
1 parent d2f84c5 commit 8dc5c28
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 14 deletions.
13 changes: 9 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions bin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
2 changes: 1 addition & 1 deletion bin/funclist.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ int main(int ac, char **av)
if (!first)
putchar(' ');
first = 0;
printf("%s", k+1);
printf("%s", k);
}
}

Expand Down
66 changes: 66 additions & 0 deletions bin/get_argument_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#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", &parameters);
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;

}
22 changes: 13 additions & 9 deletions cognac_gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {"

Expand Down Expand Up @@ -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 <<EOF
if (!strcmp("$l", av[i])) {
Expand Down

0 comments on commit 8dc5c28

Please sign in to comment.