-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.go
106 lines (90 loc) · 2.94 KB
/
main.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
105
106
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"text/template"
)
type listItem struct {
Name string
Suffix string
}
var list = []listItem{
{Name: "Buffersink"},
{Name: "Buffersrc"},
{Name: "CodecContext"},
{Name: "CodecContext", Suffix: "2"},
{Name: "CodecHardwareConfigMethod"},
{Name: "Dictionary"},
{Name: "FilterCommand"},
{Name: "FormatContext"},
{Name: "FormatContextCtx"},
{Name: "FormatEvent"},
{Name: "IOContext"},
{Name: "IOFormat"},
{Name: "Packet"},
{Name: "Seek"},
{Name: "SoftwareScaleContext"},
{Name: "StreamEvent"},
}
var tmpl = `// Code generated by astiav. DO NOT EDIT.
package astiav
import (
"github.com/asticode/go-astikit"
)
{{ range $val := . }}
type {{ $val.Name }}Flags{{ $val.Suffix }} astikit.BitFlags
func New{{ $val.Name }}Flags{{ $val.Suffix }}(fs ...{{ $val.Name }}Flag{{ $val.Suffix }}) {{ $val.Name }}Flags{{ $val.Suffix }} {
o := {{ $val.Name }}Flags{{ $val.Suffix }}(0)
for _, f := range fs {
o = o.Add(f)
}
return o
}
func (fs {{ $val.Name }}Flags{{ $val.Suffix }}) Add(f {{ $val.Name }}Flag{{ $val.Suffix }}) {{ $val.Name }}Flags{{ $val.Suffix }} {
return {{ $val.Name }}Flags{{ $val.Suffix }}(astikit.BitFlags(fs).Add(uint64(f)))
}
func (fs {{ $val.Name }}Flags{{ $val.Suffix }}) Del(f {{ $val.Name }}Flag{{ $val.Suffix }}) {{ $val.Name }}Flags{{ $val.Suffix }} {
return {{ $val.Name }}Flags{{ $val.Suffix }}(astikit.BitFlags(fs).Del(uint64(f)))
}
func (fs {{ $val.Name }}Flags{{ $val.Suffix }}) Has(f {{ $val.Name }}Flag{{ $val.Suffix }}) bool { return astikit.BitFlags(fs).Has(uint64(f)) }
{{ end }}`
var tmplTest = `// Code generated by astiav. DO NOT EDIT.
package astiav
import (
"testing"
"github.com/stretchr/testify/require"
)
{{ range $val := . }}
func Test{{ $val.Name }}Flags{{ $val.Suffix }}(t *testing.T) {
fs := New{{ $val.Name }}Flags{{ $val.Suffix }}({{ $val.Name }}Flag{{ $val.Suffix }}(1))
require.True(t, fs.Has({{ $val.Name }}Flag{{ $val.Suffix }}(1)))
fs = fs.Add({{ $val.Name }}Flag{{ $val.Suffix }}(2))
require.True(t, fs.Has({{ $val.Name }}Flag{{ $val.Suffix }}(2)))
fs = fs.Del({{ $val.Name }}Flag{{ $val.Suffix }}(2))
require.False(t, fs.Has({{ $val.Name }}Flag{{ $val.Suffix }}(2)))
}
{{ end }}`
func main() {
dir, err := os.Getwd()
if err != nil {
log.Fatal(fmt.Errorf("main: getting working directory failed: %w", err))
}
f, err := os.Create(filepath.Join(dir, "flags.go"))
if err != nil {
log.Fatal(fmt.Errorf("main: creating file failed: %w", err))
}
defer f.Close()
if err = template.Must(template.New("tmpl").Parse(tmpl)).Execute(f, list); err != nil {
log.Fatal(fmt.Errorf("main: executing template failed: %w", err))
}
ft, err := os.Create(filepath.Join(dir, "flags_test.go"))
if err != nil {
log.Fatal(fmt.Errorf("main: creating test file failed: %w", err))
}
defer ft.Close()
if err = template.Must(template.New("tmpl").Parse(tmplTest)).Execute(ft, list); err != nil {
log.Fatal(fmt.Errorf("main: executing template failed: %w", err))
}
}