64 lines
1.9 KiB
Python
Vendored
64 lines
1.9 KiB
Python
Vendored
import os
|
|
import re
|
|
import sys
|
|
|
|
# Ensure UTF-8 output
|
|
sys.stdout.reconfigure(encoding='utf-8')
|
|
|
|
TYPE_MAP = {
|
|
'uint': 'uint32',
|
|
'int': 'int32',
|
|
'ulong': 'uint64',
|
|
'long': 'int64',
|
|
'ushort': 'uint16',
|
|
'short': 'int16',
|
|
'byte': 'uint8',
|
|
'sbyte': 'int8',
|
|
'float': 'float32',
|
|
'double': 'float64',
|
|
'bool': 'bool',
|
|
'string': 'string',
|
|
'object': 'interface{}',
|
|
'DateTime': 'time.Time',
|
|
'TimeSpan': 'time.Duration',
|
|
}
|
|
|
|
def map_type(t):
|
|
t = t.strip()
|
|
if t.endswith('?'):
|
|
inner = map_type(t[:-1])
|
|
return f"*{inner}"
|
|
if t.startswith('List<') or t.startswith('IList<') or t.startswith('IReadOnlyList<'):
|
|
inner = t[t.find('<')+1 : t.rfind('>')]
|
|
return f"[]{map_type(inner)}"
|
|
if t.startswith('Dictionary<') or t.startswith('IReadOnlyDictionary<'):
|
|
parts = t[t.find('<')+1 : t.rfind('>')].split(',')
|
|
if len(parts) == 2:
|
|
return f"map[{map_type(parts[0])}]{map_type(parts[1])}"
|
|
if t.startswith('HashSet<') or t.startswith('IReadOnlySet<'):
|
|
inner = t[t.find('<')+1 : t.rfind('>')]
|
|
return f"map[{map_type(inner)}]struct{{}}"
|
|
return TYPE_MAP.get(t, t)
|
|
|
|
def convert_record(line):
|
|
# record struct Name(T1 F1, T2 F2, ...)
|
|
m = re.search(r'(?:public|internal|private)?\s*(?:readonly)?\s*record\s+(?:struct\s+)?([A-Za-z0-9_]+)\s*\(([^)]*)\)', line)
|
|
if not m:
|
|
return None
|
|
name = m.group(1)
|
|
params = m.group(2).split(',')
|
|
fields = []
|
|
for p in params:
|
|
p = p.strip()
|
|
if not p:
|
|
continue
|
|
parts = p.split()
|
|
if len(parts) >= 2:
|
|
f_type = map_type(parts[0])
|
|
f_name = parts[1]
|
|
json_tag = f_name[0].lower() + f_name[1:]
|
|
fields.append(f"\t{f_name} {f_type} `json:\"{json_tag}\"`")
|
|
return f"type {name} struct {{\n" + "\n".join(fields) + "\n}"
|
|
|
|
print("Converter core initialized")
|