Skip to content

Commit 33c85fe

Browse files
committed
feat(cmd/web): enum 子命令添加对 cbor 的支持
1 parent 95b3a0c commit 33c85fe

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

cmd/web/enum/tpl.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import(
1515
{{if .Filter}}"github.com/issue9/web/filter"{{end}}
1616
{{if .OpenAPI}}"github.com/issue9/web/openapi"{{end}}
1717
"github.com/issue9/web/locales"
18+
"github.com/fxamacker/cbor/v2"
1819
)
1920
2021
{{range .Enums}}
@@ -48,23 +49,41 @@ func Parse{{.Name}}(v string)({{.Name}},error){
4849
return 0, locales.ErrInvalidValue()
4950
}
5051
51-
// MarshalText encoding.TextMarshaler
5252
func({{.Receiver}} {{.Name}}) MarshalText()([]byte,error){
5353
if v, found := {{.Type2StringMap}}[{{.Receiver}}]; found {
5454
return []byte(v),nil
5555
}
5656
return nil, locales.ErrInvalidValue()
5757
}
5858
59-
// UnmarshalText encoding.TextUnmarshaler
60-
func({{.Receiver}} *{{.Name}}) UnmarshalText(p []byte)(error){
59+
func({{.Receiver}} *{{.Name}}) UnmarshalText(p []byte)error{
6160
tmp,err :=Parse{{.Name}}(string(p))
6261
if err==nil{
6362
*{{.Receiver}}=tmp
6463
}
6564
return err
6665
}
6766
67+
func({{.Receiver}} {{.Name}}) MarshalCBOR()([]byte,error){
68+
if v, found := {{.Type2StringMap}}[{{.Receiver}}]; found {
69+
return cbor.Marshal(v)
70+
}
71+
return nil, locales.ErrInvalidValue()
72+
}
73+
74+
func({{.Receiver}} *{{.Name}}) UnmarshalCBOR(p []byte)error{
75+
var tmp string
76+
if err := cbor.Unmarshal(p, &tmp); err != nil {
77+
return err
78+
}
79+
80+
if ss, found := {{.String2TypeMap}}[tmp]; found {
81+
*{{.Receiver}}=ss
82+
return nil
83+
}
84+
return locales.ErrInvalidValue()
85+
}
86+
6887
func({{.Receiver}} {{.Name}})IsValid()bool{
6988
_,found :={{.Type2StringMap}}[{{.Receiver}}];
7089
return found

mimetype/yaml/yaml.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// SPDX-License-Identifier: MIT
44

55
// Package yaml 支持 YAML 编码的序列化操作
6+
//
7+
// NOTE: 大部分时候,可以直接复用 json 标签,但是在对待嵌入对象时处理方式是不同的。
8+
// json 自动展开,而 yaml 则需要指定 `yaml:",inline"` 才会展开。
69
package yaml
710

811
import (

0 commit comments

Comments
 (0)