add state file parser
This commit is contained in:
@@ -66,7 +66,7 @@ func msgClearCart() *messages.ClearCartRequest {
|
|||||||
return &messages.ClearCartRequest{}
|
return &messages.ClearCartRequest{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func msgAddVoucher(code string, value int64, rules ...*messages.VoucherRule) *messages.AddVoucher {
|
func msgAddVoucher(code string, value int64, rules ...string) *messages.AddVoucher {
|
||||||
return &messages.AddVoucher{Code: code, Value: value, VoucherRules: rules}
|
return &messages.AddVoucher{Code: code, Value: value, VoucherRules: rules}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -369,3 +370,36 @@ func WalkConditions(conds []Condition, fn func(c Condition) bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PromotionState struct {
|
||||||
|
Vouchers []PromotionRule `json:"promotions"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type StateFile struct {
|
||||||
|
State PromotionState `json:"state"`
|
||||||
|
Version int `json:"version"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sf *StateFile) GetPromotion(id string) (*PromotionRule, bool) {
|
||||||
|
for _, v := range sf.State.Vouchers {
|
||||||
|
if v.ID == id {
|
||||||
|
return &v, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadStateFile(fileName string) (*PromotionState, error) {
|
||||||
|
f, err := os.Open(fileName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
dec := json.NewDecoder(f)
|
||||||
|
sf := &PromotionState{}
|
||||||
|
err = dec.Decode(sf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return sf, nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user