This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
enhancements.go
104 lines (92 loc) · 3.23 KB
/
enhancements.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2018 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License. See the AUTHORS file
// for names of contributors.
package templates
func init() {
TemplateSources["50enhancements"] = `
{{- $v := . -}}
{{- $abstract := t $v "Abstract" -}}
{{- $Abstract := T $v "Abstract" -}}
{{- $ChildAt := T $v "At" -}}
{{- $Engine := t $v "Engine" -}}
{{- $NumChildren := T $v "Count" -}}
{{- $identify := t $v "Identify" -}}
{{- $Root := $v.Root -}}
{{- $TypeID := T $v "TypeID" -}}
{{- $WalkerFn := T $v "WalkerFn" -}}
{{- $wrap := t $v "Wrap" -}}
// ------ Type Enhancements ------
// {{ $abstract }} is a type-safe facade around e.Abstract.
type {{ $abstract }} struct {
delegate *e.Abstract
}
var _ {{ $Abstract }} = &{{ $abstract }}{}
// {{ $ChildAt }} implements {{ $Abstract }}.
func (a *{{ $abstract }}) {{ $ChildAt }}(index int) (ret {{ $Abstract }}) {
impl := a.delegate.ChildAt(index)
if impl == nil {
return nil
}
switch {{ $TypeID }}(impl.TypeID()) {
{{ range $s := Structs $v -}}
case {{ TypeID $s }}: ret = (*{{ $s }})(impl.Ptr());
case {{ TypeID $s }}Ptr: ret = *(**{{ $s }})(impl.Ptr());
{{- end }}
default:
ret = &{{ $abstract}}{impl}
}
return
}
// {{ $NumChildren }} implements {{ $Abstract }}.
func (a *{{ $abstract }}) {{ $NumChildren }} () int {
return a.delegate.NumChildren()
}
// {{ $TypeID }} implements {{ $Abstract }}.
func (a *{{ $abstract }}) {{ $TypeID }}() {{ $TypeID }} {
return {{ $TypeID }}(a.delegate.TypeID())
}
{{ range $s := Structs $v }}
// {{ $ChildAt }} implements {{ $Abstract }}.
func (x *{{ $s }}) {{ $ChildAt }}(index int) {{ $Abstract }} {
self := {{ $abstract }}{ {{ $Engine }}.Abstract(e.TypeID({{ TypeID $s }}), e.Ptr(x)) }
return self.{{ $ChildAt }}(index)
}
// {{ $NumChildren }} returns {{ len $s.Fields }}.
func (x *{{ $s }}) {{ $NumChildren }}() int { return {{ len $s.Fields }} }
// {{ $TypeID }} returns {{ TypeID $s }}.
func (*{{ $s }}) {{ $TypeID }}() {{ $TypeID }} { return {{ TypeID $s }} }
// Walk{{ $Root }} visits the receiver with the provided callback.
func (x *{{ $s }}) Walk{{ $Root }}(fn {{ $WalkerFn }}) (_ *{{ $s }}, changed bool, err error) {
var y e.Ptr
_, y, changed, err = {{ $Engine }}.Execute(fn, e.TypeID({{ TypeID $s }}), e.Ptr(x), e.TypeID({{ TypeID $s }}))
if err != nil {
return nil, false, err
}
return (*{{ $s }})(y), changed, nil
}
{{ end }}
// Walk{{ $Root }} visits the receiver with the provided callback.
func Walk{{ $Root }}(x {{ $Root }}, fn {{ $WalkerFn }}) (_ {{ $Root }}, changed bool, err error) {
id, ptr := {{ $identify }}(x)
id, ptr, changed, err = {{ $Engine }}.Execute(fn, id, ptr, e.TypeID({{ TypeID $Root }}))
if err != nil {
return nil, false, err
}
if changed {
return {{ $wrap }}(id, ptr), true, nil
}
return x, false, nil
}
`
}