Implement lifecycle

This commit is contained in:
d1slike committed 2024-05-06 20:09:14 +03:00
1 parent 8a2a65d8ed
commit c4abdaa54b
21 files changed
+582 -173

No files matched your search

+25
View File
@@ -0,0 +1,25 @@
package xtypes
type Bool struct {
Value bool
}
func (l Bool) MarshalJSON() ([]byte, error) {
return l.MarshalText()
}
func (l *Bool) UnmarshalJSON(data []byte) error {
return l.UnmarshalText(data)
}
func (l Bool) MarshalText() ([]byte, error) {
if l.Value {
return []byte("1"), nil
}
return []byte("0"), nil
}
func (l *Bool) UnmarshalText(data []byte) error {
l.Value = string(data) == "1"
return nil
}
+39
View File
@@ -0,0 +1,39 @@
package xtypes
import (
"strconv"
"github.com/txix-open/isp-kit/json"
)
type Json[T any] struct {
Value T
}
func (l Json[T]) MarshalJSON() ([]byte, error) {
data, err := l.MarshalText()
if err != nil {
return nil, err
}
return []byte(strconv.Quote(string(data))), nil
}
func (l *Json[T]) UnmarshalJSON(data []byte) error {
s, err := strconv.Unquote(string(data))
if err != nil {
return err
}
return l.UnmarshalText([]byte(s))
}
func (l Json[T]) MarshalText() ([]byte, error) {
data, err := json.Marshal(l.Value)
if err != nil {
return nil, err
}
return data, nil
}
func (l *Json[T]) UnmarshalText(data []byte) error {
return json.Unmarshal(data, &l.Value)
}
+32
View File
@@ -0,0 +1,32 @@
package xtypes
import (
"strconv"
"time"
)
type Time struct {
Value time.Time
}
func (l Time) MarshalJSON() ([]byte, error) {
return l.MarshalText()
}
func (l *Time) UnmarshalJSON(data []byte) error {
return l.UnmarshalText(data)
}
func (l Time) MarshalText() ([]byte, error) {
value := strconv.Itoa(int(l.Value.Unix()))
return []byte(value), nil
}
func (l *Time) UnmarshalText(data []byte) error {
value, err := strconv.Atoi(string(data))
if err != nil {
return err
}
l.Value = time.Unix(int64(value), 0)
return nil
}