Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Aug 16, 2024
1 parent e777c4a commit bdda2b8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
7 changes: 6 additions & 1 deletion charger/keba-modbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ func init() {
registry.Add("keba-modbus", NewKebaFromConfig)
}

//go:generate go run ../cmd/tools/decorate.go -f decorateKeba -b *Keba -r api.Charger -t "api.Meter,CurrentPower,func() (float64, error)" -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" -t "api.PhaseCurrents,Currents,func() (float64, float64, float64, error)" -t "api.Identifier,Identify,func() (string, error)" -t "api.StatusReasoner,StatusReason,func() (api.Reason, error)" -t "api.PhaseSwitcher,Phases1p3p,func(int) error" -t "api.PhaseGetter,GetPhases,func() (int, error)"
//go:generate go run ../cmd/tools/decorate.go -f decorateKeba -b *Keba -r api.Charger -t "api.Meter,CurrentPower,func() (float64, error)" -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" -t "api.PhaseCurrents,Currents,func() (float64, float64, float64, error)" -t "kebaIdentifier,Identify,func() (string, error),StatusReason,func() (api.Reason, error)" -t "api.PhaseSwitcher,Phases1p3p,func(int) error" -t "api.PhaseGetter,GetPhases,func() (int, error)"

type kebaIdentifier interface {
api.Identifier
api.StatusReasoner
}

// NewKebaFromConfig creates a new Keba ModbusTCP charger
func NewKebaFromConfig(other map[string]interface{}) (api.Charger, error) {
Expand Down
15 changes: 9 additions & 6 deletions cmd/tools/decorate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ type dynamicType struct {
}

type functionStruct struct {
Signature, Function, VarName, ReturnTypes string
Params []string
Signature, Function, ReturnTypes string
Params []string
}
type typeStruct struct {
Type, ShortType string
Functions []functionStruct
Type, ShortType, VarName string
Functions []functionStruct
}

func generate(out io.Writer, packageName, functionName, baseType string, dynamicTypes ...dynamicType) error {
Expand All @@ -61,10 +61,12 @@ func generate(out io.Writer, packageName, functionName, baseType string, dynamic

for _, dt := range dynamicTypes {
parts := strings.SplitN(dt.typ, ".", 2)
lastPart := parts[len(parts)-1]

typ := typeStruct{
Type: dt.typ,
ShortType: parts[len(parts)-1],
ShortType: lastPart,
VarName: strings.ToLower(lastPart[:1]) + lastPart[1:],
}

fmt.Println("")
Expand All @@ -86,7 +88,6 @@ func generate(out io.Writer, packageName, functionName, baseType string, dynamic
returnValuesStr := fs.signature[closingBrace+1:]

function := functionStruct{
VarName: strings.ToLower(typ.ShortType[:1]) + typ.ShortType[1:],
Signature: fs.signature,
Function: fs.function,
Params: strings.Split(paramsStr, ","),
Expand Down Expand Up @@ -234,6 +235,8 @@ func main() {
formatted = []byte(generated)
}

fmt.Println(string(formatted))

formatted, err = imports.Process(name, formatted, nil)
if err != nil {
fmt.Println(err)
Expand Down
30 changes: 17 additions & 13 deletions cmd/tools/decorate.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,32 @@ import (
{{- end}}:
return &struct {
{{.BaseType}}
{{- range $typ, $def := .Types}}
{{- if contains $combo $typ}}
{{- range $typ, $def := .Types}}
{{- if contains $combo $typ}}
{{$typ}}
{{- end}}
{{- end}}
{{- end}}
{{- end}}
}{
{{.ShortBase}}: base,
{{- range $typ, $def := .Types}}
{{- if contains $combo $typ}}
{{- range $typ, $def := .Types}}
{{- if contains $combo $typ}}
{{$def.ShortType}}: &{{$prefix}}{{$def.ShortType}}Impl{
{{$def.VarName}}: {{$def.VarName}},
},
{{- end}}
{{- end}}
{{- end}}
{{- end}}
}
{{- end -}}

func {{.Function}}(base {{.BaseType}}{{range ordered}}, {{.VarName}} {{.Signature}}{{end}}) {{.ReturnType}} {
func {{.Function}}(base {{.BaseType}}{{range ordered}}, {{.VarName}} {.Signature}{{end}}) {{.ReturnType}} {
{{- $basetype := .BaseType}}
{{- $shortbase := .ShortBase}}
{{- $prefix := .Function}}
{{- $types := .Types}}
{{- $idx := 0}}
switch {
case {{- range $typ, $def := .Types}}
case
{{- range $typ, $def := .Types}}
{{- if gt $idx 0}} &&{{else}}{{$idx = 1}}{{end}} {{$def.VarName}} == nil
{{- end}}:
return base
Expand All @@ -53,17 +54,20 @@ func {{.Function}}(base {{.BaseType}}{{range ordered}}, {{.VarName}} {{.Signatur

{{range .Types -}}
type {{$prefix}}{{.ShortType}}Impl struct {
{{.VarName}} {{.Signature}}
{{- $varName := .VarName}}
{{- range .Functions}}
{{$varName}} {{.Signature}}
{{- end}}
}

func (impl *{{$prefix}}{{.ShortType}}Impl) {{.Function}}(
{{/* func (impl *{{$prefix}}{{.ShortType}}Impl) {{.Function}}(
{{- range $idx, $param := .Params -}}
{{- if gt $idx 0}}, {{end -}}
p{{$idx}} {{ $param -}}
{{end}}){{ .ReturnTypes }} {
return impl.{{.VarName}}(
{{- range $idx, $param := .Params -}}
{{- if gt $idx 0}}, {{end}}p{{- $idx -}}{{end}})
}
} */}}

{{end}}

0 comments on commit bdda2b8

Please sign in to comment.