-
-
Notifications
You must be signed in to change notification settings - Fork 758
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
13 changed files
with
114 additions
and
3 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 |
---|---|---|
@@ -1,12 +1,71 @@ | ||
package tariff | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/traefik/yaegi/interp" | ||
"github.com/traefik/yaegi/stdlib" | ||
) | ||
|
||
type embed struct { | ||
Margin float64 `mapstructure:"margin"` | ||
Charges float64 `mapstructure:"charges"` | ||
Tax float64 `mapstructure:"tax"` | ||
Uplifts float64 `mapstructure:"uplifts"` | ||
Formula string `mapstructure:"formula"` | ||
|
||
calc func(float64) (float64, error) | ||
} | ||
|
||
func (t *embed) init() error { | ||
if t.Formula == "" { | ||
return nil | ||
} | ||
|
||
vm := interp.New(interp.Options{}) | ||
if err := vm.Use(stdlib.Symbols); err != nil { | ||
return err | ||
} | ||
|
||
if _, err := vm.Eval(`import "math"`); err != nil { | ||
return err | ||
} | ||
|
||
if _, err := vm.Eval("var price, charges, tax float64"); err != nil { | ||
return err | ||
} | ||
|
||
prg, err := vm.Compile(t.Formula) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
t.calc = func(price float64) (float64, error) { | ||
if _, err := vm.Eval(fmt.Sprintf("price = %f", price)); err != nil { | ||
return 0, err | ||
} | ||
|
||
res, err := vm.Execute(prg) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
if !res.CanFloat() { | ||
return 0, errors.New("formula did not return a float value") | ||
} | ||
|
||
return res.Float(), nil | ||
} | ||
|
||
// test the formula | ||
_, err = t.calc(0) | ||
|
||
return err | ||
} | ||
|
||
func (t *embed) totalPrice(price float64) float64 { | ||
return (price*(1+t.Margin)+t.Charges)*(1+t.Tax) + t.Uplifts | ||
if t.calc != nil { | ||
res, _ := t.calc(price) | ||
return res | ||
} | ||
return (price + t.Charges) * (1 + t.Tax) | ||
} |
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
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