From 159e329dd89a1dcdc41df85041312fa497d308b2 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Tue, 6 Sep 2022 10:26:51 -0700 Subject: [PATCH] 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. --- internal/gen-atomicint/main.go | 126 ++--------------------- internal/gen-atomicint/wrapper.tmpl | 117 ++++++++++++++++++++++ internal/gen-atomicwrapper/main.go | 128 ++---------------------- internal/gen-atomicwrapper/wrapper.tmpl | 118 ++++++++++++++++++++++ 4 files changed, 249 insertions(+), 240 deletions(-) create mode 100644 internal/gen-atomicint/wrapper.tmpl create mode 100644 internal/gen-atomicwrapper/wrapper.tmpl diff --git a/internal/gen-atomicint/main.go b/internal/gen-atomicint/main.go index 6132691..719fe9c 100644 --- a/internal/gen-atomicint/main.go +++ b/internal/gen-atomicint/main.go @@ -28,6 +28,7 @@ package main import ( "bytes" + "embed" "errors" "flag" "fmt" @@ -93,7 +94,7 @@ func run(args []string) error { } var buff bytes.Buffer - if err := _tmpl.Execute(&buff, data); err != nil { + if err := _tmpl.ExecuteTemplate(&buff, "wrapper.tmpl", data); err != nil { return fmt.Errorf("render template: %v", err) } @@ -102,127 +103,14 @@ func run(args []string) error { return fmt.Errorf("reformat source: %v", err) } + io.WriteString(w, "// @generated Code generated by gen-atomicint.\n\n") _, err = w.Write(bs) return err } -var _tmpl = template.Must(template.New("value.go").Parse(`// @generated Code generated by gen-atomicint. +var ( + //go:embed *.tmpl + _tmplFS embed.FS -// Copyright (c) 2020-{{.ToYear}} Uber Technologies, Inc. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -package atomic - -import ( - "encoding/json" - "strconv" - "sync/atomic" + _tmpl = template.Must(template.New("atomicint").ParseFS(_tmplFS, "*.tmpl")) ) - -// {{ .Name }} is an atomic wrapper around {{ .Wrapped }}. -type {{ .Name }} struct { - _ nocmp // disallow non-atomic comparison - - v {{ .Wrapped }} -} - -// New{{ .Name }} creates a new {{ .Name }}. -func New{{ .Name }}(val {{ .Wrapped }}) *{{ .Name }} { - return &{{ .Name }}{v: val} -} - -// Load atomically loads the wrapped value. -func (i *{{ .Name }}) Load() {{ .Wrapped }} { - return atomic.Load{{ .Name }}(&i.v) -} - -// Add atomically adds to the wrapped {{ .Wrapped }} and returns the new value. -func (i *{{ .Name }}) Add(delta {{ .Wrapped }}) {{ .Wrapped }} { - return atomic.Add{{ .Name }}(&i.v, delta) -} - -// Sub atomically subtracts from the wrapped {{ .Wrapped }} and returns the new value. -func (i *{{ .Name }}) Sub(delta {{ .Wrapped }}) {{ .Wrapped }} { - return atomic.Add{{ .Name }}(&i.v, - {{- if .Unsigned -}} - ^(delta - 1) - {{- else -}} - -delta - {{- end -}} - ) -} - -// Inc atomically increments the wrapped {{ .Wrapped }} and returns the new value. -func (i *{{ .Name }}) Inc() {{ .Wrapped }} { - return i.Add(1) -} - -// Dec atomically decrements the wrapped {{ .Wrapped }} and returns the new value. -func (i *{{ .Name }}) Dec() {{ .Wrapped }} { - return i.Sub(1) -} - -// CAS is an atomic compare-and-swap. -// -// Deprecated: Use CompareAndSwap. -func (i *{{ .Name }}) CAS(old, new {{ .Wrapped }}) (swapped bool) { - return i.CompareAndSwap(old, new) -} - -// CompareAndSwap is an atomic compare-and-swap. -func (i *{{ .Name }}) CompareAndSwap(old, new {{ .Wrapped }}) (swapped bool) { - return atomic.CompareAndSwap{{ .Name }}(&i.v, old, new) -} - -// Store atomically stores the passed value. -func (i *{{ .Name }}) Store(val {{ .Wrapped }}) { - atomic.Store{{ .Name }}(&i.v, val) -} - -// Swap atomically swaps the wrapped {{ .Wrapped }} and returns the old value. -func (i *{{ .Name }}) Swap(val {{ .Wrapped }}) (old {{ .Wrapped }}) { - return atomic.Swap{{ .Name }}(&i.v, val) -} - -// MarshalJSON encodes the wrapped {{ .Wrapped }} into JSON. -func (i *{{ .Name }}) MarshalJSON() ([]byte, error) { - return json.Marshal(i.Load()) -} - -// UnmarshalJSON decodes JSON into the wrapped {{ .Wrapped }}. -func (i *{{ .Name }}) UnmarshalJSON(b []byte) error { - var v {{ .Wrapped }} - if err := json.Unmarshal(b, &v); err != nil { - return err - } - i.Store(v) - return nil -} - -// String encodes the wrapped value as a string. -func (i *{{ .Name }}) String() string { - v := i.Load() - {{ if .Unsigned -}} - return strconv.FormatUint(uint64(v), 10) - {{- else -}} - return strconv.FormatInt(int64(v), 10) - {{- end }} -} -`)) diff --git a/internal/gen-atomicint/wrapper.tmpl b/internal/gen-atomicint/wrapper.tmpl new file mode 100644 index 0000000..51a0e3b --- /dev/null +++ b/internal/gen-atomicint/wrapper.tmpl @@ -0,0 +1,117 @@ +// Copyright (c) 2020-{{.ToYear}} Uber Technologies, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +package atomic + +import ( + "encoding/json" + "strconv" + "sync/atomic" +) + +// {{ .Name }} is an atomic wrapper around {{ .Wrapped }}. +type {{ .Name }} struct { + _ nocmp // disallow non-atomic comparison + + v {{ .Wrapped }} +} + +// New{{ .Name }} creates a new {{ .Name }}. +func New{{ .Name }}(val {{ .Wrapped }}) *{{ .Name }} { + return &{{ .Name }}{v: val} +} + +// Load atomically loads the wrapped value. +func (i *{{ .Name }}) Load() {{ .Wrapped }} { + return atomic.Load{{ .Name }}(&i.v) +} + +// Add atomically adds to the wrapped {{ .Wrapped }} and returns the new value. +func (i *{{ .Name }}) Add(delta {{ .Wrapped }}) {{ .Wrapped }} { + return atomic.Add{{ .Name }}(&i.v, delta) +} + +// Sub atomically subtracts from the wrapped {{ .Wrapped }} and returns the new value. +func (i *{{ .Name }}) Sub(delta {{ .Wrapped }}) {{ .Wrapped }} { + return atomic.Add{{ .Name }}(&i.v, + {{- if .Unsigned -}} + ^(delta - 1) + {{- else -}} + -delta + {{- end -}} + ) +} + +// Inc atomically increments the wrapped {{ .Wrapped }} and returns the new value. +func (i *{{ .Name }}) Inc() {{ .Wrapped }} { + return i.Add(1) +} + +// Dec atomically decrements the wrapped {{ .Wrapped }} and returns the new value. +func (i *{{ .Name }}) Dec() {{ .Wrapped }} { + return i.Sub(1) +} + +// CAS is an atomic compare-and-swap. +// +// Deprecated: Use CompareAndSwap. +func (i *{{ .Name }}) CAS(old, new {{ .Wrapped }}) (swapped bool) { + return i.CompareAndSwap(old, new) +} + +// CompareAndSwap is an atomic compare-and-swap. +func (i *{{ .Name }}) CompareAndSwap(old, new {{ .Wrapped }}) (swapped bool) { + return atomic.CompareAndSwap{{ .Name }}(&i.v, old, new) +} + +// Store atomically stores the passed value. +func (i *{{ .Name }}) Store(val {{ .Wrapped }}) { + atomic.Store{{ .Name }}(&i.v, val) +} + +// Swap atomically swaps the wrapped {{ .Wrapped }} and returns the old value. +func (i *{{ .Name }}) Swap(val {{ .Wrapped }}) (old {{ .Wrapped }}) { + return atomic.Swap{{ .Name }}(&i.v, val) +} + +// MarshalJSON encodes the wrapped {{ .Wrapped }} into JSON. +func (i *{{ .Name }}) MarshalJSON() ([]byte, error) { + return json.Marshal(i.Load()) +} + +// UnmarshalJSON decodes JSON into the wrapped {{ .Wrapped }}. +func (i *{{ .Name }}) UnmarshalJSON(b []byte) error { + var v {{ .Wrapped }} + if err := json.Unmarshal(b, &v); err != nil { + return err + } + i.Store(v) + return nil +} + +// String encodes the wrapped value as a string. +func (i *{{ .Name }}) String() string { + v := i.Load() + {{ if .Unsigned -}} + return strconv.FormatUint(uint64(v), 10) + {{- else -}} + return strconv.FormatInt(int64(v), 10) + {{- end }} +} diff --git a/internal/gen-atomicwrapper/main.go b/internal/gen-atomicwrapper/main.go index b85501c..aec93b8 100644 --- a/internal/gen-atomicwrapper/main.go +++ b/internal/gen-atomicwrapper/main.go @@ -54,6 +54,7 @@ package main import ( "bytes" + "embed" "errors" "flag" "fmt" @@ -183,7 +184,7 @@ func run(args []string) error { sort.Strings([]string(opts.Imports)) var buff bytes.Buffer - if err := _tmpl.Execute(&buff, opts); err != nil { + if err := _tmpl.ExecuteTemplate(&buff, "wrapper.tmpl", opts); err != nil { return fmt.Errorf("render template: %v", err) } @@ -192,129 +193,14 @@ func run(args []string) error { return fmt.Errorf("reformat source: %v", err) } + io.WriteString(w, "// @generated Code generated by gen-atomicwrapper.\n\n") _, err = w.Write(bs) return err } -var _tmpl = template.Must(template.New("int.go").Parse(`// @generated Code generated by gen-atomicwrapper. +var ( + //go:embed *.tmpl + _tmplFS embed.FS -// Copyright (c) 2020-{{.ToYear}} Uber Technologies, Inc. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -package atomic - -{{ with .Imports }} -import ( - {{ range . -}} - {{ printf "%q" . }} - {{ end }} + _tmpl = template.Must(template.New("atomicwrapper").ParseFS(_tmplFS, "*.tmpl")) ) -{{ end }} - -// {{ .Name }} is an atomic type-safe wrapper for {{ .Type }} values. -type {{ .Name }} struct{ - _ nocmp // disallow non-atomic comparison - - v {{ .Wrapped }} -} - -var _zero{{ .Name }} {{ .Type }} - - -// New{{ .Name }} creates a new {{ .Name }}. -func New{{ .Name }}(val {{ .Type }}) *{{ .Name }} { - x := &{{ .Name }}{} - if val != _zero{{ .Name }} { - x.Store(val) - } - return x -} - -// Load atomically loads the wrapped {{ .Type }}. -func (x *{{ .Name }}) Load() {{ .Type }} { - {{ if .Unpack -}} - return {{ .Unpack }}(x.v.Load()) - {{- else -}} - if v := x.v.Load(); v != nil { - return v.({{ .Type }}) - } - return _zero{{ .Name }} - {{- end }} -} - -// Store atomically stores the passed {{ .Type }}. -func (x *{{ .Name }}) Store(val {{ .Type }}) { - {{ if .Pack -}} - x.v.Store({{ .Pack }}(val)) - {{- else -}} - x.v.Store(val) - {{- end }} -} - -{{ if .CAS -}} - // CAS is an atomic compare-and-swap for {{ .Type }} values. - // - // Deprecated: Use CompareAndSwap. - func (x *{{ .Name }}) CAS(old, new {{ .Type }}) (swapped bool) { - return x.CompareAndSwap(old, new) - } -{{- end }} - -{{ if .CompareAndSwap -}} - // CompareAndSwap is an atomic compare-and-swap for {{ .Type }} values. - func (x *{{ .Name }}) CompareAndSwap(old, new {{ .Type }}) (swapped bool) { - {{ if .Pack -}} - return x.v.CompareAndSwap({{ .Pack }}(old), {{ .Pack }}(new)) - {{- else -}}{{- /* assume go.uber.org/atomic.Value */ -}} - return x.v.CompareAndSwap(old, new) - {{- end }} - } -{{- end }} - -{{ if .Swap -}} - // Swap atomically stores the given {{ .Type }} and returns the old - // value. - func (x *{{ .Name }}) Swap(val {{ .Type }}) (old {{ .Type }}) { - {{ if .Pack -}} - return {{ .Unpack }}(x.v.Swap({{ .Pack }}(val))) - {{- else -}}{{- /* assume go.uber.org/atomic.Value */ -}} - return x.v.Swap(val).({{ .Type }}) - {{- end }} - } -{{- end }} - -{{ if .JSON -}} - // MarshalJSON encodes the wrapped {{ .Type }} into JSON. - func (x *{{ .Name }}) MarshalJSON() ([]byte, error) { - return json.Marshal(x.Load()) - } - - // UnmarshalJSON decodes a {{ .Type }} from JSON. - func (x *{{ .Name }}) UnmarshalJSON(b []byte) error { - var v {{ .Type }} - if err := json.Unmarshal(b, &v); err != nil { - return err - } - x.Store(v) - return nil - } -{{- end }} - -`)) diff --git a/internal/gen-atomicwrapper/wrapper.tmpl b/internal/gen-atomicwrapper/wrapper.tmpl new file mode 100644 index 0000000..2e078e2 --- /dev/null +++ b/internal/gen-atomicwrapper/wrapper.tmpl @@ -0,0 +1,118 @@ +// Copyright (c) 2020-{{.ToYear}} Uber Technologies, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +package atomic + +{{ with .Imports }} +import ( + {{ range . -}} + {{ printf "%q" . }} + {{ end }} +) +{{ end }} + +// {{ .Name }} is an atomic type-safe wrapper for {{ .Type }} values. +type {{ .Name }} struct{ + _ nocmp // disallow non-atomic comparison + + v {{ .Wrapped }} +} + +var _zero{{ .Name }} {{ .Type }} + + +// New{{ .Name }} creates a new {{ .Name }}. +func New{{ .Name }}(val {{ .Type }}) *{{ .Name }} { + x := &{{ .Name }}{} + if val != _zero{{ .Name }} { + x.Store(val) + } + return x +} + +// Load atomically loads the wrapped {{ .Type }}. +func (x *{{ .Name }}) Load() {{ .Type }} { + {{ if .Unpack -}} + return {{ .Unpack }}(x.v.Load()) + {{- else -}} + if v := x.v.Load(); v != nil { + return v.({{ .Type }}) + } + return _zero{{ .Name }} + {{- end }} +} + +// Store atomically stores the passed {{ .Type }}. +func (x *{{ .Name }}) Store(val {{ .Type }}) { + {{ if .Pack -}} + x.v.Store({{ .Pack }}(val)) + {{- else -}} + x.v.Store(val) + {{- end }} +} + +{{ if .CAS -}} + // CAS is an atomic compare-and-swap for {{ .Type }} values. + // + // Deprecated: Use CompareAndSwap. + func (x *{{ .Name }}) CAS(old, new {{ .Type }}) (swapped bool) { + return x.CompareAndSwap(old, new) + } +{{- end }} + +{{ if .CompareAndSwap -}} + // CompareAndSwap is an atomic compare-and-swap for {{ .Type }} values. + func (x *{{ .Name }}) CompareAndSwap(old, new {{ .Type }}) (swapped bool) { + {{ if .Pack -}} + return x.v.CompareAndSwap({{ .Pack }}(old), {{ .Pack }}(new)) + {{- else -}}{{- /* assume go.uber.org/atomic.Value */ -}} + return x.v.CompareAndSwap(old, new) + {{- end }} + } +{{- end }} + +{{ if .Swap -}} + // Swap atomically stores the given {{ .Type }} and returns the old + // value. + func (x *{{ .Name }}) Swap(val {{ .Type }}) (old {{ .Type }}) { + {{ if .Pack -}} + return {{ .Unpack }}(x.v.Swap({{ .Pack }}(val))) + {{- else -}}{{- /* assume go.uber.org/atomic.Value */ -}} + return x.v.Swap(val).({{ .Type }}) + {{- end }} + } +{{- end }} + +{{ if .JSON -}} + // MarshalJSON encodes the wrapped {{ .Type }} into JSON. + func (x *{{ .Name }}) MarshalJSON() ([]byte, error) { + return json.Marshal(x.Load()) + } + + // UnmarshalJSON decodes a {{ .Type }} from JSON. + func (x *{{ .Name }}) UnmarshalJSON(b []byte) error { + var v {{ .Type }} + if err := json.Unmarshal(b, &v); err != nil { + return err + } + x.Store(v) + return nil + } +{{- end }}