marshaler.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package pio
  2. import (
  3. "encoding/json"
  4. "encoding/xml"
  5. "errors"
  6. )
  7. // Marshaler is the interface implemented by types that
  8. // can marshal themselves into valid output.
  9. type Marshaler interface {
  10. Marshal(v interface{}) ([]byte, error)
  11. }
  12. // Marshaled or (especially British, marshalled) is an interface which
  13. // is implemented by values that can marshal theirselves.
  14. //
  15. // It's like Marshaler but it doesn't takes an argument.
  16. type Marshaled interface {
  17. Marshal() ([]byte, error)
  18. }
  19. func fromMarshaled(self Marshaled) Marshaler {
  20. return MarshalerFunc(func(v interface{}) ([]byte, error) {
  21. return self.Marshal()
  22. })
  23. }
  24. // MarshalerFunc is the signature implemented by callers that
  25. // are responsible to marshal "v" into valid printable result.
  26. //
  27. // Look `Printer#Marshal` for more.
  28. type MarshalerFunc func(v interface{}) ([]byte, error)
  29. // Marshal makes the Marshaler compatible with the
  30. // standard golang's marshalers, so a marshaler
  31. // created for a Printer, can be used on std packages as well.
  32. func (m MarshalerFunc) Marshal(v interface{}) ([]byte, error) {
  33. return m(v)
  34. }
  35. // ErrMarshalNotResponsible retruns from a marshaler
  36. // when it's not responsible and should continue to the next marshaler.
  37. var ErrMarshalNotResponsible = errors.New("this marshaler is not responsible for this type of data")
  38. // ErrMarshalNotFound or ErrSkipped can be used to skip a specific
  39. // printer's output operation.
  40. var ErrMarshalNotFound = errors.New("no marshaler found for this type of dat")
  41. // Text is a Text marshaler, it converts
  42. // string to a compatible form of []byte.
  43. var Text = MarshalerFunc(func(v interface{}) ([]byte, error) {
  44. if b, ok := v.([]byte); ok {
  45. return b, nil
  46. }
  47. if s, ok := v.(string); ok {
  48. return []byte(s), nil // maybe 0101010 010110 here, but can be overridden by fmt.Sprintf("%s", v)
  49. }
  50. return nil, ErrMarshalNotResponsible
  51. })
  52. var (
  53. // JSON returns the JSON encoding of Printer#Print%v.
  54. // A shortcut for `encoding/json#Marshal`
  55. JSON = MarshalerFunc(json.Marshal)
  56. // JSONIndent returns the JSON encoding of Printer#Print%v.
  57. // A shortcut for `encoding/json#MarshalIndent(v, ""," ")`
  58. JSONIndent = MarshalerFunc(func(v interface{}) ([]byte, error) {
  59. return json.MarshalIndent(v, "", " ")
  60. })
  61. // XML returns the XML encoding of Printer#Print%v.
  62. // A shortcut for `encoding/xml#Marshal`
  63. XML = MarshalerFunc(xml.Marshal)
  64. // XMLIndent returns the XML encoding of Printer#Print%v.
  65. // A shortcut for `encoding/xml#MarshalIndent(v, ""," ")`
  66. XMLIndent = MarshalerFunc(func(v interface{}) ([]byte, error) {
  67. return xml.MarshalIndent(v, "", " ")
  68. })
  69. )