Skip to content

Commit

Permalink
fix(enum): OnOff unmarshal
Browse files Browse the repository at this point in the history
  • Loading branch information
bububa committed Dec 2, 2024
1 parent 7d5cc86 commit d898cdb
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions marketing-api/enum/onoff.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package enum

import "strconv"

type OnOff string

const (
Expand All @@ -8,3 +10,28 @@ const (
// OFF 关闭
OFF OnOff = "OFF"
)

func (ooi *OnOff) UnmarshalJSON(b []byte) (err error) {
if b[0] == '"' && b[len(b)-1] == '"' {
b = b[1 : len(b)-1]
}
str := string(b)
var v string
switch str {
case "ON", "OFF":
v = str
default:
if str != "" {
if i, err := strconv.Atoi(str); err == nil {
switch i {
case 1:
v = "ON"
case 0:
v = "OFF"
}
}
}
}
*ooi = OnOff(v)
return nil
}

0 comments on commit d898cdb

Please sign in to comment.