github.com/mitranim/gg@v0.1.17/main_test.go (about) 1 package gg_test 2 3 import ( 4 "strconv" 5 6 "github.com/mitranim/gg" 7 ) 8 9 func init() { gg.TraceBaseDir = gg.Cwd() } 10 11 var void struct{} 12 13 // Adapted from `reflect.ValueOf`. 14 func esc(val any) any { 15 if trap.false { 16 trap.val = val 17 } 18 return val 19 } 20 21 var trap struct { 22 false bool 23 val any 24 } 25 26 type IntSet = gg.Set[int] 27 28 type IntMap = map[int]int 29 30 type SomeKey int64 31 32 type SomeModel struct { 33 Id SomeKey `json:"id"` 34 Name string `json:"name"` 35 } 36 37 func (self SomeModel) Pk() SomeKey { return self.Id } 38 39 type StructDirect struct { 40 Public0 int 41 Public1 string 42 private *string 43 } 44 45 //nolint:unused 46 type StructIndirect struct { 47 Public0 int 48 Public1 *string 49 private *string 50 } 51 52 type Outer struct { 53 OuterId int 54 OuterName string 55 Embed 56 Inner *Inner 57 } 58 59 type Embed struct { 60 EmbedId int 61 EmbedName string 62 } 63 64 type Inner struct { 65 InnerId *int 66 InnerName *string 67 } 68 69 type SomeJsonDbMapper struct { 70 SomeName string `json:"someName" db:"some_name"` 71 SomeValue string `json:"someValue" db:"some_value"` 72 SomeJson string `json:"someJson"` 73 SomeDb string `db:"some_db"` 74 } 75 76 type SomeColl = gg.Coll[SomeKey, SomeModel] 77 78 type SomeLazyColl = gg.LazyColl[SomeKey, SomeModel] 79 80 type IsZeroAlwaysTrue string 81 82 func (IsZeroAlwaysTrue) IsZero() bool { return true } 83 84 type IsZeroAlwaysFalse string 85 86 func (IsZeroAlwaysFalse) IsZero() bool { return false } 87 88 type FatStruct struct { 89 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, Id int 90 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, Name string 91 } 92 93 type FatStructNonComparable struct { 94 FatStruct 95 _ []byte 96 } 97 98 func ComparerOf[A gg.LesserPrim](val A) Comparer[A] { return Comparer[A]{val} } 99 100 type Comparer[A gg.LesserPrim] [1]A 101 102 func (self Comparer[A]) Less(val Comparer[A]) bool { return self[0] < val[0] } 103 104 func (self Comparer[A]) Get() A { return self[0] } 105 106 func ToPair[A gg.Num](val A) (A, A) { return val - 1, val + 1 } 107 108 func True1[A any](A) bool { return true } 109 110 func False1[A any](A) bool { return false } 111 112 func Id1True[A any](val A) (A, bool) { return val, true } 113 114 func Id1False[A any](val A) (A, bool) { return val, false } 115 116 type ParserStr string 117 118 func (self *ParserStr) Parse(val string) error { 119 *self = ParserStr(val) 120 return nil 121 } 122 123 type UnmarshalerBytes []byte 124 125 func (self *UnmarshalerBytes) UnmarshalText(val []byte) error { 126 *self = val 127 return nil 128 } 129 130 // Implements `error` on the pointer type, not on the value type. 131 type PtrErrStr string 132 133 func (self *PtrErrStr) Error() string { return gg.PtrGet((*string)(self)) } 134 135 type StrsParser []string 136 137 func (self *StrsParser) Parse(src string) error { 138 gg.Append(self, src) 139 return nil 140 } 141 142 type IntsValue []int 143 144 func (self *IntsValue) Set(src string) error { 145 return gg.ParseCatch(src, gg.AppendPtrZero(self)) 146 } 147 148 /* 149 Defined as a struct to verify that the flag parser supports slices of arbitrary 150 types implementing `flag.Value`, even if they're not typedefs of text types, 151 and not something normally compatible with `gg.Parse`. 152 */ 153 type IntValue struct{ Val int } 154 155 func (self *IntValue) Set(src string) error { 156 return gg.ParseCatch(src, &self.Val) 157 } 158 159 func intStrPair(src int) []string { 160 return []string{strconv.Itoa(src - 1), strconv.Itoa(src + 1)} 161 } 162 163 func intPair(src int) []int { 164 return []int{src - 1, src + 1} 165 } 166 167 type Cyclic struct { 168 Id int 169 Cyclic *Cyclic 170 }