Fix panic when charge status is empty string
This commit is contained in:
parent
91de2a00e3
commit
0f32a4386a
1 changed files with 15 additions and 9 deletions
24
api/api.go
24
api/api.go
|
|
@ -45,22 +45,28 @@ const (
|
|||
var StatusEasA = map[ChargeStatus]ChargeStatus{StatusE: StatusA}
|
||||
|
||||
// ChargeStatusString converts a string to ChargeStatus
|
||||
func ChargeStatusString(s string) (ChargeStatus, error) {
|
||||
status := strings.ToUpper(strings.TrimSpace(strings.Trim(s, "\x00")))
|
||||
switch s1 := status[:1]; s1 {
|
||||
func ChargeStatusString(status string) (ChargeStatus, error) {
|
||||
s := strings.ToUpper(strings.Trim(status, "\x00 "))
|
||||
|
||||
if len(s) == 0 {
|
||||
return StatusNone, fmt.Errorf("invalid status: %s", status)
|
||||
}
|
||||
|
||||
switch s1 := s[:1]; s1 {
|
||||
case "A", "B":
|
||||
return ChargeStatus(s1), nil
|
||||
|
||||
case "C", "D":
|
||||
switch status {
|
||||
case "C1", "D1":
|
||||
if s == "C1" || s == "D1" {
|
||||
return StatusB, nil
|
||||
default:
|
||||
return StatusC, nil
|
||||
}
|
||||
return StatusC, nil
|
||||
|
||||
case "E", "F":
|
||||
return ChargeStatus(s1), fmt.Errorf("invalid status: %s", status)
|
||||
return ChargeStatus(s1), fmt.Errorf("invalid status: %s", s)
|
||||
|
||||
default:
|
||||
return StatusNone, fmt.Errorf("invalid status: %s", s)
|
||||
return StatusNone, fmt.Errorf("invalid status: %s", status)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue