@@ -15,6 +15,7 @@ import(
15
15
{{if .Filter}}"github.com/issue9/web/filter"{{end}}
16
16
{{if .OpenAPI}}"github.com/issue9/web/openapi"{{end}}
17
17
"github.com/issue9/web/locales"
18
+ "github.com/fxamacker/cbor/v2"
18
19
)
19
20
20
21
{{range .Enums}}
@@ -48,23 +49,41 @@ func Parse{{.Name}}(v string)({{.Name}},error){
48
49
return 0, locales.ErrInvalidValue()
49
50
}
50
51
51
- // MarshalText encoding.TextMarshaler
52
52
func({{.Receiver}} {{.Name}}) MarshalText()([]byte,error){
53
53
if v, found := {{.Type2StringMap}}[{{.Receiver}}]; found {
54
54
return []byte(v),nil
55
55
}
56
56
return nil, locales.ErrInvalidValue()
57
57
}
58
58
59
- // UnmarshalText encoding.TextUnmarshaler
60
- func({{.Receiver}} *{{.Name}}) UnmarshalText(p []byte)(error){
59
+ func({{.Receiver}} *{{.Name}}) UnmarshalText(p []byte)error{
61
60
tmp,err :=Parse{{.Name}}(string(p))
62
61
if err==nil{
63
62
*{{.Receiver}}=tmp
64
63
}
65
64
return err
66
65
}
67
66
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
+
68
87
func({{.Receiver}} {{.Name}})IsValid()bool{
69
88
_,found :={{.Type2StringMap}}[{{.Receiver}}];
70
89
return found
0 commit comments