http urls monitor.

string.go 806B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package codec
  2. import (
  3. "fmt"
  4. )
  5. type StringCoder struct{}
  6. func (c *StringCoder) Encoder(data interface{}) ([]byte, error) {
  7. switch t := data.(type) {
  8. case string:
  9. return []byte(t), nil
  10. case *string:
  11. return []byte(*t), nil
  12. case []byte:
  13. return t, nil
  14. case *[]byte:
  15. return *t, nil
  16. default:
  17. return nil, fmt.Errorf("%T can not be directly converted to []byte", t)
  18. }
  19. }
  20. func (c *StringCoder) Decoder(data []byte, v interface{}) error {
  21. switch t := v.(type) {
  22. case string:
  23. return fmt.Errorf("expect %T but %T", &t, t)
  24. case *string:
  25. *t = string(data)
  26. case []byte:
  27. return fmt.Errorf("expect %T but %T", &t, t)
  28. case *[]byte:
  29. *t = data
  30. default:
  31. return fmt.Errorf("[]byte can not be directly converted to %T", t)
  32. }
  33. return nil
  34. }
  35. func init() {
  36. Register(String, &StringCoder{})
  37. }