Implement lifecycle
This commit is contained in:
1 parent
2e23e882bb
commit
8a2a65d8ed
14 files changed
+594
-38
No files matched your search
@@ -0,0 +1,45 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"strconv"
|
||||
|
||||
"github.com/txix-open/isp-kit/json"
|
||||
)
|
||||
|
||||
type JsonValue[T any] struct {
|
||||
Value T
|
||||
}
|
||||
|
||||
func (l JsonValue[T]) MarshalJSON() ([]byte, error) {
|
||||
data, err := l.MarshalText()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []byte(strconv.Quote(string(data))), nil
|
||||
}
|
||||
|
||||
func (l *JsonValue[T]) UnmarshalJSON(data []byte) error {
|
||||
s, err := strconv.Unquote(string(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return l.UnmarshalText([]byte(s))
|
||||
}
|
||||
|
||||
func (l JsonValue[T]) MarshalText() ([]byte, error) {
|
||||
data, err := json.Marshal(l.Value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s := base64.StdEncoding.EncodeToString(data)
|
||||
return []byte(s), nil
|
||||
}
|
||||
|
||||
func (l *JsonValue[T]) UnmarshalText(data []byte) error {
|
||||
arr, err := base64.StdEncoding.DecodeString(string(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.Unmarshal(arr, &l.Value)
|
||||
}
|
||||
Reference in new issue
Block a user