Files
gochat/scripts/fix_route_params.py
T
2026-06-04 15:44:48 +08:00

51 lines
2.1 KiB
Python

#!/usr/bin/env python3
"""
Fix Gin radix tree param name conflicts in router.go.
Strategy: unify all param names at the same path position to match Chatwoot conventions.
Known conflicts:
1. conversations/:id vs conversations/:conversation_id → use :conversation_id (matches Chatwoot)
2. accounts/:id (in webhook routes) → already fixed to :account_id
3. notifications/:id → no conflict with sub-resources
4. push_subscriptions/:id → no conflict with sub-resources
For conversations: change all :id under conversations group to :conversation_id
and update corresponding handler code.
"""
import re
router_path = '/home/yanghao05/Workspace/gochat/internal/router/router.go'
with open(router_path) as f:
content = f.read()
# 1. In conversations group, change bare :id to :conversation_id
# These are lines like: conversations.GET("/:id", ...)
# The conversations group starts around line 697 and all its direct :id params need changing
lines = content.split('\n')
changes = []
for i, line in enumerate(lines):
stripped = line.strip()
# Find conversations.GET("/:id", ...) style lines
# Must be under conversations group context
if 'conversations.' in stripped and ':id' in stripped and ':conversation_id' not in stripped:
# Replace :id with :conversation_id in the path
new_line = line.replace('/:id', '/:conversation_id').replace('"/:id"', '"/:conversation_id"')
# But careful: only replace the route path param, not other :id references
# Use regex to specifically target the route path string
new_line = re.sub(r'"/:id"', '"/:conversation_id"', line)
new_line = re.sub(r'"/:id/', '"/:conversation_id/', new_line)
if new_line != line:
changes.append((i+1, line.strip()[:80], new_line.strip()[:80]))
lines[i] = new_line
print(f"Changed {len(changes)} lines in router.go:")
for linenum, old, new in changes:
print(f" Line {linenum}: {old}{new}")
# Also fix line 1372: conversationAttrs Group path uses :conversation_id
# (already correct, no change needed)
with open(router_path, 'w') as f:
f.write('\n'.join(lines))