diff --git a/constant/dns.go b/constant/dns.go index 8d038a6bbb..b0874eef5b 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 f6c0e011b8..669f7a2e2b 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 ea06ee4bb9..a4c6ecbd41 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 a1697a32b0..dd89c3a7b3 100644 --- a/tunnel/mode.go +++ b/tunnel/mode.go @@ -21,6 +21,18 @@ const ( Direct ) +// UnmarshalYAML unserialize Mode with yaml +func (m *TunnelMode) UnmarshalYAML(unmarshal func(any) error) error { + var tp string + unmarshal(&tp) + mode, exist := ModeMapping[strings.ToLower(tp)] + if !exist { + return errors.New("invalid mode") + } + *m = mode + return nil +} + // UnmarshalJSON unserialize Mode func (m *TunnelMode) UnmarshalJSON(data []byte) error { var tp string @@ -33,11 +45,9 @@ func (m *TunnelMode) UnmarshalJSON(data []byte) error { return nil } -// UnmarshalYAML unserialize Mode with yaml -func (m *TunnelMode) UnmarshalYAML(unmarshal func(any) error) error { - var tp string - unmarshal(&tp) - mode, exist := ModeMapping[strings.ToLower(tp)] +// UnmarshalText unserialize Mode +func (m *TunnelMode) UnmarshalText(data []byte) error { + mode, exist := ModeMapping[strings.ToLower(string(data))] if !exist { return errors.New("invalid mode") } @@ -45,14 +55,19 @@ func (m *TunnelMode) UnmarshalYAML(unmarshal func(any) error) error { return nil } +// MarshalYAML serialize TunnelMode with yaml +func (m TunnelMode) MarshalYAML() (any, error) { + return m.String(), nil +} + // MarshalJSON serialize Mode func (m TunnelMode) MarshalJSON() ([]byte, error) { return json.Marshal(m.String()) } -// MarshalYAML serialize TunnelMode with yaml -func (m TunnelMode) MarshalYAML() (any, error) { - return m.String(), nil +// MarshalText serialize Mode +func (m TunnelMode) MarshalText() ([]byte, error) { + return []byte(m.String()), nil } func (m TunnelMode) String() string { diff --git a/tunnel/status.go b/tunnel/status.go index d81dd45e8f..388d597b38 100644 --- a/tunnel/status.go +++ b/tunnel/status.go @@ -22,23 +22,33 @@ const ( Running ) +// UnmarshalYAML unserialize Status with yaml +func (s *TunnelStatus) UnmarshalYAML(unmarshal func(any) error) error { + var tp string + unmarshal(&tp) + status, exist := StatusMapping[strings.ToLower(tp)] + if !exist { + return errors.New("invalid status") + } + *s = status + return nil +} + // 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") + return errors.New("invalid status") } *s = status return nil } -// UnmarshalYAML unserialize Status with yaml -func (s *TunnelStatus) UnmarshalYAML(unmarshal func(any) error) error { - var tp string - unmarshal(&tp) - status, exist := StatusMapping[strings.ToLower(tp)] +// UnmarshalText unserialize Status +func (s *TunnelStatus) UnmarshalText(data []byte) error { + status, exist := StatusMapping[strings.ToLower(string(data))] if !exist { return errors.New("invalid status") } @@ -46,14 +56,19 @@ func (s *TunnelStatus) UnmarshalYAML(unmarshal func(any) error) error { return nil } +// MarshalYAML serialize TunnelMode with yaml +func (s TunnelStatus) MarshalYAML() (any, error) { + return s.String(), nil +} + // MarshalJSON serialize Status func (s TunnelStatus) MarshalJSON() ([]byte, error) { return json.Marshal(s.String()) } -// MarshalYAML serialize TunnelMode with yaml -func (s TunnelStatus) MarshalYAML() (any, error) { - return s.String(), nil +// MarshalText serialize Status +func (s TunnelStatus) MarshalText() ([]byte, error) { + return []byte(s.String()), nil } func (s TunnelStatus) String() string {