mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2024-11-09 02:41:22 +08:00
chore: some internal types support encoding.TextUnmarshaler
This commit is contained in:
parent
794645b7f8
commit
a08aa10630
@ -3,6 +3,7 @@ package constant
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// DNSModeMapping is a mapping for EnhancedMode enum
|
||||
@ -27,7 +28,7 @@ func (e *DNSMode) UnmarshalYAML(unmarshal func(any) error) error {
|
||||
if err := unmarshal(&tp); err != nil {
|
||||
return err
|
||||
}
|
||||
mode, exist := DNSModeMapping[tp]
|
||||
mode, exist := DNSModeMapping[strings.ToLower(tp)]
|
||||
if !exist {
|
||||
return errors.New("invalid mode")
|
||||
}
|
||||
@ -46,7 +47,7 @@ func (e *DNSMode) UnmarshalJSON(data []byte) error {
|
||||
if err := json.Unmarshal(data, &tp); err != nil {
|
||||
return err
|
||||
}
|
||||
mode, exist := DNSModeMapping[tp]
|
||||
mode, exist := DNSModeMapping[strings.ToLower(tp)]
|
||||
if !exist {
|
||||
return errors.New("invalid mode")
|
||||
}
|
||||
@ -59,6 +60,21 @@ func (e DNSMode) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(e.String())
|
||||
}
|
||||
|
||||
// UnmarshalText unserialize EnhancedMode
|
||||
func (e *DNSMode) UnmarshalText(data []byte) error {
|
||||
mode, exist := DNSModeMapping[strings.ToLower(string(data))]
|
||||
if !exist {
|
||||
return errors.New("invalid mode")
|
||||
}
|
||||
*e = mode
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalText serialize EnhancedMode
|
||||
func (e DNSMode) MarshalText() ([]byte, error) {
|
||||
return []byte(e.String()), nil
|
||||
}
|
||||
|
||||
func (e DNSMode) String() string {
|
||||
switch e {
|
||||
case DNSNormal:
|
||||
@ -150,7 +166,7 @@ func (e *FilterMode) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
if err := unmarshal(&tp); err != nil {
|
||||
return err
|
||||
}
|
||||
mode, exist := FilterModeMapping[tp]
|
||||
mode, exist := FilterModeMapping[strings.ToLower(tp)]
|
||||
if !exist {
|
||||
return errors.New("invalid mode")
|
||||
}
|
||||
@ -167,7 +183,20 @@ func (e *FilterMode) UnmarshalJSON(data []byte) error {
|
||||
if err := json.Unmarshal(data, &tp); err != nil {
|
||||
return err
|
||||
}
|
||||
mode, exist := FilterModeMapping[tp]
|
||||
mode, exist := FilterModeMapping[strings.ToLower(tp)]
|
||||
if !exist {
|
||||
return errors.New("invalid mode")
|
||||
}
|
||||
*e = mode
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e FilterMode) MarshalText() ([]byte, error) {
|
||||
return []byte(e.String()), nil
|
||||
}
|
||||
|
||||
func (e *FilterMode) UnmarshalText(data []byte) error {
|
||||
mode, exist := FilterModeMapping[strings.ToLower(string(data))]
|
||||
if !exist {
|
||||
return errors.New("invalid mode")
|
||||
}
|
||||
|
@ -56,6 +56,21 @@ func (e TUNStack) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(e.String())
|
||||
}
|
||||
|
||||
// UnmarshalText unserialize TUNStack
|
||||
func (e *TUNStack) UnmarshalText(data []byte) error {
|
||||
mode, exist := StackTypeMapping[strings.ToLower(string(data))]
|
||||
if !exist {
|
||||
return errors.New("invalid tun stack")
|
||||
}
|
||||
*e = mode
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalText serialize TUNStack with json
|
||||
func (e TUNStack) MarshalText() ([]byte, error) {
|
||||
return []byte(e.String()), nil
|
||||
}
|
||||
|
||||
func (e TUNStack) String() string {
|
||||
switch e {
|
||||
case TunGvisor:
|
||||
|
26
log/level.go
26
log/level.go
@ -3,6 +3,7 @@ package log
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// LogLevelMapping is a mapping for LogLevel enum
|
||||
@ -28,7 +29,7 @@ type LogLevel int
|
||||
func (l *LogLevel) UnmarshalYAML(unmarshal func(any) error) error {
|
||||
var tp string
|
||||
unmarshal(&tp)
|
||||
level, exist := LogLevelMapping[tp]
|
||||
level, exist := LogLevelMapping[strings.ToLower(tp)]
|
||||
if !exist {
|
||||
return errors.New("invalid mode")
|
||||
}
|
||||
@ -40,7 +41,7 @@ func (l *LogLevel) UnmarshalYAML(unmarshal func(any) error) error {
|
||||
func (l *LogLevel) UnmarshalJSON(data []byte) error {
|
||||
var tp string
|
||||
json.Unmarshal(data, &tp)
|
||||
level, exist := LogLevelMapping[tp]
|
||||
level, exist := LogLevelMapping[strings.ToLower(tp)]
|
||||
if !exist {
|
||||
return errors.New("invalid mode")
|
||||
}
|
||||
@ -48,9 +49,14 @@ func (l *LogLevel) UnmarshalJSON(data []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON serialize LogLevel with json
|
||||
func (l LogLevel) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(l.String())
|
||||
// UnmarshalText unserialize LogLevel
|
||||
func (l *LogLevel) UnmarshalText(data []byte) error {
|
||||
level, exist := LogLevelMapping[strings.ToLower(string(data))]
|
||||
if !exist {
|
||||
return errors.New("invalid mode")
|
||||
}
|
||||
*l = level
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalYAML serialize LogLevel with yaml
|
||||
@ -58,6 +64,16 @@ func (l LogLevel) MarshalYAML() (any, error) {
|
||||
return l.String(), nil
|
||||
}
|
||||
|
||||
// MarshalJSON serialize LogLevel with json
|
||||
func (l LogLevel) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(l.String())
|
||||
}
|
||||
|
||||
// MarshalText serialize LogLevel
|
||||
func (l LogLevel) MarshalText() ([]byte, error) {
|
||||
return []byte(l.String()), nil
|
||||
}
|
||||
|
||||
func (l LogLevel) String() string {
|
||||
switch l {
|
||||
case INFO:
|
||||
|
@ -21,18 +21,6 @@ const (
|
||||
Direct
|
||||
)
|
||||
|
||||
// UnmarshalJSON unserialize Mode
|
||||
func (m *TunnelMode) UnmarshalJSON(data []byte) error {
|
||||
var tp string
|
||||
json.Unmarshal(data, &tp)
|
||||
mode, exist := ModeMapping[strings.ToLower(tp)]
|
||||
if !exist {
|
||||
return errors.New("invalid mode")
|
||||
}
|
||||
*m = mode
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalYAML unserialize Mode with yaml
|
||||
func (m *TunnelMode) UnmarshalYAML(unmarshal func(any) error) error {
|
||||
var tp string
|
||||
@ -45,9 +33,26 @@ func (m *TunnelMode) UnmarshalYAML(unmarshal func(any) error) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON serialize Mode
|
||||
func (m TunnelMode) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(m.String())
|
||||
// UnmarshalJSON unserialize Mode
|
||||
func (m *TunnelMode) UnmarshalJSON(data []byte) error {
|
||||
var tp string
|
||||
json.Unmarshal(data, &tp)
|
||||
mode, exist := ModeMapping[strings.ToLower(tp)]
|
||||
if !exist {
|
||||
return errors.New("invalid mode")
|
||||
}
|
||||
*m = mode
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalText unserialize Mode
|
||||
func (m *TunnelMode) UnmarshalText(data []byte) error {
|
||||
mode, exist := ModeMapping[strings.ToLower(string(data))]
|
||||
if !exist {
|
||||
return errors.New("invalid mode")
|
||||
}
|
||||
*m = mode
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalYAML serialize TunnelMode with yaml
|
||||
@ -55,6 +60,16 @@ func (m TunnelMode) MarshalYAML() (any, error) {
|
||||
return m.String(), nil
|
||||
}
|
||||
|
||||
// MarshalJSON serialize Mode
|
||||
func (m TunnelMode) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(m.String())
|
||||
}
|
||||
|
||||
// MarshalText serialize Mode
|
||||
func (m TunnelMode) MarshalText() ([]byte, error) {
|
||||
return []byte(m.String()), nil
|
||||
}
|
||||
|
||||
func (m TunnelMode) String() string {
|
||||
switch m {
|
||||
case Global:
|
||||
|
@ -22,18 +22,6 @@ const (
|
||||
Running
|
||||
)
|
||||
|
||||
// UnmarshalJSON unserialize Status
|
||||
func (s *TunnelStatus) UnmarshalJSON(data []byte) error {
|
||||
var tp string
|
||||
json.Unmarshal(data, &tp)
|
||||
status, exist := StatusMapping[strings.ToLower(tp)]
|
||||
if !exist {
|
||||
return errors.New("invalid mode")
|
||||
}
|
||||
*s = status
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalYAML unserialize Status with yaml
|
||||
func (s *TunnelStatus) UnmarshalYAML(unmarshal func(any) error) error {
|
||||
var tp string
|
||||
@ -46,9 +34,26 @@ func (s *TunnelStatus) UnmarshalYAML(unmarshal func(any) error) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON serialize Status
|
||||
func (s TunnelStatus) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(s.String())
|
||||
// UnmarshalJSON unserialize Status
|
||||
func (s *TunnelStatus) UnmarshalJSON(data []byte) error {
|
||||
var tp string
|
||||
json.Unmarshal(data, &tp)
|
||||
status, exist := StatusMapping[strings.ToLower(tp)]
|
||||
if !exist {
|
||||
return errors.New("invalid status")
|
||||
}
|
||||
*s = status
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalText unserialize Status
|
||||
func (s *TunnelStatus) UnmarshalText(data []byte) error {
|
||||
status, exist := StatusMapping[strings.ToLower(string(data))]
|
||||
if !exist {
|
||||
return errors.New("invalid status")
|
||||
}
|
||||
*s = status
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalYAML serialize TunnelMode with yaml
|
||||
@ -56,6 +61,16 @@ func (s TunnelStatus) MarshalYAML() (any, error) {
|
||||
return s.String(), nil
|
||||
}
|
||||
|
||||
// MarshalJSON serialize Status
|
||||
func (s TunnelStatus) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(s.String())
|
||||
}
|
||||
|
||||
// MarshalText serialize Status
|
||||
func (s TunnelStatus) MarshalText() ([]byte, error) {
|
||||
return []byte(s.String()), nil
|
||||
}
|
||||
|
||||
func (s TunnelStatus) String() string {
|
||||
switch s {
|
||||
case Suspend:
|
||||
|
Loading…
Reference in New Issue
Block a user