Skip to content

Instantly share code, notes, and snippets.

@spencerpogo
Last active October 15, 2020 16:38
Show Gist options
  • Save spencerpogo/e06f27840fa4815d6bfb4c042b178979 to your computer and use it in GitHub Desktop.
Save spencerpogo/e06f27840fa4815d6bfb4c042b178979 to your computer and use it in GitHub Desktop.
Poor man's go field refactoring tool
#!/usr/bin/env python3
import sys
if len(sys.argv) < 4:
print(
f"Usage: {sys.argv[0]} <Struct type name (no star)> <field name> <field type>"
)
sys.exit(1)
try:
from pyperclip import copy
except ImportError:
print("[WARN] pyperclip not installed, will not copy", file=sys.stderr)
def copy(text):
pass
struct_type = sys.argv[1]
name = sys.argv[2]
atype = sys.argv[3]
public = name[0].upper() + name[1:]
private = name[0].lower() + name[1:]
val = (
"""// <public> gets <private>
func (<sh> *<st>) <public>() <type> {
<sh>.mu.Lock()
defer <sh>.mu.Unlock()
return <sh>.<private>
}
// Set<public> sets <private>
func (<sh> *<st>) Set<public>(val <type>) {
<sh>.mu.Lock()
defer <sh>.mu.Unlock()
<sh>.<private> = val
}""".replace(
"<sh>", struct_type.strip("*")[0].lower()
)
.replace("<st>", struct_type)
.replace("<public>", public)
.replace("<private>", private)
.replace("<type>", atype)
)
print(val)
copy(val)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment