package main import ( "bytes" _ "embed" "errors" "flag" "fmt" "go/format" "os" "strconv" "text/template" ) //go:embed bind.go.tmpl var bind string //go:embed seq.go.tmpl var seq string var bindTmpl, seqTmpl *template.Template func main() { err := run() if err != nil { fmt.Fprint(os.Stderr, err) os.Exit(1) } } func run() error { bindPath := flag.String("bindpath", "", "bind file path") seqPath := flag.String("seqpath", "", "seq file path") maxBindLen := flag.Int("max", 0, "max bind length") pkg := flag.String("pkg", "", "output package") flag.Parse() if *bindPath == "" { return errors.New("bindpath required") } if *seqPath == "" { return errors.New("seqpath required") } if *maxBindLen == 0 { return errors.New("maxbind required") } if *pkg == "" { return errors.New("pkg required") } bindTmpl = template.Must(template.New("bind").Parse(bind)) seqTmpl = template.Must(template.New("seq").Parse(seq)) data := File{ Package: *pkg, Counter: Counter(*maxBindLen), } err := genCodeFile(*bindPath, bindTmpl, data) if err != nil { return err } return genCodeFile(*seqPath, seqTmpl, data) } func genCodeFile(path string, tmpl *template.Template, data any) error { var buf bytes.Buffer err := tmpl.Execute(&buf, data) if err != nil { return err } src, err := format.Source(buf.Bytes()) if err != nil { return err } return os.WriteFile(path, src, 0777) } type File struct { Package string Counter } // func (f File) Funcs() []Func { // counters := f.Max.Count() // funcs := make([]Func, len(counters)) // for i, counter := range counters { // funcs[i] = Func{ // Max: f.Max, // Length: counter, // } // } // return funcs // } type Func struct { Max Counter Length Counter } type Counter int func (c Counter) String() string { if c == 1 { return "" } return strconv.Itoa(int(c)) } func (c Counter) Int() int { return int(c) } func (c Counter) Next() Counter { return c + 1 } func (c Counter) Prev() Counter { return c - 1 } func (c Counter) Count() []Counter { a := make([]Counter, c) for i := range a { a[i] = Counter(i + 1) } return a }