Skip to content

Commit

Permalink
chore: some internal types support encoding.TextUnmarshaler
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Sep 19, 2024
1 parent 794645b commit a08aa10
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 26 deletions.
37 changes: 33 additions & 4 deletions constant/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package constant
import (
"encoding/json"
"errors"
"strings"
)

// DNSModeMapping is a mapping for EnhancedMode enum
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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:
Expand Down Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand Down
15 changes: 15 additions & 0 deletions constant/tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
26 changes: 21 additions & 5 deletions log/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package log
import (
"encoding/json"
"errors"
"strings"
)

// LogLevelMapping is a mapping for LogLevel enum
Expand All @@ -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")
}
Expand All @@ -40,24 +41,39 @@ 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")
}
*l = level
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
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:
Expand Down
31 changes: 23 additions & 8 deletions tunnel/mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,26 +45,29 @@ 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")
}
*m = mode
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 {
Expand Down
33 changes: 24 additions & 9 deletions tunnel/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,53 @@ 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")
}
*s = status
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 {
Expand Down

0 comments on commit a08aa10

Please sign in to comment.