2024-12-01 16:05:07 +08:00
|
|
|
package blog
|
|
|
|
|
2025-01-19 14:51:14 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-12-01 16:05:07 +08:00
|
|
|
type STAGE int
|
|
|
|
|
2025-01-19 14:51:14 +08:00
|
|
|
func (s STAGE) String() string {
|
|
|
|
return STAGE_MAPPING[s]
|
|
|
|
}
|
|
|
|
|
|
|
|
// 自定义 类型的序列化方式
|
|
|
|
// "草稿"
|
|
|
|
func (s STAGE) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(`"` + s.String() + `"`), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON([]byte) error
|
|
|
|
func (s *STAGE) UnmarshalJSON(data []byte) error {
|
|
|
|
str := strings.Trim(string(data), `"`)
|
|
|
|
switch str {
|
|
|
|
case "草稿":
|
|
|
|
*s = STAGE_DRAFT
|
|
|
|
// v := STAGE_DRAFT
|
|
|
|
// s = &v
|
|
|
|
case "已发布":
|
|
|
|
*s = STAGE_PUBLISHED
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("不支持的发布类型")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var STAGE_MAPPING = map[STAGE]string{
|
|
|
|
STAGE_DRAFT: "草稿",
|
|
|
|
STAGE_PUBLISHED: "已发布",
|
|
|
|
}
|
|
|
|
|
2024-12-01 16:05:07 +08:00
|
|
|
const (
|
|
|
|
STAGE_DRAFT STAGE = iota
|
|
|
|
STAGE_PUBLISHED
|
|
|
|
)
|