Implement lifecycle

This commit is contained in:
d1slike committed 2024-05-06 09:43:11 +03:00
1 parent 2e23e882bb
commit 8a2a65d8ed
14 files changed
+594 -38

No files matched your search

+45
View File
@@ -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)
}