meta.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package exchange
  2. import (
  3. "strings"
  4. "github.com/iancoleman/strcase"
  5. )
  6. type MetaValues interface {
  7. Get(field string) (val string)
  8. }
  9. type Meta struct {
  10. field string
  11. snakeField string
  12. columnHeader string
  13. primaryKey bool
  14. setter MetaSetter
  15. valuer MetaValuer
  16. }
  17. func NewMeta(field string) *Meta {
  18. field = strings.TrimSpace(field)
  19. return &Meta{
  20. field: field,
  21. columnHeader: field,
  22. snakeField: strcase.ToSnake(field),
  23. }
  24. }
  25. // default is field name
  26. func (m *Meta) Header(s string) *Meta {
  27. m.columnHeader = strings.TrimSpace(s)
  28. return m
  29. }
  30. func (m *Meta) PrimaryKey(b bool) *Meta {
  31. m.primaryKey = b
  32. return m
  33. }
  34. type MetaSetter func(record interface{}, value string, metaValues MetaValues) error
  35. // set values to special fields
  36. // e.g. time.Time, struct, associated records ...
  37. func (m *Meta) Setter(f MetaSetter) *Meta {
  38. m.setter = f
  39. return m
  40. }
  41. type MetaValuer func(record interface{}) (string, error)
  42. // format values when exporting
  43. func (m *Meta) Valuer(f MetaValuer) *Meta {
  44. m.valuer = f
  45. return m
  46. }