Skip to content

Commit 159e329

Browse files
authored
chore(gen-*): Move templates into external files (#121)
This moves the templates used by gen-atomicint and gen-atomicwrapper into external files embedded into the corresponding gen binary with go:embed.
1 parent 9680036 commit 159e329

File tree

4 files changed

+249
-240
lines changed

4 files changed

+249
-240
lines changed

internal/gen-atomicint/main.go

Lines changed: 7 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ package main
2828

2929
import (
3030
"bytes"
31+
"embed"
3132
"errors"
3233
"flag"
3334
"fmt"
@@ -93,7 +94,7 @@ func run(args []string) error {
9394
}
9495

9596
var buff bytes.Buffer
96-
if err := _tmpl.Execute(&buff, data); err != nil {
97+
if err := _tmpl.ExecuteTemplate(&buff, "wrapper.tmpl", data); err != nil {
9798
return fmt.Errorf("render template: %v", err)
9899
}
99100

@@ -102,127 +103,14 @@ func run(args []string) error {
102103
return fmt.Errorf("reformat source: %v", err)
103104
}
104105

106+
io.WriteString(w, "// @generated Code generated by gen-atomicint.\n\n")
105107
_, err = w.Write(bs)
106108
return err
107109
}
108110

109-
var _tmpl = template.Must(template.New("value.go").Parse(`// @generated Code generated by gen-atomicint.
111+
var (
112+
//go:embed *.tmpl
113+
_tmplFS embed.FS
110114

111-
// Copyright (c) 2020-{{.ToYear}} Uber Technologies, Inc.
112-
//
113-
// Permission is hereby granted, free of charge, to any person obtaining a copy
114-
// of this software and associated documentation files (the "Software"), to deal
115-
// in the Software without restriction, including without limitation the rights
116-
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
117-
// copies of the Software, and to permit persons to whom the Software is
118-
// furnished to do so, subject to the following conditions:
119-
//
120-
// The above copyright notice and this permission notice shall be included in
121-
// all copies or substantial portions of the Software.
122-
//
123-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
124-
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
125-
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
126-
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
127-
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
128-
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
129-
// THE SOFTWARE.
130-
131-
package atomic
132-
133-
import (
134-
"encoding/json"
135-
"strconv"
136-
"sync/atomic"
115+
_tmpl = template.Must(template.New("atomicint").ParseFS(_tmplFS, "*.tmpl"))
137116
)
138-
139-
// {{ .Name }} is an atomic wrapper around {{ .Wrapped }}.
140-
type {{ .Name }} struct {
141-
_ nocmp // disallow non-atomic comparison
142-
143-
v {{ .Wrapped }}
144-
}
145-
146-
// New{{ .Name }} creates a new {{ .Name }}.
147-
func New{{ .Name }}(val {{ .Wrapped }}) *{{ .Name }} {
148-
return &{{ .Name }}{v: val}
149-
}
150-
151-
// Load atomically loads the wrapped value.
152-
func (i *{{ .Name }}) Load() {{ .Wrapped }} {
153-
return atomic.Load{{ .Name }}(&i.v)
154-
}
155-
156-
// Add atomically adds to the wrapped {{ .Wrapped }} and returns the new value.
157-
func (i *{{ .Name }}) Add(delta {{ .Wrapped }}) {{ .Wrapped }} {
158-
return atomic.Add{{ .Name }}(&i.v, delta)
159-
}
160-
161-
// Sub atomically subtracts from the wrapped {{ .Wrapped }} and returns the new value.
162-
func (i *{{ .Name }}) Sub(delta {{ .Wrapped }}) {{ .Wrapped }} {
163-
return atomic.Add{{ .Name }}(&i.v,
164-
{{- if .Unsigned -}}
165-
^(delta - 1)
166-
{{- else -}}
167-
-delta
168-
{{- end -}}
169-
)
170-
}
171-
172-
// Inc atomically increments the wrapped {{ .Wrapped }} and returns the new value.
173-
func (i *{{ .Name }}) Inc() {{ .Wrapped }} {
174-
return i.Add(1)
175-
}
176-
177-
// Dec atomically decrements the wrapped {{ .Wrapped }} and returns the new value.
178-
func (i *{{ .Name }}) Dec() {{ .Wrapped }} {
179-
return i.Sub(1)
180-
}
181-
182-
// CAS is an atomic compare-and-swap.
183-
//
184-
// Deprecated: Use CompareAndSwap.
185-
func (i *{{ .Name }}) CAS(old, new {{ .Wrapped }}) (swapped bool) {
186-
return i.CompareAndSwap(old, new)
187-
}
188-
189-
// CompareAndSwap is an atomic compare-and-swap.
190-
func (i *{{ .Name }}) CompareAndSwap(old, new {{ .Wrapped }}) (swapped bool) {
191-
return atomic.CompareAndSwap{{ .Name }}(&i.v, old, new)
192-
}
193-
194-
// Store atomically stores the passed value.
195-
func (i *{{ .Name }}) Store(val {{ .Wrapped }}) {
196-
atomic.Store{{ .Name }}(&i.v, val)
197-
}
198-
199-
// Swap atomically swaps the wrapped {{ .Wrapped }} and returns the old value.
200-
func (i *{{ .Name }}) Swap(val {{ .Wrapped }}) (old {{ .Wrapped }}) {
201-
return atomic.Swap{{ .Name }}(&i.v, val)
202-
}
203-
204-
// MarshalJSON encodes the wrapped {{ .Wrapped }} into JSON.
205-
func (i *{{ .Name }}) MarshalJSON() ([]byte, error) {
206-
return json.Marshal(i.Load())
207-
}
208-
209-
// UnmarshalJSON decodes JSON into the wrapped {{ .Wrapped }}.
210-
func (i *{{ .Name }}) UnmarshalJSON(b []byte) error {
211-
var v {{ .Wrapped }}
212-
if err := json.Unmarshal(b, &v); err != nil {
213-
return err
214-
}
215-
i.Store(v)
216-
return nil
217-
}
218-
219-
// String encodes the wrapped value as a string.
220-
func (i *{{ .Name }}) String() string {
221-
v := i.Load()
222-
{{ if .Unsigned -}}
223-
return strconv.FormatUint(uint64(v), 10)
224-
{{- else -}}
225-
return strconv.FormatInt(int64(v), 10)
226-
{{- end }}
227-
}
228-
`))

internal/gen-atomicint/wrapper.tmpl

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Copyright (c) 2020-{{.ToYear}} Uber Technologies, Inc.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
package atomic
22+
23+
import (
24+
"encoding/json"
25+
"strconv"
26+
"sync/atomic"
27+
)
28+
29+
// {{ .Name }} is an atomic wrapper around {{ .Wrapped }}.
30+
type {{ .Name }} struct {
31+
_ nocmp // disallow non-atomic comparison
32+
33+
v {{ .Wrapped }}
34+
}
35+
36+
// New{{ .Name }} creates a new {{ .Name }}.
37+
func New{{ .Name }}(val {{ .Wrapped }}) *{{ .Name }} {
38+
return &{{ .Name }}{v: val}
39+
}
40+
41+
// Load atomically loads the wrapped value.
42+
func (i *{{ .Name }}) Load() {{ .Wrapped }} {
43+
return atomic.Load{{ .Name }}(&i.v)
44+
}
45+
46+
// Add atomically adds to the wrapped {{ .Wrapped }} and returns the new value.
47+
func (i *{{ .Name }}) Add(delta {{ .Wrapped }}) {{ .Wrapped }} {
48+
return atomic.Add{{ .Name }}(&i.v, delta)
49+
}
50+
51+
// Sub atomically subtracts from the wrapped {{ .Wrapped }} and returns the new value.
52+
func (i *{{ .Name }}) Sub(delta {{ .Wrapped }}) {{ .Wrapped }} {
53+
return atomic.Add{{ .Name }}(&i.v,
54+
{{- if .Unsigned -}}
55+
^(delta - 1)
56+
{{- else -}}
57+
-delta
58+
{{- end -}}
59+
)
60+
}
61+
62+
// Inc atomically increments the wrapped {{ .Wrapped }} and returns the new value.
63+
func (i *{{ .Name }}) Inc() {{ .Wrapped }} {
64+
return i.Add(1)
65+
}
66+
67+
// Dec atomically decrements the wrapped {{ .Wrapped }} and returns the new value.
68+
func (i *{{ .Name }}) Dec() {{ .Wrapped }} {
69+
return i.Sub(1)
70+
}
71+
72+
// CAS is an atomic compare-and-swap.
73+
//
74+
// Deprecated: Use CompareAndSwap.
75+
func (i *{{ .Name }}) CAS(old, new {{ .Wrapped }}) (swapped bool) {
76+
return i.CompareAndSwap(old, new)
77+
}
78+
79+
// CompareAndSwap is an atomic compare-and-swap.
80+
func (i *{{ .Name }}) CompareAndSwap(old, new {{ .Wrapped }}) (swapped bool) {
81+
return atomic.CompareAndSwap{{ .Name }}(&i.v, old, new)
82+
}
83+
84+
// Store atomically stores the passed value.
85+
func (i *{{ .Name }}) Store(val {{ .Wrapped }}) {
86+
atomic.Store{{ .Name }}(&i.v, val)
87+
}
88+
89+
// Swap atomically swaps the wrapped {{ .Wrapped }} and returns the old value.
90+
func (i *{{ .Name }}) Swap(val {{ .Wrapped }}) (old {{ .Wrapped }}) {
91+
return atomic.Swap{{ .Name }}(&i.v, val)
92+
}
93+
94+
// MarshalJSON encodes the wrapped {{ .Wrapped }} into JSON.
95+
func (i *{{ .Name }}) MarshalJSON() ([]byte, error) {
96+
return json.Marshal(i.Load())
97+
}
98+
99+
// UnmarshalJSON decodes JSON into the wrapped {{ .Wrapped }}.
100+
func (i *{{ .Name }}) UnmarshalJSON(b []byte) error {
101+
var v {{ .Wrapped }}
102+
if err := json.Unmarshal(b, &v); err != nil {
103+
return err
104+
}
105+
i.Store(v)
106+
return nil
107+
}
108+
109+
// String encodes the wrapped value as a string.
110+
func (i *{{ .Name }}) String() string {
111+
v := i.Load()
112+
{{ if .Unsigned -}}
113+
return strconv.FormatUint(uint64(v), 10)
114+
{{- else -}}
115+
return strconv.FormatInt(int64(v), 10)
116+
{{- end }}
117+
}

0 commit comments

Comments
 (0)