forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/evcc-io/evcc/provider/golang" | ||
"github.com/evcc-io/evcc/util" | ||
"github.com/traefik/yaegi/interp" | ||
) | ||
|
||
// Go implements Go request provider | ||
type Go struct { | ||
vm *interp.Interpreter | ||
script string | ||
} | ||
|
||
func init() { | ||
registry.Add("go", NewGoProviderFromConfig) | ||
} | ||
|
||
// NewGoProviderFromConfig creates a Go provider | ||
func NewGoProviderFromConfig(other map[string]interface{}) (IntProvider, error) { | ||
var cc struct { | ||
VM string | ||
Script string | ||
} | ||
|
||
if err := util.DecodeOther(other, &cc); err != nil { | ||
return nil, err | ||
} | ||
|
||
vm, err := golang.RegisteredVM(cc.VM, "") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
p := &Go{ | ||
vm: vm, | ||
script: cc.Script, | ||
} | ||
|
||
return p, nil | ||
} | ||
|
||
// FloatGetter parses float from request | ||
func (p *Go) FloatGetter() func() (float64, error) { | ||
return func() (res float64, err error) { | ||
v, err := p.vm.Eval(p.script) | ||
if err == nil { | ||
if typ := reflect.TypeOf(res); v.CanConvert(typ) { | ||
res = v.Convert(typ).Float() | ||
} else { | ||
err = fmt.Errorf("not a float: %v", v) | ||
} | ||
} | ||
|
||
return res, err | ||
} | ||
} | ||
|
||
// IntGetter parses int64 from request | ||
func (p *Go) IntGetter() func() (int64, error) { | ||
return func() (res int64, err error) { | ||
v, err := p.vm.Eval(p.script) | ||
if err == nil { | ||
if typ := reflect.TypeOf(res); v.CanConvert(typ) { | ||
res = v.Convert(typ).Int() | ||
} else { | ||
err = fmt.Errorf("not an int: %v", v) | ||
} | ||
} | ||
|
||
return res, err | ||
} | ||
} | ||
|
||
// StringGetter parses string from request | ||
func (p *Go) StringGetter() func() (string, error) { | ||
return func() (res string, err error) { | ||
v, err := p.vm.Eval(p.script) | ||
if err == nil { | ||
if typ := reflect.TypeOf(res); v.CanConvert(typ) { | ||
res = v.Convert(typ).String() | ||
} else { | ||
err = fmt.Errorf("not a string: %v", v) | ||
} | ||
} | ||
|
||
return res, err | ||
} | ||
} | ||
|
||
// BoolGetter parses bool from request | ||
func (p *Go) BoolGetter() func() (bool, error) { | ||
return func() (res bool, err error) { | ||
v, err := p.vm.Eval(p.script) | ||
if err == nil { | ||
if typ := reflect.TypeOf(res); v.CanConvert(typ) { | ||
res = v.Convert(typ).Bool() | ||
} else { | ||
err = fmt.Errorf("not a boolean: %v", v) | ||
} | ||
} | ||
|
||
return res, err | ||
} | ||
} | ||
|
||
func (p *Go) paramAndEval(param string, val any) error { | ||
_, err := p.vm.Eval(fmt.Sprintf("%s := %v;", param, val)) | ||
if err == nil { | ||
_, err = p.vm.Eval(p.script) | ||
} | ||
return err | ||
} | ||
|
||
func (p *Go) IntSetter(param string) func(int64) error { | ||
return func(val int64) error { | ||
return p.paramAndEval(param, val) | ||
} | ||
} | ||
|
||
// StringSetter sends string request | ||
func (p *Go) StringSetter(param string) func(string) error { | ||
return func(val string) error { | ||
return p.paramAndEval(param, val) | ||
} | ||
} | ||
|
||
// BoolSetter sends bool request | ||
func (p *Go) BoolSetter(param string) func(bool) error { | ||
return func(val bool) error { | ||
return p.paramAndEval(param, val) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package golang | ||
|
||
import ( | ||
"github.com/traefik/yaegi/interp" | ||
"github.com/traefik/yaegi/stdlib" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
var ( | ||
mu sync.Mutex | ||
registry = make(map[string]*interp.Interpreter) | ||
) | ||
|
||
// RegisteredVM returns a JS VM. If name is not empty, it will return a shared instance. | ||
func RegisteredVM(name, init string) (*interp.Interpreter, error) { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
|
||
name = strings.ToLower(name) | ||
vm, ok := registry[name] | ||
|
||
// create new VM | ||
if !ok { | ||
vm = interp.New(interp.Options{}) | ||
|
||
if err := vm.Use(stdlib.Symbols); err != nil { | ||
return nil, err | ||
} | ||
|
||
if init != "" { | ||
if _, err := vm.Eval(init); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
if name != "" { | ||
registry[name] = vm | ||
} | ||
} | ||
|
||
return vm, nil | ||
} |