From a08aa10630813485d7899e65ff59c3fae56f8364 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Thu, 19 Sep 2024 18:06:36 +0800 Subject: [PATCH] chore: some internal types support encoding.TextUnmarshaler --- constant/dns.go | 37 +++++++++++++++++++++++++++++++++---- constant/tun.go | 15 +++++++++++++++ log/level.go | 26 +++++++++++++++++++++----- tunnel/mode.go | 45 ++++++++++++++++++++++++++++++--------------- tunnel/status.go | 45 ++++++++++++++++++++++++++++++--------------- 5 files changed, 129 insertions(+), 39 deletions(-) diff --git a/constant/dns.go b/constant/dns.go index 8d038a6b..b0874eef 100644 --- a/constant/dns.go +++ b/constant/dns.go @@ -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") } diff --git a/constant/tun.go b/constant/tun.go index f6c0e011..669f7a2e 100644 --- a/constant/tun.go +++ b/constant/tun.go @@ -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: diff --git a/log/level.go b/log/level.go index ea06ee4b..a4c6ecbd 100644 --- a/log/level.go +++ b/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: diff --git a/tunnel/mode.go b/tunnel/mode.go index a1697a32..dd89c3a7 100644 --- a/tunnel/mode.go +++ b/tunnel/mode.go @@ -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: diff --git a/tunnel/status.go b/tunnel/status.go index d81dd45e..388d597b 100644 --- a/tunnel/status.go +++ b/tunnel/status.go @@ -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: