writer_hooks.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package file
  15. type writerHooks interface {
  16. Header() []byte
  17. Line() []byte
  18. Footer() []byte
  19. }
  20. type jsonWriterHooks struct{}
  21. func (j *jsonWriterHooks) Header() []byte {
  22. return []byte("[")
  23. }
  24. func (j *jsonWriterHooks) Line() []byte {
  25. return nil
  26. }
  27. func (j *jsonWriterHooks) Footer() []byte {
  28. return []byte("]")
  29. }
  30. var jsonHooks = &jsonWriterHooks{}
  31. type linesWriterHooks struct{}
  32. func (l *linesWriterHooks) Header() []byte {
  33. return nil
  34. }
  35. func (l *linesWriterHooks) Line() []byte {
  36. return []byte("\n")
  37. }
  38. func (l *linesWriterHooks) Footer() []byte {
  39. return nil
  40. }
  41. var linesHooks = &linesWriterHooks{}
  42. type csvWriterHooks struct {
  43. header []byte
  44. }
  45. func (c *csvWriterHooks) Header() []byte {
  46. return c.header
  47. }
  48. func (c *csvWriterHooks) Line() []byte {
  49. return []byte("\n")
  50. }
  51. func (c *csvWriterHooks) Footer() []byte {
  52. return nil
  53. }
  54. func (c *csvWriterHooks) SetHeader(header string) {
  55. c.header = []byte(header)
  56. }