Improve generating decorators (#1648)
This commit is contained in:
parent
d2e3c47e10
commit
71f0cae5ea
18 changed files with 99 additions and 98 deletions
|
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
_ "embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"go/format"
|
||||
|
|
@ -14,71 +15,8 @@ import (
|
|||
"github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
var srcTmpl = `
|
||||
package {{.Package}}
|
||||
|
||||
// Code generated by github.com/andig/cmd/tools/decorate.go. DO NOT EDIT.
|
||||
|
||||
import (
|
||||
"{{.API}}"
|
||||
)
|
||||
|
||||
{{define "case"}}
|
||||
{{- $combo := .Combo}}
|
||||
{{- $prefix := .Prefix}}
|
||||
{{- $idx := 0}}
|
||||
|
||||
{{- range $typ, $def := .Types}}
|
||||
{{- if gt $idx 0}} &&{{else}}{{$idx = 1}}{{end}} {{$def.VarName}} {{if contains $combo $typ}}!={{else}}=={{end}} nil
|
||||
{{- end}}:
|
||||
return &struct {
|
||||
{{.BaseType}}
|
||||
{{- range $typ, $def := .Types}}
|
||||
{{- if contains $combo $typ}}
|
||||
{{$typ}}
|
||||
{{- end}}
|
||||
{{- end}}
|
||||
}{
|
||||
{{.ShortBase}}: base,
|
||||
{{- range $typ, $def := .Types}}
|
||||
{{- if contains $combo $typ}}
|
||||
{{$def.ShortType}}: &{{$prefix}}{{$def.ShortType}}Impl{
|
||||
{{$def.VarName}}: {{$def.VarName}},
|
||||
},
|
||||
{{- end}}
|
||||
{{- end}}
|
||||
}
|
||||
{{- end -}}
|
||||
|
||||
func {{.Function}}(base {{.BaseType}}{{range ordered}}, {{.VarName}} func() {{slice .Signature 7}}{{end}}) {{.ReturnType}} {
|
||||
{{- $basetype := .BaseType}}
|
||||
{{- $shortbase := .ShortBase}}
|
||||
{{- $prefix := .Function}}
|
||||
{{- $types := .Types}}
|
||||
{{- $idx := 0}}
|
||||
switch {
|
||||
case {{- range $typ, $def := .Types}}
|
||||
{{- if gt $idx 0}} &&{{else}}{{$idx = 1}}{{end}} {{$def.VarName}} == nil
|
||||
{{- end}}:
|
||||
return base
|
||||
{{range $combo := .Combinations}}
|
||||
case {{- template "case" dict "BaseType" $basetype "Prefix" $prefix "ShortBase" $shortbase "Types" $types "Combo" $combo}}
|
||||
{{end}} }
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
{{range .Types -}}
|
||||
type {{$prefix}}{{.ShortType}}Impl struct {
|
||||
{{.VarName}} {{.Signature}}
|
||||
}
|
||||
|
||||
func (impl *{{$prefix}}{{.ShortType}}Impl) {{.Function}}{{slice .Signature 4}} {
|
||||
return impl.{{.VarName}}()
|
||||
}
|
||||
|
||||
{{end}}
|
||||
`
|
||||
//go:embed decorate.tpl
|
||||
var srcTmpl string
|
||||
|
||||
type dynamicType struct {
|
||||
typ, function, signature string
|
||||
|
|
@ -248,9 +186,7 @@ func main() {
|
|||
|
||||
formatted, err := format.Source([]byte(generated))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
fmt.Println(generated)
|
||||
os.Exit(2)
|
||||
formatted = []byte(generated)
|
||||
}
|
||||
|
||||
if _, err := out.Write(formatted); err != nil {
|
||||
|
|
|
|||
63
cmd/tools/decorate.tpl
Normal file
63
cmd/tools/decorate.tpl
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
package {{.Package}}
|
||||
|
||||
// Code generated by github.com/evcc-io/evcc/cmd/tools/decorate.go. DO NOT EDIT.
|
||||
|
||||
import (
|
||||
"{{.API}}"
|
||||
)
|
||||
|
||||
{{define "case"}}
|
||||
{{- $combo := .Combo}}
|
||||
{{- $prefix := .Prefix}}
|
||||
{{- $idx := 0}}
|
||||
|
||||
{{- range $typ, $def := .Types}}
|
||||
{{- if gt $idx 0}} &&{{else}}{{$idx = 1}}{{end}} {{$def.VarName}} {{if contains $combo $typ}}!={{else}}=={{end}} nil
|
||||
{{- end}}:
|
||||
return &struct {
|
||||
{{.BaseType}}
|
||||
{{- range $typ, $def := .Types}}
|
||||
{{- if contains $combo $typ}}
|
||||
{{$typ}}
|
||||
{{- end}}
|
||||
{{- end}}
|
||||
}{
|
||||
{{.ShortBase}}: base,
|
||||
{{- range $typ, $def := .Types}}
|
||||
{{- if contains $combo $typ}}
|
||||
{{$def.ShortType}}: &{{$prefix}}{{$def.ShortType}}Impl{
|
||||
{{$def.VarName}}: {{$def.VarName}},
|
||||
},
|
||||
{{- end}}
|
||||
{{- end}}
|
||||
}
|
||||
{{- end -}}
|
||||
|
||||
func {{.Function}}(base {{.BaseType}}{{range ordered}}, {{.VarName}} func() {{slice .Signature 7}}{{end}}) {{.ReturnType}} {
|
||||
{{- $basetype := .BaseType}}
|
||||
{{- $shortbase := .ShortBase}}
|
||||
{{- $prefix := .Function}}
|
||||
{{- $types := .Types}}
|
||||
{{- $idx := 0}}
|
||||
switch {
|
||||
case {{- range $typ, $def := .Types}}
|
||||
{{- if gt $idx 0}} &&{{else}}{{$idx = 1}}{{end}} {{$def.VarName}} == nil
|
||||
{{- end}}:
|
||||
return base
|
||||
{{range $combo := .Combinations}}
|
||||
case {{- template "case" dict "BaseType" $basetype "Prefix" $prefix "ShortBase" $shortbase "Types" $types "Combo" $combo}}
|
||||
{{end}} }
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
{{range .Types -}}
|
||||
type {{$prefix}}{{.ShortType}}Impl struct {
|
||||
{{.VarName}} {{.Signature}}
|
||||
}
|
||||
|
||||
func (impl *{{$prefix}}{{.ShortType}}Impl) {{.Function}}{{slice .Signature 4}} {
|
||||
return impl.{{.VarName}}()
|
||||
}
|
||||
|
||||
{{end}}
|
||||
Loading…
Add table
Add a link
Reference in a new issue