writer_hooks.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. type linesWriterHooks struct{}
  31. func (l *linesWriterHooks) Header() []byte {
  32. return nil
  33. }
  34. func (l *linesWriterHooks) Line() []byte {
  35. return []byte("\n")
  36. }
  37. func (l *linesWriterHooks) Footer() []byte {
  38. return nil
  39. }
  40. type csvWriterHooks struct {
  41. header []byte
  42. }
  43. func (c *csvWriterHooks) Header() []byte {
  44. return c.header
  45. }
  46. func (c *csvWriterHooks) Line() []byte {
  47. return []byte("\n")
  48. }
  49. func (c *csvWriterHooks) Footer() []byte {
  50. return nil
  51. }
  52. func (c *csvWriterHooks) SetHeader(header string) {
  53. c.header = []byte(header)
  54. }