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.
chore: make type conversion provider more flexible
- Loading branch information
Showing
3 changed files
with
57 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/evcc-io/evcc/util" | ||
) | ||
|
||
type convertProvider struct { | ||
Convert string | ||
Set Config | ||
} | ||
|
||
func init() { | ||
registry.Add("convert", NewConvertFromConfig) | ||
} | ||
|
||
// NewConvertFromConfig creates type conversion provider | ||
func NewConvertFromConfig(other map[string]interface{}) (Provider, error) { | ||
var cc convertProvider | ||
|
||
if err := util.DecodeOther(other, &cc); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &cc, nil | ||
} | ||
|
||
var _ SetFloatProvider = (*convertProvider)(nil) | ||
|
||
func (o *convertProvider) FloatSetter(param string) (func(float64) error, error) { | ||
if o.Convert == "float2int" { | ||
return nil, fmt.Errorf("convert: invalid conversion: %s", o.Convert) | ||
} | ||
|
||
set, err := NewIntSetterFromConfig(param, o.Set) | ||
|
||
return func(val float64) error { | ||
return set(int64(val)) | ||
}, err | ||
} | ||
|
||
var _ SetIntProvider = (*convertProvider)(nil) | ||
|
||
func (o *convertProvider) IntSetter(param string) (func(int64) error, error) { | ||
if o.Convert == "int2float" { | ||
return nil, fmt.Errorf("convert: invalid conversion: %s", o.Convert) | ||
} | ||
|
||
set, err := NewFloatSetterFromConfig(param, o.Set) | ||
|
||
return func(val int64) error { | ||
return set(float64(val)) | ||
}, err | ||
} |
This file was deleted.
Oops, something went wrong.
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