github.com/jgarto/itcv@v0.0.0-20180826224514-4eea09c1aa0d/examples/sites/globalstate/model/gen_person_immutableGen.go (about) 1 // Code generated by immutableGen. DO NOT EDIT. 2 3 package model 4 5 //immutableVet:skipFile 6 7 import ( 8 "myitcv.io/immutable" 9 ) 10 11 // 12 // People is an immutable type and has the following template: 13 // 14 // []*Person 15 // 16 type People struct { 17 theSlice []*Person 18 mutable bool 19 __tmpl _Imm_People 20 } 21 22 var _ immutable.Immutable = new(People) 23 var _ = new(People).__tmpl 24 25 func NewPeople(s ...*Person) *People { 26 c := make([]*Person, len(s)) 27 copy(c, s) 28 29 return &People{ 30 theSlice: c, 31 } 32 } 33 34 func NewPeopleLen(l int) *People { 35 c := make([]*Person, l) 36 37 return &People{ 38 theSlice: c, 39 } 40 } 41 42 func (m *People) Mutable() bool { 43 return m.mutable 44 } 45 46 func (m *People) Len() int { 47 if m == nil { 48 return 0 49 } 50 51 return len(m.theSlice) 52 } 53 54 func (m *People) Get(i int) *Person { 55 return m.theSlice[i] 56 } 57 58 func (m *People) AsMutable() *People { 59 if m == nil { 60 return nil 61 } 62 63 if m.Mutable() { 64 return m 65 } 66 67 res := m.dup() 68 res.mutable = true 69 70 return res 71 } 72 73 func (m *People) dup() *People { 74 resSlice := make([]*Person, len(m.theSlice)) 75 76 for i := range m.theSlice { 77 resSlice[i] = m.theSlice[i] 78 } 79 80 res := &People{ 81 theSlice: resSlice, 82 } 83 84 return res 85 } 86 87 func (m *People) AsImmutable(v *People) *People { 88 if m == nil { 89 return nil 90 } 91 92 if v == m { 93 return m 94 } 95 96 m.mutable = false 97 return m 98 } 99 100 func (m *People) Range() []*Person { 101 if m == nil { 102 return nil 103 } 104 105 return m.theSlice 106 } 107 108 func (m *People) WithMutable(f func(mi *People)) *People { 109 res := m.AsMutable() 110 f(res) 111 res = res.AsImmutable(m) 112 113 return res 114 } 115 116 func (m *People) WithImmutable(f func(mi *People)) *People { 117 prev := m.mutable 118 m.mutable = false 119 f(m) 120 m.mutable = prev 121 122 return m 123 } 124 125 func (m *People) Set(i int, v *Person) *People { 126 if m.mutable { 127 m.theSlice[i] = v 128 return m 129 } 130 131 res := m.dup() 132 res.theSlice[i] = v 133 134 return res 135 } 136 137 func (m *People) Append(v ...*Person) *People { 138 if m.mutable { 139 m.theSlice = append(m.theSlice, v...) 140 return m 141 } 142 143 res := m.dup() 144 res.theSlice = append(res.theSlice, v...) 145 146 return res 147 } 148 func (s *People) IsDeeplyNonMutable(seen map[interface{}]bool) bool { 149 if s == nil { 150 return true 151 } 152 153 if s.Mutable() { 154 return false 155 } 156 if s.Len() == 0 { 157 return true 158 } 159 160 if seen == nil { 161 return s.IsDeeplyNonMutable(make(map[interface{}]bool)) 162 } 163 164 if seen[s] { 165 return true 166 } 167 168 seen[s] = true 169 170 for _, v := range s.theSlice { 171 if v != nil && !v.IsDeeplyNonMutable(seen) { 172 return false 173 } 174 } 175 return true 176 } 177 178 // 179 // Person is an immutable type and has the following template: 180 // 181 // struct { 182 // Name string 183 // Age int 184 // } 185 // 186 type Person struct { 187 _Name string 188 _Age int 189 190 mutable bool 191 __tmpl _Imm_Person 192 } 193 194 var _ immutable.Immutable = new(Person) 195 var _ = new(Person).__tmpl 196 197 func (s *Person) AsMutable() *Person { 198 if s.Mutable() { 199 return s 200 } 201 202 res := *s 203 res.mutable = true 204 return &res 205 } 206 207 func (s *Person) AsImmutable(v *Person) *Person { 208 if s == nil { 209 return nil 210 } 211 212 if s == v { 213 return s 214 } 215 216 s.mutable = false 217 return s 218 } 219 220 func (s *Person) Mutable() bool { 221 return s.mutable 222 } 223 224 func (s *Person) WithMutable(f func(si *Person)) *Person { 225 res := s.AsMutable() 226 f(res) 227 res = res.AsImmutable(s) 228 229 return res 230 } 231 232 func (s *Person) WithImmutable(f func(si *Person)) *Person { 233 prev := s.mutable 234 s.mutable = false 235 f(s) 236 s.mutable = prev 237 238 return s 239 } 240 func (s *Person) IsDeeplyNonMutable(seen map[interface{}]bool) bool { 241 if s == nil { 242 return true 243 } 244 245 if s.Mutable() { 246 return false 247 } 248 249 if seen == nil { 250 return s.IsDeeplyNonMutable(make(map[interface{}]bool)) 251 } 252 253 if seen[s] { 254 return true 255 } 256 257 seen[s] = true 258 return true 259 } 260 func (s *Person) Name() string { 261 return s._Name 262 } 263 264 // SetName is the setter for Name() 265 func (s *Person) SetName(n string) *Person { 266 if s.mutable { 267 s._Name = n 268 return s 269 } 270 271 res := *s 272 res._Name = n 273 return &res 274 } 275 func (s *Person) Age() int { 276 return s._Age 277 } 278 279 // SetAge is the setter for Age() 280 func (s *Person) SetAge(n int) *Person { 281 if s.mutable { 282 s._Age = n 283 return s 284 } 285 286 res := *s 287 res._Age = n 288 return &res 289 }