diff --git a/scripts/gen_type_asserts.sh b/scripts/gen_type_asserts.sh index 74a9a26f..6f874243 100755 --- a/scripts/gen_type_asserts.sh +++ b/scripts/gen_type_asserts.sh @@ -42,43 +42,19 @@ EOF # Create the file and initialize it with the specified content echo -e "$INITIAL_CONTENT" >"$ASSERT_FILE_PATH" -declare -A assert_functions -pattern="\/\/ (\w+) checks(.|\n)+?\n\}\n" -shell_pattern="\/\/ (\w+) checks[^\/?]+?" - # Iterate over files starting with "model_" in the internal/server/openapi/ folder for file in "$PROJECT_ROOT"/internal/server/openapi/model_*; do # Check if the file is a regular file if [ -f "$file" ]; then - - # grab all functions in the file - functions=$(grep -Pzo "$pattern" "${file}" | tr -d '\0') - while [[ $functions =~ $shell_pattern ]]; do - - name="${BASH_REMATCH[1]}" - body="${BASH_REMATCH[0]}" - - # add function to associative array - assert_functions["$name"]="$body" - - # Remove the matched function to process next function - functions=${functions//"$body"/} - done + # Ignore first 15 lines containing license, package and imports + sed -n '13,$p' "$file" >>"$ASSERT_FILE_PATH" # Remove the merged file rm "$file" fi done -# get sorted function names in another array -sorted_names=$(for name in "${!assert_functions[@]}"; do - echo "$name" -done | sort) - -# iterate over the sorted function names array and print bodies -for name in $sorted_names; do - echo "${assert_functions[$name]}" >>"$ASSERT_FILE_PATH" -done +python "$PROJECT_ROOT"/scripts/reorder_type_asserts.py "$PROJECT_ROOT"/internal/server/openapi/type_asserts.go gofmt -w "$ASSERT_FILE_PATH" diff --git a/scripts/reorder_type_asserts.py b/scripts/reorder_type_asserts.py new file mode 100644 index 00000000..55840896 --- /dev/null +++ b/scripts/reorder_type_asserts.py @@ -0,0 +1,38 @@ +import sys + + +def reorder_go_functions(file_path): + with open(file_path, 'r') as f: + lines = f.readlines() + + header = [] + functions = [] + current_function = [] + inside_function = False + + for line in lines: + if line.startswith("// Assert"): + inside_function = True + + if inside_function: + current_function.append(line) + if line.rstrip() == "}": + functions.append("".join(current_function)) + current_function = [] + inside_function = False + elif len(functions) == 0: + header.append(line) + + functions.sort(key=lambda func: func.split('\n')[0].strip()) + + sorted_content = "".join(header) + "\n".join(functions) + + with open(file_path, 'w') as f: + f.write(sorted_content) + + +if __name__ == "__main__": + if len(sys.argv) == 2: + reorder_go_functions(sys.argv[1]) + else: + print("Provide internal/server/openapi/type_asserts.go path as first argument")