config_json.go 744 B

123456789101112131415161718192021222324252627282930313233343536
  1. // +build json
  2. package transport
  3. import (
  4. "encoding/json"
  5. "strings"
  6. )
  7. func (this *Config) UnmarshalJSON(data []byte) error {
  8. type TypeConfig struct {
  9. StreamType string `json:"streamType"`
  10. Settings json.RawMessage `json:"settings"`
  11. }
  12. type JsonTCPConfig struct {
  13. ConnectionReuse bool `json:"connectionReuse"`
  14. }
  15. typeConfig := new(TypeConfig)
  16. if err := json.Unmarshal(data, typeConfig); err != nil {
  17. return err
  18. }
  19. streamType := strings.ToLower(typeConfig.StreamType)
  20. if streamType == "tcp" {
  21. jsonTCPConfig := new(JsonTCPConfig)
  22. if err := json.Unmarshal(data, jsonTCPConfig); err != nil {
  23. return err
  24. }
  25. this.TCPConfig = &TCPConfig{
  26. ConnectionReuse: jsonTCPConfig.ConnectionReuse,
  27. }
  28. }
  29. return nil
  30. }