1 // Copyright (c) 2020-2022 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 // gen-atomicint generates an atomic wrapper around an integer type.
22 //
23 // gen-atomicint -name Int32 -wrapped int32 -file out.go
24 //
25 // The generated wrapper will use the functions in the sync/atomic package
26 // named after the generated type.
27 package main
28 29 import (
30 "bytes"
31 "embed"
32 "errors"
33 "flag"
34 "fmt"
35 "go/format"
36 "io"
37 "log"
38 "os"
39 "text/template"
40 "time"
41 )
42 43 func main() {
44 log.SetFlags(0)
45 if err := run(os.Args[1:]); err != nil {
46 log.Fatalf("%+v", err)
47 }
48 }
49 50 func run(args []string) error {
51 var opts struct {
52 Name string
53 Wrapped string
54 File string
55 Unsigned bool
56 }
57 58 flag := flag.NewFlagSet("gen-atomicint", flag.ContinueOnError)
59 60 flag.StringVar(&opts.Name, "name", "", "name of the generated type (e.g. Int32)")
61 flag.StringVar(&opts.Wrapped, "wrapped", "", "name of the wrapped type (e.g. int32)")
62 flag.StringVar(&opts.File, "file", "", "output file path (default: stdout)")
63 flag.BoolVar(&opts.Unsigned, "unsigned", false, "whether the type is unsigned")
64 65 if err := flag.Parse(args); err != nil {
66 return err
67 }
68 69 if len(opts.Name) == 0 || len(opts.Wrapped) == 0 {
70 return errors.New("flags -name and -wrapped are required")
71 }
72 73 var w io.Writer = os.Stdout
74 if file := opts.File; len(file) > 0 {
75 f, err := os.Create(file)
76 if err != nil {
77 return fmt.Errorf("create %q: %v", file, err)
78 }
79 defer f.Close()
80 81 w = f
82 }
83 84 data := struct {
85 Name string
86 Wrapped string
87 Unsigned bool
88 ToYear int
89 }{
90 Name: opts.Name,
91 Wrapped: opts.Wrapped,
92 Unsigned: opts.Unsigned,
93 ToYear: time.Now().Year(),
94 }
95 96 var buff bytes.Buffer
97 if err := _tmpl.ExecuteTemplate(&buff, "wrapper.tmpl", data); err != nil {
98 return fmt.Errorf("render template: %v", err)
99 }
100 101 bs, err := format.Source(buff.Bytes())
102 if err != nil {
103 return fmt.Errorf("reformat source: %v", err)
104 }
105 106 io.WriteString(w, "// @generated Code generated by gen-atomicint.\n\n")
107 _, err = w.Write(bs)
108 return err
109 }
110 111 var (
112 //go:embed *.tmpl
113 _tmplFS embed.FS
114 115 _tmpl = template.Must(template.New("atomicint").ParseFS(_tmplFS, "*.tmpl"))
116 )
117