github.com/tursom/GoCollections@v0.3.10/lang/types_generator.py (about) 1 # Copyright (c) 2022 tursom. All rights reserved. 2 # Use of this source code is governed by a GPL-3 3 # license that can be found in the LICENSE file. 4 5 def generate(base_type: str): 6 if base_type[0] == 'u': 7 i = 2 8 else: 9 i = 1 10 package_type = base_type[0:i].upper() + base_type[i:] 11 12 lines = [] 13 # package 14 lines.append("package lang\n") 15 16 # imports 17 lines.append("import \"strconv\"\n") 18 19 # type define 20 lines.append(f"type {package_type} {base_type}\n") 21 22 if base_type == "int32": 23 lines.append("type Rune = Int32\n") 24 25 # As(Type) interface 26 lines.append(f"type As{package_type} interface ""{\n" 27 " Object\n" 28 f" As{package_type}() {package_type}\n" 29 "}\n") 30 31 # Cast(Type) function 32 lines.append(f"func Cast{package_type}(v any) ({base_type}, bool) ""{\n" 33 " switch i := v.(type) {\n" 34 f" case {base_type}:\n" 35 " return i, true\n" 36 f" case As{package_type}:\n" 37 f" return i.As{package_type}().V(), true\n" 38 " default:\n" 39 " return 0, false\n" 40 " }\n" 41 "}\n") 42 43 # Equals(Type) function 44 lines.append(f"func Equals{package_type}(i1 As{package_type}, i2 any) bool ""{\n" 45 f" i2, ok := Cast{package_type}(i2)\n" 46 f" return ok && i2 == i1.As{package_type}().V()\n" 47 "}\n") 48 49 # type.V() function 50 lines.append(f"func (i {package_type}) V() {base_type} ""{\n" 51 f" return {base_type}(i)\n" 52 "}\n") 53 54 # type.P() function 55 lines.append(f"func (i *{package_type}) P() *{base_type} ""{\n" 56 f" return (*{base_type})(i)\n" 57 "}\n") 58 59 # As(Type) interface implement 60 lines.append(f"func (i {package_type}) As{package_type}() {package_type} ""{\n" 61 f" return i\n" 62 "}\n") 63 64 # String 65 if base_type[0] == 'u': 66 lines.append(f"func (i {package_type}) String() string ""{\n" 67 f" return strconv.FormatUint(uint64(i), 10)\n" 68 "}\n") 69 else: 70 lines.append(f"func (i {package_type}) String() string ""{\n" 71 f" return strconv.FormatInt(int64(i), 10)\n" 72 "}\n") 73 74 # AsObject 75 lines.append(f"func (i {package_type}) AsObject() Object ""{\n" 76 f" return i\n" 77 "}\n") 78 79 # Equals 80 lines.append(f"func (i {package_type}) Equals(e Object) bool ""{\n" 81 f" return Equals{package_type}(i, e)\n" 82 "}\n") 83 84 # ToString 85 lines.append(f"func (i {package_type}) ToString() String ""{\n" 86 " return NewString(i.String())\n" 87 "}\n") 88 89 # HashCode 90 if package_type.endswith("64"): 91 lines.append(f"func (i {package_type}) HashCode() int32 ""{\n" 92 " return Hash64(&i)\n" 93 "}\n") 94 else: 95 lines.append(f"func (i {package_type}) HashCode() int32 ""{\n" 96 " return int32(i)\n" 97 "}\n") 98 99 # Compare 100 lines.append(f"func (i {package_type}) Compare(t {package_type}) int ""{\n" 101 " switch {\n" 102 " case i > t:\n" 103 " return 1\n" 104 " case i == t:\n" 105 " return 0\n" 106 " default:\n" 107 " return -1\n" 108 " }\n" 109 "}\n") 110 111 with open(f"{package_type}.go", "w", encoding="utf-8") as out: 112 out.writelines([f"{l}\n" for l in lines])