Skip to content

Commit

Permalink
build: reorder type_asserts.go using Python
Browse files Browse the repository at this point in the history
- restore gen_type_asserts to avoid regex
- use Python for re-ordering without regex

Signed-off-by: Matteo Mortari <[email protected]>
  • Loading branch information
tarilabs committed Sep 19, 2024
1 parent 3b00296 commit ec404de
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
30 changes: 3 additions & 27 deletions scripts/gen_type_asserts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
38 changes: 38 additions & 0 deletions scripts/reorder_type_asserts.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit ec404de

Please sign in to comment.