Skip to content

Instantly share code, notes, and snippets.

@auyer
Created February 8, 2024 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save auyer/dd648f19fd4f32c57f486c30b4c9d35d to your computer and use it in GitHub Desktop.
Save auyer/dd648f19fd4f32c57f486c30b4c9d35d to your computer and use it in GitHub Desktop.
Script for GO files to add msgpack struct tags to every struct that has a Json tag.
import re
import sys
def add_new_text(filename):
with open(filename, "r") as file:
data = file.readlines()
pattern = re.compile(r'json:"([\w,]+)"')
new_data = []
for line in data:
match = re.search(pattern, line)
if match:
new_line = line.replace(
match.group(), f'{match.group()} msgpack:"{match.group(1)}"'
)
new_data.append(new_line)
else:
new_data.append(line)
with open(filename, "w") as file:
file.writelines(new_data)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("usage: python script.py <filename>")
sys.exit(1)
add_new_text(sys.argv[1])
print(f"Added msgpack tags to {sys.argv[1]}")
@auyer
Copy link
Author

auyer commented Feb 8, 2024

to run for every file in the repository with ripgrep and xargs:

rg -iPlg '*.go' 'json:"' . | xargs -L 1 python add_msgpack_struct_tags.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment